[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 /** 4 * \defgroup Database 5 * 6 * <p>This module includes all database-related objects.</p> 7 * 8 * <p>The most important one is the Db object, which provides one centralized way to get 9 * access to the current database connection. In fact, there is no reason for classes that require 10 * database access to use the PDb framework directly, as the Db::getDb() singleton method will 11 * transparently load the database information from the configuration file, connect to it and return 12 * a working connection.</p> 13 * 14 * <p>In order to get a working database connection, this is all you need to do:</p> 15 * 16 * <pre> 17 * $db =& Db::getDb(); 18 * </pre> 19 * 20 * <p>For those classes that extend the Model base class, the private attribute Model::_db is a database 21 * connection so there is no need to initialize a new one.</p> 22 */ 23 24 lt_include( PLOG_CLASS_PATH."class/config/configfilestorage.class.php" ); 25 lt_include( PLOG_CLASS_PATH."class/database/pdb/pdb.class.php" ); 26 27 /** 28 * \ingroup Database 29 * 30 * Provides a singleton for accessing the db and interfaces with PDb. Please use the 31 * getDb() singleton method to get access to the global database object instead of creating 32 * new objects every time. 33 */ 34 class Db 35 { 36 var $_prefix; 37 38 /** 39 * Constructor of the class 40 */ 41 function Db() 42 { 43 44 } 45 46 /** 47 * Singleto method that should be used to get access to the global database connection. This method 48 * will load the database information from the database configuration file (config/config.properties.php) and 49 * will initialize a connection based on its information. If it is unable to start a new database connection, this 50 * method wil stop all processing and display an error message. 51 * 52 * @return Returns a reference to a PDb driver, with a working connection to the database. 53 * @see PDb::getDriver() 54 */ 55 function &getDb() 56 { 57 static $db; 58 59 if( !isset( $db )) { 60 // we need to connect to the db 61 $fileConfig = new ConfigFileStorage(); 62 63 //$db = NewADOConnection('mysql'); 64 $db = PDb::getDriver('mysql'); 65 66 $username = $fileConfig->getValue( "db_username" ); 67 $password = $fileConfig->getValue( "db_password" ); 68 $host = $fileConfig->getValue( "db_host" ); 69 $dbname = $fileConfig->getValue( "db_database" ); 70 $dbcharset = $fileConfig->getValue( "db_character_set" ); 71 $dbpersistent = $fileConfig->getValue( "db_persistent" ); 72 if($dbpersistent == true) { 73 if( !$db->PConnect( $host, $username, $password, $dbname, $dbcharset )) { 74 $message = "Fatal error: could not connect to the database!". 75 " Error: ".$db->ErrorMsg(); 76 throw( new Exception( $message )); 77 die(); 78 } 79 } 80 else { 81 if( !$db->Connect( $host, $username, $password, $dbname, $dbcharset )) { 82 $message = "Fatal error: could not connect to the database!". 83 " Error: ".$db->ErrorMsg(); 84 throw( new Exception( $message )); 85 die(); 86 } 87 } 88 89 // pass the options to the driver, if any 90 $db->setDriverOpts( $fileConfig->getValue( "db_options" )); 91 } 92 93 return $db; 94 } 95 96 /** 97 * Creates a new database instance based on the information provided 98 * 99 * @param host The database host where to connect 100 * @param username The username used for the connection 101 * @param password The password used for the connection 102 * @param dbname The name of the database 103 * @param returns a reference to a PDb driver or dies if there was an error connecting 104 */ 105 function &getNewDb( $host, $username, $password, $dbname ) 106 { 107 static $dbs; 108 109 if( !is_array( $dbs )) 110 $dbs = Array(); 111 112 $key = $username.$password.$host.$dbname; 113 114 if( !isset( $dbs[$key] )) { 115 $dbs[$key] = PDb::getDriver('mysql'); 116 if( !$dbs[$key]->PConnect( $host, $username, $password, $dbname )) { 117 throw( new Exception( "getNewDb: Fatal error: could not connect to the database!" )); 118 die(); 119 } 120 } 121 122 $conn = $dbs[$key]; 123 124 return( $conn ); 125 } 126 127 /** 128 * returns the prefix as configured for this database connection 129 * 130 * @return A string containing the database prefix 131 * @static 132 */ 133 function getPrefix() 134 { 135 static $prefix; 136 137 if ( isset($prefix) ) { 138 return $prefix; 139 } else { 140 $fileConfig = new ConfigFileStorage(); 141 $prefix = $fileConfig->getValue( "db_prefix" ); 142 return( $prefix ); 143 } 144 } 145 146 /** 147 * Prepares a string for an SQL query by escaping apostrophe 148 * characters. If the PHP configuration setting 'magic_quotes_gpc' 149 * is set to ON, it will first strip the added slashes. Apostrophe 150 * characters are doubled, conforming with the ANSI SQL standard. 151 * The SQL parser makes sure that the escape token is not entered 152 * in the database so there is no need to modify the data when it 153 * is read from the database. 154 * 155 * @param string $string 156 * @return string 157 * @access public 158 */ 159 function qstr($string) { 160 161 if (get_magic_quotes_gpc()) { 162 $string = stripslashes($string); 163 //$string = stripslashes($string); 164 $string = str_replace('\\\\', '\\', $string); 165 $string = str_replace("\\'", "'", $string); 166 $string = str_replace('\\"', '"', $string); 167 } 168 169 $string = str_replace("\\", "\\\\", $string); 170 $string = str_replace("'", "''", $string); 171 172 return $string; 173 } 174 } 175 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |