4 * @defgroup Database Database
6 * This file deals with database interface functions
7 * and query specifics/optimisations.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
29 * Database abstraction object
32 abstract class DatabaseBase
implements IDatabase
{
33 /** Number of times to re-try an operation in case of deadlock */
34 const DEADLOCK_TRIES
= 4;
35 /** Minimum time to wait before retry, in microseconds */
36 const DEADLOCK_DELAY_MIN
= 500000;
37 /** Maximum time to wait before retry */
38 const DEADLOCK_DELAY_MAX
= 1500000;
40 /** How long before it is worth doing a dummy query to test the connection */
42 const PING_QUERY
= 'SELECT 1 AS ping';
44 const TINY_WRITE_SEC
= .010;
45 const SLOW_WRITE_SEC
= .500;
46 const SMALL_WRITE_ROWS
= 100;
48 /** @var string SQL query */
49 protected $mLastQuery = '';
51 protected $mDoneWrites = false;
52 /** @var string|bool */
53 protected $mPHPError = false;
63 /** @var BagOStuff APC cache */
66 /** @var resource Database connection */
67 protected $mConn = null;
69 protected $mOpened = false;
71 /** @var array[] List of (callable, method name) */
72 protected $mTrxIdleCallbacks = [];
73 /** @var array[] List of (callable, method name) */
74 protected $mTrxPreCommitCallbacks = [];
75 /** @var array[] List of (callable, method name) */
76 protected $mTrxEndCallbacks = [];
77 /** @var array[] Map of (name => (callable, method name)) */
78 protected $mTrxRecurringCallbacks = [];
79 /** @var bool Whether to suppress triggering of transaction end callbacks */
80 protected $mTrxEndCallbacksSuppressed = false;
83 protected $mTablePrefix;
91 protected $mLBInfo = [];
93 protected $mDefaultBigSelects = null;
94 /** @var array|bool */
95 protected $mSchemaVars = false;
97 protected $mSessionVars = [];
98 /** @var array|null */
99 protected $preparedArgs;
100 /** @var string|bool|null Stashed value of html_errors INI setting */
101 protected $htmlErrors;
103 protected $delimiter = ';';
106 * Either 1 if a transaction is active or 0 otherwise.
107 * The other Trx fields may not be meaningfull if this is 0.
111 protected $mTrxLevel = 0;
113 * Either a short hexidecimal string if a transaction is active or ""
116 * @see DatabaseBase::mTrxLevel
118 protected $mTrxShortId = '';
120 * The UNIX time that the transaction started. Callers can assume that if
121 * snapshot isolation is used, then the data is *at least* up to date to that
122 * point (possibly more up-to-date since the first SELECT defines the snapshot).
125 * @see DatabaseBase::mTrxLevel
127 private $mTrxTimestamp = null;
128 /** @var float Lag estimate at the time of BEGIN */
129 private $mTrxSlaveLag = null;
131 * Remembers the function name given for starting the most recent transaction via begin().
132 * Used to provide additional context for error reporting.
135 * @see DatabaseBase::mTrxLevel
137 private $mTrxFname = null;
139 * Record if possible write queries were done in the last transaction started
142 * @see DatabaseBase::mTrxLevel
144 private $mTrxDoneWrites = false;
146 * Record if the current transaction was started implicitly due to DBO_TRX being set.
149 * @see DatabaseBase::mTrxLevel
151 private $mTrxAutomatic = false;
153 * Array of levels of atomicity within transactions
157 private $mTrxAtomicLevels = [];
159 * Record if the current transaction was started implicitly by DatabaseBase::startAtomic
163 private $mTrxAutomaticAtomic = false;
165 * Track the write query callers of the current transaction
169 private $mTrxWriteCallers = [];
171 * @var float Seconds spent in write queries for the current transaction
173 private $mTrxWriteDuration = 0.0;
175 * @var integer Number of write queries for the current transaction
177 private $mTrxWriteQueryCount = 0;
179 * @var float Like mTrxWriteQueryCount but excludes lock-bound, easy to replicate, queries
181 private $mTrxWriteAdjDuration = 0.0;
183 * @var integer Number of write queries counted in mTrxWriteAdjDuration
185 private $mTrxWriteAdjQueryCount = 0;
187 * @var float RTT time estimate
189 private $mRTTEstimate = 0.0;
191 /** @var array Map of (name => 1) for locks obtained via lock() */
192 private $mNamedLocksHeld = [];
194 /** @var IDatabase|null Lazy handle to the master DB this server replicates from */
195 private $lazyMasterHandle;
199 * @var resource File handle for upgrade
201 protected $fileHandle = null;
205 * @var string[] Process cache of VIEWs names in the database
207 protected $allViews = null;
209 /** @var float UNIX timestamp */
210 protected $lastPing = 0.0;
212 /** @var int[] Prior mFlags values */
213 private $priorFlags = [];
217 /** @var TransactionProfiler */
218 protected $trxProfiler;
220 public function getServerInfo() {
221 return $this->getServerVersion();
225 * @return string Command delimiter used by this database engine
227 public function getDelimiter() {
228 return $this->delimiter
;
232 * Boolean, controls output of large amounts of debug information.
233 * @param bool|null $debug
234 * - true to enable debugging
235 * - false to disable debugging
236 * - omitted or null to do nothing
238 * @return bool|null Previous value of the flag
240 public function debug( $debug = null ) {
241 return wfSetBit( $this->mFlags
, DBO_DEBUG
, $debug );
244 public function bufferResults( $buffer = null ) {
245 if ( is_null( $buffer ) ) {
246 return !(bool)( $this->mFlags
& DBO_NOBUFFER
);
248 return !wfSetBit( $this->mFlags
, DBO_NOBUFFER
, !$buffer );
253 * Turns on (false) or off (true) the automatic generation and sending
254 * of a "we're sorry, but there has been a database error" page on
255 * database errors. Default is on (false). When turned off, the
256 * code should use lastErrno() and lastError() to handle the
257 * situation as appropriate.
259 * Do not use this function outside of the Database classes.
261 * @param null|bool $ignoreErrors
262 * @return bool The previous value of the flag.
264 protected function ignoreErrors( $ignoreErrors = null ) {
265 return wfSetBit( $this->mFlags
, DBO_IGNORE
, $ignoreErrors );
268 public function trxLevel() {
269 return $this->mTrxLevel
;
272 public function trxTimestamp() {
273 return $this->mTrxLevel ?
$this->mTrxTimestamp
: null;
276 public function tablePrefix( $prefix = null ) {
277 return wfSetVar( $this->mTablePrefix
, $prefix );
280 public function dbSchema( $schema = null ) {
281 return wfSetVar( $this->mSchema
, $schema );
285 * Set the filehandle to copy write statements to.
287 * @param resource $fh File handle
289 public function setFileHandle( $fh ) {
290 $this->fileHandle
= $fh;
293 public function getLBInfo( $name = null ) {
294 if ( is_null( $name ) ) {
295 return $this->mLBInfo
;
297 if ( array_key_exists( $name, $this->mLBInfo
) ) {
298 return $this->mLBInfo
[$name];
305 public function setLBInfo( $name, $value = null ) {
306 if ( is_null( $value ) ) {
307 $this->mLBInfo
= $name;
309 $this->mLBInfo
[$name] = $value;
314 * Set a lazy-connecting DB handle to the master DB (for replication status purposes)
316 * @param IDatabase $conn
319 public function setLazyMasterHandle( IDatabase
$conn ) {
320 $this->lazyMasterHandle
= $conn;
324 * @return IDatabase|null
325 * @see setLazyMasterHandle()
328 public function getLazyMasterHandle() {
329 return $this->lazyMasterHandle
;
333 * @return TransactionProfiler
335 protected function getTransactionProfiler() {
336 return $this->trxProfiler
;
340 * @param TransactionProfiler $profiler
343 public function setTransactionProfiler( TransactionProfiler
$profiler ) {
344 $this->trxProfiler
= $profiler;
348 * Returns true if this database supports (and uses) cascading deletes
352 public function cascadingDeletes() {
357 * Returns true if this database supports (and uses) triggers (e.g. on the page table)
361 public function cleanupTriggers() {
366 * Returns true if this database is strict about what can be put into an IP field.
367 * Specifically, it uses a NULL value instead of an empty string.
371 public function strictIPs() {
376 * Returns true if this database uses timestamps rather than integers
380 public function realTimestamps() {
384 public function implicitGroupby() {
388 public function implicitOrderby() {
393 * Returns true if this database can do a native search on IP columns
394 * e.g. this works as expected: .. WHERE rc_ip = '127.42.12.102/32';
398 public function searchableIPs() {
403 * Returns true if this database can use functional indexes
407 public function functionalIndexes() {
411 public function lastQuery() {
412 return $this->mLastQuery
;
415 public function doneWrites() {
416 return (bool)$this->mDoneWrites
;
419 public function lastDoneWrites() {
420 return $this->mDoneWrites ?
: false;
423 public function writesPending() {
424 return $this->mTrxLevel
&& $this->mTrxDoneWrites
;
427 public function writesOrCallbacksPending() {
428 return $this->mTrxLevel
&& (
429 $this->mTrxDoneWrites ||
$this->mTrxIdleCallbacks ||
$this->mTrxPreCommitCallbacks
433 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
434 if ( !$this->mTrxLevel
) {
436 } elseif ( !$this->mTrxDoneWrites
) {
441 case self
::ESTIMATE_DB_APPLY
:
443 $rttAdjTotal = $this->mTrxWriteAdjQueryCount
* $rtt;
444 $applyTime = max( $this->mTrxWriteAdjDuration
- $rttAdjTotal, 0 );
445 // For omitted queries, make them count as something at least
446 $omitted = $this->mTrxWriteQueryCount
- $this->mTrxWriteAdjQueryCount
;
447 $applyTime +
= self
::TINY_WRITE_SEC
* $omitted;
450 default: // everything
451 return $this->mTrxWriteDuration
;
455 public function pendingWriteCallers() {
456 return $this->mTrxLevel ?
$this->mTrxWriteCallers
: [];
459 public function isOpen() {
460 return $this->mOpened
;
463 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
464 if ( $remember === self
::REMEMBER_PRIOR
) {
465 array_push( $this->priorFlags
, $this->mFlags
);
467 $this->mFlags |
= $flag;
470 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
471 if ( $remember === self
::REMEMBER_PRIOR
) {
472 array_push( $this->priorFlags
, $this->mFlags
);
474 $this->mFlags
&= ~
$flag;
477 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
478 if ( !$this->priorFlags
) {
482 if ( $state === self
::RESTORE_INITIAL
) {
483 $this->mFlags
= reset( $this->priorFlags
);
484 $this->priorFlags
= [];
486 $this->mFlags
= array_pop( $this->priorFlags
);
490 public function getFlag( $flag ) {
491 return !!( $this->mFlags
& $flag );
494 public function getProperty( $name ) {
498 public function getWikiID() {
499 if ( $this->mTablePrefix
) {
500 return "{$this->mDBname}-{$this->mTablePrefix}";
502 return $this->mDBname
;
507 * Return a path to the DBMS-specific SQL file if it exists,
508 * otherwise default SQL file
510 * @param string $filename
513 private function getSqlFilePath( $filename ) {
515 $dbmsSpecificFilePath = "$IP/maintenance/" . $this->getType() . "/$filename";
516 if ( file_exists( $dbmsSpecificFilePath ) ) {
517 return $dbmsSpecificFilePath;
519 return "$IP/maintenance/$filename";
524 * Return a path to the DBMS-specific schema file,
525 * otherwise default to tables.sql
529 public function getSchemaPath() {
530 return $this->getSqlFilePath( 'tables.sql' );
534 * Return a path to the DBMS-specific update key file,
535 * otherwise default to update-keys.sql
539 public function getUpdateKeysPath() {
540 return $this->getSqlFilePath( 'update-keys.sql' );
544 * Get information about an index into an object
545 * @param string $table Table name
546 * @param string $index Index name
547 * @param string $fname Calling function name
548 * @return mixed Database-specific index description class or false if the index does not exist
550 abstract function indexInfo( $table, $index, $fname = __METHOD__
);
553 * Wrapper for addslashes()
555 * @param string $s String to be slashed.
556 * @return string Slashed string.
558 abstract function strencode( $s );
563 * FIXME: It is possible to construct a Database object with no associated
564 * connection object, by specifying no parameters to __construct(). This
565 * feature is deprecated and should be removed.
567 * DatabaseBase subclasses should not be constructed directly in external
568 * code. DatabaseBase::factory() should be used instead.
570 * @param array $params Parameters passed from DatabaseBase::factory()
572 function __construct( array $params ) {
573 global $wgDBprefix, $wgDBmwschema, $wgCommandLineMode;
575 $this->srvCache
= ObjectCache
::getLocalServerInstance( 'hash' );
577 $server = $params['host'];
578 $user = $params['user'];
579 $password = $params['password'];
580 $dbName = $params['dbname'];
581 $flags = $params['flags'];
582 $tablePrefix = $params['tablePrefix'];
583 $schema = $params['schema'];
584 $foreign = $params['foreign'];
586 $this->mFlags
= $flags;
587 if ( $this->mFlags
& DBO_DEFAULT
) {
588 if ( $wgCommandLineMode ) {
589 $this->mFlags
&= ~DBO_TRX
;
591 $this->mFlags |
= DBO_TRX
;
595 $this->mSessionVars
= $params['variables'];
597 /** Get the default table prefix*/
598 if ( $tablePrefix === 'get from global' ) {
599 $this->mTablePrefix
= $wgDBprefix;
601 $this->mTablePrefix
= $tablePrefix;
604 /** Get the database schema*/
605 if ( $schema === 'get from global' ) {
606 $this->mSchema
= $wgDBmwschema;
608 $this->mSchema
= $schema;
611 $this->mForeign
= $foreign;
613 $this->profiler
= isset( $params['profiler'] )
614 ?
$params['profiler']
615 : Profiler
::instance(); // @TODO: remove global state
616 $this->trxProfiler
= isset( $params['trxProfiler'] )
617 ?
$params['trxProfiler']
618 : new TransactionProfiler();
621 $this->open( $server, $user, $password, $dbName );
627 * Called by serialize. Throw an exception when DB connection is serialized.
628 * This causes problems on some database engines because the connection is
629 * not restored on unserialize.
631 public function __sleep() {
632 throw new MWException( 'Database serialization may cause problems, since ' .
633 'the connection is not restored on wakeup.' );
637 * Given a DB type, construct the name of the appropriate child class of
638 * DatabaseBase. This is designed to replace all of the manual stuff like:
639 * $class = 'Database' . ucfirst( strtolower( $dbType ) );
640 * as well as validate against the canonical list of DB types we have
642 * This factory function is mostly useful for when you need to connect to a
643 * database other than the MediaWiki default (such as for external auth,
644 * an extension, et cetera). Do not use this to connect to the MediaWiki
645 * database. Example uses in core:
646 * @see LoadBalancer::reallyOpenConnection()
647 * @see ForeignDBRepo::getMasterDB()
648 * @see WebInstallerDBConnect::execute()
652 * @param string $dbType A possible DB type
653 * @param array $p An array of options to pass to the constructor.
654 * Valid options are: host, user, password, dbname, flags, tablePrefix, schema, driver
655 * @throws MWException If the database driver or extension cannot be found
656 * @return DatabaseBase|null DatabaseBase subclass or null
658 final public static function factory( $dbType, $p = [] ) {
659 $canonicalDBTypes = [
660 'mysql' => [ 'mysqli', 'mysql' ],
668 $dbType = strtolower( $dbType );
669 if ( isset( $canonicalDBTypes[$dbType] ) && $canonicalDBTypes[$dbType] ) {
670 $possibleDrivers = $canonicalDBTypes[$dbType];
671 if ( !empty( $p['driver'] ) ) {
672 if ( in_array( $p['driver'], $possibleDrivers ) ) {
673 $driver = $p['driver'];
675 throw new MWException( __METHOD__
.
676 " cannot construct Database with type '$dbType' and driver '{$p['driver']}'" );
679 foreach ( $possibleDrivers as $posDriver ) {
680 if ( extension_loaded( $posDriver ) ) {
681 $driver = $posDriver;
689 if ( $driver === false ) {
690 throw new MWException( __METHOD__
.
691 " no viable database extension found for type '$dbType'" );
694 // Determine schema defaults. Currently Microsoft SQL Server uses $wgDBmwschema,
695 // and everything else doesn't use a schema (e.g. null)
696 // Although postgres and oracle support schemas, we don't use them (yet)
697 // to maintain backwards compatibility
699 'mssql' => 'get from global',
702 $class = 'Database' . ucfirst( $driver );
703 if ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) {
704 // Resolve some defaults for b/c
705 $p['host'] = isset( $p['host'] ) ?
$p['host'] : false;
706 $p['user'] = isset( $p['user'] ) ?
$p['user'] : false;
707 $p['password'] = isset( $p['password'] ) ?
$p['password'] : false;
708 $p['dbname'] = isset( $p['dbname'] ) ?
$p['dbname'] : false;
709 $p['flags'] = isset( $p['flags'] ) ?
$p['flags'] : 0;
710 $p['variables'] = isset( $p['variables'] ) ?
$p['variables'] : [];
711 $p['tablePrefix'] = isset( $p['tablePrefix'] ) ?
$p['tablePrefix'] : 'get from global';
712 if ( !isset( $p['schema'] ) ) {
713 $p['schema'] = isset( $defaultSchemas[$dbType] ) ?
$defaultSchemas[$dbType] : null;
715 $p['foreign'] = isset( $p['foreign'] ) ?
$p['foreign'] : false;
717 return new $class( $p );
723 protected function installErrorHandler() {
724 $this->mPHPError
= false;
725 $this->htmlErrors
= ini_set( 'html_errors', '0' );
726 set_error_handler( [ $this, 'connectionErrorHandler' ] );
730 * @return bool|string
732 protected function restoreErrorHandler() {
733 restore_error_handler();
734 if ( $this->htmlErrors
!== false ) {
735 ini_set( 'html_errors', $this->htmlErrors
);
737 if ( $this->mPHPError
) {
738 $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError
);
739 $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
749 * @param string $errstr
751 public function connectionErrorHandler( $errno, $errstr ) {
752 $this->mPHPError
= $errstr;
756 * Create a log context to pass to wfLogDBError or other logging functions.
758 * @param array $extras Additional data to add to context
761 protected function getLogContext( array $extras = [] ) {
764 'db_server' => $this->mServer
,
765 'db_name' => $this->mDBname
,
766 'db_user' => $this->mUser
,
772 public function close() {
773 if ( $this->mConn
) {
774 if ( $this->trxLevel() ) {
775 if ( !$this->mTrxAutomatic
) {
776 wfWarn( "Transaction still in progress (from {$this->mTrxFname}), " .
777 " performing implicit commit before closing connection!" );
780 $this->commit( __METHOD__
, self
::FLUSHING_INTERNAL
);
783 $closed = $this->closeConnection();
784 $this->mConn
= false;
785 } elseif ( $this->mTrxIdleCallbacks ||
$this->mTrxEndCallbacks
) { // sanity
786 throw new MWException( "Transaction callbacks still pending." );
790 $this->mOpened
= false;
796 * Make sure isOpen() returns true as a sanity check
798 * @throws DBUnexpectedError
800 protected function assertOpen() {
801 if ( !$this->isOpen() ) {
802 throw new DBUnexpectedError( $this, "DB connection was already closed." );
807 * Closes underlying database connection
809 * @return bool Whether connection was closed successfully
811 abstract protected function closeConnection();
813 function reportConnectionError( $error = 'Unknown error' ) {
814 $myError = $this->lastError();
820 throw new DBConnectionError( $this, $error );
824 * The DBMS-dependent part of query()
826 * @param string $sql SQL query.
827 * @return ResultWrapper|bool Result object to feed to fetchObject,
828 * fetchRow, ...; or false on failure
830 abstract protected function doQuery( $sql );
833 * Determine whether a query writes to the DB.
834 * Should return true if unsure.
839 protected function isWriteQuery( $sql ) {
841 '/^(?:SELECT|BEGIN|ROLLBACK|COMMIT|SET|SHOW|EXPLAIN|\(SELECT)\b/i', $sql );
846 * @return string|null
848 protected function getQueryVerb( $sql ) {
849 return preg_match( '/^\s*([a-z]+)/i', $sql, $m ) ?
strtoupper( $m[1] ) : null;
853 * Determine whether a SQL statement is sensitive to isolation level.
854 * A SQL statement is considered transactable if its result could vary
855 * depending on the transaction isolation level. Operational commands
856 * such as 'SET' and 'SHOW' are not considered to be transactable.
861 protected function isTransactableQuery( $sql ) {
862 $verb = $this->getQueryVerb( $sql );
863 return !in_array( $verb, [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET' ], true );
866 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
869 $priorWritesPending = $this->writesOrCallbacksPending();
870 $this->mLastQuery
= $sql;
872 $isWrite = $this->isWriteQuery( $sql );
874 $reason = $this->getReadOnlyReason();
875 if ( $reason !== false ) {
876 throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
878 # Set a flag indicating that writes have been done
879 $this->mDoneWrites
= microtime( true );
882 # Add a comment for easy SHOW PROCESSLIST interpretation
883 if ( is_object( $wgUser ) && $wgUser->isItemLoaded( 'name' ) ) {
884 $userName = $wgUser->getName();
885 if ( mb_strlen( $userName ) > 15 ) {
886 $userName = mb_substr( $userName, 0, 15 ) . '...';
888 $userName = str_replace( '/', '', $userName );
893 // Add trace comment to the begin of the sql string, right after the operator.
894 // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (bug 42598)
895 $commentedSql = preg_replace( '/\s|$/', " /* $fname $userName */ ", $sql, 1 );
897 # Start implicit transactions that wrap the request if DBO_TRX is enabled
898 if ( !$this->mTrxLevel
&& $this->getFlag( DBO_TRX
)
899 && $this->isTransactableQuery( $sql )
901 $this->begin( __METHOD__
. " ($fname)", self
::TRANSACTION_INTERNAL
);
902 $this->mTrxAutomatic
= true;
905 # Keep track of whether the transaction has write queries pending
906 if ( $this->mTrxLevel
&& !$this->mTrxDoneWrites
&& $isWrite ) {
907 $this->mTrxDoneWrites
= true;
908 $this->getTransactionProfiler()->transactionWritingIn(
909 $this->mServer
, $this->mDBname
, $this->mTrxShortId
);
912 if ( $this->debug() ) {
913 wfDebugLog( 'queries', sprintf( "%s: %s", $this->mDBname
, $commentedSql ) );
916 # Avoid fatals if close() was called
919 # Send the query to the server
920 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
922 # Try reconnecting if the connection was lost
923 if ( false === $ret && $this->wasErrorReissuable() ) {
924 $recoverable = $this->canRecoverFromDisconnect( $sql, $priorWritesPending );
925 # Stash the last error values before anything might clear them
926 $lastError = $this->lastError();
927 $lastErrno = $this->lastErrno();
928 # Update state tracking to reflect transaction loss due to disconnection
929 $this->handleTransactionLoss();
930 wfDebug( "Connection lost, reconnecting...\n" );
931 if ( $this->reconnect() ) {
932 wfDebug( "Reconnected\n" );
933 $msg = __METHOD__
. ": lost connection to {$this->getServer()}; reconnected";
934 wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
936 if ( !$recoverable ) {
937 # Callers may catch the exception and continue to use the DB
938 $this->reportQueryError( $lastError, $lastErrno, $sql, $fname );
940 # Should be safe to silently retry the query
941 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
944 wfDebug( "Failed\n" );
948 if ( false === $ret ) {
949 # Deadlocks cause the entire transaction to abort, not just the statement.
950 # http://dev.mysql.com/doc/refman/5.7/en/innodb-error-handling.html
951 # https://www.postgresql.org/docs/9.1/static/explicit-locking.html
952 if ( $this->wasDeadlock() ) {
953 if ( $this->explicitTrxActive() ||
$priorWritesPending ) {
954 $tempIgnore = false; // not recoverable
956 # Update state tracking to reflect transaction loss
957 $this->handleTransactionLoss();
960 $this->reportQueryError(
961 $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
964 $res = $this->resultObject( $ret );
969 private function doProfiledQuery( $sql, $commentedSql, $isWrite, $fname ) {
970 $isMaster = !is_null( $this->getLBInfo( 'master' ) );
971 # generalizeSQL() will probably cut down the query to reasonable
972 # logging size most of the time. The substr is really just a sanity check.
974 $queryProf = 'query-m: ' . substr( DatabaseBase
::generalizeSQL( $sql ), 0, 255 );
976 $queryProf = 'query: ' . substr( DatabaseBase
::generalizeSQL( $sql ), 0, 255 );
979 # Include query transaction state
980 $queryProf .= $this->mTrxShortId ?
" [TRX#{$this->mTrxShortId}]" : "";
982 $startTime = microtime( true );
983 $this->profiler
->profileIn( $queryProf );
984 $ret = $this->doQuery( $commentedSql );
985 $this->profiler
->profileOut( $queryProf );
986 $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
988 unset( $queryProfSection ); // profile out (if set)
990 if ( $ret !== false ) {
991 $this->lastPing
= $startTime;
992 if ( $isWrite && $this->mTrxLevel
) {
993 $this->updateTrxWriteQueryTime( $sql, $queryRuntime );
994 $this->mTrxWriteCallers
[] = $fname;
998 if ( $sql === self
::PING_QUERY
) {
999 $this->mRTTEstimate
= $queryRuntime;
1002 $this->getTransactionProfiler()->recordQueryCompletion(
1003 $queryProf, $startTime, $isWrite, $this->affectedRows()
1005 MWDebug
::query( $sql, $fname, $isMaster, $queryRuntime );
1011 * Update the estimated run-time of a query, not counting large row lock times
1013 * LoadBalancer can be set to rollback transactions that will create huge replication
1014 * lag. It bases this estimate off of pendingWriteQueryDuration(). Certain simple
1015 * queries, like inserting a row can take a long time due to row locking. This method
1016 * uses some simple heuristics to discount those cases.
1018 * @param string $sql
1019 * @param float $runtime Total runtime, including RTT
1021 private function updateTrxWriteQueryTime( $sql, $runtime ) {
1022 $indicativeOfSlaveRuntime = true;
1023 if ( $runtime > self
::SLOW_WRITE_SEC
) {
1024 $verb = $this->getQueryVerb( $sql );
1025 // insert(), upsert(), replace() are fast unless bulky in size or blocked on locks
1026 if ( $verb === 'INSERT' ) {
1027 $indicativeOfSlaveRuntime = $this->affectedRows() > self
::SMALL_WRITE_ROWS
;
1028 } elseif ( $verb === 'REPLACE' ) {
1029 $indicativeOfSlaveRuntime = $this->affectedRows() > self
::SMALL_WRITE_ROWS
/ 2;
1033 $this->mTrxWriteDuration +
= $runtime;
1034 $this->mTrxWriteQueryCount +
= 1;
1035 if ( $indicativeOfSlaveRuntime ) {
1036 $this->mTrxWriteAdjDuration +
= $runtime;
1037 $this->mTrxWriteAdjQueryCount +
= 1;
1041 private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
1042 # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
1043 # Dropped connections also mean that named locks are automatically released.
1044 # Only allow error suppression in autocommit mode or when the lost transaction
1045 # didn't matter anyway (aside from DBO_TRX snapshot loss).
1046 if ( $this->mNamedLocksHeld
) {
1047 return false; // possible critical section violation
1048 } elseif ( $sql === 'COMMIT' ) {
1049 return !$priorWritesPending; // nothing written anyway? (T127428)
1050 } elseif ( $sql === 'ROLLBACK' ) {
1051 return true; // transaction lost...which is also what was requested :)
1052 } elseif ( $this->explicitTrxActive() ) {
1053 return false; // don't drop atomocity
1054 } elseif ( $priorWritesPending ) {
1055 return false; // prior writes lost from implicit transaction
1061 private function handleTransactionLoss() {
1062 $this->mTrxLevel
= 0;
1063 $this->mTrxIdleCallbacks
= []; // bug 65263
1064 $this->mTrxPreCommitCallbacks
= []; // bug 65263
1066 // Handle callbacks in mTrxEndCallbacks
1067 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_ROLLBACK
);
1068 $this->runTransactionListenerCallbacks( self
::TRIGGER_ROLLBACK
);
1070 } catch ( Exception
$e ) {
1071 // Already logged; move on...
1076 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
1077 if ( $this->ignoreErrors() ||
$tempIgnore ) {
1078 wfDebug( "SQL ERROR (ignored): $error\n" );
1080 $sql1line = mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 );
1082 "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
1083 $this->getLogContext( [
1084 'method' => __METHOD__
,
1087 'sql1line' => $sql1line,
1091 wfDebug( "SQL ERROR: " . $error . "\n" );
1092 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
1097 * Intended to be compatible with the PEAR::DB wrapper functions.
1098 * http://pear.php.net/manual/en/package.database.db.intro-execute.php
1100 * ? = scalar value, quoted as necessary
1101 * ! = raw SQL bit (a function for instance)
1102 * & = filename; reads the file and inserts as a blob
1103 * (we don't use this though...)
1105 * @param string $sql
1106 * @param string $func
1110 protected function prepare( $sql, $func = 'DatabaseBase::prepare' ) {
1111 /* MySQL doesn't support prepared statements (yet), so just
1112 * pack up the query for reference. We'll manually replace
1115 return [ 'query' => $sql, 'func' => $func ];
1119 * Free a prepared query, generated by prepare().
1120 * @param string $prepared
1122 protected function freePrepared( $prepared ) {
1123 /* No-op by default */
1127 * Execute a prepared query with the various arguments
1128 * @param string $prepared The prepared sql
1129 * @param mixed $args Either an array here, or put scalars as varargs
1131 * @return ResultWrapper
1133 public function execute( $prepared, $args = null ) {
1134 if ( !is_array( $args ) ) {
1136 $args = func_get_args();
1137 array_shift( $args );
1140 $sql = $this->fillPrepared( $prepared['query'], $args );
1142 return $this->query( $sql, $prepared['func'] );
1146 * For faking prepared SQL statements on DBs that don't support it directly.
1148 * @param string $preparedQuery A 'preparable' SQL statement
1149 * @param array $args Array of Arguments to fill it with
1150 * @return string Executable SQL
1152 public function fillPrepared( $preparedQuery, $args ) {
1154 $this->preparedArgs
=& $args;
1156 return preg_replace_callback( '/(\\\\[?!&]|[?!&])/',
1157 [ &$this, 'fillPreparedArg' ], $preparedQuery );
1161 * preg_callback func for fillPrepared()
1162 * The arguments should be in $this->preparedArgs and must not be touched
1163 * while we're doing this.
1165 * @param array $matches
1166 * @throws DBUnexpectedError
1169 protected function fillPreparedArg( $matches ) {
1170 switch ( $matches[1] ) {
1179 list( /* $n */, $arg ) = each( $this->preparedArgs
);
1181 switch ( $matches[1] ) {
1183 return $this->addQuotes( $arg );
1187 # return $this->addQuotes( file_get_contents( $arg ) );
1188 throw new DBUnexpectedError(
1190 '& mode is not implemented. If it\'s really needed, uncomment the line above.'
1193 throw new DBUnexpectedError(
1195 'Received invalid match. This should never happen!'
1200 public function freeResult( $res ) {
1203 public function selectField(
1204 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
1206 if ( $var === '*' ) { // sanity
1207 throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
1210 if ( !is_array( $options ) ) {
1211 $options = [ $options ];
1214 $options['LIMIT'] = 1;
1216 $res = $this->select( $table, $var, $cond, $fname, $options );
1217 if ( $res === false ||
!$this->numRows( $res ) ) {
1221 $row = $this->fetchRow( $res );
1223 if ( $row !== false ) {
1224 return reset( $row );
1230 public function selectFieldValues(
1231 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
1233 if ( $var === '*' ) { // sanity
1234 throw new DBUnexpectedError( $this, "Cannot use a * field" );
1235 } elseif ( !is_string( $var ) ) { // sanity
1236 throw new DBUnexpectedError( $this, "Cannot use an array of fields" );
1239 if ( !is_array( $options ) ) {
1240 $options = [ $options ];
1243 $res = $this->select( $table, $var, $cond, $fname, $options, $join_conds );
1244 if ( $res === false ) {
1249 foreach ( $res as $row ) {
1250 $values[] = $row->$var;
1257 * Returns an optional USE INDEX clause to go after the table, and a
1258 * string to go at the end of the query.
1260 * @param array $options Associative array of options to be turned into
1261 * an SQL query, valid keys are listed in the function.
1263 * @see DatabaseBase::select()
1265 public function makeSelectOptions( $options ) {
1266 $preLimitTail = $postLimitTail = '';
1271 foreach ( $options as $key => $option ) {
1272 if ( is_numeric( $key ) ) {
1273 $noKeyOptions[$option] = true;
1277 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1279 $preLimitTail .= $this->makeOrderBy( $options );
1281 // if (isset($options['LIMIT'])) {
1282 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
1283 // isset($options['OFFSET']) ? $options['OFFSET']
1287 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1288 $postLimitTail .= ' FOR UPDATE';
1291 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1292 $postLimitTail .= ' LOCK IN SHARE MODE';
1295 if ( isset( $noKeyOptions['DISTINCT'] ) ||
isset( $noKeyOptions['DISTINCTROW'] ) ) {
1296 $startOpts .= 'DISTINCT';
1299 # Various MySQL extensions
1300 if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
1301 $startOpts .= ' /*! STRAIGHT_JOIN */';
1304 if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) {
1305 $startOpts .= ' HIGH_PRIORITY';
1308 if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
1309 $startOpts .= ' SQL_BIG_RESULT';
1312 if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
1313 $startOpts .= ' SQL_BUFFER_RESULT';
1316 if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
1317 $startOpts .= ' SQL_SMALL_RESULT';
1320 if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
1321 $startOpts .= ' SQL_CALC_FOUND_ROWS';
1324 if ( isset( $noKeyOptions['SQL_CACHE'] ) ) {
1325 $startOpts .= ' SQL_CACHE';
1328 if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) {
1329 $startOpts .= ' SQL_NO_CACHE';
1332 if ( isset( $options['USE INDEX'] ) && is_string( $options['USE INDEX'] ) ) {
1333 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1338 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail ];
1342 * Returns an optional GROUP BY with an optional HAVING
1344 * @param array $options Associative array of options
1346 * @see DatabaseBase::select()
1349 public function makeGroupByWithHaving( $options ) {
1351 if ( isset( $options['GROUP BY'] ) ) {
1352 $gb = is_array( $options['GROUP BY'] )
1353 ?
implode( ',', $options['GROUP BY'] )
1354 : $options['GROUP BY'];
1355 $sql .= ' GROUP BY ' . $gb;
1357 if ( isset( $options['HAVING'] ) ) {
1358 $having = is_array( $options['HAVING'] )
1359 ?
$this->makeList( $options['HAVING'], LIST_AND
)
1360 : $options['HAVING'];
1361 $sql .= ' HAVING ' . $having;
1368 * Returns an optional ORDER BY
1370 * @param array $options Associative array of options
1372 * @see DatabaseBase::select()
1375 public function makeOrderBy( $options ) {
1376 if ( isset( $options['ORDER BY'] ) ) {
1377 $ob = is_array( $options['ORDER BY'] )
1378 ?
implode( ',', $options['ORDER BY'] )
1379 : $options['ORDER BY'];
1381 return ' ORDER BY ' . $ob;
1387 // See IDatabase::select for the docs for this function
1388 public function select( $table, $vars, $conds = '', $fname = __METHOD__
,
1389 $options = [], $join_conds = [] ) {
1390 $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
1392 return $this->query( $sql, $fname );
1395 public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__
,
1396 $options = [], $join_conds = []
1398 if ( is_array( $vars ) ) {
1399 $vars = implode( ',', $this->fieldNamesWithAlias( $vars ) );
1402 $options = (array)$options;
1403 $useIndexes = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
1404 ?
$options['USE INDEX']
1407 if ( is_array( $table ) ) {
1409 $this->tableNamesWithUseIndexOrJOIN( $table, $useIndexes, $join_conds );
1410 } elseif ( $table != '' ) {
1411 if ( $table[0] == ' ' ) {
1412 $from = ' FROM ' . $table;
1415 $this->tableNamesWithUseIndexOrJOIN( [ $table ], $useIndexes, [] );
1421 list( $startOpts, $useIndex, $preLimitTail, $postLimitTail ) =
1422 $this->makeSelectOptions( $options );
1424 if ( !empty( $conds ) ) {
1425 if ( is_array( $conds ) ) {
1426 $conds = $this->makeList( $conds, LIST_AND
);
1428 $sql = "SELECT $startOpts $vars $from $useIndex WHERE $conds $preLimitTail";
1430 $sql = "SELECT $startOpts $vars $from $useIndex $preLimitTail";
1433 if ( isset( $options['LIMIT'] ) ) {
1434 $sql = $this->limitResult( $sql, $options['LIMIT'],
1435 isset( $options['OFFSET'] ) ?
$options['OFFSET'] : false );
1437 $sql = "$sql $postLimitTail";
1439 if ( isset( $options['EXPLAIN'] ) ) {
1440 $sql = 'EXPLAIN ' . $sql;
1446 public function selectRow( $table, $vars, $conds, $fname = __METHOD__
,
1447 $options = [], $join_conds = []
1449 $options = (array)$options;
1450 $options['LIMIT'] = 1;
1451 $res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
1453 if ( $res === false ) {
1457 if ( !$this->numRows( $res ) ) {
1461 $obj = $this->fetchObject( $res );
1466 public function estimateRowCount(
1467 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
1470 $res = $this->select( $table, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options );
1473 $row = $this->fetchRow( $res );
1474 $rows = ( isset( $row['rowcount'] ) ) ?
(int)$row['rowcount'] : 0;
1480 public function selectRowCount(
1481 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
1484 $sql = $this->selectSQLText( $tables, '1', $conds, $fname, $options, $join_conds );
1485 $res = $this->query( "SELECT COUNT(*) AS rowcount FROM ($sql) tmp_count", $fname );
1488 $row = $this->fetchRow( $res );
1489 $rows = ( isset( $row['rowcount'] ) ) ?
(int)$row['rowcount'] : 0;
1496 * Removes most variables from an SQL query and replaces them with X or N for numbers.
1497 * It's only slightly flawed. Don't use for anything important.
1499 * @param string $sql A SQL Query
1503 protected static function generalizeSQL( $sql ) {
1504 # This does the same as the regexp below would do, but in such a way
1505 # as to avoid crashing php on some large strings.
1506 # $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql );
1508 $sql = str_replace( "\\\\", '', $sql );
1509 $sql = str_replace( "\\'", '', $sql );
1510 $sql = str_replace( "\\\"", '', $sql );
1511 $sql = preg_replace( "/'.*'/s", "'X'", $sql );
1512 $sql = preg_replace( '/".*"/s', "'X'", $sql );
1514 # All newlines, tabs, etc replaced by single space
1515 $sql = preg_replace( '/\s+/', ' ', $sql );
1518 # except the ones surrounded by characters, e.g. l10n
1519 $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
1520 $sql = preg_replace( '/(?<![a-zA-Z])-?\d+(?![a-zA-Z])/s', 'N', $sql );
1525 public function fieldExists( $table, $field, $fname = __METHOD__
) {
1526 $info = $this->fieldInfo( $table, $field );
1531 public function indexExists( $table, $index, $fname = __METHOD__
) {
1532 if ( !$this->tableExists( $table ) ) {
1536 $info = $this->indexInfo( $table, $index, $fname );
1537 if ( is_null( $info ) ) {
1540 return $info !== false;
1544 public function tableExists( $table, $fname = __METHOD__
) {
1545 $table = $this->tableName( $table );
1546 $old = $this->ignoreErrors( true );
1547 $res = $this->query( "SELECT 1 FROM $table LIMIT 1", $fname );
1548 $this->ignoreErrors( $old );
1553 public function indexUnique( $table, $index ) {
1554 $indexInfo = $this->indexInfo( $table, $index );
1556 if ( !$indexInfo ) {
1560 return !$indexInfo[0]->Non_unique
;
1564 * Helper for DatabaseBase::insert().
1566 * @param array $options
1569 protected function makeInsertOptions( $options ) {
1570 return implode( ' ', $options );
1573 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
1574 # No rows to insert, easy just return now
1575 if ( !count( $a ) ) {
1579 $table = $this->tableName( $table );
1581 if ( !is_array( $options ) ) {
1582 $options = [ $options ];
1586 if ( isset( $options['fileHandle'] ) ) {
1587 $fh = $options['fileHandle'];
1589 $options = $this->makeInsertOptions( $options );
1591 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
1593 $keys = array_keys( $a[0] );
1596 $keys = array_keys( $a );
1599 $sql = 'INSERT ' . $options .
1600 " INTO $table (" . implode( ',', $keys ) . ') VALUES ';
1604 foreach ( $a as $row ) {
1610 $sql .= '(' . $this->makeList( $row ) . ')';
1613 $sql .= '(' . $this->makeList( $a ) . ')';
1616 if ( $fh !== null && false === fwrite( $fh, $sql ) ) {
1618 } elseif ( $fh !== null ) {
1622 return (bool)$this->query( $sql, $fname );
1626 * Make UPDATE options array for DatabaseBase::makeUpdateOptions
1628 * @param array $options
1631 protected function makeUpdateOptionsArray( $options ) {
1632 if ( !is_array( $options ) ) {
1633 $options = [ $options ];
1638 if ( in_array( 'LOW_PRIORITY', $options ) ) {
1639 $opts[] = $this->lowPriorityOption();
1642 if ( in_array( 'IGNORE', $options ) ) {
1650 * Make UPDATE options for the DatabaseBase::update function
1652 * @param array $options The options passed to DatabaseBase::update
1655 protected function makeUpdateOptions( $options ) {
1656 $opts = $this->makeUpdateOptionsArray( $options );
1658 return implode( ' ', $opts );
1661 function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
1662 $table = $this->tableName( $table );
1663 $opts = $this->makeUpdateOptions( $options );
1664 $sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET
);
1666 if ( $conds !== [] && $conds !== '*' ) {
1667 $sql .= " WHERE " . $this->makeList( $conds, LIST_AND
);
1670 return $this->query( $sql, $fname );
1673 public function makeList( $a, $mode = LIST_COMMA
) {
1674 if ( !is_array( $a ) ) {
1675 throw new DBUnexpectedError( $this, 'DatabaseBase::makeList called with incorrect parameters' );
1681 foreach ( $a as $field => $value ) {
1683 if ( $mode == LIST_AND
) {
1685 } elseif ( $mode == LIST_OR
) {
1694 if ( ( $mode == LIST_AND ||
$mode == LIST_OR
) && is_numeric( $field ) ) {
1695 $list .= "($value)";
1696 } elseif ( ( $mode == LIST_SET
) && is_numeric( $field ) ) {
1698 } elseif ( ( $mode == LIST_AND ||
$mode == LIST_OR
) && is_array( $value ) ) {
1699 // Remove null from array to be handled separately if found
1700 $includeNull = false;
1701 foreach ( array_keys( $value, null, true ) as $nullKey ) {
1702 $includeNull = true;
1703 unset( $value[$nullKey] );
1705 if ( count( $value ) == 0 && !$includeNull ) {
1706 throw new MWException( __METHOD__
. ": empty input for field $field" );
1707 } elseif ( count( $value ) == 0 ) {
1708 // only check if $field is null
1709 $list .= "$field IS NULL";
1711 // IN clause contains at least one valid element
1712 if ( $includeNull ) {
1713 // Group subconditions to ensure correct precedence
1716 if ( count( $value ) == 1 ) {
1717 // Special-case single values, as IN isn't terribly efficient
1718 // Don't necessarily assume the single key is 0; we don't
1719 // enforce linear numeric ordering on other arrays here.
1720 $value = array_values( $value )[0];
1721 $list .= $field . " = " . $this->addQuotes( $value );
1723 $list .= $field . " IN (" . $this->makeList( $value ) . ") ";
1725 // if null present in array, append IS NULL
1726 if ( $includeNull ) {
1727 $list .= " OR $field IS NULL)";
1730 } elseif ( $value === null ) {
1731 if ( $mode == LIST_AND ||
$mode == LIST_OR
) {
1732 $list .= "$field IS ";
1733 } elseif ( $mode == LIST_SET
) {
1734 $list .= "$field = ";
1738 if ( $mode == LIST_AND ||
$mode == LIST_OR ||
$mode == LIST_SET
) {
1739 $list .= "$field = ";
1741 $list .= $mode == LIST_NAMES ?
$value : $this->addQuotes( $value );
1748 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
1751 foreach ( $data as $base => $sub ) {
1752 if ( count( $sub ) ) {
1753 $conds[] = $this->makeList(
1754 [ $baseKey => $base, $subKey => array_keys( $sub ) ],
1760 return $this->makeList( $conds, LIST_OR
);
1762 // Nothing to search for...
1768 * Return aggregated value alias
1770 * @param array $valuedata
1771 * @param string $valuename
1775 public function aggregateValue( $valuedata, $valuename = 'value' ) {
1779 public function bitNot( $field ) {
1783 public function bitAnd( $fieldLeft, $fieldRight ) {
1784 return "($fieldLeft & $fieldRight)";
1787 public function bitOr( $fieldLeft, $fieldRight ) {
1788 return "($fieldLeft | $fieldRight)";
1791 public function buildConcat( $stringList ) {
1792 return 'CONCAT(' . implode( ',', $stringList ) . ')';
1795 public function buildGroupConcatField(
1796 $delim, $table, $field, $conds = '', $join_conds = []
1798 $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')';
1800 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1803 public function selectDB( $db ) {
1804 # Stub. Shouldn't cause serious problems if it's not overridden, but
1805 # if your database engine supports a concept similar to MySQL's
1806 # databases you may as well.
1807 $this->mDBname
= $db;
1812 public function getDBname() {
1813 return $this->mDBname
;
1816 public function getServer() {
1817 return $this->mServer
;
1821 * Format a table name ready for use in constructing an SQL query
1823 * This does two important things: it quotes the table names to clean them up,
1824 * and it adds a table prefix if only given a table name with no quotes.
1826 * All functions of this object which require a table name call this function
1827 * themselves. Pass the canonical name to such functions. This is only needed
1828 * when calling query() directly.
1830 * @note This function does not sanitize user input. It is not safe to use
1831 * this function to escape user input.
1832 * @param string $name Database table name
1833 * @param string $format One of:
1834 * quoted - Automatically pass the table name through addIdentifierQuotes()
1835 * so that it can be used in a query.
1836 * raw - Do not add identifier quotes to the table name
1837 * @return string Full database name
1839 public function tableName( $name, $format = 'quoted' ) {
1840 global $wgSharedDB, $wgSharedPrefix, $wgSharedTables, $wgSharedSchema;
1841 # Skip the entire process when we have a string quoted on both ends.
1842 # Note that we check the end so that we will still quote any use of
1843 # use of `database`.table. But won't break things if someone wants
1844 # to query a database table with a dot in the name.
1845 if ( $this->isQuotedIdentifier( $name ) ) {
1849 # Lets test for any bits of text that should never show up in a table
1850 # name. Basically anything like JOIN or ON which are actually part of
1851 # SQL queries, but may end up inside of the table value to combine
1852 # sql. Such as how the API is doing.
1853 # Note that we use a whitespace test rather than a \b test to avoid
1854 # any remote case where a word like on may be inside of a table name
1855 # surrounded by symbols which may be considered word breaks.
1856 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
1860 # Split database and table into proper variables.
1861 # We reverse the explode so that database.table and table both output
1862 # the correct table.
1863 $dbDetails = explode( '.', $name, 3 );
1864 if ( count( $dbDetails ) == 3 ) {
1865 list( $database, $schema, $table ) = $dbDetails;
1866 # We don't want any prefix added in this case
1868 } elseif ( count( $dbDetails ) == 2 ) {
1869 list( $database, $table ) = $dbDetails;
1870 # We don't want any prefix added in this case
1871 # In dbs that support it, $database may actually be the schema
1872 # but that doesn't affect any of the functionality here
1876 list( $table ) = $dbDetails;
1877 if ( $wgSharedDB !== null # We have a shared database
1878 && $this->mForeign
== false # We're not working on a foreign database
1879 && !$this->isQuotedIdentifier( $table ) # Prevent shared tables listing '`table`'
1880 && in_array( $table, $wgSharedTables ) # A shared table is selected
1882 $database = $wgSharedDB;
1883 $schema = $wgSharedSchema === null ?
$this->mSchema
: $wgSharedSchema;
1884 $prefix = $wgSharedPrefix === null ?
$this->mTablePrefix
: $wgSharedPrefix;
1887 $schema = $this->mSchema
; # Default schema
1888 $prefix = $this->mTablePrefix
; # Default prefix
1892 # Quote $table and apply the prefix if not quoted.
1893 # $tableName might be empty if this is called from Database::replaceVars()
1894 $tableName = "{$prefix}{$table}";
1895 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $tableName ) && $tableName !== '' ) {
1896 $tableName = $this->addIdentifierQuotes( $tableName );
1899 # Quote $schema and merge it with the table name if needed
1900 if ( strlen( $schema ) ) {
1901 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $schema ) ) {
1902 $schema = $this->addIdentifierQuotes( $schema );
1904 $tableName = $schema . '.' . $tableName;
1907 # Quote $database and merge it with the table name if needed
1908 if ( $database !== null ) {
1909 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $database ) ) {
1910 $database = $this->addIdentifierQuotes( $database );
1912 $tableName = $database . '.' . $tableName;
1919 * Fetch a number of table names into an array
1920 * This is handy when you need to construct SQL for joins
1923 * extract( $dbr->tableNames( 'user', 'watchlist' ) );
1924 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1925 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1929 public function tableNames() {
1930 $inArray = func_get_args();
1933 foreach ( $inArray as $name ) {
1934 $retVal[$name] = $this->tableName( $name );
1941 * Fetch a number of table names into an zero-indexed numerical array
1942 * This is handy when you need to construct SQL for joins
1945 * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' );
1946 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1947 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1951 public function tableNamesN() {
1952 $inArray = func_get_args();
1955 foreach ( $inArray as $name ) {
1956 $retVal[] = $this->tableName( $name );
1963 * Get an aliased table name
1964 * e.g. tableName AS newTableName
1966 * @param string $name Table name, see tableName()
1967 * @param string|bool $alias Alias (optional)
1968 * @return string SQL name for aliased table. Will not alias a table to its own name
1970 public function tableNameWithAlias( $name, $alias = false ) {
1971 if ( !$alias ||
$alias == $name ) {
1972 return $this->tableName( $name );
1974 return $this->tableName( $name ) . ' ' . $this->addIdentifierQuotes( $alias );
1979 * Gets an array of aliased table names
1981 * @param array $tables [ [alias] => table ]
1982 * @return string[] See tableNameWithAlias()
1984 public function tableNamesWithAlias( $tables ) {
1986 foreach ( $tables as $alias => $table ) {
1987 if ( is_numeric( $alias ) ) {
1990 $retval[] = $this->tableNameWithAlias( $table, $alias );
1997 * Get an aliased field name
1998 * e.g. fieldName AS newFieldName
2000 * @param string $name Field name
2001 * @param string|bool $alias Alias (optional)
2002 * @return string SQL name for aliased field. Will not alias a field to its own name
2004 public function fieldNameWithAlias( $name, $alias = false ) {
2005 if ( !$alias ||
(string)$alias === (string)$name ) {
2008 return $name . ' AS ' . $this->addIdentifierQuotes( $alias ); // PostgreSQL needs AS
2013 * Gets an array of aliased field names
2015 * @param array $fields [ [alias] => field ]
2016 * @return string[] See fieldNameWithAlias()
2018 public function fieldNamesWithAlias( $fields ) {
2020 foreach ( $fields as $alias => $field ) {
2021 if ( is_numeric( $alias ) ) {
2024 $retval[] = $this->fieldNameWithAlias( $field, $alias );
2031 * Get the aliased table name clause for a FROM clause
2032 * which might have a JOIN and/or USE INDEX clause
2034 * @param array $tables ( [alias] => table )
2035 * @param array $use_index Same as for select()
2036 * @param array $join_conds Same as for select()
2039 protected function tableNamesWithUseIndexOrJOIN(
2040 $tables, $use_index = [], $join_conds = []
2044 $use_index = (array)$use_index;
2045 $join_conds = (array)$join_conds;
2047 foreach ( $tables as $alias => $table ) {
2048 if ( !is_string( $alias ) ) {
2049 // No alias? Set it equal to the table name
2052 // Is there a JOIN clause for this table?
2053 if ( isset( $join_conds[$alias] ) ) {
2054 list( $joinType, $conds ) = $join_conds[$alias];
2055 $tableClause = $joinType;
2056 $tableClause .= ' ' . $this->tableNameWithAlias( $table, $alias );
2057 if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
2058 $use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
2060 $tableClause .= ' ' . $use;
2063 $on = $this->makeList( (array)$conds, LIST_AND
);
2065 $tableClause .= ' ON (' . $on . ')';
2068 $retJOIN[] = $tableClause;
2069 } elseif ( isset( $use_index[$alias] ) ) {
2070 // Is there an INDEX clause for this table?
2071 $tableClause = $this->tableNameWithAlias( $table, $alias );
2072 $tableClause .= ' ' . $this->useIndexClause(
2073 implode( ',', (array)$use_index[$alias] )
2076 $ret[] = $tableClause;
2078 $tableClause = $this->tableNameWithAlias( $table, $alias );
2080 $ret[] = $tableClause;
2084 // We can't separate explicit JOIN clauses with ',', use ' ' for those
2085 $implicitJoins = !empty( $ret ) ?
implode( ',', $ret ) : "";
2086 $explicitJoins = !empty( $retJOIN ) ?
implode( ' ', $retJOIN ) : "";
2088 // Compile our final table clause
2089 return implode( ' ', [ $implicitJoins, $explicitJoins ] );
2093 * Get the name of an index in a given table.
2095 * @param string $index
2098 protected function indexName( $index ) {
2099 // Backwards-compatibility hack
2101 'ar_usertext_timestamp' => 'usertext_timestamp',
2102 'un_user_id' => 'user_id',
2103 'un_user_ip' => 'user_ip',
2106 if ( isset( $renamed[$index] ) ) {
2107 return $renamed[$index];
2113 public function addQuotes( $s ) {
2114 if ( $s instanceof Blob
) {
2117 if ( $s === null ) {
2120 # This will also quote numeric values. This should be harmless,
2121 # and protects against weird problems that occur when they really
2122 # _are_ strings such as article titles and string->number->string
2123 # conversion is not 1:1.
2124 return "'" . $this->strencode( $s ) . "'";
2129 * Quotes an identifier using `backticks` or "double quotes" depending on the database type.
2130 * MySQL uses `backticks` while basically everything else uses double quotes.
2131 * Since MySQL is the odd one out here the double quotes are our generic
2132 * and we implement backticks in DatabaseMysql.
2137 public function addIdentifierQuotes( $s ) {
2138 return '"' . str_replace( '"', '""', $s ) . '"';
2142 * Returns if the given identifier looks quoted or not according to
2143 * the database convention for quoting identifiers .
2145 * @note Do not use this to determine if untrusted input is safe.
2146 * A malicious user can trick this function.
2147 * @param string $name
2150 public function isQuotedIdentifier( $name ) {
2151 return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
2158 protected function escapeLikeInternal( $s ) {
2159 return addcslashes( $s, '\%_' );
2162 public function buildLike() {
2163 $params = func_get_args();
2165 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
2166 $params = $params[0];
2171 foreach ( $params as $value ) {
2172 if ( $value instanceof LikeMatch
) {
2173 $s .= $value->toString();
2175 $s .= $this->escapeLikeInternal( $value );
2179 return " LIKE {$this->addQuotes( $s )} ";
2182 public function anyChar() {
2183 return new LikeMatch( '_' );
2186 public function anyString() {
2187 return new LikeMatch( '%' );
2190 public function nextSequenceValue( $seqName ) {
2195 * USE INDEX clause. Unlikely to be useful for anything but MySQL. This
2196 * is only needed because a) MySQL must be as efficient as possible due to
2197 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2198 * which index to pick. Anyway, other databases might have different
2199 * indexes on a given table. So don't bother overriding this unless you're
2201 * @param string $index
2204 public function useIndexClause( $index ) {
2208 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
2209 $quotedTable = $this->tableName( $table );
2211 if ( count( $rows ) == 0 ) {
2216 if ( !is_array( reset( $rows ) ) ) {
2220 // @FXIME: this is not atomic, but a trx would break affectedRows()
2221 foreach ( $rows as $row ) {
2222 # Delete rows which collide
2223 if ( $uniqueIndexes ) {
2224 $sql = "DELETE FROM $quotedTable WHERE ";
2226 foreach ( $uniqueIndexes as $index ) {
2233 if ( is_array( $index ) ) {
2235 foreach ( $index as $col ) {
2241 $sql .= $col . '=' . $this->addQuotes( $row[$col] );
2244 $sql .= $index . '=' . $this->addQuotes( $row[$index] );
2248 $this->query( $sql, $fname );
2251 # Now insert the row
2252 $this->insert( $table, $row, $fname );
2257 * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
2260 * @param string $table Table name
2261 * @param array|string $rows Row(s) to insert
2262 * @param string $fname Caller function name
2264 * @return ResultWrapper
2266 protected function nativeReplace( $table, $rows, $fname ) {
2267 $table = $this->tableName( $table );
2270 if ( !is_array( reset( $rows ) ) ) {
2274 $sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) . ') VALUES ';
2277 foreach ( $rows as $row ) {
2284 $sql .= '(' . $this->makeList( $row ) . ')';
2287 return $this->query( $sql, $fname );
2290 public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
2293 if ( !count( $rows ) ) {
2294 return true; // nothing to do
2297 if ( !is_array( reset( $rows ) ) ) {
2301 if ( count( $uniqueIndexes ) ) {
2302 $clauses = []; // list WHERE clauses that each identify a single row
2303 foreach ( $rows as $row ) {
2304 foreach ( $uniqueIndexes as $index ) {
2305 $index = is_array( $index ) ?
$index : [ $index ]; // columns
2306 $rowKey = []; // unique key to this row
2307 foreach ( $index as $column ) {
2308 $rowKey[$column] = $row[$column];
2310 $clauses[] = $this->makeList( $rowKey, LIST_AND
);
2313 $where = [ $this->makeList( $clauses, LIST_OR
) ];
2318 $useTrx = !$this->mTrxLevel
;
2320 $this->begin( $fname, self
::TRANSACTION_INTERNAL
);
2323 # Update any existing conflicting row(s)
2324 if ( $where !== false ) {
2325 $ok = $this->update( $table, $set, $where, $fname );
2329 # Now insert any non-conflicting row(s)
2330 $ok = $this->insert( $table, $rows, $fname, [ 'IGNORE' ] ) && $ok;
2331 } catch ( Exception
$e ) {
2333 $this->rollback( $fname );
2338 $this->commit( $fname, self
::TRANSACTION_INTERNAL
);
2344 public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
2348 throw new DBUnexpectedError( $this,
2349 'DatabaseBase::deleteJoin() called with empty $conds' );
2352 $delTable = $this->tableName( $delTable );
2353 $joinTable = $this->tableName( $joinTable );
2354 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
2355 if ( $conds != '*' ) {
2356 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
2360 $this->query( $sql, $fname );
2364 * Returns the size of a text field, or -1 for "unlimited"
2366 * @param string $table
2367 * @param string $field
2370 public function textFieldSize( $table, $field ) {
2371 $table = $this->tableName( $table );
2372 $sql = "SHOW COLUMNS FROM $table LIKE \"$field\";";
2373 $res = $this->query( $sql, 'DatabaseBase::textFieldSize' );
2374 $row = $this->fetchObject( $res );
2378 if ( preg_match( '/\((.*)\)/', $row->Type
, $m ) ) {
2388 * A string to insert into queries to show that they're low-priority, like
2389 * MySQL's LOW_PRIORITY. If no such feature exists, return an empty
2390 * string and nothing bad should happen.
2392 * @return string Returns the text of the low priority option if it is
2393 * supported, or a blank string otherwise
2395 public function lowPriorityOption() {
2399 public function delete( $table, $conds, $fname = __METHOD__
) {
2401 throw new DBUnexpectedError( $this, 'DatabaseBase::delete() called with no conditions' );
2404 $table = $this->tableName( $table );
2405 $sql = "DELETE FROM $table";
2407 if ( $conds != '*' ) {
2408 if ( is_array( $conds ) ) {
2409 $conds = $this->makeList( $conds, LIST_AND
);
2411 $sql .= ' WHERE ' . $conds;
2414 return $this->query( $sql, $fname );
2417 public function insertSelect( $destTable, $srcTable, $varMap, $conds,
2418 $fname = __METHOD__
,
2419 $insertOptions = [], $selectOptions = []
2421 $destTable = $this->tableName( $destTable );
2423 if ( !is_array( $insertOptions ) ) {
2424 $insertOptions = [ $insertOptions ];
2427 $insertOptions = $this->makeInsertOptions( $insertOptions );
2429 if ( !is_array( $selectOptions ) ) {
2430 $selectOptions = [ $selectOptions ];
2433 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
2435 if ( is_array( $srcTable ) ) {
2436 $srcTable = implode( ',', array_map( [ &$this, 'tableName' ], $srcTable ) );
2438 $srcTable = $this->tableName( $srcTable );
2441 $sql = "INSERT $insertOptions INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
2442 " SELECT $startOpts " . implode( ',', $varMap ) .
2443 " FROM $srcTable $useIndex ";
2445 if ( $conds != '*' ) {
2446 if ( is_array( $conds ) ) {
2447 $conds = $this->makeList( $conds, LIST_AND
);
2449 $sql .= " WHERE $conds";
2452 $sql .= " $tailOpts";
2454 return $this->query( $sql, $fname );
2458 * Construct a LIMIT query with optional offset. This is used for query
2459 * pages. The SQL should be adjusted so that only the first $limit rows
2460 * are returned. If $offset is provided as well, then the first $offset
2461 * rows should be discarded, and the next $limit rows should be returned.
2462 * If the result of the query is not ordered, then the rows to be returned
2463 * are theoretically arbitrary.
2465 * $sql is expected to be a SELECT, if that makes a difference.
2467 * The version provided by default works in MySQL and SQLite. It will very
2468 * likely need to be overridden for most other DBMSes.
2470 * @param string $sql SQL query we will append the limit too
2471 * @param int $limit The SQL limit
2472 * @param int|bool $offset The SQL offset (default false)
2473 * @throws DBUnexpectedError
2476 public function limitResult( $sql, $limit, $offset = false ) {
2477 if ( !is_numeric( $limit ) ) {
2478 throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" );
2481 return "$sql LIMIT "
2482 . ( ( is_numeric( $offset ) && $offset != 0 ) ?
"{$offset}," : "" )
2486 public function unionSupportsOrderAndLimit() {
2487 return true; // True for almost every DB supported
2490 public function unionQueries( $sqls, $all ) {
2491 $glue = $all ?
') UNION ALL (' : ') UNION (';
2493 return '(' . implode( $glue, $sqls ) . ')';
2496 public function conditional( $cond, $trueVal, $falseVal ) {
2497 if ( is_array( $cond ) ) {
2498 $cond = $this->makeList( $cond, LIST_AND
);
2501 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
2504 public function strreplace( $orig, $old, $new ) {
2505 return "REPLACE({$orig}, {$old}, {$new})";
2508 public function getServerUptime() {
2512 public function wasDeadlock() {
2516 public function wasLockTimeout() {
2520 public function wasErrorReissuable() {
2524 public function wasReadOnlyError() {
2529 * Determines if the given query error was a connection drop
2532 * @param integer|string $errno
2535 public function wasConnectionError( $errno ) {
2540 * Perform a deadlock-prone transaction.
2542 * This function invokes a callback function to perform a set of write
2543 * queries. If a deadlock occurs during the processing, the transaction
2544 * will be rolled back and the callback function will be called again.
2546 * Avoid using this method outside of Job or Maintenance classes.
2549 * $dbw->deadlockLoop( callback, ... );
2551 * Extra arguments are passed through to the specified callback function.
2552 * This method requires that no transactions are already active to avoid
2553 * causing premature commits or exceptions.
2555 * Returns whatever the callback function returned on its successful,
2556 * iteration, or false on error, for example if the retry limit was
2560 * @throws DBUnexpectedError
2563 public function deadlockLoop() {
2564 $args = func_get_args();
2565 $function = array_shift( $args );
2566 $tries = self
::DEADLOCK_TRIES
;
2568 $this->begin( __METHOD__
);
2571 /** @var Exception $e */
2575 $retVal = call_user_func_array( $function, $args );
2577 } catch ( DBQueryError
$e ) {
2578 if ( $this->wasDeadlock() ) {
2579 // Retry after a randomized delay
2580 usleep( mt_rand( self
::DEADLOCK_DELAY_MIN
, self
::DEADLOCK_DELAY_MAX
) );
2582 // Throw the error back up
2586 } while ( --$tries > 0 );
2588 if ( $tries <= 0 ) {
2589 // Too many deadlocks; give up
2590 $this->rollback( __METHOD__
);
2593 $this->commit( __METHOD__
);
2599 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
2600 # Real waits are implemented in the subclass.
2604 public function getSlavePos() {
2609 public function getMasterPos() {
2614 public function serverIsReadOnly() {
2618 final public function onTransactionResolution( callable
$callback ) {
2619 if ( !$this->mTrxLevel
) {
2620 throw new DBUnexpectedError( $this, "No transaction is active." );
2622 $this->mTrxEndCallbacks
[] = [ $callback, wfGetCaller() ];
2625 final public function onTransactionIdle( callable
$callback ) {
2626 $this->mTrxIdleCallbacks
[] = [ $callback, wfGetCaller() ];
2627 if ( !$this->mTrxLevel
) {
2628 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_IDLE
);
2632 final public function onTransactionPreCommitOrIdle( callable
$callback ) {
2633 if ( $this->mTrxLevel
) {
2634 $this->mTrxPreCommitCallbacks
[] = [ $callback, wfGetCaller() ];
2636 // If no transaction is active, then make one for this callback
2637 $this->begin( __METHOD__
, self
::TRANSACTION_INTERNAL
);
2639 call_user_func( $callback );
2640 $this->commit( __METHOD__
);
2641 } catch ( Exception
$e ) {
2642 $this->rollback( __METHOD__
);
2648 final public function setTransactionListener( $name, callable
$callback = null ) {
2650 $this->mTrxRecurringCallbacks
[$name] = [ $callback, wfGetCaller() ];
2652 unset( $this->mTrxRecurringCallbacks
[$name] );
2657 * Whether to disable running of post-COMMIT/ROLLBACK callbacks
2659 * This method should not be used outside of Database/LoadBalancer
2661 * @param bool $suppress
2664 final public function setTrxEndCallbackSuppression( $suppress ) {
2665 $this->mTrxEndCallbacksSuppressed
= $suppress;
2669 * Actually run and consume any "on transaction idle/resolution" callbacks.
2671 * This method should not be used outside of Database/LoadBalancer
2673 * @param integer $trigger IDatabase::TRIGGER_* constant
2677 public function runOnTransactionIdleCallbacks( $trigger ) {
2678 if ( $this->mTrxEndCallbacksSuppressed
) {
2682 $autoTrx = $this->getFlag( DBO_TRX
); // automatic begin() enabled?
2683 /** @var Exception $e */
2684 $e = null; // first exception
2685 do { // callbacks may add callbacks :)
2686 $callbacks = array_merge(
2687 $this->mTrxIdleCallbacks
,
2688 $this->mTrxEndCallbacks
// include "transaction resolution" callbacks
2690 $this->mTrxIdleCallbacks
= []; // consumed (and recursion guard)
2691 $this->mTrxEndCallbacks
= []; // consumed (recursion guard)
2692 foreach ( $callbacks as $callback ) {
2694 list( $phpCallback ) = $callback;
2695 $this->clearFlag( DBO_TRX
); // make each query its own transaction
2696 call_user_func_array( $phpCallback, [ $trigger ] );
2698 $this->setFlag( DBO_TRX
); // restore automatic begin()
2700 $this->clearFlag( DBO_TRX
); // restore auto-commit
2702 } catch ( Exception
$ex ) {
2703 MWExceptionHandler
::logException( $ex );
2705 // Some callbacks may use startAtomic/endAtomic, so make sure
2706 // their transactions are ended so other callbacks don't fail
2707 if ( $this->trxLevel() ) {
2708 $this->rollback( __METHOD__
);
2712 } while ( count( $this->mTrxIdleCallbacks
) );
2714 if ( $e instanceof Exception
) {
2715 throw $e; // re-throw any first exception
2720 * Actually run and consume any "on transaction pre-commit" callbacks.
2722 * This method should not be used outside of Database/LoadBalancer
2727 public function runOnTransactionPreCommitCallbacks() {
2728 $e = null; // first exception
2729 do { // callbacks may add callbacks :)
2730 $callbacks = $this->mTrxPreCommitCallbacks
;
2731 $this->mTrxPreCommitCallbacks
= []; // consumed (and recursion guard)
2732 foreach ( $callbacks as $callback ) {
2734 list( $phpCallback ) = $callback;
2735 call_user_func( $phpCallback );
2736 } catch ( Exception
$ex ) {
2737 MWExceptionHandler
::logException( $ex );
2741 } while ( count( $this->mTrxPreCommitCallbacks
) );
2743 if ( $e instanceof Exception
) {
2744 throw $e; // re-throw any first exception
2749 * Actually run any "transaction listener" callbacks.
2751 * This method should not be used outside of Database/LoadBalancer
2753 * @param integer $trigger IDatabase::TRIGGER_* constant
2757 public function runTransactionListenerCallbacks( $trigger ) {
2758 if ( $this->mTrxEndCallbacksSuppressed
) {
2762 /** @var Exception $e */
2763 $e = null; // first exception
2765 foreach ( $this->mTrxRecurringCallbacks
as $callback ) {
2767 list( $phpCallback ) = $callback;
2768 $phpCallback( $trigger, $this );
2769 } catch ( Exception
$ex ) {
2770 MWExceptionHandler
::logException( $ex );
2775 if ( $e instanceof Exception
) {
2776 throw $e; // re-throw any first exception
2780 final public function startAtomic( $fname = __METHOD__
) {
2781 if ( !$this->mTrxLevel
) {
2782 $this->begin( $fname, self
::TRANSACTION_INTERNAL
);
2783 $this->mTrxAutomatic
= true;
2784 // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
2785 // in all changes being in one transaction to keep requests transactional.
2786 if ( !$this->getFlag( DBO_TRX
) ) {
2787 $this->mTrxAutomaticAtomic
= true;
2791 $this->mTrxAtomicLevels
[] = $fname;
2794 final public function endAtomic( $fname = __METHOD__
) {
2795 if ( !$this->mTrxLevel
) {
2796 throw new DBUnexpectedError( $this, "No atomic transaction is open (got $fname)." );
2798 if ( !$this->mTrxAtomicLevels ||
2799 array_pop( $this->mTrxAtomicLevels
) !== $fname
2801 throw new DBUnexpectedError( $this, "Invalid atomic section ended (got $fname)." );
2804 if ( !$this->mTrxAtomicLevels
&& $this->mTrxAutomaticAtomic
) {
2805 $this->commit( $fname, self
::FLUSHING_INTERNAL
);
2809 final public function doAtomicSection( $fname, callable
$callback ) {
2810 $this->startAtomic( $fname );
2812 $res = call_user_func_array( $callback, [ $this, $fname ] );
2813 } catch ( Exception
$e ) {
2814 $this->rollback( $fname );
2817 $this->endAtomic( $fname );
2822 final public function begin( $fname = __METHOD__
, $mode = self
::TRANSACTION_EXPLICIT
) {
2823 // Protect against mismatched atomic section, transaction nesting, and snapshot loss
2824 if ( $this->mTrxLevel
) {
2825 if ( $this->mTrxAtomicLevels
) {
2826 $levels = implode( ', ', $this->mTrxAtomicLevels
);
2827 $msg = "$fname: Got explicit BEGIN while atomic section(s) $levels are open.";
2828 throw new DBUnexpectedError( $this, $msg );
2829 } elseif ( !$this->mTrxAutomatic
) {
2830 $msg = "$fname: Explicit transaction already active (from {$this->mTrxFname}).";
2831 throw new DBUnexpectedError( $this, $msg );
2833 // @TODO: make this an exception at some point
2834 $msg = "$fname: Implicit transaction already active (from {$this->mTrxFname}).";
2835 wfLogDBError( $msg );
2836 return; // join the main transaction set
2838 } elseif ( $this->getFlag( DBO_TRX
) && $mode !== self
::TRANSACTION_INTERNAL
) {
2839 // @TODO: make this an exception at some point
2840 wfLogDBError( "$fname: Implicit transaction expected (DBO_TRX set)." );
2841 return; // let any writes be in the main transaction
2844 // Avoid fatals if close() was called
2845 $this->assertOpen();
2847 $this->doBegin( $fname );
2848 $this->mTrxTimestamp
= microtime( true );
2849 $this->mTrxFname
= $fname;
2850 $this->mTrxDoneWrites
= false;
2851 $this->mTrxAutomatic
= false;
2852 $this->mTrxAutomaticAtomic
= false;
2853 $this->mTrxAtomicLevels
= [];
2854 $this->mTrxShortId
= wfRandomString( 12 );
2855 $this->mTrxWriteDuration
= 0.0;
2856 $this->mTrxWriteQueryCount
= 0;
2857 $this->mTrxWriteAdjDuration
= 0.0;
2858 $this->mTrxWriteAdjQueryCount
= 0;
2859 $this->mTrxWriteCallers
= [];
2860 // First SELECT after BEGIN will establish the snapshot in REPEATABLE-READ.
2861 // Get an estimate of the slave lag before then, treating estimate staleness
2862 // as lag itself just to be safe
2863 $status = $this->getApproximateLagStatus();
2864 $this->mTrxSlaveLag
= $status['lag'] +
( microtime( true ) - $status['since'] );
2868 * Issues the BEGIN command to the database server.
2870 * @see DatabaseBase::begin()
2871 * @param string $fname
2873 protected function doBegin( $fname ) {
2874 $this->query( 'BEGIN', $fname );
2875 $this->mTrxLevel
= 1;
2878 final public function commit( $fname = __METHOD__
, $flush = '' ) {
2879 if ( $this->mTrxLevel
&& $this->mTrxAtomicLevels
) {
2880 // There are still atomic sections open. This cannot be ignored
2881 $levels = implode( ', ', $this->mTrxAtomicLevels
);
2882 throw new DBUnexpectedError(
2884 "$fname: Got COMMIT while atomic sections $levels are still open."
2888 if ( $flush === self
::FLUSHING_INTERNAL ||
$flush === self
::FLUSHING_ALL_PEERS
) {
2889 if ( !$this->mTrxLevel
) {
2890 return; // nothing to do
2891 } elseif ( !$this->mTrxAutomatic
) {
2892 throw new DBUnexpectedError(
2894 "$fname: Flushing an explicit transaction, getting out of sync."
2898 if ( !$this->mTrxLevel
) {
2899 wfWarn( "$fname: No transaction to commit, something got out of sync." );
2900 return; // nothing to do
2901 } elseif ( $this->mTrxAutomatic
) {
2902 // @TODO: make this an exception at some point
2903 wfLogDBError( "$fname: Explicit commit of implicit transaction." );
2904 return; // wait for the main transaction set commit round
2908 // Avoid fatals if close() was called
2909 $this->assertOpen();
2911 $this->runOnTransactionPreCommitCallbacks();
2912 $writeTime = $this->pendingWriteQueryDuration();
2913 $this->doCommit( $fname );
2914 if ( $this->mTrxDoneWrites
) {
2915 $this->mDoneWrites
= microtime( true );
2916 $this->getTransactionProfiler()->transactionWritingOut(
2917 $this->mServer
, $this->mDBname
, $this->mTrxShortId
, $writeTime );
2920 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_COMMIT
);
2921 $this->runTransactionListenerCallbacks( self
::TRIGGER_COMMIT
);
2925 * Issues the COMMIT command to the database server.
2927 * @see DatabaseBase::commit()
2928 * @param string $fname
2930 protected function doCommit( $fname ) {
2931 if ( $this->mTrxLevel
) {
2932 $this->query( 'COMMIT', $fname );
2933 $this->mTrxLevel
= 0;
2937 final public function rollback( $fname = __METHOD__
, $flush = '' ) {
2938 if ( $flush === self
::FLUSHING_INTERNAL ||
$flush === self
::FLUSHING_ALL_PEERS
) {
2939 if ( !$this->mTrxLevel
) {
2940 return; // nothing to do
2943 if ( !$this->mTrxLevel
) {
2944 wfWarn( "$fname: No transaction to rollback, something got out of sync." );
2945 return; // nothing to do
2946 } elseif ( $this->getFlag( DBO_TRX
) ) {
2947 throw new DBUnexpectedError(
2949 "$fname: Expected mass rollback of all peer databases (DBO_TRX set)."
2954 // Avoid fatals if close() was called
2955 $this->assertOpen();
2957 $this->doRollback( $fname );
2958 $this->mTrxAtomicLevels
= [];
2959 if ( $this->mTrxDoneWrites
) {
2960 $this->getTransactionProfiler()->transactionWritingOut(
2961 $this->mServer
, $this->mDBname
, $this->mTrxShortId
);
2964 $this->mTrxIdleCallbacks
= []; // clear
2965 $this->mTrxPreCommitCallbacks
= []; // clear
2966 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_ROLLBACK
);
2967 $this->runTransactionListenerCallbacks( self
::TRIGGER_ROLLBACK
);
2971 * Issues the ROLLBACK command to the database server.
2973 * @see DatabaseBase::rollback()
2974 * @param string $fname
2976 protected function doRollback( $fname ) {
2977 if ( $this->mTrxLevel
) {
2978 # Disconnects cause rollback anyway, so ignore those errors
2979 $ignoreErrors = true;
2980 $this->query( 'ROLLBACK', $fname, $ignoreErrors );
2981 $this->mTrxLevel
= 0;
2985 public function clearSnapshot( $fname = __METHOD__
) {
2986 if ( $this->writesOrCallbacksPending() ||
$this->explicitTrxActive() ) {
2987 // This only flushes transactions to clear snapshots, not to write data
2988 throw new DBUnexpectedError(
2990 "$fname: Cannot COMMIT to clear snapshot because writes are pending."
2994 $this->commit( $fname, self
::FLUSHING_INTERNAL
);
2997 public function explicitTrxActive() {
2998 return $this->mTrxLevel
&& ( $this->mTrxAtomicLevels ||
!$this->mTrxAutomatic
);
3002 * Creates a new table with structure copied from existing table
3003 * Note that unlike most database abstraction functions, this function does not
3004 * automatically append database prefix, because it works at a lower
3005 * abstraction level.
3006 * The table names passed to this function shall not be quoted (this
3007 * function calls addIdentifierQuotes when needed).
3009 * @param string $oldName Name of table whose structure should be copied
3010 * @param string $newName Name of table to be created
3011 * @param bool $temporary Whether the new table should be temporary
3012 * @param string $fname Calling function name
3013 * @throws MWException
3014 * @return bool True if operation was successful
3016 public function duplicateTableStructure( $oldName, $newName, $temporary = false,
3019 throw new MWException(
3020 'DatabaseBase::duplicateTableStructure is not implemented in descendant class' );
3023 function listTables( $prefix = null, $fname = __METHOD__
) {
3024 throw new MWException( 'DatabaseBase::listTables is not implemented in descendant class' );
3028 * Reset the views process cache set by listViews()
3031 final public function clearViewsCache() {
3032 $this->allViews
= null;
3036 * Lists all the VIEWs in the database
3038 * For caching purposes the list of all views should be stored in
3039 * $this->allViews. The process cache can be cleared with clearViewsCache()
3041 * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_
3042 * @param string $fname Name of calling function
3043 * @throws MWException
3047 public function listViews( $prefix = null, $fname = __METHOD__
) {
3048 throw new MWException( 'DatabaseBase::listViews is not implemented in descendant class' );
3052 * Differentiates between a TABLE and a VIEW
3054 * @param string $name Name of the database-structure to test.
3055 * @throws MWException
3059 public function isView( $name ) {
3060 throw new MWException( 'DatabaseBase::isView is not implemented in descendant class' );
3063 public function timestamp( $ts = 0 ) {
3064 return wfTimestamp( TS_MW
, $ts );
3067 public function timestampOrNull( $ts = null ) {
3068 if ( is_null( $ts ) ) {
3071 return $this->timestamp( $ts );
3076 * Take the result from a query, and wrap it in a ResultWrapper if
3077 * necessary. Boolean values are passed through as is, to indicate success
3078 * of write queries or failure.
3080 * Once upon a time, DatabaseBase::query() returned a bare MySQL result
3081 * resource, and it was necessary to call this function to convert it to
3082 * a wrapper. Nowadays, raw database objects are never exposed to external
3083 * callers, so this is unnecessary in external code.
3085 * @param bool|ResultWrapper|resource|object $result
3086 * @return bool|ResultWrapper
3088 protected function resultObject( $result ) {
3091 } elseif ( $result instanceof ResultWrapper
) {
3093 } elseif ( $result === true ) {
3094 // Successful write query
3097 return new ResultWrapper( $this, $result );
3101 public function ping( &$rtt = null ) {
3102 // Avoid hitting the server if it was hit recently
3103 if ( $this->isOpen() && ( microtime( true ) - $this->lastPing
) < self
::PING_TTL
) {
3104 if ( !func_num_args() ||
$this->mRTTEstimate
> 0 ) {
3105 $rtt = $this->mRTTEstimate
;
3106 return true; // don't care about $rtt
3110 // This will reconnect if possible or return false if not
3111 $this->clearFlag( DBO_TRX
, self
::REMEMBER_PRIOR
);
3112 $ok = ( $this->query( self
::PING_QUERY
, __METHOD__
, true ) !== false );
3113 $this->restoreFlags( self
::RESTORE_PRIOR
);
3116 $rtt = $this->mRTTEstimate
;
3125 protected function reconnect() {
3126 $this->closeConnection();
3127 $this->mOpened
= false;
3128 $this->mConn
= false;
3130 $this->open( $this->mServer
, $this->mUser
, $this->mPassword
, $this->mDBname
);
3131 $this->lastPing
= microtime( true );
3133 } catch ( DBConnectionError
$e ) {
3140 public function getSessionLagStatus() {
3141 return $this->getTransactionLagStatus() ?
: $this->getApproximateLagStatus();
3145 * Get the slave lag when the current transaction started
3147 * This is useful when transactions might use snapshot isolation
3148 * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
3149 * is this lag plus transaction duration. If they don't, it is still
3150 * safe to be pessimistic. This returns null if there is no transaction.
3152 * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
3155 public function getTransactionLagStatus() {
3156 return $this->mTrxLevel
3157 ?
[ 'lag' => $this->mTrxSlaveLag
, 'since' => $this->trxTimestamp() ]
3162 * Get a slave lag estimate for this server
3164 * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate)
3167 public function getApproximateLagStatus() {
3169 'lag' => $this->getLBInfo( 'slave' ) ?
$this->getLag() : 0,
3170 'since' => microtime( true )
3175 * Merge the result of getSessionLagStatus() for several DBs
3176 * using the most pessimistic values to estimate the lag of
3177 * any data derived from them in combination
3179 * This is information is useful for caching modules
3181 * @see WANObjectCache::set()
3182 * @see WANObjectCache::getWithSetCallback()
3184 * @param IDatabase $db1
3185 * @param IDatabase ...
3186 * @return array Map of values:
3187 * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped)
3188 * - since: oldest UNIX timestamp of any of the DB lag estimates
3189 * - pending: whether any of the DBs have uncommitted changes
3192 public static function getCacheSetOptions( IDatabase
$db1 ) {
3193 $res = [ 'lag' => 0, 'since' => INF
, 'pending' => false ];
3194 foreach ( func_get_args() as $db ) {
3195 /** @var IDatabase $db */
3196 $status = $db->getSessionLagStatus();
3197 if ( $status['lag'] === false ) {
3198 $res['lag'] = false;
3199 } elseif ( $res['lag'] !== false ) {
3200 $res['lag'] = max( $res['lag'], $status['lag'] );
3202 $res['since'] = min( $res['since'], $status['since'] );
3203 $res['pending'] = $res['pending'] ?
: $db->writesPending();
3209 public function getLag() {
3213 function maxListLen() {
3217 public function encodeBlob( $b ) {
3221 public function decodeBlob( $b ) {
3222 if ( $b instanceof Blob
) {
3228 public function setSessionOptions( array $options ) {
3232 * Read and execute SQL commands from a file.
3234 * Returns true on success, error string or exception on failure (depending
3235 * on object's error ignore settings).
3237 * @param string $filename File name to open
3238 * @param bool|callable $lineCallback Optional function called before reading each line
3239 * @param bool|callable $resultCallback Optional function called for each MySQL result
3240 * @param bool|string $fname Calling function name or false if name should be
3241 * generated dynamically using $filename
3242 * @param bool|callable $inputCallback Optional function called for each
3243 * complete line sent
3244 * @throws Exception|MWException
3245 * @return bool|string
3247 public function sourceFile(
3248 $filename, $lineCallback = false, $resultCallback = false, $fname = false, $inputCallback = false
3250 MediaWiki\
suppressWarnings();
3251 $fp = fopen( $filename, 'r' );
3252 MediaWiki\restoreWarnings
();
3254 if ( false === $fp ) {
3255 throw new MWException( "Could not open \"{$filename}\".\n" );
3259 $fname = __METHOD__
. "( $filename )";
3263 $error = $this->sourceStream( $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
3264 } catch ( Exception
$e ) {
3275 * Get the full path of a patch file. Originally based on archive()
3276 * from updaters.inc. Keep in mind this always returns a patch, as
3277 * it fails back to MySQL if no DB-specific patch can be found
3279 * @param string $patch The name of the patch, like patch-something.sql
3280 * @return string Full path to patch file
3282 public function patchPath( $patch ) {
3285 $dbType = $this->getType();
3286 if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
3287 return "$IP/maintenance/$dbType/archives/$patch";
3289 return "$IP/maintenance/archives/$patch";
3293 public function setSchemaVars( $vars ) {
3294 $this->mSchemaVars
= $vars;
3298 * Read and execute commands from an open file handle.
3300 * Returns true on success, error string or exception on failure (depending
3301 * on object's error ignore settings).
3303 * @param resource $fp File handle
3304 * @param bool|callable $lineCallback Optional function called before reading each query
3305 * @param bool|callable $resultCallback Optional function called for each MySQL result
3306 * @param string $fname Calling function name
3307 * @param bool|callable $inputCallback Optional function called for each complete query sent
3308 * @return bool|string
3310 public function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
3311 $fname = __METHOD__
, $inputCallback = false
3315 while ( !feof( $fp ) ) {
3316 if ( $lineCallback ) {
3317 call_user_func( $lineCallback );
3320 $line = trim( fgets( $fp ) );
3322 if ( $line == '' ) {
3326 if ( '-' == $line[0] && '-' == $line[1] ) {
3334 $done = $this->streamStatementEnd( $cmd, $line );
3338 if ( $done ||
feof( $fp ) ) {
3339 $cmd = $this->replaceVars( $cmd );
3341 if ( ( $inputCallback && call_user_func( $inputCallback, $cmd ) ) ||
!$inputCallback ) {
3342 $res = $this->query( $cmd, $fname );
3344 if ( $resultCallback ) {
3345 call_user_func( $resultCallback, $res, $this );
3348 if ( false === $res ) {
3349 $err = $this->lastError();
3351 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
3362 * Called by sourceStream() to check if we've reached a statement end
3364 * @param string $sql SQL assembled so far
3365 * @param string $newLine New line about to be added to $sql
3366 * @return bool Whether $newLine contains end of the statement
3368 public function streamStatementEnd( &$sql, &$newLine ) {
3369 if ( $this->delimiter
) {
3371 $newLine = preg_replace( '/' . preg_quote( $this->delimiter
, '/' ) . '$/', '', $newLine );
3372 if ( $newLine != $prev ) {
3381 * Database independent variable replacement. Replaces a set of variables
3382 * in an SQL statement with their contents as given by $this->getSchemaVars().
3384 * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables.
3386 * - '{$var}' should be used for text and is passed through the database's
3388 * - `{$var}` should be used for identifiers (e.g. table and database names).
3389 * It is passed through the database's addIdentifierQuotes method which
3390 * can be overridden if the database uses something other than backticks.
3391 * - / *_* / or / *$wgDBprefix* / passes the name that follows through the
3392 * database's tableName method.
3393 * - / *i* / passes the name that follows through the database's indexName method.
3394 * - In all other cases, / *$var* / is left unencoded. Except for table options,
3395 * its use should be avoided. In 1.24 and older, string encoding was applied.
3397 * @param string $ins SQL statement to replace variables in
3398 * @return string The new SQL statement with variables replaced
3400 protected function replaceVars( $ins ) {
3401 $vars = $this->getSchemaVars();
3402 return preg_replace_callback(
3404 /\* (\$wgDBprefix|[_i]) \*/ (\w*) | # 1-2. tableName, indexName
3405 \'\{\$ (\w+) }\' | # 3. addQuotes
3406 `\{\$ (\w+) }` | # 4. addIdentifierQuotes
3407 /\*\$ (\w+) \*/ # 5. leave unencoded
3409 function ( $m ) use ( $vars ) {
3410 // Note: Because of <https://bugs.php.net/bug.php?id=51881>,
3411 // check for both nonexistent keys *and* the empty string.
3412 if ( isset( $m[1] ) && $m[1] !== '' ) {
3413 if ( $m[1] === 'i' ) {
3414 return $this->indexName( $m[2] );
3416 return $this->tableName( $m[2] );
3418 } elseif ( isset( $m[3] ) && $m[3] !== '' && array_key_exists( $m[3], $vars ) ) {
3419 return $this->addQuotes( $vars[$m[3]] );
3420 } elseif ( isset( $m[4] ) && $m[4] !== '' && array_key_exists( $m[4], $vars ) ) {
3421 return $this->addIdentifierQuotes( $vars[$m[4]] );
3422 } elseif ( isset( $m[5] ) && $m[5] !== '' && array_key_exists( $m[5], $vars ) ) {
3423 return $vars[$m[5]];
3433 * Get schema variables. If none have been set via setSchemaVars(), then
3434 * use some defaults from the current object.
3438 protected function getSchemaVars() {
3439 if ( $this->mSchemaVars
) {
3440 return $this->mSchemaVars
;
3442 return $this->getDefaultSchemaVars();
3447 * Get schema variables to use if none have been set via setSchemaVars().
3449 * Override this in derived classes to provide variables for tables.sql
3450 * and SQL patch files.
3454 protected function getDefaultSchemaVars() {
3458 public function lockIsFree( $lockName, $method ) {
3462 public function lock( $lockName, $method, $timeout = 5 ) {
3463 $this->mNamedLocksHeld
[$lockName] = 1;
3468 public function unlock( $lockName, $method ) {
3469 unset( $this->mNamedLocksHeld
[$lockName] );
3474 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
3475 if ( $this->writesOrCallbacksPending() ) {
3476 // This only flushes transactions to clear snapshots, not to write data
3477 throw new DBUnexpectedError(
3479 "$fname: Cannot COMMIT to clear snapshot because writes are pending."
3483 if ( !$this->lock( $lockKey, $fname, $timeout ) ) {
3487 $unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) {
3488 if ( $this->trxLevel() ) {
3489 // There is a good chance an exception was thrown, causing any early return
3490 // from the caller. Let any error handler get a chance to issue rollback().
3491 // If there isn't one, let the error bubble up and trigger server-side rollback.
3492 $this->onTransactionResolution( function () use ( $lockKey, $fname ) {
3493 $this->unlock( $lockKey, $fname );
3496 $this->unlock( $lockKey, $fname );
3500 $this->commit( __METHOD__
, self
::FLUSHING_INTERNAL
);
3505 public function namedLocksEnqueue() {
3510 * Lock specific tables
3512 * @param array $read Array of tables to lock for read access
3513 * @param array $write Array of tables to lock for write access
3514 * @param string $method Name of caller
3515 * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY
3518 public function lockTables( $read, $write, $method, $lowPriority = true ) {
3523 * Unlock specific tables
3525 * @param string $method The caller
3528 public function unlockTables( $method ) {
3534 * @param string $tableName
3535 * @param string $fName
3536 * @return bool|ResultWrapper
3539 public function dropTable( $tableName, $fName = __METHOD__
) {
3540 if ( !$this->tableExists( $tableName, $fName ) ) {
3543 $sql = "DROP TABLE " . $this->tableName( $tableName );
3544 if ( $this->cascadingDeletes() ) {
3548 return $this->query( $sql, $fName );
3552 * Get search engine class. All subclasses of this need to implement this
3553 * if they wish to use searching.
3557 public function getSearchEngine() {
3558 return 'SearchEngineDummy';
3561 public function getInfinity() {
3565 public function encodeExpiry( $expiry ) {
3566 return ( $expiry == '' ||
$expiry == 'infinity' ||
$expiry == $this->getInfinity() )
3567 ?
$this->getInfinity()
3568 : $this->timestamp( $expiry );
3571 public function decodeExpiry( $expiry, $format = TS_MW
) {
3572 return ( $expiry == '' ||
$expiry == 'infinity' ||
$expiry == $this->getInfinity() )
3574 : wfTimestamp( $format, $expiry );
3577 public function setBigSelects( $value = true ) {
3581 public function isReadOnly() {
3582 return ( $this->getReadOnlyReason() !== false );
3586 * @return string|bool Reason this DB is read-only or false if it is not
3588 protected function getReadOnlyReason() {
3589 $reason = $this->getLBInfo( 'readOnlyReason' );
3591 return is_string( $reason ) ?
$reason : false;
3598 public function __toString() {
3599 return (string)$this->mConn
;
3603 * Run a few simple sanity checks
3605 public function __destruct() {
3606 if ( $this->mTrxLevel
&& $this->mTrxDoneWrites
) {
3607 trigger_error( "Uncommitted DB writes (transaction from {$this->mTrxFname})." );
3609 $danglingCallbacks = array_merge(
3610 $this->mTrxIdleCallbacks
,
3611 $this->mTrxPreCommitCallbacks
,
3612 $this->mTrxEndCallbacks
3614 if ( $danglingCallbacks ) {
3616 foreach ( $danglingCallbacks as $callbackInfo ) {
3617 $callers[] = $callbackInfo[1];
3619 $callers = implode( ', ', $callers );
3620 trigger_error( "DB transaction callbacks still pending (from $callers)." );
3628 abstract class Database
extends DatabaseBase
{
3629 // B/C until nothing type hints for DatabaseBase
3630 // @TODO: finish renaming DatabaseBase => Database