3 * @defgroup Database Database
5 * This file deals with database interface functions
6 * and query specifics/optimisations.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 use Psr\Log\LoggerAwareInterface
;
27 use Psr\Log\LoggerInterface
;
30 * Database abstraction object
33 abstract class Database
implements IDatabase
, LoggerAwareInterface
{
34 /** Number of times to re-try an operation in case of deadlock */
35 const DEADLOCK_TRIES
= 4;
36 /** Minimum time to wait before retry, in microseconds */
37 const DEADLOCK_DELAY_MIN
= 500000;
38 /** Maximum time to wait before retry */
39 const DEADLOCK_DELAY_MAX
= 1500000;
41 /** How long before it is worth doing a dummy query to test the connection */
43 const PING_QUERY
= 'SELECT 1 AS ping';
45 const TINY_WRITE_SEC
= .010;
46 const SLOW_WRITE_SEC
= .500;
47 const SMALL_WRITE_ROWS
= 100;
49 /** @var string SQL query */
50 protected $mLastQuery = '';
52 protected $mDoneWrites = false;
53 /** @var string|bool */
54 protected $mPHPError = false;
63 /** @var array[] $aliases Map of (table => (dbname, schema, prefix) map) */
64 protected $tableAliases = [];
65 /** @var bool Whether this PHP instance is for a CLI script */
67 /** @var string Agent name for query profiling */
70 /** @var BagOStuff APC cache */
72 /** @var LoggerInterface */
73 protected $connLogger;
74 /** @var LoggerInterface */
75 protected $queryLogger;
76 /** @var callback Error logging callback */
77 protected $errorLogger;
79 /** @var resource Database connection */
80 protected $mConn = null;
82 protected $mOpened = false;
84 /** @var array[] List of (callable, method name) */
85 protected $mTrxIdleCallbacks = [];
86 /** @var array[] List of (callable, method name) */
87 protected $mTrxPreCommitCallbacks = [];
88 /** @var array[] List of (callable, method name) */
89 protected $mTrxEndCallbacks = [];
90 /** @var callable[] Map of (name => callable) */
91 protected $mTrxRecurringCallbacks = [];
92 /** @var bool Whether to suppress triggering of transaction end callbacks */
93 protected $mTrxEndCallbacksSuppressed = false;
96 protected $mTablePrefix = '';
98 protected $mSchema = '';
102 protected $mLBInfo = [];
103 /** @var bool|null */
104 protected $mDefaultBigSelects = null;
105 /** @var array|bool */
106 protected $mSchemaVars = false;
108 protected $mSessionVars = [];
109 /** @var array|null */
110 protected $preparedArgs;
111 /** @var string|bool|null Stashed value of html_errors INI setting */
112 protected $htmlErrors;
114 protected $delimiter = ';';
117 * Either 1 if a transaction is active or 0 otherwise.
118 * The other Trx fields may not be meaningfull if this is 0.
122 protected $mTrxLevel = 0;
124 * Either a short hexidecimal string if a transaction is active or ""
127 * @see DatabaseBase::mTrxLevel
129 protected $mTrxShortId = '';
131 * The UNIX time that the transaction started. Callers can assume that if
132 * snapshot isolation is used, then the data is *at least* up to date to that
133 * point (possibly more up-to-date since the first SELECT defines the snapshot).
136 * @see DatabaseBase::mTrxLevel
138 private $mTrxTimestamp = null;
139 /** @var float Lag estimate at the time of BEGIN */
140 private $mTrxReplicaLag = null;
142 * Remembers the function name given for starting the most recent transaction via begin().
143 * Used to provide additional context for error reporting.
146 * @see DatabaseBase::mTrxLevel
148 private $mTrxFname = null;
150 * Record if possible write queries were done in the last transaction started
153 * @see DatabaseBase::mTrxLevel
155 private $mTrxDoneWrites = false;
157 * Record if the current transaction was started implicitly due to DBO_TRX being set.
160 * @see DatabaseBase::mTrxLevel
162 private $mTrxAutomatic = false;
164 * Array of levels of atomicity within transactions
168 private $mTrxAtomicLevels = [];
170 * Record if the current transaction was started implicitly by DatabaseBase::startAtomic
174 private $mTrxAutomaticAtomic = false;
176 * Track the write query callers of the current transaction
180 private $mTrxWriteCallers = [];
182 * @var float Seconds spent in write queries for the current transaction
184 private $mTrxWriteDuration = 0.0;
186 * @var integer Number of write queries for the current transaction
188 private $mTrxWriteQueryCount = 0;
190 * @var float Like mTrxWriteQueryCount but excludes lock-bound, easy to replicate, queries
192 private $mTrxWriteAdjDuration = 0.0;
194 * @var integer Number of write queries counted in mTrxWriteAdjDuration
196 private $mTrxWriteAdjQueryCount = 0;
198 * @var float RTT time estimate
200 private $mRTTEstimate = 0.0;
202 /** @var array Map of (name => 1) for locks obtained via lock() */
203 private $mNamedLocksHeld = [];
205 /** @var IDatabase|null Lazy handle to the master DB this server replicates from */
206 private $lazyMasterHandle;
210 * @var resource File handle for upgrade
212 protected $fileHandle = null;
216 * @var string[] Process cache of VIEWs names in the database
218 protected $allViews = null;
220 /** @var float UNIX timestamp */
221 protected $lastPing = 0.0;
223 /** @var int[] Prior mFlags values */
224 private $priorFlags = [];
226 /** @var object|string Class name or object With profileIn/profileOut methods */
228 /** @var TransactionProfiler */
229 protected $trxProfiler;
234 * FIXME: It is possible to construct a Database object with no associated
235 * connection object, by specifying no parameters to __construct(). This
236 * feature is deprecated and should be removed.
238 * IDatabase classes should not be constructed directly in external
239 * code. DatabaseBase::factory() should be used instead.
241 * @param array $params Parameters passed from DatabaseBase::factory()
243 function __construct( array $params ) {
244 $server = $params['host'];
245 $user = $params['user'];
246 $password = $params['password'];
247 $dbName = $params['dbname'];
248 $flags = $params['flags'];
250 $this->mSchema
= $params['schema'];
251 $this->mTablePrefix
= $params['tablePrefix'];
253 $this->cliMode
= isset( $params['cliMode'] )
255 : ( PHP_SAPI
=== 'cli' );
256 $this->agent
= isset( $params['agent'] )
257 ?
str_replace( '/', '-', $params['agent'] ) // escape for comment
260 $this->mFlags
= $flags;
261 if ( $this->mFlags
& DBO_DEFAULT
) {
262 if ( $this->cliMode
) {
263 $this->mFlags
&= ~DBO_TRX
;
265 $this->mFlags |
= DBO_TRX
;
269 $this->mSessionVars
= $params['variables'];
271 $this->srvCache
= isset( $params['srvCache'] )
272 ?
$params['srvCache']
273 : new HashBagOStuff();
275 $this->profiler
= isset( $params['profiler'] ) ?
$params['profiler'] : null;
276 $this->trxProfiler
= isset( $params['trxProfiler'] )
277 ?
$params['trxProfiler']
278 : new TransactionProfiler();
279 $this->connLogger
= isset( $params['connLogger'] )
280 ?
$params['connLogger']
281 : new \Psr\Log\
NullLogger();
282 $this->queryLogger
= isset( $params['queryLogger'] )
283 ?
$params['queryLogger']
284 : new \Psr\Log\
NullLogger();
287 $this->open( $server, $user, $password, $dbName );
292 * Given a DB type, construct the name of the appropriate child class of
293 * IDatabase. This is designed to replace all of the manual stuff like:
294 * $class = 'Database' . ucfirst( strtolower( $dbType ) );
295 * as well as validate against the canonical list of DB types we have
297 * This factory function is mostly useful for when you need to connect to a
298 * database other than the MediaWiki default (such as for external auth,
299 * an extension, et cetera). Do not use this to connect to the MediaWiki
300 * database. Example uses in core:
301 * @see LoadBalancer::reallyOpenConnection()
302 * @see ForeignDBRepo::getMasterDB()
303 * @see WebInstallerDBConnect::execute()
307 * @param string $dbType A possible DB type
308 * @param array $p An array of options to pass to the constructor.
309 * Valid options are: host, user, password, dbname, flags, tablePrefix, schema, driver
310 * @return IDatabase|null If the database driver or extension cannot be found
311 * @throws InvalidArgumentException If the database driver or extension cannot be found
313 final public static function factory( $dbType, $p = [] ) {
314 $canonicalDBTypes = [
315 'mysql' => [ 'mysqli', 'mysql' ],
323 $dbType = strtolower( $dbType );
324 if ( isset( $canonicalDBTypes[$dbType] ) && $canonicalDBTypes[$dbType] ) {
325 $possibleDrivers = $canonicalDBTypes[$dbType];
326 if ( !empty( $p['driver'] ) ) {
327 if ( in_array( $p['driver'], $possibleDrivers ) ) {
328 $driver = $p['driver'];
330 throw new InvalidArgumentException( __METHOD__
.
331 " type '$dbType' does not support driver '{$p['driver']}'" );
334 foreach ( $possibleDrivers as $posDriver ) {
335 if ( extension_loaded( $posDriver ) ) {
336 $driver = $posDriver;
344 if ( $driver === false ) {
345 throw new InvalidArgumentException( __METHOD__
.
346 " no viable database extension found for type '$dbType'" );
349 // Determine schema defaults. Currently Microsoft SQL Server uses $wgDBmwschema,
350 // and everything else doesn't use a schema (e.g. null)
351 // Although postgres and oracle support schemas, we don't use them (yet)
352 // to maintain backwards compatibility
354 'mssql' => 'get from global',
357 $class = 'Database' . ucfirst( $driver );
358 if ( class_exists( $class ) && is_subclass_of( $class, 'IDatabase' ) ) {
359 // Resolve some defaults for b/c
360 $p['host'] = isset( $p['host'] ) ?
$p['host'] : false;
361 $p['user'] = isset( $p['user'] ) ?
$p['user'] : false;
362 $p['password'] = isset( $p['password'] ) ?
$p['password'] : false;
363 $p['dbname'] = isset( $p['dbname'] ) ?
$p['dbname'] : false;
364 $p['flags'] = isset( $p['flags'] ) ?
$p['flags'] : 0;
365 $p['variables'] = isset( $p['variables'] ) ?
$p['variables'] : [];
366 $p['tablePrefix'] = isset( $p['tablePrefix'] ) ?
$p['tablePrefix'] : '';
367 if ( !isset( $p['schema'] ) ) {
368 $p['schema'] = isset( $defaultSchemas[$dbType] ) ?
$defaultSchemas[$dbType] : '';
370 $p['foreign'] = isset( $p['foreign'] ) ?
$p['foreign'] : false;
372 $conn = new $class( $p );
373 if ( isset( $p['connLogger'] ) ) {
374 $conn->connLogger
= $p['connLogger'];
376 if ( isset( $p['queryLogger'] ) ) {
377 $conn->queryLogger
= $p['queryLogger'];
379 if ( isset( $p['errorLogger'] ) ) {
380 $conn->errorLogger
= $p['errorLogger'];
382 $conn->errorLogger
= function ( Exception
$e ) {
383 trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_WARNING
);
393 public function setLogger( LoggerInterface
$logger ) {
394 $this->queryLogger
= $logger;
397 public function getServerInfo() {
398 return $this->getServerVersion();
401 public function bufferResults( $buffer = null ) {
402 $res = !$this->getFlag( DBO_NOBUFFER
);
403 if ( $buffer !== null ) {
404 $buffer ?
$this->clearFlag( DBO_NOBUFFER
) : $this->setFlag( DBO_NOBUFFER
);
411 * Turns on (false) or off (true) the automatic generation and sending
412 * of a "we're sorry, but there has been a database error" page on
413 * database errors. Default is on (false). When turned off, the
414 * code should use lastErrno() and lastError() to handle the
415 * situation as appropriate.
417 * Do not use this function outside of the Database classes.
419 * @param null|bool $ignoreErrors
420 * @return bool The previous value of the flag.
422 protected function ignoreErrors( $ignoreErrors = null ) {
423 $res = $this->getFlag( DBO_IGNORE
);
424 if ( $ignoreErrors !== null ) {
425 $ignoreErrors ?
$this->setFlag( DBO_IGNORE
) : $this->clearFlag( DBO_IGNORE
);
431 public function trxLevel() {
432 return $this->mTrxLevel
;
435 public function trxTimestamp() {
436 return $this->mTrxLevel ?
$this->mTrxTimestamp
: null;
439 public function tablePrefix( $prefix = null ) {
440 $old = $this->mTablePrefix
;
441 if ( $prefix !== null ) {
442 $this->mTablePrefix
= $prefix;
448 public function dbSchema( $schema = null ) {
449 $old = $this->mSchema
;
450 if ( $schema !== null ) {
451 $this->mSchema
= $schema;
458 * Set the filehandle to copy write statements to.
460 * @param resource $fh File handle
462 public function setFileHandle( $fh ) {
463 $this->fileHandle
= $fh;
466 public function getLBInfo( $name = null ) {
467 if ( is_null( $name ) ) {
468 return $this->mLBInfo
;
470 if ( array_key_exists( $name, $this->mLBInfo
) ) {
471 return $this->mLBInfo
[$name];
478 public function setLBInfo( $name, $value = null ) {
479 if ( is_null( $value ) ) {
480 $this->mLBInfo
= $name;
482 $this->mLBInfo
[$name] = $value;
486 public function setLazyMasterHandle( IDatabase
$conn ) {
487 $this->lazyMasterHandle
= $conn;
491 * @return IDatabase|null
492 * @see setLazyMasterHandle()
495 public function getLazyMasterHandle() {
496 return $this->lazyMasterHandle
;
499 public function implicitGroupby() {
503 public function implicitOrderby() {
507 public function lastQuery() {
508 return $this->mLastQuery
;
511 public function doneWrites() {
512 return (bool)$this->mDoneWrites
;
515 public function lastDoneWrites() {
516 return $this->mDoneWrites ?
: false;
519 public function writesPending() {
520 return $this->mTrxLevel
&& $this->mTrxDoneWrites
;
523 public function writesOrCallbacksPending() {
524 return $this->mTrxLevel
&& (
525 $this->mTrxDoneWrites ||
$this->mTrxIdleCallbacks ||
$this->mTrxPreCommitCallbacks
529 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
530 if ( !$this->mTrxLevel
) {
532 } elseif ( !$this->mTrxDoneWrites
) {
537 case self
::ESTIMATE_DB_APPLY
:
539 $rttAdjTotal = $this->mTrxWriteAdjQueryCount
* $rtt;
540 $applyTime = max( $this->mTrxWriteAdjDuration
- $rttAdjTotal, 0 );
541 // For omitted queries, make them count as something at least
542 $omitted = $this->mTrxWriteQueryCount
- $this->mTrxWriteAdjQueryCount
;
543 $applyTime +
= self
::TINY_WRITE_SEC
* $omitted;
546 default: // everything
547 return $this->mTrxWriteDuration
;
551 public function pendingWriteCallers() {
552 return $this->mTrxLevel ?
$this->mTrxWriteCallers
: [];
555 protected function pendingWriteAndCallbackCallers() {
556 if ( !$this->mTrxLevel
) {
560 $fnames = $this->mTrxWriteCallers
;
562 $this->mTrxIdleCallbacks
,
563 $this->mTrxPreCommitCallbacks
,
564 $this->mTrxEndCallbacks
566 foreach ( $callbacks as $callback ) {
567 $fnames[] = $callback[1];
574 public function isOpen() {
575 return $this->mOpened
;
578 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
579 if ( $remember === self
::REMEMBER_PRIOR
) {
580 array_push( $this->priorFlags
, $this->mFlags
);
582 $this->mFlags |
= $flag;
585 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
586 if ( $remember === self
::REMEMBER_PRIOR
) {
587 array_push( $this->priorFlags
, $this->mFlags
);
589 $this->mFlags
&= ~
$flag;
592 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
593 if ( !$this->priorFlags
) {
597 if ( $state === self
::RESTORE_INITIAL
) {
598 $this->mFlags
= reset( $this->priorFlags
);
599 $this->priorFlags
= [];
601 $this->mFlags
= array_pop( $this->priorFlags
);
605 public function getFlag( $flag ) {
606 return !!( $this->mFlags
& $flag );
609 public function getProperty( $name ) {
613 public function getDomainID() {
614 if ( $this->mTablePrefix
!= '' ) {
615 return "{$this->mDBname}-{$this->mTablePrefix}";
617 return $this->mDBname
;
621 final public function getWikiID() {
622 return $this->getDomainID();
626 * Get information about an index into an object
627 * @param string $table Table name
628 * @param string $index Index name
629 * @param string $fname Calling function name
630 * @return mixed Database-specific index description class or false if the index does not exist
632 abstract function indexInfo( $table, $index, $fname = __METHOD__
);
635 * Wrapper for addslashes()
637 * @param string $s String to be slashed.
638 * @return string Slashed string.
640 abstract function strencode( $s );
642 protected function installErrorHandler() {
643 $this->mPHPError
= false;
644 $this->htmlErrors
= ini_set( 'html_errors', '0' );
645 set_error_handler( [ $this, 'connectionerrorLogger' ] );
649 * @return bool|string
651 protected function restoreErrorHandler() {
652 restore_error_handler();
653 if ( $this->htmlErrors
!== false ) {
654 ini_set( 'html_errors', $this->htmlErrors
);
656 if ( $this->mPHPError
) {
657 $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError
);
658 $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
668 * @param string $errstr
670 public function connectionerrorLogger( $errno, $errstr ) {
671 $this->mPHPError
= $errstr;
675 * Create a log context to pass to PSR logging functions.
677 * @param array $extras Additional data to add to context
680 protected function getLogContext( array $extras = [] ) {
683 'db_server' => $this->mServer
,
684 'db_name' => $this->mDBname
,
685 'db_user' => $this->mUser
,
691 public function close() {
692 if ( $this->mConn
) {
693 if ( $this->trxLevel() ) {
694 $this->commit( __METHOD__
, self
::FLUSHING_INTERNAL
);
697 $closed = $this->closeConnection();
698 $this->mConn
= false;
699 } elseif ( $this->mTrxIdleCallbacks ||
$this->mTrxEndCallbacks
) { // sanity
700 throw new RuntimeException( "Transaction callbacks still pending." );
704 $this->mOpened
= false;
710 * Make sure isOpen() returns true as a sanity check
712 * @throws DBUnexpectedError
714 protected function assertOpen() {
715 if ( !$this->isOpen() ) {
716 throw new DBUnexpectedError( $this, "DB connection was already closed." );
721 * Closes underlying database connection
723 * @return bool Whether connection was closed successfully
725 abstract protected function closeConnection();
727 function reportConnectionError( $error = 'Unknown error' ) {
728 $myError = $this->lastError();
734 throw new DBConnectionError( $this, $error );
738 * The DBMS-dependent part of query()
740 * @param string $sql SQL query.
741 * @return ResultWrapper|bool Result object to feed to fetchObject,
742 * fetchRow, ...; or false on failure
744 abstract protected function doQuery( $sql );
747 * Determine whether a query writes to the DB.
748 * Should return true if unsure.
753 protected function isWriteQuery( $sql ) {
755 '/^(?:SELECT|BEGIN|ROLLBACK|COMMIT|SET|SHOW|EXPLAIN|\(SELECT)\b/i', $sql );
760 * @return string|null
762 protected function getQueryVerb( $sql ) {
763 return preg_match( '/^\s*([a-z]+)/i', $sql, $m ) ?
strtoupper( $m[1] ) : null;
767 * Determine whether a SQL statement is sensitive to isolation level.
768 * A SQL statement is considered transactable if its result could vary
769 * depending on the transaction isolation level. Operational commands
770 * such as 'SET' and 'SHOW' are not considered to be transactable.
775 protected function isTransactableQuery( $sql ) {
776 $verb = $this->getQueryVerb( $sql );
777 return !in_array( $verb, [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET' ], true );
780 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
781 $priorWritesPending = $this->writesOrCallbacksPending();
782 $this->mLastQuery
= $sql;
784 $isWrite = $this->isWriteQuery( $sql );
786 $reason = $this->getReadOnlyReason();
787 if ( $reason !== false ) {
788 throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
790 # Set a flag indicating that writes have been done
791 $this->mDoneWrites
= microtime( true );
794 // Add trace comment to the begin of the sql string, right after the operator.
795 // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (bug 42598)
796 $commentedSql = preg_replace( '/\s|$/', " /* $fname {$this->agent} */ ", $sql, 1 );
798 # Start implicit transactions that wrap the request if DBO_TRX is enabled
799 if ( !$this->mTrxLevel
&& $this->getFlag( DBO_TRX
)
800 && $this->isTransactableQuery( $sql )
802 $this->begin( __METHOD__
. " ($fname)", self
::TRANSACTION_INTERNAL
);
803 $this->mTrxAutomatic
= true;
806 # Keep track of whether the transaction has write queries pending
807 if ( $this->mTrxLevel
&& !$this->mTrxDoneWrites
&& $isWrite ) {
808 $this->mTrxDoneWrites
= true;
809 $this->trxProfiler
->transactionWritingIn(
810 $this->mServer
, $this->mDBname
, $this->mTrxShortId
);
813 if ( $this->getFlag( DBO_DEBUG
) ) {
814 $this->queryLogger
->debug( "{$this->mDBname} {$commentedSql}" );
817 # Avoid fatals if close() was called
820 # Send the query to the server
821 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
823 # Try reconnecting if the connection was lost
824 if ( false === $ret && $this->wasErrorReissuable() ) {
825 $recoverable = $this->canRecoverFromDisconnect( $sql, $priorWritesPending );
826 # Stash the last error values before anything might clear them
827 $lastError = $this->lastError();
828 $lastErrno = $this->lastErrno();
829 # Update state tracking to reflect transaction loss due to disconnection
830 $this->handleTransactionLoss();
831 if ( $this->reconnect() ) {
832 $msg = __METHOD__
. ": lost connection to {$this->getServer()}; reconnected";
833 $this->connLogger
->warning( $msg );
834 $this->queryLogger
->warning(
835 "$msg:\n" . ( new RuntimeException() )->getTraceAsString() );
837 if ( !$recoverable ) {
838 # Callers may catch the exception and continue to use the DB
839 $this->reportQueryError( $lastError, $lastErrno, $sql, $fname );
841 # Should be safe to silently retry the query
842 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
845 $msg = __METHOD__
. ": lost connection to {$this->getServer()} permanently";
846 $this->connLogger
->error( $msg );
850 if ( false === $ret ) {
851 # Deadlocks cause the entire transaction to abort, not just the statement.
852 # http://dev.mysql.com/doc/refman/5.7/en/innodb-error-handling.html
853 # https://www.postgresql.org/docs/9.1/static/explicit-locking.html
854 if ( $this->wasDeadlock() ) {
855 if ( $this->explicitTrxActive() ||
$priorWritesPending ) {
856 $tempIgnore = false; // not recoverable
858 # Update state tracking to reflect transaction loss
859 $this->handleTransactionLoss();
862 $this->reportQueryError(
863 $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
866 $res = $this->resultObject( $ret );
871 private function doProfiledQuery( $sql, $commentedSql, $isWrite, $fname ) {
872 $isMaster = !is_null( $this->getLBInfo( 'master' ) );
873 # generalizeSQL() will probably cut down the query to reasonable
874 # logging size most of the time. The substr is really just a sanity check.
876 $queryProf = 'query-m: ' . substr( self
::generalizeSQL( $sql ), 0, 255 );
878 $queryProf = 'query: ' . substr( self
::generalizeSQL( $sql ), 0, 255 );
881 # Include query transaction state
882 $queryProf .= $this->mTrxShortId ?
" [TRX#{$this->mTrxShortId}]" : "";
884 $startTime = microtime( true );
885 if ( $this->profiler
) {
886 call_user_func( [ $this->profiler
, 'profileIn' ], $queryProf );
888 $ret = $this->doQuery( $commentedSql );
889 if ( $this->profiler
) {
890 call_user_func( [ $this->profiler
, 'profileOut' ], $queryProf );
892 $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
894 unset( $queryProfSection ); // profile out (if set)
896 if ( $ret !== false ) {
897 $this->lastPing
= $startTime;
898 if ( $isWrite && $this->mTrxLevel
) {
899 $this->updateTrxWriteQueryTime( $sql, $queryRuntime );
900 $this->mTrxWriteCallers
[] = $fname;
904 if ( $sql === self
::PING_QUERY
) {
905 $this->mRTTEstimate
= $queryRuntime;
908 $this->trxProfiler
->recordQueryCompletion(
909 $queryProf, $startTime, $isWrite, $this->affectedRows()
911 MWDebug
::query( $sql, $fname, $isMaster, $queryRuntime );
917 * Update the estimated run-time of a query, not counting large row lock times
919 * LoadBalancer can be set to rollback transactions that will create huge replication
920 * lag. It bases this estimate off of pendingWriteQueryDuration(). Certain simple
921 * queries, like inserting a row can take a long time due to row locking. This method
922 * uses some simple heuristics to discount those cases.
924 * @param string $sql A SQL write query
925 * @param float $runtime Total runtime, including RTT
927 private function updateTrxWriteQueryTime( $sql, $runtime ) {
928 // Whether this is indicative of replica DB runtime (except for RBR or ws_repl)
929 $indicativeOfReplicaRuntime = true;
930 if ( $runtime > self
::SLOW_WRITE_SEC
) {
931 $verb = $this->getQueryVerb( $sql );
932 // insert(), upsert(), replace() are fast unless bulky in size or blocked on locks
933 if ( $verb === 'INSERT' ) {
934 $indicativeOfReplicaRuntime = $this->affectedRows() > self
::SMALL_WRITE_ROWS
;
935 } elseif ( $verb === 'REPLACE' ) {
936 $indicativeOfReplicaRuntime = $this->affectedRows() > self
::SMALL_WRITE_ROWS
/ 2;
940 $this->mTrxWriteDuration +
= $runtime;
941 $this->mTrxWriteQueryCount +
= 1;
942 if ( $indicativeOfReplicaRuntime ) {
943 $this->mTrxWriteAdjDuration +
= $runtime;
944 $this->mTrxWriteAdjQueryCount +
= 1;
948 private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
949 # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
950 # Dropped connections also mean that named locks are automatically released.
951 # Only allow error suppression in autocommit mode or when the lost transaction
952 # didn't matter anyway (aside from DBO_TRX snapshot loss).
953 if ( $this->mNamedLocksHeld
) {
954 return false; // possible critical section violation
955 } elseif ( $sql === 'COMMIT' ) {
956 return !$priorWritesPending; // nothing written anyway? (T127428)
957 } elseif ( $sql === 'ROLLBACK' ) {
958 return true; // transaction lost...which is also what was requested :)
959 } elseif ( $this->explicitTrxActive() ) {
960 return false; // don't drop atomocity
961 } elseif ( $priorWritesPending ) {
962 return false; // prior writes lost from implicit transaction
968 private function handleTransactionLoss() {
969 $this->mTrxLevel
= 0;
970 $this->mTrxIdleCallbacks
= []; // bug 65263
971 $this->mTrxPreCommitCallbacks
= []; // bug 65263
973 // Handle callbacks in mTrxEndCallbacks
974 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_ROLLBACK
);
975 $this->runTransactionListenerCallbacks( self
::TRIGGER_ROLLBACK
);
977 } catch ( Exception
$e ) {
978 // Already logged; move on...
983 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
984 if ( $this->ignoreErrors() ||
$tempIgnore ) {
985 $this->queryLogger
->debug( "SQL ERROR (ignored): $error\n" );
987 $sql1line = mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 );
988 $this->queryLogger
->error(
989 "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
990 $this->getLogContext( [
991 'method' => __METHOD__
,
994 'sql1line' => $sql1line,
998 $this->queryLogger
->debug( "SQL ERROR: " . $error . "\n" );
999 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
1003 public function freeResult( $res ) {
1006 public function selectField(
1007 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
1009 if ( $var === '*' ) { // sanity
1010 throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
1013 if ( !is_array( $options ) ) {
1014 $options = [ $options ];
1017 $options['LIMIT'] = 1;
1019 $res = $this->select( $table, $var, $cond, $fname, $options );
1020 if ( $res === false ||
!$this->numRows( $res ) ) {
1024 $row = $this->fetchRow( $res );
1026 if ( $row !== false ) {
1027 return reset( $row );
1033 public function selectFieldValues(
1034 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
1036 if ( $var === '*' ) { // sanity
1037 throw new DBUnexpectedError( $this, "Cannot use a * field" );
1038 } elseif ( !is_string( $var ) ) { // sanity
1039 throw new DBUnexpectedError( $this, "Cannot use an array of fields" );
1042 if ( !is_array( $options ) ) {
1043 $options = [ $options ];
1046 $res = $this->select( $table, $var, $cond, $fname, $options, $join_conds );
1047 if ( $res === false ) {
1052 foreach ( $res as $row ) {
1053 $values[] = $row->$var;
1060 * Returns an optional USE INDEX clause to go after the table, and a
1061 * string to go at the end of the query.
1063 * @param array $options Associative array of options to be turned into
1064 * an SQL query, valid keys are listed in the function.
1066 * @see DatabaseBase::select()
1068 public function makeSelectOptions( $options ) {
1069 $preLimitTail = $postLimitTail = '';
1074 foreach ( $options as $key => $option ) {
1075 if ( is_numeric( $key ) ) {
1076 $noKeyOptions[$option] = true;
1080 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1082 $preLimitTail .= $this->makeOrderBy( $options );
1084 // if (isset($options['LIMIT'])) {
1085 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
1086 // isset($options['OFFSET']) ? $options['OFFSET']
1090 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1091 $postLimitTail .= ' FOR UPDATE';
1094 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1095 $postLimitTail .= ' LOCK IN SHARE MODE';
1098 if ( isset( $noKeyOptions['DISTINCT'] ) ||
isset( $noKeyOptions['DISTINCTROW'] ) ) {
1099 $startOpts .= 'DISTINCT';
1102 # Various MySQL extensions
1103 if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
1104 $startOpts .= ' /*! STRAIGHT_JOIN */';
1107 if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) {
1108 $startOpts .= ' HIGH_PRIORITY';
1111 if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
1112 $startOpts .= ' SQL_BIG_RESULT';
1115 if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
1116 $startOpts .= ' SQL_BUFFER_RESULT';
1119 if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
1120 $startOpts .= ' SQL_SMALL_RESULT';
1123 if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
1124 $startOpts .= ' SQL_CALC_FOUND_ROWS';
1127 if ( isset( $noKeyOptions['SQL_CACHE'] ) ) {
1128 $startOpts .= ' SQL_CACHE';
1131 if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) {
1132 $startOpts .= ' SQL_NO_CACHE';
1135 if ( isset( $options['USE INDEX'] ) && is_string( $options['USE INDEX'] ) ) {
1136 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1140 if ( isset( $options['IGNORE INDEX'] ) && is_string( $options['IGNORE INDEX'] ) ) {
1141 $ignoreIndex = $this->ignoreIndexClause( $options['IGNORE INDEX'] );
1146 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1150 * Returns an optional GROUP BY with an optional HAVING
1152 * @param array $options Associative array of options
1154 * @see DatabaseBase::select()
1157 public function makeGroupByWithHaving( $options ) {
1159 if ( isset( $options['GROUP BY'] ) ) {
1160 $gb = is_array( $options['GROUP BY'] )
1161 ?
implode( ',', $options['GROUP BY'] )
1162 : $options['GROUP BY'];
1163 $sql .= ' GROUP BY ' . $gb;
1165 if ( isset( $options['HAVING'] ) ) {
1166 $having = is_array( $options['HAVING'] )
1167 ?
$this->makeList( $options['HAVING'], LIST_AND
)
1168 : $options['HAVING'];
1169 $sql .= ' HAVING ' . $having;
1176 * Returns an optional ORDER BY
1178 * @param array $options Associative array of options
1180 * @see DatabaseBase::select()
1183 public function makeOrderBy( $options ) {
1184 if ( isset( $options['ORDER BY'] ) ) {
1185 $ob = is_array( $options['ORDER BY'] )
1186 ?
implode( ',', $options['ORDER BY'] )
1187 : $options['ORDER BY'];
1189 return ' ORDER BY ' . $ob;
1195 // See IDatabase::select for the docs for this function
1196 public function select( $table, $vars, $conds = '', $fname = __METHOD__
,
1197 $options = [], $join_conds = [] ) {
1198 $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
1200 return $this->query( $sql, $fname );
1203 public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__
,
1204 $options = [], $join_conds = []
1206 if ( is_array( $vars ) ) {
1207 $vars = implode( ',', $this->fieldNamesWithAlias( $vars ) );
1210 $options = (array)$options;
1211 $useIndexes = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
1212 ?
$options['USE INDEX']
1214 $ignoreIndexes = ( isset( $options['IGNORE INDEX'] ) && is_array( $options['IGNORE INDEX'] ) )
1215 ?
$options['IGNORE INDEX']
1218 if ( is_array( $table ) ) {
1220 $this->tableNamesWithIndexClauseOrJOIN( $table, $useIndexes, $ignoreIndexes, $join_conds );
1221 } elseif ( $table != '' ) {
1222 if ( $table[0] == ' ' ) {
1223 $from = ' FROM ' . $table;
1226 $this->tableNamesWithIndexClauseOrJOIN( [ $table ], $useIndexes, $ignoreIndexes, [] );
1232 list( $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ) =
1233 $this->makeSelectOptions( $options );
1235 if ( !empty( $conds ) ) {
1236 if ( is_array( $conds ) ) {
1237 $conds = $this->makeList( $conds, LIST_AND
);
1239 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex WHERE $conds $preLimitTail";
1241 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex $preLimitTail";
1244 if ( isset( $options['LIMIT'] ) ) {
1245 $sql = $this->limitResult( $sql, $options['LIMIT'],
1246 isset( $options['OFFSET'] ) ?
$options['OFFSET'] : false );
1248 $sql = "$sql $postLimitTail";
1250 if ( isset( $options['EXPLAIN'] ) ) {
1251 $sql = 'EXPLAIN ' . $sql;
1257 public function selectRow( $table, $vars, $conds, $fname = __METHOD__
,
1258 $options = [], $join_conds = []
1260 $options = (array)$options;
1261 $options['LIMIT'] = 1;
1262 $res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
1264 if ( $res === false ) {
1268 if ( !$this->numRows( $res ) ) {
1272 $obj = $this->fetchObject( $res );
1277 public function estimateRowCount(
1278 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
1281 $res = $this->select( $table, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options );
1284 $row = $this->fetchRow( $res );
1285 $rows = ( isset( $row['rowcount'] ) ) ?
(int)$row['rowcount'] : 0;
1291 public function selectRowCount(
1292 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
1295 $sql = $this->selectSQLText( $tables, '1', $conds, $fname, $options, $join_conds );
1296 $res = $this->query( "SELECT COUNT(*) AS rowcount FROM ($sql) tmp_count", $fname );
1299 $row = $this->fetchRow( $res );
1300 $rows = ( isset( $row['rowcount'] ) ) ?
(int)$row['rowcount'] : 0;
1307 * Removes most variables from an SQL query and replaces them with X or N for numbers.
1308 * It's only slightly flawed. Don't use for anything important.
1310 * @param string $sql A SQL Query
1314 protected static function generalizeSQL( $sql ) {
1315 # This does the same as the regexp below would do, but in such a way
1316 # as to avoid crashing php on some large strings.
1317 # $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql );
1319 $sql = str_replace( "\\\\", '', $sql );
1320 $sql = str_replace( "\\'", '', $sql );
1321 $sql = str_replace( "\\\"", '', $sql );
1322 $sql = preg_replace( "/'.*'/s", "'X'", $sql );
1323 $sql = preg_replace( '/".*"/s', "'X'", $sql );
1325 # All newlines, tabs, etc replaced by single space
1326 $sql = preg_replace( '/\s+/', ' ', $sql );
1329 # except the ones surrounded by characters, e.g. l10n
1330 $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
1331 $sql = preg_replace( '/(?<![a-zA-Z])-?\d+(?![a-zA-Z])/s', 'N', $sql );
1336 public function fieldExists( $table, $field, $fname = __METHOD__
) {
1337 $info = $this->fieldInfo( $table, $field );
1342 public function indexExists( $table, $index, $fname = __METHOD__
) {
1343 if ( !$this->tableExists( $table ) ) {
1347 $info = $this->indexInfo( $table, $index, $fname );
1348 if ( is_null( $info ) ) {
1351 return $info !== false;
1355 public function tableExists( $table, $fname = __METHOD__
) {
1356 $table = $this->tableName( $table );
1357 $old = $this->ignoreErrors( true );
1358 $res = $this->query( "SELECT 1 FROM $table LIMIT 1", $fname );
1359 $this->ignoreErrors( $old );
1364 public function indexUnique( $table, $index ) {
1365 $indexInfo = $this->indexInfo( $table, $index );
1367 if ( !$indexInfo ) {
1371 return !$indexInfo[0]->Non_unique
;
1375 * Helper for DatabaseBase::insert().
1377 * @param array $options
1380 protected function makeInsertOptions( $options ) {
1381 return implode( ' ', $options );
1384 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
1385 # No rows to insert, easy just return now
1386 if ( !count( $a ) ) {
1390 $table = $this->tableName( $table );
1392 if ( !is_array( $options ) ) {
1393 $options = [ $options ];
1397 if ( isset( $options['fileHandle'] ) ) {
1398 $fh = $options['fileHandle'];
1400 $options = $this->makeInsertOptions( $options );
1402 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
1404 $keys = array_keys( $a[0] );
1407 $keys = array_keys( $a );
1410 $sql = 'INSERT ' . $options .
1411 " INTO $table (" . implode( ',', $keys ) . ') VALUES ';
1415 foreach ( $a as $row ) {
1421 $sql .= '(' . $this->makeList( $row ) . ')';
1424 $sql .= '(' . $this->makeList( $a ) . ')';
1427 if ( $fh !== null && false === fwrite( $fh, $sql ) ) {
1429 } elseif ( $fh !== null ) {
1433 return (bool)$this->query( $sql, $fname );
1437 * Make UPDATE options array for DatabaseBase::makeUpdateOptions
1439 * @param array $options
1442 protected function makeUpdateOptionsArray( $options ) {
1443 if ( !is_array( $options ) ) {
1444 $options = [ $options ];
1449 if ( in_array( 'IGNORE', $options ) ) {
1457 * Make UPDATE options for the DatabaseBase::update function
1459 * @param array $options The options passed to DatabaseBase::update
1462 protected function makeUpdateOptions( $options ) {
1463 $opts = $this->makeUpdateOptionsArray( $options );
1465 return implode( ' ', $opts );
1468 function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
1469 $table = $this->tableName( $table );
1470 $opts = $this->makeUpdateOptions( $options );
1471 $sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET
);
1473 if ( $conds !== [] && $conds !== '*' ) {
1474 $sql .= " WHERE " . $this->makeList( $conds, LIST_AND
);
1477 return $this->query( $sql, $fname );
1480 public function makeList( $a, $mode = LIST_COMMA
) {
1481 if ( !is_array( $a ) ) {
1482 throw new DBUnexpectedError( $this, __METHOD__
. ' called with incorrect parameters' );
1488 foreach ( $a as $field => $value ) {
1490 if ( $mode == LIST_AND
) {
1492 } elseif ( $mode == LIST_OR
) {
1501 if ( ( $mode == LIST_AND ||
$mode == LIST_OR
) && is_numeric( $field ) ) {
1502 $list .= "($value)";
1503 } elseif ( ( $mode == LIST_SET
) && is_numeric( $field ) ) {
1505 } elseif ( ( $mode == LIST_AND ||
$mode == LIST_OR
) && is_array( $value ) ) {
1506 // Remove null from array to be handled separately if found
1507 $includeNull = false;
1508 foreach ( array_keys( $value, null, true ) as $nullKey ) {
1509 $includeNull = true;
1510 unset( $value[$nullKey] );
1512 if ( count( $value ) == 0 && !$includeNull ) {
1513 throw new InvalidArgumentException( __METHOD__
. ": empty input for field $field" );
1514 } elseif ( count( $value ) == 0 ) {
1515 // only check if $field is null
1516 $list .= "$field IS NULL";
1518 // IN clause contains at least one valid element
1519 if ( $includeNull ) {
1520 // Group subconditions to ensure correct precedence
1523 if ( count( $value ) == 1 ) {
1524 // Special-case single values, as IN isn't terribly efficient
1525 // Don't necessarily assume the single key is 0; we don't
1526 // enforce linear numeric ordering on other arrays here.
1527 $value = array_values( $value )[0];
1528 $list .= $field . " = " . $this->addQuotes( $value );
1530 $list .= $field . " IN (" . $this->makeList( $value ) . ") ";
1532 // if null present in array, append IS NULL
1533 if ( $includeNull ) {
1534 $list .= " OR $field IS NULL)";
1537 } elseif ( $value === null ) {
1538 if ( $mode == LIST_AND ||
$mode == LIST_OR
) {
1539 $list .= "$field IS ";
1540 } elseif ( $mode == LIST_SET
) {
1541 $list .= "$field = ";
1545 if ( $mode == LIST_AND ||
$mode == LIST_OR ||
$mode == LIST_SET
) {
1546 $list .= "$field = ";
1548 $list .= $mode == LIST_NAMES ?
$value : $this->addQuotes( $value );
1555 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
1558 foreach ( $data as $base => $sub ) {
1559 if ( count( $sub ) ) {
1560 $conds[] = $this->makeList(
1561 [ $baseKey => $base, $subKey => array_keys( $sub ) ],
1567 return $this->makeList( $conds, LIST_OR
);
1569 // Nothing to search for...
1575 * Return aggregated value alias
1577 * @param array $valuedata
1578 * @param string $valuename
1582 public function aggregateValue( $valuedata, $valuename = 'value' ) {
1586 public function bitNot( $field ) {
1590 public function bitAnd( $fieldLeft, $fieldRight ) {
1591 return "($fieldLeft & $fieldRight)";
1594 public function bitOr( $fieldLeft, $fieldRight ) {
1595 return "($fieldLeft | $fieldRight)";
1598 public function buildConcat( $stringList ) {
1599 return 'CONCAT(' . implode( ',', $stringList ) . ')';
1602 public function buildGroupConcatField(
1603 $delim, $table, $field, $conds = '', $join_conds = []
1605 $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')';
1607 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1611 * @param string $field Field or column to cast
1615 public function buildStringCast( $field ) {
1619 public function selectDB( $db ) {
1620 # Stub. Shouldn't cause serious problems if it's not overridden, but
1621 # if your database engine supports a concept similar to MySQL's
1622 # databases you may as well.
1623 $this->mDBname
= $db;
1628 public function getDBname() {
1629 return $this->mDBname
;
1632 public function getServer() {
1633 return $this->mServer
;
1637 * Format a table name ready for use in constructing an SQL query
1639 * This does two important things: it quotes the table names to clean them up,
1640 * and it adds a table prefix if only given a table name with no quotes.
1642 * All functions of this object which require a table name call this function
1643 * themselves. Pass the canonical name to such functions. This is only needed
1644 * when calling query() directly.
1646 * @note This function does not sanitize user input. It is not safe to use
1647 * this function to escape user input.
1648 * @param string $name Database table name
1649 * @param string $format One of:
1650 * quoted - Automatically pass the table name through addIdentifierQuotes()
1651 * so that it can be used in a query.
1652 * raw - Do not add identifier quotes to the table name
1653 * @return string Full database name
1655 public function tableName( $name, $format = 'quoted' ) {
1656 # Skip the entire process when we have a string quoted on both ends.
1657 # Note that we check the end so that we will still quote any use of
1658 # use of `database`.table. But won't break things if someone wants
1659 # to query a database table with a dot in the name.
1660 if ( $this->isQuotedIdentifier( $name ) ) {
1664 # Lets test for any bits of text that should never show up in a table
1665 # name. Basically anything like JOIN or ON which are actually part of
1666 # SQL queries, but may end up inside of the table value to combine
1667 # sql. Such as how the API is doing.
1668 # Note that we use a whitespace test rather than a \b test to avoid
1669 # any remote case where a word like on may be inside of a table name
1670 # surrounded by symbols which may be considered word breaks.
1671 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
1675 # Split database and table into proper variables.
1676 # We reverse the explode so that database.table and table both output
1677 # the correct table.
1678 $dbDetails = explode( '.', $name, 3 );
1679 if ( count( $dbDetails ) == 3 ) {
1680 list( $database, $schema, $table ) = $dbDetails;
1681 # We don't want any prefix added in this case
1683 } elseif ( count( $dbDetails ) == 2 ) {
1684 list( $database, $table ) = $dbDetails;
1685 # We don't want any prefix added in this case
1686 # In dbs that support it, $database may actually be the schema
1687 # but that doesn't affect any of the functionality here
1691 list( $table ) = $dbDetails;
1692 if ( isset( $this->tableAliases
[$table] ) ) {
1693 $database = $this->tableAliases
[$table]['dbname'];
1694 $schema = is_string( $this->tableAliases
[$table]['schema'] )
1695 ?
$this->tableAliases
[$table]['schema']
1697 $prefix = is_string( $this->tableAliases
[$table]['prefix'] )
1698 ?
$this->tableAliases
[$table]['prefix']
1699 : $this->mTablePrefix
;
1702 $schema = $this->mSchema
; # Default schema
1703 $prefix = $this->mTablePrefix
; # Default prefix
1707 # Quote $table and apply the prefix if not quoted.
1708 # $tableName might be empty if this is called from Database::replaceVars()
1709 $tableName = "{$prefix}{$table}";
1710 if ( $format == 'quoted'
1711 && !$this->isQuotedIdentifier( $tableName ) && $tableName !== ''
1713 $tableName = $this->addIdentifierQuotes( $tableName );
1716 # Quote $schema and merge it with the table name if needed
1717 if ( strlen( $schema ) ) {
1718 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $schema ) ) {
1719 $schema = $this->addIdentifierQuotes( $schema );
1721 $tableName = $schema . '.' . $tableName;
1724 # Quote $database and merge it with the table name if needed
1725 if ( $database !== '' ) {
1726 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $database ) ) {
1727 $database = $this->addIdentifierQuotes( $database );
1729 $tableName = $database . '.' . $tableName;
1736 * Fetch a number of table names into an array
1737 * This is handy when you need to construct SQL for joins
1740 * extract( $dbr->tableNames( 'user', 'watchlist' ) );
1741 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1742 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1746 public function tableNames() {
1747 $inArray = func_get_args();
1750 foreach ( $inArray as $name ) {
1751 $retVal[$name] = $this->tableName( $name );
1758 * Fetch a number of table names into an zero-indexed numerical array
1759 * This is handy when you need to construct SQL for joins
1762 * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' );
1763 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1764 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1768 public function tableNamesN() {
1769 $inArray = func_get_args();
1772 foreach ( $inArray as $name ) {
1773 $retVal[] = $this->tableName( $name );
1780 * Get an aliased table name
1781 * e.g. tableName AS newTableName
1783 * @param string $name Table name, see tableName()
1784 * @param string|bool $alias Alias (optional)
1785 * @return string SQL name for aliased table. Will not alias a table to its own name
1787 public function tableNameWithAlias( $name, $alias = false ) {
1788 if ( !$alias ||
$alias == $name ) {
1789 return $this->tableName( $name );
1791 return $this->tableName( $name ) . ' ' . $this->addIdentifierQuotes( $alias );
1796 * Gets an array of aliased table names
1798 * @param array $tables [ [alias] => table ]
1799 * @return string[] See tableNameWithAlias()
1801 public function tableNamesWithAlias( $tables ) {
1803 foreach ( $tables as $alias => $table ) {
1804 if ( is_numeric( $alias ) ) {
1807 $retval[] = $this->tableNameWithAlias( $table, $alias );
1814 * Get an aliased field name
1815 * e.g. fieldName AS newFieldName
1817 * @param string $name Field name
1818 * @param string|bool $alias Alias (optional)
1819 * @return string SQL name for aliased field. Will not alias a field to its own name
1821 public function fieldNameWithAlias( $name, $alias = false ) {
1822 if ( !$alias ||
(string)$alias === (string)$name ) {
1825 return $name . ' AS ' . $this->addIdentifierQuotes( $alias ); // PostgreSQL needs AS
1830 * Gets an array of aliased field names
1832 * @param array $fields [ [alias] => field ]
1833 * @return string[] See fieldNameWithAlias()
1835 public function fieldNamesWithAlias( $fields ) {
1837 foreach ( $fields as $alias => $field ) {
1838 if ( is_numeric( $alias ) ) {
1841 $retval[] = $this->fieldNameWithAlias( $field, $alias );
1848 * Get the aliased table name clause for a FROM clause
1849 * which might have a JOIN and/or USE INDEX or IGNORE INDEX clause
1851 * @param array $tables ( [alias] => table )
1852 * @param array $use_index Same as for select()
1853 * @param array $ignore_index Same as for select()
1854 * @param array $join_conds Same as for select()
1857 protected function tableNamesWithIndexClauseOrJOIN(
1858 $tables, $use_index = [], $ignore_index = [], $join_conds = []
1862 $use_index = (array)$use_index;
1863 $ignore_index = (array)$ignore_index;
1864 $join_conds = (array)$join_conds;
1866 foreach ( $tables as $alias => $table ) {
1867 if ( !is_string( $alias ) ) {
1868 // No alias? Set it equal to the table name
1871 // Is there a JOIN clause for this table?
1872 if ( isset( $join_conds[$alias] ) ) {
1873 list( $joinType, $conds ) = $join_conds[$alias];
1874 $tableClause = $joinType;
1875 $tableClause .= ' ' . $this->tableNameWithAlias( $table, $alias );
1876 if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
1877 $use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
1879 $tableClause .= ' ' . $use;
1882 if ( isset( $ignore_index[$alias] ) ) { // has IGNORE INDEX?
1883 $ignore = $this->ignoreIndexClause( implode( ',', (array)$ignore_index[$alias] ) );
1884 if ( $ignore != '' ) {
1885 $tableClause .= ' ' . $ignore;
1888 $on = $this->makeList( (array)$conds, LIST_AND
);
1890 $tableClause .= ' ON (' . $on . ')';
1893 $retJOIN[] = $tableClause;
1894 } elseif ( isset( $use_index[$alias] ) ) {
1895 // Is there an INDEX clause for this table?
1896 $tableClause = $this->tableNameWithAlias( $table, $alias );
1897 $tableClause .= ' ' . $this->useIndexClause(
1898 implode( ',', (array)$use_index[$alias] )
1901 $ret[] = $tableClause;
1902 } elseif ( isset( $ignore_index[$alias] ) ) {
1903 // Is there an INDEX clause for this table?
1904 $tableClause = $this->tableNameWithAlias( $table, $alias );
1905 $tableClause .= ' ' . $this->ignoreIndexClause(
1906 implode( ',', (array)$ignore_index[$alias] )
1909 $ret[] = $tableClause;
1911 $tableClause = $this->tableNameWithAlias( $table, $alias );
1913 $ret[] = $tableClause;
1917 // We can't separate explicit JOIN clauses with ',', use ' ' for those
1918 $implicitJoins = !empty( $ret ) ?
implode( ',', $ret ) : "";
1919 $explicitJoins = !empty( $retJOIN ) ?
implode( ' ', $retJOIN ) : "";
1921 // Compile our final table clause
1922 return implode( ' ', [ $implicitJoins, $explicitJoins ] );
1926 * Get the name of an index in a given table.
1928 * @param string $index
1931 protected function indexName( $index ) {
1932 // Backwards-compatibility hack
1934 'ar_usertext_timestamp' => 'usertext_timestamp',
1935 'un_user_id' => 'user_id',
1936 'un_user_ip' => 'user_ip',
1939 if ( isset( $renamed[$index] ) ) {
1940 return $renamed[$index];
1946 public function addQuotes( $s ) {
1947 if ( $s instanceof Blob
) {
1950 if ( $s === null ) {
1953 # This will also quote numeric values. This should be harmless,
1954 # and protects against weird problems that occur when they really
1955 # _are_ strings such as article titles and string->number->string
1956 # conversion is not 1:1.
1957 return "'" . $this->strencode( $s ) . "'";
1962 * Quotes an identifier using `backticks` or "double quotes" depending on the database type.
1963 * MySQL uses `backticks` while basically everything else uses double quotes.
1964 * Since MySQL is the odd one out here the double quotes are our generic
1965 * and we implement backticks in DatabaseMysql.
1970 public function addIdentifierQuotes( $s ) {
1971 return '"' . str_replace( '"', '""', $s ) . '"';
1975 * Returns if the given identifier looks quoted or not according to
1976 * the database convention for quoting identifiers .
1978 * @note Do not use this to determine if untrusted input is safe.
1979 * A malicious user can trick this function.
1980 * @param string $name
1983 public function isQuotedIdentifier( $name ) {
1984 return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
1991 protected function escapeLikeInternal( $s ) {
1992 return addcslashes( $s, '\%_' );
1995 public function buildLike() {
1996 $params = func_get_args();
1998 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
1999 $params = $params[0];
2004 foreach ( $params as $value ) {
2005 if ( $value instanceof LikeMatch
) {
2006 $s .= $value->toString();
2008 $s .= $this->escapeLikeInternal( $value );
2012 return " LIKE {$this->addQuotes( $s )} ";
2015 public function anyChar() {
2016 return new LikeMatch( '_' );
2019 public function anyString() {
2020 return new LikeMatch( '%' );
2023 public function nextSequenceValue( $seqName ) {
2028 * USE INDEX clause. Unlikely to be useful for anything but MySQL. This
2029 * is only needed because a) MySQL must be as efficient as possible due to
2030 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2031 * which index to pick. Anyway, other databases might have different
2032 * indexes on a given table. So don't bother overriding this unless you're
2034 * @param string $index
2037 public function useIndexClause( $index ) {
2042 * IGNORE INDEX clause. Unlikely to be useful for anything but MySQL. This
2043 * is only needed because a) MySQL must be as efficient as possible due to
2044 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2045 * which index to pick. Anyway, other databases might have different
2046 * indexes on a given table. So don't bother overriding this unless you're
2048 * @param string $index
2051 public function ignoreIndexClause( $index ) {
2055 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
2056 $quotedTable = $this->tableName( $table );
2058 if ( count( $rows ) == 0 ) {
2063 if ( !is_array( reset( $rows ) ) ) {
2067 // @FXIME: this is not atomic, but a trx would break affectedRows()
2068 foreach ( $rows as $row ) {
2069 # Delete rows which collide
2070 if ( $uniqueIndexes ) {
2071 $sql = "DELETE FROM $quotedTable WHERE ";
2073 foreach ( $uniqueIndexes as $index ) {
2080 if ( is_array( $index ) ) {
2082 foreach ( $index as $col ) {
2088 $sql .= $col . '=' . $this->addQuotes( $row[$col] );
2091 $sql .= $index . '=' . $this->addQuotes( $row[$index] );
2095 $this->query( $sql, $fname );
2098 # Now insert the row
2099 $this->insert( $table, $row, $fname );
2104 * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
2107 * @param string $table Table name
2108 * @param array|string $rows Row(s) to insert
2109 * @param string $fname Caller function name
2111 * @return ResultWrapper
2113 protected function nativeReplace( $table, $rows, $fname ) {
2114 $table = $this->tableName( $table );
2117 if ( !is_array( reset( $rows ) ) ) {
2121 $sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) . ') VALUES ';
2124 foreach ( $rows as $row ) {
2131 $sql .= '(' . $this->makeList( $row ) . ')';
2134 return $this->query( $sql, $fname );
2137 public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
2140 if ( !count( $rows ) ) {
2141 return true; // nothing to do
2144 if ( !is_array( reset( $rows ) ) ) {
2148 if ( count( $uniqueIndexes ) ) {
2149 $clauses = []; // list WHERE clauses that each identify a single row
2150 foreach ( $rows as $row ) {
2151 foreach ( $uniqueIndexes as $index ) {
2152 $index = is_array( $index ) ?
$index : [ $index ]; // columns
2153 $rowKey = []; // unique key to this row
2154 foreach ( $index as $column ) {
2155 $rowKey[$column] = $row[$column];
2157 $clauses[] = $this->makeList( $rowKey, LIST_AND
);
2160 $where = [ $this->makeList( $clauses, LIST_OR
) ];
2165 $useTrx = !$this->mTrxLevel
;
2167 $this->begin( $fname, self
::TRANSACTION_INTERNAL
);
2170 # Update any existing conflicting row(s)
2171 if ( $where !== false ) {
2172 $ok = $this->update( $table, $set, $where, $fname );
2176 # Now insert any non-conflicting row(s)
2177 $ok = $this->insert( $table, $rows, $fname, [ 'IGNORE' ] ) && $ok;
2178 } catch ( Exception
$e ) {
2180 $this->rollback( $fname, self
::FLUSHING_INTERNAL
);
2185 $this->commit( $fname, self
::FLUSHING_INTERNAL
);
2191 public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
2195 throw new DBUnexpectedError( $this, __METHOD__
. ' called with empty $conds' );
2198 $delTable = $this->tableName( $delTable );
2199 $joinTable = $this->tableName( $joinTable );
2200 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
2201 if ( $conds != '*' ) {
2202 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
2206 $this->query( $sql, $fname );
2210 * Returns the size of a text field, or -1 for "unlimited"
2212 * @param string $table
2213 * @param string $field
2216 public function textFieldSize( $table, $field ) {
2217 $table = $this->tableName( $table );
2218 $sql = "SHOW COLUMNS FROM $table LIKE \"$field\";";
2219 $res = $this->query( $sql, __METHOD__
);
2220 $row = $this->fetchObject( $res );
2224 if ( preg_match( '/\((.*)\)/', $row->Type
, $m ) ) {
2233 public function delete( $table, $conds, $fname = __METHOD__
) {
2235 throw new DBUnexpectedError( $this, __METHOD__
. ' called with no conditions' );
2238 $table = $this->tableName( $table );
2239 $sql = "DELETE FROM $table";
2241 if ( $conds != '*' ) {
2242 if ( is_array( $conds ) ) {
2243 $conds = $this->makeList( $conds, LIST_AND
);
2245 $sql .= ' WHERE ' . $conds;
2248 return $this->query( $sql, $fname );
2251 public function insertSelect(
2252 $destTable, $srcTable, $varMap, $conds,
2253 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
2255 if ( $this->cliMode
) {
2256 // For massive migrations with downtime, we don't want to select everything
2257 // into memory and OOM, so do all this native on the server side if possible.
2258 return $this->nativeInsertSelect(
2269 // For web requests, do a locking SELECT and then INSERT. This puts the SELECT burden
2270 // on only the master (without needing row-based-replication). It also makes it easy to
2271 // know how big the INSERT is going to be.
2273 foreach ( $varMap as $dstColumn => $sourceColumnOrSql ) {
2274 $fields[] = $this->fieldNameWithAlias( $sourceColumnOrSql, $dstColumn );
2276 $selectOptions[] = 'FOR UPDATE';
2277 $res = $this->select( $srcTable, implode( ',', $fields ), $conds, $fname, $selectOptions );
2283 foreach ( $res as $row ) {
2284 $rows[] = (array)$row;
2287 return $this->insert( $destTable, $rows, $fname, $insertOptions );
2290 public function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds,
2291 $fname = __METHOD__
,
2292 $insertOptions = [], $selectOptions = []
2294 $destTable = $this->tableName( $destTable );
2296 if ( !is_array( $insertOptions ) ) {
2297 $insertOptions = [ $insertOptions ];
2300 $insertOptions = $this->makeInsertOptions( $insertOptions );
2302 if ( !is_array( $selectOptions ) ) {
2303 $selectOptions = [ $selectOptions ];
2306 list( $startOpts, $useIndex, $tailOpts, $ignoreIndex ) = $this->makeSelectOptions(
2309 if ( is_array( $srcTable ) ) {
2310 $srcTable = implode( ',', array_map( [ &$this, 'tableName' ], $srcTable ) );
2312 $srcTable = $this->tableName( $srcTable );
2315 $sql = "INSERT $insertOptions INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
2316 " SELECT $startOpts " . implode( ',', $varMap ) .
2317 " FROM $srcTable $useIndex $ignoreIndex ";
2319 if ( $conds != '*' ) {
2320 if ( is_array( $conds ) ) {
2321 $conds = $this->makeList( $conds, LIST_AND
);
2323 $sql .= " WHERE $conds";
2326 $sql .= " $tailOpts";
2328 return $this->query( $sql, $fname );
2332 * Construct a LIMIT query with optional offset. This is used for query
2333 * pages. The SQL should be adjusted so that only the first $limit rows
2334 * are returned. If $offset is provided as well, then the first $offset
2335 * rows should be discarded, and the next $limit rows should be returned.
2336 * If the result of the query is not ordered, then the rows to be returned
2337 * are theoretically arbitrary.
2339 * $sql is expected to be a SELECT, if that makes a difference.
2341 * The version provided by default works in MySQL and SQLite. It will very
2342 * likely need to be overridden for most other DBMSes.
2344 * @param string $sql SQL query we will append the limit too
2345 * @param int $limit The SQL limit
2346 * @param int|bool $offset The SQL offset (default false)
2347 * @throws DBUnexpectedError
2350 public function limitResult( $sql, $limit, $offset = false ) {
2351 if ( !is_numeric( $limit ) ) {
2352 throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" );
2355 return "$sql LIMIT "
2356 . ( ( is_numeric( $offset ) && $offset != 0 ) ?
"{$offset}," : "" )
2360 public function unionSupportsOrderAndLimit() {
2361 return true; // True for almost every DB supported
2364 public function unionQueries( $sqls, $all ) {
2365 $glue = $all ?
') UNION ALL (' : ') UNION (';
2367 return '(' . implode( $glue, $sqls ) . ')';
2370 public function conditional( $cond, $trueVal, $falseVal ) {
2371 if ( is_array( $cond ) ) {
2372 $cond = $this->makeList( $cond, LIST_AND
);
2375 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
2378 public function strreplace( $orig, $old, $new ) {
2379 return "REPLACE({$orig}, {$old}, {$new})";
2382 public function getServerUptime() {
2386 public function wasDeadlock() {
2390 public function wasLockTimeout() {
2394 public function wasErrorReissuable() {
2398 public function wasReadOnlyError() {
2403 * Determines if the given query error was a connection drop
2406 * @param integer|string $errno
2409 public function wasConnectionError( $errno ) {
2414 * Perform a deadlock-prone transaction.
2416 * This function invokes a callback function to perform a set of write
2417 * queries. If a deadlock occurs during the processing, the transaction
2418 * will be rolled back and the callback function will be called again.
2420 * Avoid using this method outside of Job or Maintenance classes.
2423 * $dbw->deadlockLoop( callback, ... );
2425 * Extra arguments are passed through to the specified callback function.
2426 * This method requires that no transactions are already active to avoid
2427 * causing premature commits or exceptions.
2429 * Returns whatever the callback function returned on its successful,
2430 * iteration, or false on error, for example if the retry limit was
2434 * @throws DBUnexpectedError
2437 public function deadlockLoop() {
2438 $args = func_get_args();
2439 $function = array_shift( $args );
2440 $tries = self
::DEADLOCK_TRIES
;
2442 $this->begin( __METHOD__
);
2445 /** @var Exception $e */
2449 $retVal = call_user_func_array( $function, $args );
2451 } catch ( DBQueryError
$e ) {
2452 if ( $this->wasDeadlock() ) {
2453 // Retry after a randomized delay
2454 usleep( mt_rand( self
::DEADLOCK_DELAY_MIN
, self
::DEADLOCK_DELAY_MAX
) );
2456 // Throw the error back up
2460 } while ( --$tries > 0 );
2462 if ( $tries <= 0 ) {
2463 // Too many deadlocks; give up
2464 $this->rollback( __METHOD__
);
2467 $this->commit( __METHOD__
);
2473 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
2474 # Real waits are implemented in the subclass.
2478 public function getSlavePos() {
2483 public function getMasterPos() {
2488 public function serverIsReadOnly() {
2492 final public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
2493 if ( !$this->mTrxLevel
) {
2494 throw new DBUnexpectedError( $this, "No transaction is active." );
2496 $this->mTrxEndCallbacks
[] = [ $callback, $fname ];
2499 final public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
2500 $this->mTrxIdleCallbacks
[] = [ $callback, $fname ];
2501 if ( !$this->mTrxLevel
) {
2502 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_IDLE
);
2506 final public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
2507 if ( $this->mTrxLevel
) {
2508 $this->mTrxPreCommitCallbacks
[] = [ $callback, $fname ];
2510 // If no transaction is active, then make one for this callback
2511 $this->startAtomic( __METHOD__
);
2513 call_user_func( $callback );
2514 $this->endAtomic( __METHOD__
);
2515 } catch ( Exception
$e ) {
2516 $this->rollback( __METHOD__
, self
::FLUSHING_INTERNAL
);
2522 final public function setTransactionListener( $name, callable
$callback = null ) {
2524 $this->mTrxRecurringCallbacks
[$name] = $callback;
2526 unset( $this->mTrxRecurringCallbacks
[$name] );
2531 * Whether to disable running of post-COMMIT/ROLLBACK callbacks
2533 * This method should not be used outside of Database/LoadBalancer
2535 * @param bool $suppress
2538 final public function setTrxEndCallbackSuppression( $suppress ) {
2539 $this->mTrxEndCallbacksSuppressed
= $suppress;
2543 * Actually run and consume any "on transaction idle/resolution" callbacks.
2545 * This method should not be used outside of Database/LoadBalancer
2547 * @param integer $trigger IDatabase::TRIGGER_* constant
2551 public function runOnTransactionIdleCallbacks( $trigger ) {
2552 if ( $this->mTrxEndCallbacksSuppressed
) {
2556 $autoTrx = $this->getFlag( DBO_TRX
); // automatic begin() enabled?
2557 /** @var Exception $e */
2558 $e = null; // first exception
2559 do { // callbacks may add callbacks :)
2560 $callbacks = array_merge(
2561 $this->mTrxIdleCallbacks
,
2562 $this->mTrxEndCallbacks
// include "transaction resolution" callbacks
2564 $this->mTrxIdleCallbacks
= []; // consumed (and recursion guard)
2565 $this->mTrxEndCallbacks
= []; // consumed (recursion guard)
2566 foreach ( $callbacks as $callback ) {
2568 list( $phpCallback ) = $callback;
2569 $this->clearFlag( DBO_TRX
); // make each query its own transaction
2570 call_user_func_array( $phpCallback, [ $trigger ] );
2572 $this->setFlag( DBO_TRX
); // restore automatic begin()
2574 $this->clearFlag( DBO_TRX
); // restore auto-commit
2576 } catch ( Exception
$ex ) {
2577 call_user_func( $this->errorLogger
, $ex );
2579 // Some callbacks may use startAtomic/endAtomic, so make sure
2580 // their transactions are ended so other callbacks don't fail
2581 if ( $this->trxLevel() ) {
2582 $this->rollback( __METHOD__
, self
::FLUSHING_INTERNAL
);
2586 } while ( count( $this->mTrxIdleCallbacks
) );
2588 if ( $e instanceof Exception
) {
2589 throw $e; // re-throw any first exception
2594 * Actually run and consume any "on transaction pre-commit" callbacks.
2596 * This method should not be used outside of Database/LoadBalancer
2601 public function runOnTransactionPreCommitCallbacks() {
2602 $e = null; // first exception
2603 do { // callbacks may add callbacks :)
2604 $callbacks = $this->mTrxPreCommitCallbacks
;
2605 $this->mTrxPreCommitCallbacks
= []; // consumed (and recursion guard)
2606 foreach ( $callbacks as $callback ) {
2608 list( $phpCallback ) = $callback;
2609 call_user_func( $phpCallback );
2610 } catch ( Exception
$ex ) {
2611 call_user_func( $this->errorLogger
, $ex );
2615 } while ( count( $this->mTrxPreCommitCallbacks
) );
2617 if ( $e instanceof Exception
) {
2618 throw $e; // re-throw any first exception
2623 * Actually run any "transaction listener" callbacks.
2625 * This method should not be used outside of Database/LoadBalancer
2627 * @param integer $trigger IDatabase::TRIGGER_* constant
2631 public function runTransactionListenerCallbacks( $trigger ) {
2632 if ( $this->mTrxEndCallbacksSuppressed
) {
2636 /** @var Exception $e */
2637 $e = null; // first exception
2639 foreach ( $this->mTrxRecurringCallbacks
as $phpCallback ) {
2641 $phpCallback( $trigger, $this );
2642 } catch ( Exception
$ex ) {
2643 call_user_func( $this->errorLogger
, $ex );
2648 if ( $e instanceof Exception
) {
2649 throw $e; // re-throw any first exception
2653 final public function startAtomic( $fname = __METHOD__
) {
2654 if ( !$this->mTrxLevel
) {
2655 $this->begin( $fname, self
::TRANSACTION_INTERNAL
);
2656 // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
2657 // in all changes being in one transaction to keep requests transactional.
2658 if ( !$this->getFlag( DBO_TRX
) ) {
2659 $this->mTrxAutomaticAtomic
= true;
2663 $this->mTrxAtomicLevels
[] = $fname;
2666 final public function endAtomic( $fname = __METHOD__
) {
2667 if ( !$this->mTrxLevel
) {
2668 throw new DBUnexpectedError( $this, "No atomic transaction is open (got $fname)." );
2670 if ( !$this->mTrxAtomicLevels ||
2671 array_pop( $this->mTrxAtomicLevels
) !== $fname
2673 throw new DBUnexpectedError( $this, "Invalid atomic section ended (got $fname)." );
2676 if ( !$this->mTrxAtomicLevels
&& $this->mTrxAutomaticAtomic
) {
2677 $this->commit( $fname, self
::FLUSHING_INTERNAL
);
2681 final public function doAtomicSection( $fname, callable
$callback ) {
2682 $this->startAtomic( $fname );
2684 $res = call_user_func_array( $callback, [ $this, $fname ] );
2685 } catch ( Exception
$e ) {
2686 $this->rollback( $fname, self
::FLUSHING_INTERNAL
);
2689 $this->endAtomic( $fname );
2694 final public function begin( $fname = __METHOD__
, $mode = self
::TRANSACTION_EXPLICIT
) {
2695 // Protect against mismatched atomic section, transaction nesting, and snapshot loss
2696 if ( $this->mTrxLevel
) {
2697 if ( $this->mTrxAtomicLevels
) {
2698 $levels = implode( ', ', $this->mTrxAtomicLevels
);
2699 $msg = "$fname: Got explicit BEGIN while atomic section(s) $levels are open.";
2700 throw new DBUnexpectedError( $this, $msg );
2701 } elseif ( !$this->mTrxAutomatic
) {
2702 $msg = "$fname: Explicit transaction already active (from {$this->mTrxFname}).";
2703 throw new DBUnexpectedError( $this, $msg );
2705 // @TODO: make this an exception at some point
2706 $msg = "$fname: Implicit transaction already active (from {$this->mTrxFname}).";
2707 $this->queryLogger
->error( $msg );
2708 return; // join the main transaction set
2710 } elseif ( $this->getFlag( DBO_TRX
) && $mode !== self
::TRANSACTION_INTERNAL
) {
2711 // @TODO: make this an exception at some point
2712 $msg = "$fname: Implicit transaction expected (DBO_TRX set).";
2713 $this->queryLogger
->error( $msg );
2714 return; // let any writes be in the main transaction
2717 // Avoid fatals if close() was called
2718 $this->assertOpen();
2720 $this->doBegin( $fname );
2721 $this->mTrxTimestamp
= microtime( true );
2722 $this->mTrxFname
= $fname;
2723 $this->mTrxDoneWrites
= false;
2724 $this->mTrxAutomatic
= ( $mode === self
::TRANSACTION_INTERNAL
);
2725 $this->mTrxAutomaticAtomic
= false;
2726 $this->mTrxAtomicLevels
= [];
2727 $this->mTrxShortId
= sprintf( '%06x', mt_rand( 0, 0xffffff ) );
2728 $this->mTrxWriteDuration
= 0.0;
2729 $this->mTrxWriteQueryCount
= 0;
2730 $this->mTrxWriteAdjDuration
= 0.0;
2731 $this->mTrxWriteAdjQueryCount
= 0;
2732 $this->mTrxWriteCallers
= [];
2733 // First SELECT after BEGIN will establish the snapshot in REPEATABLE-READ.
2734 // Get an estimate of the replica DB lag before then, treating estimate staleness
2735 // as lag itself just to be safe
2736 $status = $this->getApproximateLagStatus();
2737 $this->mTrxReplicaLag
= $status['lag'] +
( microtime( true ) - $status['since'] );
2741 * Issues the BEGIN command to the database server.
2743 * @see DatabaseBase::begin()
2744 * @param string $fname
2746 protected function doBegin( $fname ) {
2747 $this->query( 'BEGIN', $fname );
2748 $this->mTrxLevel
= 1;
2751 final public function commit( $fname = __METHOD__
, $flush = '' ) {
2752 if ( $this->mTrxLevel
&& $this->mTrxAtomicLevels
) {
2753 // There are still atomic sections open. This cannot be ignored
2754 $levels = implode( ', ', $this->mTrxAtomicLevels
);
2755 throw new DBUnexpectedError(
2757 "$fname: Got COMMIT while atomic sections $levels are still open."
2761 if ( $flush === self
::FLUSHING_INTERNAL ||
$flush === self
::FLUSHING_ALL_PEERS
) {
2762 if ( !$this->mTrxLevel
) {
2763 return; // nothing to do
2764 } elseif ( !$this->mTrxAutomatic
) {
2765 throw new DBUnexpectedError(
2767 "$fname: Flushing an explicit transaction, getting out of sync."
2771 if ( !$this->mTrxLevel
) {
2772 $this->queryLogger
->error( "$fname: No transaction to commit, something got out of sync." );
2773 return; // nothing to do
2774 } elseif ( $this->mTrxAutomatic
) {
2775 // @TODO: make this an exception at some point
2776 $msg = "$fname: Explicit commit of implicit transaction.";
2777 $this->queryLogger
->error( $msg );
2778 return; // wait for the main transaction set commit round
2782 // Avoid fatals if close() was called
2783 $this->assertOpen();
2785 $this->runOnTransactionPreCommitCallbacks();
2786 $writeTime = $this->pendingWriteQueryDuration( self
::ESTIMATE_DB_APPLY
);
2787 $this->doCommit( $fname );
2788 if ( $this->mTrxDoneWrites
) {
2789 $this->mDoneWrites
= microtime( true );
2790 $this->trxProfiler
->transactionWritingOut(
2791 $this->mServer
, $this->mDBname
, $this->mTrxShortId
, $writeTime );
2794 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_COMMIT
);
2795 $this->runTransactionListenerCallbacks( self
::TRIGGER_COMMIT
);
2799 * Issues the COMMIT command to the database server.
2801 * @see DatabaseBase::commit()
2802 * @param string $fname
2804 protected function doCommit( $fname ) {
2805 if ( $this->mTrxLevel
) {
2806 $this->query( 'COMMIT', $fname );
2807 $this->mTrxLevel
= 0;
2811 final public function rollback( $fname = __METHOD__
, $flush = '' ) {
2812 if ( $flush === self
::FLUSHING_INTERNAL ||
$flush === self
::FLUSHING_ALL_PEERS
) {
2813 if ( !$this->mTrxLevel
) {
2814 return; // nothing to do
2817 if ( !$this->mTrxLevel
) {
2818 $this->queryLogger
->error(
2819 "$fname: No transaction to rollback, something got out of sync." );
2820 return; // nothing to do
2821 } elseif ( $this->getFlag( DBO_TRX
) ) {
2822 throw new DBUnexpectedError(
2824 "$fname: Expected mass rollback of all peer databases (DBO_TRX set)."
2829 // Avoid fatals if close() was called
2830 $this->assertOpen();
2832 $this->doRollback( $fname );
2833 $this->mTrxAtomicLevels
= [];
2834 if ( $this->mTrxDoneWrites
) {
2835 $this->trxProfiler
->transactionWritingOut(
2836 $this->mServer
, $this->mDBname
, $this->mTrxShortId
);
2839 $this->mTrxIdleCallbacks
= []; // clear
2840 $this->mTrxPreCommitCallbacks
= []; // clear
2841 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_ROLLBACK
);
2842 $this->runTransactionListenerCallbacks( self
::TRIGGER_ROLLBACK
);
2846 * Issues the ROLLBACK command to the database server.
2848 * @see DatabaseBase::rollback()
2849 * @param string $fname
2851 protected function doRollback( $fname ) {
2852 if ( $this->mTrxLevel
) {
2853 # Disconnects cause rollback anyway, so ignore those errors
2854 $ignoreErrors = true;
2855 $this->query( 'ROLLBACK', $fname, $ignoreErrors );
2856 $this->mTrxLevel
= 0;
2860 public function flushSnapshot( $fname = __METHOD__
) {
2861 if ( $this->writesOrCallbacksPending() ||
$this->explicitTrxActive() ) {
2862 // This only flushes transactions to clear snapshots, not to write data
2863 $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
2864 throw new DBUnexpectedError(
2866 "$fname: Cannot COMMIT to clear snapshot because writes are pending ($fnames)."
2870 $this->commit( $fname, self
::FLUSHING_INTERNAL
);
2873 public function explicitTrxActive() {
2874 return $this->mTrxLevel
&& ( $this->mTrxAtomicLevels ||
!$this->mTrxAutomatic
);
2878 * Creates a new table with structure copied from existing table
2879 * Note that unlike most database abstraction functions, this function does not
2880 * automatically append database prefix, because it works at a lower
2881 * abstraction level.
2882 * The table names passed to this function shall not be quoted (this
2883 * function calls addIdentifierQuotes when needed).
2885 * @param string $oldName Name of table whose structure should be copied
2886 * @param string $newName Name of table to be created
2887 * @param bool $temporary Whether the new table should be temporary
2888 * @param string $fname Calling function name
2889 * @throws RuntimeException
2890 * @return bool True if operation was successful
2892 public function duplicateTableStructure( $oldName, $newName, $temporary = false,
2895 throw new RuntimeException( __METHOD__
. ' is not implemented in descendant class' );
2898 function listTables( $prefix = null, $fname = __METHOD__
) {
2899 throw new RuntimeException( __METHOD__
. ' is not implemented in descendant class' );
2903 * Reset the views process cache set by listViews()
2906 final public function clearViewsCache() {
2907 $this->allViews
= null;
2911 * Lists all the VIEWs in the database
2913 * For caching purposes the list of all views should be stored in
2914 * $this->allViews. The process cache can be cleared with clearViewsCache()
2916 * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_
2917 * @param string $fname Name of calling function
2918 * @throws RuntimeException
2922 public function listViews( $prefix = null, $fname = __METHOD__
) {
2923 throw new RuntimeException( __METHOD__
. ' is not implemented in descendant class' );
2927 * Differentiates between a TABLE and a VIEW
2929 * @param string $name Name of the database-structure to test.
2930 * @throws RuntimeException
2934 public function isView( $name ) {
2935 throw new RuntimeException( __METHOD__
. ' is not implemented in descendant class' );
2938 public function timestamp( $ts = 0 ) {
2939 $t = new ConvertableTimestamp( $ts );
2940 // Let errors bubble up to avoid putting garbage in the DB
2941 return $t->getTimestamp( TS_MW
);
2944 public function timestampOrNull( $ts = null ) {
2945 if ( is_null( $ts ) ) {
2948 return $this->timestamp( $ts );
2953 * Take the result from a query, and wrap it in a ResultWrapper if
2954 * necessary. Boolean values are passed through as is, to indicate success
2955 * of write queries or failure.
2957 * Once upon a time, DatabaseBase::query() returned a bare MySQL result
2958 * resource, and it was necessary to call this function to convert it to
2959 * a wrapper. Nowadays, raw database objects are never exposed to external
2960 * callers, so this is unnecessary in external code.
2962 * @param bool|ResultWrapper|resource|object $result
2963 * @return bool|ResultWrapper
2965 protected function resultObject( $result ) {
2968 } elseif ( $result instanceof ResultWrapper
) {
2970 } elseif ( $result === true ) {
2971 // Successful write query
2974 return new ResultWrapper( $this, $result );
2978 public function ping( &$rtt = null ) {
2979 // Avoid hitting the server if it was hit recently
2980 if ( $this->isOpen() && ( microtime( true ) - $this->lastPing
) < self
::PING_TTL
) {
2981 if ( !func_num_args() ||
$this->mRTTEstimate
> 0 ) {
2982 $rtt = $this->mRTTEstimate
;
2983 return true; // don't care about $rtt
2987 // This will reconnect if possible or return false if not
2988 $this->clearFlag( DBO_TRX
, self
::REMEMBER_PRIOR
);
2989 $ok = ( $this->query( self
::PING_QUERY
, __METHOD__
, true ) !== false );
2990 $this->restoreFlags( self
::RESTORE_PRIOR
);
2993 $rtt = $this->mRTTEstimate
;
3002 protected function reconnect() {
3003 $this->closeConnection();
3004 $this->mOpened
= false;
3005 $this->mConn
= false;
3007 $this->open( $this->mServer
, $this->mUser
, $this->mPassword
, $this->mDBname
);
3008 $this->lastPing
= microtime( true );
3010 } catch ( DBConnectionError
$e ) {
3017 public function getSessionLagStatus() {
3018 return $this->getTransactionLagStatus() ?
: $this->getApproximateLagStatus();
3022 * Get the replica DB lag when the current transaction started
3024 * This is useful when transactions might use snapshot isolation
3025 * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
3026 * is this lag plus transaction duration. If they don't, it is still
3027 * safe to be pessimistic. This returns null if there is no transaction.
3029 * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
3032 public function getTransactionLagStatus() {
3033 return $this->mTrxLevel
3034 ?
[ 'lag' => $this->mTrxReplicaLag
, 'since' => $this->trxTimestamp() ]
3039 * Get a replica DB lag estimate for this server
3041 * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate)
3044 public function getApproximateLagStatus() {
3046 'lag' => $this->getLBInfo( 'replica' ) ?
$this->getLag() : 0,
3047 'since' => microtime( true )
3052 * Merge the result of getSessionLagStatus() for several DBs
3053 * using the most pessimistic values to estimate the lag of
3054 * any data derived from them in combination
3056 * This is information is useful for caching modules
3058 * @see WANObjectCache::set()
3059 * @see WANObjectCache::getWithSetCallback()
3061 * @param IDatabase $db1
3062 * @param IDatabase ...
3063 * @return array Map of values:
3064 * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped)
3065 * - since: oldest UNIX timestamp of any of the DB lag estimates
3066 * - pending: whether any of the DBs have uncommitted changes
3069 public static function getCacheSetOptions( IDatabase
$db1 ) {
3070 $res = [ 'lag' => 0, 'since' => INF
, 'pending' => false ];
3071 foreach ( func_get_args() as $db ) {
3072 /** @var IDatabase $db */
3073 $status = $db->getSessionLagStatus();
3074 if ( $status['lag'] === false ) {
3075 $res['lag'] = false;
3076 } elseif ( $res['lag'] !== false ) {
3077 $res['lag'] = max( $res['lag'], $status['lag'] );
3079 $res['since'] = min( $res['since'], $status['since'] );
3080 $res['pending'] = $res['pending'] ?
: $db->writesPending();
3086 public function getLag() {
3090 function maxListLen() {
3094 public function encodeBlob( $b ) {
3098 public function decodeBlob( $b ) {
3099 if ( $b instanceof Blob
) {
3105 public function setSessionOptions( array $options ) {
3109 * Read and execute SQL commands from a file.
3111 * Returns true on success, error string or exception on failure (depending
3112 * on object's error ignore settings).
3114 * @param string $filename File name to open
3115 * @param bool|callable $lineCallback Optional function called before reading each line
3116 * @param bool|callable $resultCallback Optional function called for each MySQL result
3117 * @param bool|string $fname Calling function name or false if name should be
3118 * generated dynamically using $filename
3119 * @param bool|callable $inputCallback Optional function called for each
3120 * complete line sent
3121 * @return bool|string
3124 public function sourceFile(
3125 $filename, $lineCallback = false, $resultCallback = false, $fname = false, $inputCallback = false
3127 MediaWiki\
suppressWarnings();
3128 $fp = fopen( $filename, 'r' );
3129 MediaWiki\restoreWarnings
();
3131 if ( false === $fp ) {
3132 throw new RuntimeException( "Could not open \"{$filename}\".\n" );
3136 $fname = __METHOD__
. "( $filename )";
3140 $error = $this->sourceStream( $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
3141 } catch ( Exception
$e ) {
3151 public function setSchemaVars( $vars ) {
3152 $this->mSchemaVars
= $vars;
3156 * Read and execute commands from an open file handle.
3158 * Returns true on success, error string or exception on failure (depending
3159 * on object's error ignore settings).
3161 * @param resource $fp File handle
3162 * @param bool|callable $lineCallback Optional function called before reading each query
3163 * @param bool|callable $resultCallback Optional function called for each MySQL result
3164 * @param string $fname Calling function name
3165 * @param bool|callable $inputCallback Optional function called for each complete query sent
3166 * @return bool|string
3168 public function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
3169 $fname = __METHOD__
, $inputCallback = false
3173 while ( !feof( $fp ) ) {
3174 if ( $lineCallback ) {
3175 call_user_func( $lineCallback );
3178 $line = trim( fgets( $fp ) );
3180 if ( $line == '' ) {
3184 if ( '-' == $line[0] && '-' == $line[1] ) {
3192 $done = $this->streamStatementEnd( $cmd, $line );
3196 if ( $done ||
feof( $fp ) ) {
3197 $cmd = $this->replaceVars( $cmd );
3199 if ( ( $inputCallback && call_user_func( $inputCallback, $cmd ) ) ||
!$inputCallback ) {
3200 $res = $this->query( $cmd, $fname );
3202 if ( $resultCallback ) {
3203 call_user_func( $resultCallback, $res, $this );
3206 if ( false === $res ) {
3207 $err = $this->lastError();
3209 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
3220 * Called by sourceStream() to check if we've reached a statement end
3222 * @param string $sql SQL assembled so far
3223 * @param string $newLine New line about to be added to $sql
3224 * @return bool Whether $newLine contains end of the statement
3226 public function streamStatementEnd( &$sql, &$newLine ) {
3227 if ( $this->delimiter
) {
3229 $newLine = preg_replace( '/' . preg_quote( $this->delimiter
, '/' ) . '$/', '', $newLine );
3230 if ( $newLine != $prev ) {
3239 * Database independent variable replacement. Replaces a set of variables
3240 * in an SQL statement with their contents as given by $this->getSchemaVars().
3242 * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables.
3244 * - '{$var}' should be used for text and is passed through the database's
3246 * - `{$var}` should be used for identifiers (e.g. table and database names).
3247 * It is passed through the database's addIdentifierQuotes method which
3248 * can be overridden if the database uses something other than backticks.
3249 * - / *_* / or / *$wgDBprefix* / passes the name that follows through the
3250 * database's tableName method.
3251 * - / *i* / passes the name that follows through the database's indexName method.
3252 * - In all other cases, / *$var* / is left unencoded. Except for table options,
3253 * its use should be avoided. In 1.24 and older, string encoding was applied.
3255 * @param string $ins SQL statement to replace variables in
3256 * @return string The new SQL statement with variables replaced
3258 protected function replaceVars( $ins ) {
3259 $vars = $this->getSchemaVars();
3260 return preg_replace_callback(
3262 /\* (\$wgDBprefix|[_i]) \*/ (\w*) | # 1-2. tableName, indexName
3263 \'\{\$ (\w+) }\' | # 3. addQuotes
3264 `\{\$ (\w+) }` | # 4. addIdentifierQuotes
3265 /\*\$ (\w+) \*/ # 5. leave unencoded
3267 function ( $m ) use ( $vars ) {
3268 // Note: Because of <https://bugs.php.net/bug.php?id=51881>,
3269 // check for both nonexistent keys *and* the empty string.
3270 if ( isset( $m[1] ) && $m[1] !== '' ) {
3271 if ( $m[1] === 'i' ) {
3272 return $this->indexName( $m[2] );
3274 return $this->tableName( $m[2] );
3276 } elseif ( isset( $m[3] ) && $m[3] !== '' && array_key_exists( $m[3], $vars ) ) {
3277 return $this->addQuotes( $vars[$m[3]] );
3278 } elseif ( isset( $m[4] ) && $m[4] !== '' && array_key_exists( $m[4], $vars ) ) {
3279 return $this->addIdentifierQuotes( $vars[$m[4]] );
3280 } elseif ( isset( $m[5] ) && $m[5] !== '' && array_key_exists( $m[5], $vars ) ) {
3281 return $vars[$m[5]];
3291 * Get schema variables. If none have been set via setSchemaVars(), then
3292 * use some defaults from the current object.
3296 protected function getSchemaVars() {
3297 if ( $this->mSchemaVars
) {
3298 return $this->mSchemaVars
;
3300 return $this->getDefaultSchemaVars();
3305 * Get schema variables to use if none have been set via setSchemaVars().
3307 * Override this in derived classes to provide variables for tables.sql
3308 * and SQL patch files.
3312 protected function getDefaultSchemaVars() {
3316 public function lockIsFree( $lockName, $method ) {
3320 public function lock( $lockName, $method, $timeout = 5 ) {
3321 $this->mNamedLocksHeld
[$lockName] = 1;
3326 public function unlock( $lockName, $method ) {
3327 unset( $this->mNamedLocksHeld
[$lockName] );
3332 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
3333 if ( $this->writesOrCallbacksPending() ) {
3334 // This only flushes transactions to clear snapshots, not to write data
3335 $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
3336 throw new DBUnexpectedError(
3338 "$fname: Cannot COMMIT to clear snapshot because writes are pending ($fnames)."
3342 if ( !$this->lock( $lockKey, $fname, $timeout ) ) {
3346 $unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) {
3347 if ( $this->trxLevel() ) {
3348 // There is a good chance an exception was thrown, causing any early return
3349 // from the caller. Let any error handler get a chance to issue rollback().
3350 // If there isn't one, let the error bubble up and trigger server-side rollback.
3351 $this->onTransactionResolution(
3352 function () use ( $lockKey, $fname ) {
3353 $this->unlock( $lockKey, $fname );
3358 $this->unlock( $lockKey, $fname );
3362 $this->commit( $fname, self
::FLUSHING_INTERNAL
);
3367 public function namedLocksEnqueue() {
3372 * Lock specific tables
3374 * @param array $read Array of tables to lock for read access
3375 * @param array $write Array of tables to lock for write access
3376 * @param string $method Name of caller
3377 * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY
3380 public function lockTables( $read, $write, $method, $lowPriority = true ) {
3385 * Unlock specific tables
3387 * @param string $method The caller
3390 public function unlockTables( $method ) {
3396 * @param string $tableName
3397 * @param string $fName
3398 * @return bool|ResultWrapper
3401 public function dropTable( $tableName, $fName = __METHOD__
) {
3402 if ( !$this->tableExists( $tableName, $fName ) ) {
3405 $sql = "DROP TABLE " . $this->tableName( $tableName ) . " CASCADE";
3407 return $this->query( $sql, $fName );
3410 public function getInfinity() {
3414 public function encodeExpiry( $expiry ) {
3415 return ( $expiry == '' ||
$expiry == 'infinity' ||
$expiry == $this->getInfinity() )
3416 ?
$this->getInfinity()
3417 : $this->timestamp( $expiry );
3420 public function decodeExpiry( $expiry, $format = TS_MW
) {
3421 if ( $expiry == '' ||
$expiry == 'infinity' ||
$expiry == $this->getInfinity() ) {
3426 $t = new ConvertableTimestamp( $expiry );
3428 return $t->getTimestamp( $format );
3429 } catch ( TimestampException
$e ) {
3434 public function setBigSelects( $value = true ) {
3438 public function isReadOnly() {
3439 return ( $this->getReadOnlyReason() !== false );
3443 * @return string|bool Reason this DB is read-only or false if it is not
3445 protected function getReadOnlyReason() {
3446 $reason = $this->getLBInfo( 'readOnlyReason' );
3448 return is_string( $reason ) ?
$reason : false;
3451 public function setTableAliases( array $aliases ) {
3452 $this->tableAliases
= $aliases;
3459 public function __toString() {
3460 return (string)$this->mConn
;
3464 * Called by serialize. Throw an exception when DB connection is serialized.
3465 * This causes problems on some database engines because the connection is
3466 * not restored on unserialize.
3468 public function __sleep() {
3469 throw new RuntimeException( 'Database serialization may cause problems, since ' .
3470 'the connection is not restored on wakeup.' );
3474 * Run a few simple sanity checks
3476 public function __destruct() {
3477 if ( $this->mTrxLevel
&& $this->mTrxDoneWrites
) {
3478 trigger_error( "Uncommitted DB writes (transaction from {$this->mTrxFname})." );
3481 $danglingWriters = $this->pendingWriteAndCallbackCallers();
3482 if ( $danglingWriters ) {
3483 $fnames = implode( ', ', $danglingWriters );
3484 trigger_error( "DB transaction writes or callbacks still pending ($fnames)." );