[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // $Id$ 4 // 5 // Definition of eZDB class 6 // 7 // Created on: <12-Feb-2002 15:41:03 bf> 8 // 9 // SOFTWARE NAME: eZ publish 10 // SOFTWARE RELEASE: 3.9.0 11 // BUILD VERSION: 17785 12 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 13 // SOFTWARE LICENSE: GNU General Public License v2.0 14 // NOTICE: > 15 // This program is free software; you can redistribute it and/or 16 // modify it under the terms of version 2.0 of the GNU General 17 // Public License as published by the Free Software Foundation. 18 // 19 // This program is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU General Public License for more details. 23 // 24 // You should have received a copy of version 2.0 of the GNU General 25 // Public License along with this program; if not, write to the Free 26 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 27 // MA 02110-1301, USA. 28 // 29 // 30 31 /*! \file ezdb.php 32 Database abstraction layer. 33 */ 34 35 /*! \defgroup eZDB Database abstraction layer */ 36 37 /*! 38 \class eZDB ezdb.php 39 \ingroup eZDB 40 \brief The eZDB class provides database wrapper functions 41 The eZ db library procides a database independent framework for 42 SQL databases. The current supported databases are: PostgreSQL and 43 MySQL. 44 45 eZ db is designed to be used with the following type subset of SQL: 46 int, float, varchar and text. 47 48 To store date and time values int's are used. eZ locale is used to 49 present the date and times on a localized format. That way we don't have 50 to worry about the different date and time formats used in the different 51 databases. 52 53 Auto incrementing numbers, sequences, are used to generate unique id's 54 for a table row. This functionality is abstracted as it works different 55 in the different databases. 56 57 Limit and offset functionality is also abstracted by the eZ db library. 58 59 eZ db is designed to use lowercase in all table/column names. This is 60 done to prevent errors as the different databases handles this differently. 61 Especially when returning the data as an associative array. 62 63 \code 64 // include the library 65 include_once( 'lib/ezdb/classes/ezdb.php' ); 66 67 // Get the current database instance 68 // will create a new database object and connect to the database backend 69 // if there is already created an instance for this session the existing 70 // object will be returned. 71 // the settings for the database connections are set in site.ini 72 $db =& eZDB::instance(); 73 74 // Run a simple query 75 $db->query( 'DELETE FROM sql_test' ); 76 77 // insert some data 78 $str = $db->escapeString( "Testing escaping'\"" ); 79 $db->query( "INSERT INTO sql_test ( name, description ) VALUES ( 'New test', '$str' )" ); 80 81 // Get the last serial value for the sql_test table 82 $rowID = $db->lastSerialID( 'sql_test', 'id' ); 83 84 // fetch some data into an array of associative arrays 85 $rows = $db->arrayQuery( 'SELECT * FROM sql_test' ); 86 87 foreach ( $rows as $row ) 88 { 89 print( $row['name'] ); 90 } 91 92 // fetch some data with a limit 93 // will return the 10 first rows in the result 94 $ret = $db->arrayQuery( 'SELECT id, name, description, rownum FROM sql_test', 95 array( 'offset' => 0, 'limit' => 10 ) ); 96 97 // check which implementation we're running 98 print( $db->databaseName() ); 99 100 \endcode 101 102 \sa eZLocale eZINI 103 */ 104 105 include_once ( 'lib/ezutils/classes/ezdebug.php' ); 106 107 class eZDB 108 { 109 /*! 110 Constructor. 111 NOTE: Should not be used. 112 */ 113 function eZDB() 114 { 115 eZDebug::writeError( 'This class should not be instantiated', 'eZDB::eZDB' ); 116 } 117 118 /*! 119 \static 120 Returns an instance of the database object. 121 */ 122 function hasInstance() 123 { 124 $impl =& $GLOBALS['eZDBGlobalInstance']; 125 $class = get_class( $impl ); 126 $hasDB = false; 127 if ( preg_match( '/.*?db/', $class ) ) 128 { 129 $hasDB = true; 130 } 131 return $hasDB; 132 } 133 134 /*! 135 \static 136 Sets the global database instance to \a $instance. 137 */ 138 function setInstance( &$instance ) 139 { 140 $GLOBALS['eZDBGlobalInstance'] =& $instance; 141 } 142 143 /*! 144 \static 145 Returns an instance of the database object. 146 If you want to change the current database values you should set \a $forceNewInstance to \c true to force a new instance. 147 */ 148 function &instance( $databaseImplementation = false, $databaseParameters = false, $forceNewInstance = false ) 149 { 150 $impl =& $GLOBALS['eZDBGlobalInstance']; 151 $class = get_class( $impl ); 152 153 $fetchInstance = false; 154 if ( strstr( $class, 'db' ) === false ) 155 $fetchInstance = true; 156 157 if ( $forceNewInstance ) 158 { 159 unset($impl); 160 $impl = false; 161 $fetchInstance = true; 162 } 163 164 $useDefaults = true; 165 if ( is_array( $databaseParameters ) and isset( $databaseParameters['use_defaults'] ) ) 166 $useDefaults = $databaseParameters['use_defaults']; 167 168 if ( $fetchInstance ) 169 { 170 include_once ( 'lib/ezutils/classes/ezini.php' ); 171 $ini =& eZINI::instance(); 172 if ( $databaseImplementation === false and $useDefaults ) 173 $databaseImplementation = $ini->variable( 'DatabaseSettings', 'DatabaseImplementation' ); 174 175 $server = $user = $pwd = $db = $usePersistentConnection = false; 176 if ( $useDefaults ) 177 list( $server, $user, $pwd, $db, $usePersistentConnection ) = 178 $ini->variableMulti( 'DatabaseSettings', array( 'Server', 'User', 'Password', 'Database', 'UsePersistentConnection' ) ); 179 180 $socketPath = false; 181 if ( $useDefaults ) 182 { 183 $socket = $ini->variable( 'DatabaseSettings', 'Socket' ); 184 if ( trim( $socket != "" ) and $socket != "disabled" ) 185 { 186 $socketPath = $socket; 187 } 188 } 189 190 // Check slave servers 191 $slaveServer = null; 192 $slaveServerUser = null; 193 $slaveServerPassword = null; 194 $slaveServerDatabase = null; 195 $useSlave = $ini->variable( 'DatabaseSettings', 'UseSlaveServer' ); 196 if ( $useSlave == "enabled" ) 197 { 198 $slaveServers = $ini->variable( 'DatabaseSettings', 'SlaveServerArray' ); 199 $slaveServerUsers = $ini->variable( 'DatabaseSettings', 'SlaverServerUser' ); 200 $slaveServerPasswords = $ini->variable( 'DatabaseSettings', 'SlaverServerPassword' ); 201 $slaveServerDatabases = $ini->variable( 'DatabaseSettings', 'SlaverServerDatabase' ); 202 $numberServers = count( $slaveServers ); 203 if ( $numberServers > 1 ) 204 { 205 $index = rand( 1, $numberServers ) - 1; 206 } 207 else 208 $index = 0; 209 $slaveServer = $slaveServers[$index]; 210 $slaveServerUser = $slaveServerUsers[$index]; 211 $slaveServerPassword = $slaveServerPasswords[$index]; 212 $slaveServerDatabase = $slaveServerDatabases[$index]; 213 } 214 215 list( $charset, $retries ) = 216 $ini->variableMulti( 'DatabaseSettings', array( 'Charset', 'ConnectRetries' ) ); 217 218 $isInternalCharset = false; 219 if ( trim( $charset ) == '' ) 220 { 221 $charset = eZTextCodec::internalCharset(); 222 $isInternalCharset = true; 223 } 224 $builtinEncoding = ( $ini->variable( 'DatabaseSettings', 'UseBuiltinEncoding' ) == 'true' ); 225 226 $extraPluginPathArray = $ini->variableArray( 'DatabaseSettings', 'DatabasePluginPath' ); 227 $pluginPathArray = array_merge( array( 'lib/ezdb/classes/' ), 228 $extraPluginPathArray ); 229 $impl = null; 230 231 $useSlaveServer = false; 232 if ( $useSlave == "enabled" ) 233 $useSlaveServer = true; 234 $defaultDatabaseParameters = array( 'server' => $server, 235 'user' => $user, 236 'password' => $pwd, 237 'database' => $db, 238 'use_slave_server' => $useSlaveServer, 239 'slave_server' => $slaveServer, 240 'slave_user' => $slaveServerUser, 241 'slave_password' => $slaveServerPassword, 242 'slave_database' => $slaveServerDatabase, 243 'charset' => $charset, 244 'is_internal_charset' => $isInternalCharset, 245 'socket' => $socketPath, 246 'builtin_encoding' => $builtinEncoding, 247 'connect_retries' => $retries, 248 'use_persistent_connection' => $usePersistentConnection, 249 'show_errors' => true ); 250 /* This looks funny, but is needed to fix a crash in PHP */ 251 $b = $databaseParameters; 252 $databaseParameters = $defaultDatabaseParameters; 253 if ( isset( $b['server'] ) ) 254 $databaseParameters['server'] = $b['server']; 255 if ( isset( $b['user'] ) ) 256 $databaseParameters['user'] = $b['user']; 257 if ( isset( $b['password'] ) ) 258 $databaseParameters['password'] = $b['password']; 259 if ( isset( $b['database'] ) ) 260 $databaseParameters['database'] = $b['database']; 261 if ( isset( $b['use_slave_server'] ) ) 262 $databaseParameters['use_slave_server'] = $b['use_slave_server']; 263 if ( isset( $b['slave_server'] ) ) 264 $databaseParameters['slave_server'] = $b['slave_server']; 265 if ( isset( $b['slave_user'] ) ) 266 $databaseParameters['slave_user'] = $b['slave_user']; 267 if ( isset( $b['slave_password'] ) ) 268 $databaseParameters['slave_password'] = $b['slave_password']; 269 if ( isset( $b['slave_database'] ) ) 270 $databaseParameters['slave_database'] = $b['slave_database']; 271 if ( isset( $b['charset'] ) ) 272 { 273 $databaseParameters['charset'] = $b['charset']; 274 $databaseParameters['is_internal_charset'] = false; 275 } 276 if ( isset( $b['socket'] ) ) 277 $databaseParameters['socket'] = $b['socket']; 278 if ( isset( $b['builtin_encoding'] ) ) 279 $databaseParameters['builtin_encoding'] = $b['builtin_encoding']; 280 if ( isset( $b['connect_retries'] ) ) 281 $databaseParameters['connect_retries'] = $b['connect_retries']; 282 if ( isset( $b['use_persistent_connection'] ) ) 283 $databaseParameters['use_persistent_connection'] = $b['use_persistent_connection']; 284 if ( isset( $b['show_errors'] ) ) 285 $databaseParameters['show_errors'] = $b['show_errors']; 286 287 // Search for the db interface implementations in active extensions directories. 288 include_once ( 'lib/ezutils/classes/ezextension.php' ); 289 $baseDirectory = eZExtension::baseDirectory(); 290 $extensionDirectories = eZExtension::activeExtensions(); 291 $extensionDirectories = array_unique( $extensionDirectories ); 292 $repositoryDirectories = array(); 293 foreach ( $extensionDirectories as $extDir ) 294 { 295 $newRepositoryDir = "$baseDirectory/$extDir/ezdb/dbms-drivers/"; 296 if ( file_exists( $newRepositoryDir ) ) 297 $repositoryDirectories[] = $newRepositoryDir; 298 } 299 $repositoryDirectories = array_merge( $repositoryDirectories, $pluginPathArray ); 300 301 foreach( $repositoryDirectories as $repositoryDir ) 302 { 303 // If we have an alias get the real name 304 $aliasList = $ini->variable( 'DatabaseSettings', 'ImplementationAlias' ); 305 if ( isset( $aliasList[$databaseImplementation] ) ) 306 { 307 $databaseImplementation = $aliasList[$databaseImplementation]; 308 } 309 310 $dbFile = $repositoryDir . $databaseImplementation . 'db.php'; 311 if ( file_exists( $dbFile ) ) 312 { 313 include_once( $dbFile ); 314 $className = $databaseImplementation . 'db'; 315 $impl = new $className( $databaseParameters ); 316 break; 317 } 318 } 319 if ( $impl === null ) 320 { 321 include_once ( 'lib/ezdb/classes/eznulldb.php' ); 322 $impl = new eZNullDB( $databaseParameters ); 323 $impl->ErrorMessage = "No database handler was found for '$databaseImplementation'"; 324 $impl->ErrorNumber = -1; 325 if ( $databaseParameters['show_errors'] ) 326 { 327 eZDebug::writeError( 'Database implementation not supported: ' . $databaseImplementation, 'eZDB::instance' ); 328 } 329 } 330 331 } 332 return $impl; 333 } 334 335 /*! 336 Checks transaction counter 337 If the current transaction counter is 1 or higher 338 means 1 or more transactions are running and a negative value 339 means something is wrong. 340 Prints the error. 341 */ 342 function checkTransactionCounter() 343 { 344 $result = true; 345 include_once ( 'lib/ezutils/classes/ezini.php' ); 346 $ini =& eZINI::instance(); 347 $checkValidity = ( $ini->variable( "SiteAccessSettings", "CheckValidity" ) == "true" ); 348 if ( $checkValidity ) 349 return $result; 350 351 $db =& eZDB::instance(); 352 353 if ( $db->transactionCounter() > 0 ) 354 { 355 $result = array(); 356 $result['error'] = "Internal transaction counter mismatch : " . $db->transactionCounter() . ". Should be zero."; 357 eZDebug::writeError( $result['error'] ); 358 $stack = $db->generateFailedTransactionStack(); 359 if ( $stack !== false ) 360 { 361 eZDebug::writeError( $stack, 'Transaction stack' ); 362 } 363 include_once ( 'lib/ezutils/classes/ezini.php' ); 364 $ini =& eZINI::instance(); 365 // In debug mode the transaction will be invalidated causing the top-level commit 366 // to issue an error. 367 if ( $ini->variable( "DatabaseSettings", "DebugTransactions" ) == "enabled" ) 368 { 369 $db->invalidateTransaction(); 370 $db->reportError(); 371 } 372 else 373 { 374 while ( $db->transactionCounter() > 0 ) 375 { 376 $db->commit(); 377 } 378 } 379 } 380 381 return $result; 382 } 383 384 } 385 386 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |