Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezdb.php
Go to the documentation of this file.
1 <?php
79 class eZDB
80 {
85 private function __construct()
86 {
87 eZDebug::writeError( 'This class should not be instantiated', __METHOD__ );
88 }
89
94 static function hasInstance()
95 {
96 return isset( $GLOBALS['eZDBGlobalInstance'] ) && $GLOBALS['eZDBGlobalInstance'] instanceof eZDBInterface;
97 }
98
103 static function setInstance( $instance )
104 {
105 $GLOBALS['eZDBGlobalInstance'] = $instance;
106 }
107
117 static function instance( $databaseImplementation = false, $databaseParameters = false, $forceNewInstance = false )
118 {
119 $impl =& $GLOBALS['eZDBGlobalInstance'];
120
121 $fetchInstance = false;
122 if ( !( $impl instanceof eZDBInterface ) )
123 $fetchInstance = true;
124
125 if ( $forceNewInstance )
126 {
127 unset($impl);
128 $impl = false;
129 $fetchInstance = true;
130 }
131
132 $useDefaults = true;
133 if ( is_array( $databaseParameters ) and isset( $databaseParameters['use_defaults'] ) )
134 $useDefaults = $databaseParameters['use_defaults'];
135
136 if ( $fetchInstance )
137 {
138 $ini = eZINI::instance();
139 if ( $databaseImplementation === false and $useDefaults )
140 $databaseImplementation = $ini->variable( 'DatabaseSettings', 'DatabaseImplementation' );
141
142 $server = $user = $pwd = $db = $usePersistentConnection = $port = false;
143 if ( $useDefaults )
144 list( $server, $port, $user, $pwd, $db, $usePersistentConnection ) =
145 $ini->variableMulti( 'DatabaseSettings', array( 'Server', 'Port', 'User', 'Password', 'Database', 'UsePersistentConnection', ) );
146
147 $socketPath = false;
148 if ( $useDefaults )
149 {
150 $socket = $ini->variable( 'DatabaseSettings', 'Socket' );
151 if ( trim( $socket != "" ) and $socket != "disabled" )
152 {
153 $socketPath = $socket;
154 }
155 }
156
157 // Check slave servers
158 $slaveServer = null;
159 $slaveServerPort = null;
160 $slaveServerUser = null;
161 $slaveServerPassword = null;
162 $slaveServerDatabase = null;
163 $useSlave = $ini->variable( 'DatabaseSettings', 'UseSlaveServer' );
164 if ( $useSlave == "enabled" )
165 {
166 $slaveServers = $ini->variable( 'DatabaseSettings', 'SlaveServerArray' );
167 $slaveServerPorts = $ini->variable( 'DatabaseSettings', 'SlaveServerPort' );
168 $slaveServerUsers = $ini->variable( 'DatabaseSettings', 'SlaverServerUser' );
169 $slaveServerPasswords = $ini->variable( 'DatabaseSettings', 'SlaverServerPassword' );
170 $slaveServerDatabases = $ini->variable( 'DatabaseSettings', 'SlaverServerDatabase' );
171 $numberServers = count( $slaveServers );
172 if ( $numberServers > 1 )
173 {
174 $index = mt_rand( 1, $numberServers ) - 1;
175 }
176 else
177 $index = 0;
178 $slaveServer = $slaveServers[$index];
179 $slaveServerPort = $slaveServerPorts[$index];
180 $slaveServerUser = $slaveServerUsers[$index];
181 $slaveServerPassword = $slaveServerPasswords[$index];
182 $slaveServerDatabase = $slaveServerDatabases[$index];
183 }
184
185 list( $charset, $retries ) =
186 $ini->variableMulti( 'DatabaseSettings', array( 'Charset', 'ConnectRetries' ) );
187
188 $isInternalCharset = false;
189 if ( trim( $charset ) == '' )
190 {
191 $charset = eZTextCodec::internalCharset();
192 $isInternalCharset = true;
193 }
194 $builtinEncoding = ( $ini->variable( 'DatabaseSettings', 'UseBuiltinEncoding' ) == 'true' );
195
196 $impl = null;
197
198 $useSlaveServer = false;
199 if ( $useSlave == "enabled" )
200 $useSlaveServer = true;
201 $defaultDatabaseParameters = array( 'server' => $server,
202 'port' => $port,
203 'user' => $user,
204 'password' => $pwd,
205 'database' => $db,
206 'use_slave_server' => $useSlaveServer,
207 'slave_server' => $slaveServer,
208 'slave_port' => $slaveServerPort,
209 'slave_user' => $slaveServerUser,
210 'slave_password' => $slaveServerPassword,
211 'slave_database' => $slaveServerDatabase,
212 'charset' => $charset,
213 'is_internal_charset' => $isInternalCharset,
214 'socket' => $socketPath,
215 'builtin_encoding' => $builtinEncoding,
216 'connect_retries' => $retries,
217 'use_persistent_connection' => $usePersistentConnection,
218 'show_errors' => true );
219 /* This looks funny, but is needed to fix a crash in PHP */
220 $b = $databaseParameters;
221 $databaseParameters = $defaultDatabaseParameters;
222 if ( isset( $b['server'] ) )
223 $databaseParameters['server'] = $b['server'];
224 if ( isset( $b['port'] ) )
225 $databaseParameters['port'] = $b['port'];
226 if ( isset( $b['user'] ) )
227 $databaseParameters['user'] = $b['user'];
228 if ( isset( $b['password'] ) )
229 $databaseParameters['password'] = $b['password'];
230 if ( isset( $b['database'] ) )
231 $databaseParameters['database'] = $b['database'];
232 if ( isset( $b['use_slave_server'] ) )
233 $databaseParameters['use_slave_server'] = $b['use_slave_server'];
234 if ( isset( $b['slave_server'] ) )
235 $databaseParameters['slave_server'] = $b['slave_server'];
236 if ( isset( $b['slave_port'] ) )
237 $databaseParameters['slave_port'] = $b['slave_port'];
238 if ( isset( $b['slave_user'] ) )
239 $databaseParameters['slave_user'] = $b['slave_user'];
240 if ( isset( $b['slave_password'] ) )
241 $databaseParameters['slave_password'] = $b['slave_password'];
242 if ( isset( $b['slave_database'] ) )
243 $databaseParameters['slave_database'] = $b['slave_database'];
244 if ( isset( $b['charset'] ) )
245 {
246 $databaseParameters['charset'] = $b['charset'];
247 $databaseParameters['is_internal_charset'] = false;
248 }
249 if ( isset( $b['socket'] ) )
250 $databaseParameters['socket'] = $b['socket'];
251 if ( isset( $b['builtin_encoding'] ) )
252 $databaseParameters['builtin_encoding'] = $b['builtin_encoding'];
253 if ( isset( $b['connect_retries'] ) )
254 $databaseParameters['connect_retries'] = $b['connect_retries'];
255 if ( isset( $b['use_persistent_connection'] ) )
256 $databaseParameters['use_persistent_connection'] = $b['use_persistent_connection'];
257 if ( isset( $b['show_errors'] ) )
258 $databaseParameters['show_errors'] = $b['show_errors'];
259
260 $optionArray = array( 'iniFile' => 'site.ini',
261 'iniSection' => 'DatabaseSettings',
262 'iniVariable' => 'ImplementationAlias',
263 'handlerIndex' => $databaseImplementation,
264 'handlerParams' => array( $databaseParameters ) );
265
266 $options = new ezpExtensionOptions( $optionArray );
267
268 $impl = eZExtension::getHandlerClass( $options );
269
270 if ( !$impl )
271 {
272 $impl = new eZNullDB( $databaseParameters );
273 $impl->ErrorMessage = "No database handler was found for '$databaseImplementation'";
274 $impl->ErrorNumber = -1;
275 if ( $databaseParameters['show_errors'] )
276 {
277 eZDebug::writeError( 'Database implementation not supported: ' . $databaseImplementation, __METHOD__ );
278 }
279 }
280
281 $impl->setErrorHandling( self::$errorHandling );
282 }
283 return $impl;
284 }
285
293 static function checkTransactionCounter()
294 {
295 $result = true;
296 $ini = eZINI::instance();
297 $checkValidity = ( $ini->variable( "SiteAccessSettings", "CheckValidity" ) == "true" );
298 if ( $checkValidity )
299 return $result;
300
301 $db = eZDB::instance();
302
303 if ( $db->transactionCounter() > 0 )
304 {
305 $result = array();
306 $result['error'] = "Internal transaction counter mismatch : " . $db->transactionCounter() . ". Should be zero.";
307 eZDebug::writeError( $result['error'] );
308 $stack = $db->generateFailedTransactionStack();
309 if ( $stack !== false )
310 {
311 eZDebug::writeError( $stack, 'Transaction stack' );
312 }
313 $ini = eZINI::instance();
314 // In debug mode the transaction will be invalidated causing the top-level commit
315 // to issue an error.
316 if ( $ini->variable( "DatabaseSettings", "DebugTransactions" ) == "enabled" )
317 {
318 $db->invalidateTransaction();
319 $db->reportError();
320 }
321 else
322 {
323 while ( $db->transactionCounter() > 0 )
324 {
325 $db->commit();
326 }
327 }
328 }
329
330 return $result;
331 }
332
345 static function setErrorHandling( $errorHandling )
346 {
347 if ( $errorHandling != self::ERROR_HANDLING_EXCEPTIONS && $errorHandling != self::ERROR_HANDLING_STANDARD )
348 throw new RuntimeException( "Unknown eZDB error handling mode '$errorHandling'" );
349 self::$errorHandling = $errorHandling;
350
351 if ( self::hasInstance() )
352 {
353 self::instance()->setErrorHandling( $errorHandling );
354 }
355 }
356
360 const ERROR_HANDLING_STANDARD = 1;
361 const ERROR_HANDLING_EXCEPTIONS = 2;
362
363 protected static $errorHandling = self::ERROR_HANDLING_STANDARD;
364 }
365
366 ?>
eZDBInterface
Definition: ezdbinterface.php:20
eZDB
The eZDB class provides database wrapper functions The eZ db library procides a database independent ...
Definition: ezdb.php:80
eZDB\__construct
__construct()
Definition: ezdb.php:85
eZDB\ERROR_HANDLING_STANDARD
const ERROR_HANDLING_STANDARD
Definition: ezdb.php:360
eZDB\setInstance
static setInstance( $instance)
Definition: ezdb.php:103
eZDB\$errorHandling
static $errorHandling
Definition: ezdb.php:363
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDB\setErrorHandling
static setErrorHandling( $errorHandling)
Definition: ezdb.php:345
eZDB\checkTransactionCounter
static checkTransactionCounter()
Definition: ezdb.php:293
eZDB\ERROR_HANDLING_EXCEPTIONS
const ERROR_HANDLING_EXCEPTIONS
Definition: ezdb.php:361
eZDB\hasInstance
static hasInstance()
Definition: ezdb.php:94
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZExtension\getHandlerClass
static getHandlerClass(ezpExtensionOptions $options)
Definition: ezextension.php:531
eZNullDB
The eZNullDB class provides a interface which does nothing.
Definition: eznulldb.php:20
eZTextCodec\internalCharset
static internalCharset()
Definition: eztextcodec.php:546
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.