DatabaseMysqlBase visibility cleanups
[mediawiki.git] / includes / libs / rdbms / database / Database.php
blobee4524fae8121ecee807f8eb3ca4d3453350a8ce
1 <?php
2 /**
3 * @defgroup Database Database
5 * This file deals with database interface functions
6 * and query specifics/optimisations.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
24 * @ingroup Database
26 use Psr\Log\LoggerAwareInterface;
27 use Psr\Log\LoggerInterface;
28 use Wikimedia\ScopedCallback;
30 /**
31 * Relational database abstraction object
33 * @ingroup Database
34 * @since 1.28
36 abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAwareInterface {
37 /** Number of times to re-try an operation in case of deadlock */
38 const DEADLOCK_TRIES = 4;
39 /** Minimum time to wait before retry, in microseconds */
40 const DEADLOCK_DELAY_MIN = 500000;
41 /** Maximum time to wait before retry */
42 const DEADLOCK_DELAY_MAX = 1500000;
44 /** How long before it is worth doing a dummy query to test the connection */
45 const PING_TTL = 1.0;
46 const PING_QUERY = 'SELECT 1 AS ping';
48 const TINY_WRITE_SEC = .010;
49 const SLOW_WRITE_SEC = .500;
50 const SMALL_WRITE_ROWS = 100;
52 /** @var string SQL query */
53 protected $mLastQuery = '';
54 /** @var float|bool UNIX timestamp of last write query */
55 protected $mLastWriteTime = false;
56 /** @var string|bool */
57 protected $mPHPError = false;
58 /** @var string */
59 protected $mServer;
60 /** @var string */
61 protected $mUser;
62 /** @var string */
63 protected $mPassword;
64 /** @var string */
65 protected $mDBname;
66 /** @var array[] $aliases Map of (table => (dbname, schema, prefix) map) */
67 protected $tableAliases = [];
68 /** @var bool Whether this PHP instance is for a CLI script */
69 protected $cliMode;
70 /** @var string Agent name for query profiling */
71 protected $agent;
73 /** @var BagOStuff APC cache */
74 protected $srvCache;
75 /** @var LoggerInterface */
76 protected $connLogger;
77 /** @var LoggerInterface */
78 protected $queryLogger;
79 /** @var callback Error logging callback */
80 protected $errorLogger;
82 /** @var resource|null Database connection */
83 protected $mConn = null;
84 /** @var bool */
85 protected $mOpened = false;
87 /** @var array[] List of (callable, method name) */
88 protected $mTrxIdleCallbacks = [];
89 /** @var array[] List of (callable, method name) */
90 protected $mTrxPreCommitCallbacks = [];
91 /** @var array[] List of (callable, method name) */
92 protected $mTrxEndCallbacks = [];
93 /** @var callable[] Map of (name => callable) */
94 protected $mTrxRecurringCallbacks = [];
95 /** @var bool Whether to suppress triggering of transaction end callbacks */
96 protected $mTrxEndCallbacksSuppressed = false;
98 /** @var string */
99 protected $mTablePrefix = '';
100 /** @var string */
101 protected $mSchema = '';
102 /** @var integer */
103 protected $mFlags;
104 /** @var array */
105 protected $mLBInfo = [];
106 /** @var bool|null */
107 protected $mDefaultBigSelects = null;
108 /** @var array|bool */
109 protected $mSchemaVars = false;
110 /** @var array */
111 protected $mSessionVars = [];
112 /** @var array|null */
113 protected $preparedArgs;
114 /** @var string|bool|null Stashed value of html_errors INI setting */
115 protected $htmlErrors;
116 /** @var string */
117 protected $delimiter = ';';
118 /** @var DatabaseDomain */
119 protected $currentDomain;
122 * Either 1 if a transaction is active or 0 otherwise.
123 * The other Trx fields may not be meaningfull if this is 0.
125 * @var int
127 protected $mTrxLevel = 0;
129 * Either a short hexidecimal string if a transaction is active or ""
131 * @var string
132 * @see Database::mTrxLevel
134 protected $mTrxShortId = '';
136 * The UNIX time that the transaction started. Callers can assume that if
137 * snapshot isolation is used, then the data is *at least* up to date to that
138 * point (possibly more up-to-date since the first SELECT defines the snapshot).
140 * @var float|null
141 * @see Database::mTrxLevel
143 private $mTrxTimestamp = null;
144 /** @var float Lag estimate at the time of BEGIN */
145 private $mTrxReplicaLag = null;
147 * Remembers the function name given for starting the most recent transaction via begin().
148 * Used to provide additional context for error reporting.
150 * @var string
151 * @see Database::mTrxLevel
153 private $mTrxFname = null;
155 * Record if possible write queries were done in the last transaction started
157 * @var bool
158 * @see Database::mTrxLevel
160 private $mTrxDoneWrites = false;
162 * Record if the current transaction was started implicitly due to DBO_TRX being set.
164 * @var bool
165 * @see Database::mTrxLevel
167 private $mTrxAutomatic = false;
169 * Array of levels of atomicity within transactions
171 * @var array
173 private $mTrxAtomicLevels = [];
175 * Record if the current transaction was started implicitly by Database::startAtomic
177 * @var bool
179 private $mTrxAutomaticAtomic = false;
181 * Track the write query callers of the current transaction
183 * @var string[]
185 private $mTrxWriteCallers = [];
187 * @var float Seconds spent in write queries for the current transaction
189 private $mTrxWriteDuration = 0.0;
191 * @var integer Number of write queries for the current transaction
193 private $mTrxWriteQueryCount = 0;
195 * @var float Like mTrxWriteQueryCount but excludes lock-bound, easy to replicate, queries
197 private $mTrxWriteAdjDuration = 0.0;
199 * @var integer Number of write queries counted in mTrxWriteAdjDuration
201 private $mTrxWriteAdjQueryCount = 0;
203 * @var float RTT time estimate
205 private $mRTTEstimate = 0.0;
207 /** @var array Map of (name => 1) for locks obtained via lock() */
208 private $mNamedLocksHeld = [];
209 /** @var array Map of (table name => 1) for TEMPORARY tables */
210 protected $mSessionTempTables = [];
212 /** @var IDatabase|null Lazy handle to the master DB this server replicates from */
213 private $lazyMasterHandle;
215 /** @var float UNIX timestamp */
216 protected $lastPing = 0.0;
218 /** @var int[] Prior mFlags values */
219 private $priorFlags = [];
221 /** @var object|string Class name or object With profileIn/profileOut methods */
222 protected $profiler;
223 /** @var TransactionProfiler */
224 protected $trxProfiler;
227 * Constructor and database handle and attempt to connect to the DB server
229 * IDatabase classes should not be constructed directly in external
230 * code. Database::factory() should be used instead.
232 * @param array $params Parameters passed from Database::factory()
234 function __construct( array $params ) {
235 $server = $params['host'];
236 $user = $params['user'];
237 $password = $params['password'];
238 $dbName = $params['dbname'];
240 $this->mSchema = $params['schema'];
241 $this->mTablePrefix = $params['tablePrefix'];
243 $this->cliMode = $params['cliMode'];
244 // Agent name is added to SQL queries in a comment, so make sure it can't break out
245 $this->agent = str_replace( '/', '-', $params['agent'] );
247 $this->mFlags = $params['flags'];
248 if ( $this->mFlags & self::DBO_DEFAULT ) {
249 if ( $this->cliMode ) {
250 $this->mFlags &= ~self::DBO_TRX;
251 } else {
252 $this->mFlags |= self::DBO_TRX;
256 $this->mSessionVars = $params['variables'];
258 $this->srvCache = isset( $params['srvCache'] )
259 ? $params['srvCache']
260 : new HashBagOStuff();
262 $this->profiler = $params['profiler'];
263 $this->trxProfiler = $params['trxProfiler'];
264 $this->connLogger = $params['connLogger'];
265 $this->queryLogger = $params['queryLogger'];
266 $this->errorLogger = $params['errorLogger'];
268 // Set initial dummy domain until open() sets the final DB/prefix
269 $this->currentDomain = DatabaseDomain::newUnspecified();
271 if ( $user ) {
272 $this->open( $server, $user, $password, $dbName );
273 } elseif ( $this->requiresDatabaseUser() ) {
274 throw new InvalidArgumentException( "No database user provided." );
277 // Set the domain object after open() sets the relevant fields
278 if ( $this->mDBname != '' ) {
279 // Domains with server scope but a table prefix are not used by IDatabase classes
280 $this->currentDomain = new DatabaseDomain( $this->mDBname, null, $this->mTablePrefix );
285 * Construct a Database subclass instance given a database type and parameters
287 * This also connects to the database immediately upon object construction
289 * @param string $dbType A possible DB type (sqlite, mysql, postgres)
290 * @param array $p Parameter map with keys:
291 * - host : The hostname of the DB server
292 * - user : The name of the database user the client operates under
293 * - password : The password for the database user
294 * - dbname : The name of the database to use where queries do not specify one.
295 * The database must exist or an error might be thrown. Setting this to the empty string
296 * will avoid any such errors and make the handle have no implicit database scope. This is
297 * useful for queries like SHOW STATUS, CREATE DATABASE, or DROP DATABASE. Note that a
298 * "database" in Postgres is rougly equivalent to an entire MySQL server. This the domain
299 * in which user names and such are defined, e.g. users are database-specific in Postgres.
300 * - schema : The database schema to use (if supported). A "schema" in Postgres is roughly
301 * equivalent to a "database" in MySQL. Note that MySQL and SQLite do not use schemas.
302 * - tablePrefix : Optional table prefix that is implicitly added on to all table names
303 * recognized in queries. This can be used in place of schemas for handle site farms.
304 * - flags : Optional bitfield of DBO_* constants that define connection, protocol,
305 * buffering, and transaction behavior. It is STRONGLY adviced to leave the DBO_DEFAULT
306 * flag in place UNLESS this this database simply acts as a key/value store.
307 * - driver: Optional name of a specific DB client driver. For MySQL, there is the old
308 * 'mysql' driver and the newer 'mysqli' driver.
309 * - variables: Optional map of session variables to set after connecting. This can be
310 * used to adjust lock timeouts or encoding modes and the like.
311 * - connLogger: Optional PSR-3 logger interface instance.
312 * - queryLogger: Optional PSR-3 logger interface instance.
313 * - profiler: Optional class name or object with profileIn()/profileOut() methods.
314 * These will be called in query(), using a simplified version of the SQL that also
315 * includes the agent as a SQL comment.
316 * - trxProfiler: Optional TransactionProfiler instance.
317 * - errorLogger: Optional callback that takes an Exception and logs it.
318 * - cliMode: Whether to consider the execution context that of a CLI script.
319 * - agent: Optional name used to identify the end-user in query profiling/logging.
320 * - srvCache: Optional BagOStuff instance to an APC-style cache.
321 * @return Database|null If the database driver or extension cannot be found
322 * @throws InvalidArgumentException If the database driver or extension cannot be found
323 * @since 1.18
325 final public static function factory( $dbType, $p = [] ) {
326 static $canonicalDBTypes = [
327 'mysql' => [ 'mysqli', 'mysql' ],
328 'postgres' => [],
329 'sqlite' => [],
330 'oracle' => [],
331 'mssql' => [],
334 $driver = false;
335 $dbType = strtolower( $dbType );
336 if ( isset( $canonicalDBTypes[$dbType] ) && $canonicalDBTypes[$dbType] ) {
337 $possibleDrivers = $canonicalDBTypes[$dbType];
338 if ( !empty( $p['driver'] ) ) {
339 if ( in_array( $p['driver'], $possibleDrivers ) ) {
340 $driver = $p['driver'];
341 } else {
342 throw new InvalidArgumentException( __METHOD__ .
343 " type '$dbType' does not support driver '{$p['driver']}'" );
345 } else {
346 foreach ( $possibleDrivers as $posDriver ) {
347 if ( extension_loaded( $posDriver ) ) {
348 $driver = $posDriver;
349 break;
353 } else {
354 $driver = $dbType;
356 if ( $driver === false || $driver === '' ) {
357 throw new InvalidArgumentException( __METHOD__ .
358 " no viable database extension found for type '$dbType'" );
361 $class = 'Database' . ucfirst( $driver );
362 if ( class_exists( $class ) && is_subclass_of( $class, 'IDatabase' ) ) {
363 // Resolve some defaults for b/c
364 $p['host'] = isset( $p['host'] ) ? $p['host'] : false;
365 $p['user'] = isset( $p['user'] ) ? $p['user'] : false;
366 $p['password'] = isset( $p['password'] ) ? $p['password'] : false;
367 $p['dbname'] = isset( $p['dbname'] ) ? $p['dbname'] : false;
368 $p['flags'] = isset( $p['flags'] ) ? $p['flags'] : 0;
369 $p['variables'] = isset( $p['variables'] ) ? $p['variables'] : [];
370 $p['tablePrefix'] = isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : '';
371 $p['schema'] = isset( $p['schema'] ) ? $p['schema'] : '';
372 $p['cliMode'] = isset( $p['cliMode'] ) ? $p['cliMode'] : ( PHP_SAPI === 'cli' );
373 $p['agent'] = isset( $p['agent'] ) ? $p['agent'] : '';
374 if ( !isset( $p['connLogger'] ) ) {
375 $p['connLogger'] = new \Psr\Log\NullLogger();
377 if ( !isset( $p['queryLogger'] ) ) {
378 $p['queryLogger'] = new \Psr\Log\NullLogger();
380 $p['profiler'] = isset( $p['profiler'] ) ? $p['profiler'] : null;
381 if ( !isset( $p['trxProfiler'] ) ) {
382 $p['trxProfiler'] = new TransactionProfiler();
384 if ( !isset( $p['errorLogger'] ) ) {
385 $p['errorLogger'] = function ( Exception $e ) {
386 trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
390 $conn = new $class( $p );
391 } else {
392 $conn = null;
395 return $conn;
398 public function setLogger( LoggerInterface $logger ) {
399 $this->queryLogger = $logger;
402 public function getServerInfo() {
403 return $this->getServerVersion();
406 public function bufferResults( $buffer = null ) {
407 $res = !$this->getFlag( self::DBO_NOBUFFER );
408 if ( $buffer !== null ) {
409 $buffer
410 ? $this->clearFlag( self::DBO_NOBUFFER )
411 : $this->setFlag( self::DBO_NOBUFFER );
414 return $res;
418 * Turns on (false) or off (true) the automatic generation and sending
419 * of a "we're sorry, but there has been a database error" page on
420 * database errors. Default is on (false). When turned off, the
421 * code should use lastErrno() and lastError() to handle the
422 * situation as appropriate.
424 * Do not use this function outside of the Database classes.
426 * @param null|bool $ignoreErrors
427 * @return bool The previous value of the flag.
429 protected function ignoreErrors( $ignoreErrors = null ) {
430 $res = $this->getFlag( self::DBO_IGNORE );
431 if ( $ignoreErrors !== null ) {
432 $ignoreErrors
433 ? $this->setFlag( self::DBO_IGNORE )
434 : $this->clearFlag( self::DBO_IGNORE );
437 return $res;
440 public function trxLevel() {
441 return $this->mTrxLevel;
444 public function trxTimestamp() {
445 return $this->mTrxLevel ? $this->mTrxTimestamp : null;
448 public function tablePrefix( $prefix = null ) {
449 $old = $this->mTablePrefix;
450 if ( $prefix !== null ) {
451 $this->mTablePrefix = $prefix;
452 $this->currentDomain = ( $this->mDBname != '' )
453 ? new DatabaseDomain( $this->mDBname, null, $this->mTablePrefix )
454 : DatabaseDomain::newUnspecified();
457 return $old;
460 public function dbSchema( $schema = null ) {
461 $old = $this->mSchema;
462 if ( $schema !== null ) {
463 $this->mSchema = $schema;
466 return $old;
469 public function getLBInfo( $name = null ) {
470 if ( is_null( $name ) ) {
471 return $this->mLBInfo;
472 } else {
473 if ( array_key_exists( $name, $this->mLBInfo ) ) {
474 return $this->mLBInfo[$name];
475 } else {
476 return null;
481 public function setLBInfo( $name, $value = null ) {
482 if ( is_null( $value ) ) {
483 $this->mLBInfo = $name;
484 } else {
485 $this->mLBInfo[$name] = $value;
489 public function setLazyMasterHandle( IDatabase $conn ) {
490 $this->lazyMasterHandle = $conn;
494 * @return IDatabase|null
495 * @see setLazyMasterHandle()
496 * @since 1.27
498 protected function getLazyMasterHandle() {
499 return $this->lazyMasterHandle;
502 public function implicitGroupby() {
503 return true;
506 public function implicitOrderby() {
507 return true;
510 public function lastQuery() {
511 return $this->mLastQuery;
514 public function doneWrites() {
515 return (bool)$this->mLastWriteTime;
518 public function lastDoneWrites() {
519 return $this->mLastWriteTime ?: false;
522 public function writesPending() {
523 return $this->mTrxLevel && $this->mTrxDoneWrites;
526 public function writesOrCallbacksPending() {
527 return $this->mTrxLevel && (
528 $this->mTrxDoneWrites || $this->mTrxIdleCallbacks || $this->mTrxPreCommitCallbacks
532 public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
533 if ( !$this->mTrxLevel ) {
534 return false;
535 } elseif ( !$this->mTrxDoneWrites ) {
536 return 0.0;
539 switch ( $type ) {
540 case self::ESTIMATE_DB_APPLY:
541 $this->ping( $rtt );
542 $rttAdjTotal = $this->mTrxWriteAdjQueryCount * $rtt;
543 $applyTime = max( $this->mTrxWriteAdjDuration - $rttAdjTotal, 0 );
544 // For omitted queries, make them count as something at least
545 $omitted = $this->mTrxWriteQueryCount - $this->mTrxWriteAdjQueryCount;
546 $applyTime += self::TINY_WRITE_SEC * $omitted;
548 return $applyTime;
549 default: // everything
550 return $this->mTrxWriteDuration;
554 public function pendingWriteCallers() {
555 return $this->mTrxLevel ? $this->mTrxWriteCallers : [];
558 protected function pendingWriteAndCallbackCallers() {
559 if ( !$this->mTrxLevel ) {
560 return [];
563 $fnames = $this->mTrxWriteCallers;
564 foreach ( [
565 $this->mTrxIdleCallbacks,
566 $this->mTrxPreCommitCallbacks,
567 $this->mTrxEndCallbacks
568 ] as $callbacks ) {
569 foreach ( $callbacks as $callback ) {
570 $fnames[] = $callback[1];
574 return $fnames;
577 public function isOpen() {
578 return $this->mOpened;
581 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
582 if ( $remember === self::REMEMBER_PRIOR ) {
583 array_push( $this->priorFlags, $this->mFlags );
585 $this->mFlags |= $flag;
588 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
589 if ( $remember === self::REMEMBER_PRIOR ) {
590 array_push( $this->priorFlags, $this->mFlags );
592 $this->mFlags &= ~$flag;
595 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
596 if ( !$this->priorFlags ) {
597 return;
600 if ( $state === self::RESTORE_INITIAL ) {
601 $this->mFlags = reset( $this->priorFlags );
602 $this->priorFlags = [];
603 } else {
604 $this->mFlags = array_pop( $this->priorFlags );
608 public function getFlag( $flag ) {
609 return !!( $this->mFlags & $flag );
613 * @param string $name Class field name
614 * @return mixed
615 * @deprecated Since 1.28
617 public function getProperty( $name ) {
618 return $this->$name;
621 public function getDomainID() {
622 return $this->currentDomain->getId();
625 final public function getWikiID() {
626 return $this->getDomainID();
630 * Get information about an index into an object
631 * @param string $table Table name
632 * @param string $index Index name
633 * @param string $fname Calling function name
634 * @return mixed Database-specific index description class or false if the index does not exist
636 abstract function indexInfo( $table, $index, $fname = __METHOD__ );
639 * Wrapper for addslashes()
641 * @param string $s String to be slashed.
642 * @return string Slashed string.
644 abstract function strencode( $s );
646 protected function installErrorHandler() {
647 $this->mPHPError = false;
648 $this->htmlErrors = ini_set( 'html_errors', '0' );
649 set_error_handler( [ $this, 'connectionErrorLogger' ] );
653 * @return bool|string
655 protected function restoreErrorHandler() {
656 restore_error_handler();
657 if ( $this->htmlErrors !== false ) {
658 ini_set( 'html_errors', $this->htmlErrors );
661 return $this->getLastPHPError();
665 * @return string|bool Last PHP error for this DB (typically connection errors)
667 protected function getLastPHPError() {
668 if ( $this->mPHPError ) {
669 $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError );
670 $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
672 return $error;
675 return false;
679 * This method should not be used outside of Database classes
681 * @param int $errno
682 * @param string $errstr
684 public function connectionErrorLogger( $errno, $errstr ) {
685 $this->mPHPError = $errstr;
689 * Create a log context to pass to PSR-3 logger functions.
691 * @param array $extras Additional data to add to context
692 * @return array
694 protected function getLogContext( array $extras = [] ) {
695 return array_merge(
697 'db_server' => $this->mServer,
698 'db_name' => $this->mDBname,
699 'db_user' => $this->mUser,
701 $extras
705 public function close() {
706 if ( $this->mConn ) {
707 if ( $this->trxLevel() ) {
708 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
711 $closed = $this->closeConnection();
712 $this->mConn = false;
713 } elseif ( $this->mTrxIdleCallbacks || $this->mTrxEndCallbacks ) { // sanity
714 throw new RuntimeException( "Transaction callbacks still pending." );
715 } else {
716 $closed = true;
718 $this->mOpened = false;
720 return $closed;
724 * Make sure isOpen() returns true as a sanity check
726 * @throws DBUnexpectedError
728 protected function assertOpen() {
729 if ( !$this->isOpen() ) {
730 throw new DBUnexpectedError( $this, "DB connection was already closed." );
735 * Closes underlying database connection
736 * @since 1.20
737 * @return bool Whether connection was closed successfully
739 abstract protected function closeConnection();
741 public function reportConnectionError( $error = 'Unknown error' ) {
742 $myError = $this->lastError();
743 if ( $myError ) {
744 $error = $myError;
747 # New method
748 throw new DBConnectionError( $this, $error );
752 * The DBMS-dependent part of query()
754 * @param string $sql SQL query.
755 * @return ResultWrapper|bool Result object to feed to fetchObject,
756 * fetchRow, ...; or false on failure
758 abstract protected function doQuery( $sql );
761 * Determine whether a query writes to the DB.
762 * Should return true if unsure.
764 * @param string $sql
765 * @return bool
767 protected function isWriteQuery( $sql ) {
768 return !preg_match(
769 '/^(?:SELECT|BEGIN|ROLLBACK|COMMIT|SET|SHOW|EXPLAIN|\(SELECT)\b/i', $sql );
773 * @param $sql
774 * @return string|null
776 protected function getQueryVerb( $sql ) {
777 return preg_match( '/^\s*([a-z]+)/i', $sql, $m ) ? strtoupper( $m[1] ) : null;
781 * Determine whether a SQL statement is sensitive to isolation level.
782 * A SQL statement is considered transactable if its result could vary
783 * depending on the transaction isolation level. Operational commands
784 * such as 'SET' and 'SHOW' are not considered to be transactable.
786 * @param string $sql
787 * @return bool
789 protected function isTransactableQuery( $sql ) {
790 return !in_array(
791 $this->getQueryVerb( $sql ),
792 [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET', 'CREATE', 'ALTER' ],
793 true
798 * @param string $sql A SQL query
799 * @return bool Whether $sql is SQL for creating/dropping a new TEMPORARY table
801 protected function registerTempTableOperation( $sql ) {
802 if ( preg_match(
803 '/^CREATE\s+TEMPORARY\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?[`"\']?(\w+)[`"\']?/i',
804 $sql,
805 $matches
806 ) ) {
807 $this->mSessionTempTables[$matches[1]] = 1;
809 return true;
810 } elseif ( preg_match(
811 '/^DROP\s+(?:TEMPORARY\s+)?TABLE\s+(?:IF\s+EXISTS\s+)?[`"\']?(\w+)[`"\']?/i',
812 $sql,
813 $matches
814 ) ) {
815 unset( $this->mSessionTempTables[$matches[1]] );
817 return true;
818 } elseif ( preg_match(
819 '/^(?:INSERT\s+(?:\w+\s+)?INTO|UPDATE|DELETE\s+FROM)\s+[`"\']?(\w+)[`"\']?/i',
820 $sql,
821 $matches
822 ) ) {
823 return isset( $this->mSessionTempTables[$matches[1]] );
826 return false;
829 public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
830 $priorWritesPending = $this->writesOrCallbacksPending();
831 $this->mLastQuery = $sql;
833 $isWrite = $this->isWriteQuery( $sql ) && !$this->registerTempTableOperation( $sql );
834 if ( $isWrite ) {
835 $reason = $this->getReadOnlyReason();
836 if ( $reason !== false ) {
837 throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
839 # Set a flag indicating that writes have been done
840 $this->mLastWriteTime = microtime( true );
843 // Add trace comment to the begin of the sql string, right after the operator.
844 // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (bug 42598)
845 $commentedSql = preg_replace( '/\s|$/', " /* $fname {$this->agent} */ ", $sql, 1 );
847 # Start implicit transactions that wrap the request if DBO_TRX is enabled
848 if ( !$this->mTrxLevel && $this->getFlag( self::DBO_TRX )
849 && $this->isTransactableQuery( $sql )
851 $this->begin( __METHOD__ . " ($fname)", self::TRANSACTION_INTERNAL );
852 $this->mTrxAutomatic = true;
855 # Keep track of whether the transaction has write queries pending
856 if ( $this->mTrxLevel && !$this->mTrxDoneWrites && $isWrite ) {
857 $this->mTrxDoneWrites = true;
858 $this->trxProfiler->transactionWritingIn(
859 $this->mServer, $this->mDBname, $this->mTrxShortId );
862 if ( $this->getFlag( self::DBO_DEBUG ) ) {
863 $this->queryLogger->debug( "{$this->mDBname} {$commentedSql}" );
866 # Avoid fatals if close() was called
867 $this->assertOpen();
869 # Send the query to the server
870 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
872 # Try reconnecting if the connection was lost
873 if ( false === $ret && $this->wasErrorReissuable() ) {
874 $recoverable = $this->canRecoverFromDisconnect( $sql, $priorWritesPending );
875 # Stash the last error values before anything might clear them
876 $lastError = $this->lastError();
877 $lastErrno = $this->lastErrno();
878 # Update state tracking to reflect transaction loss due to disconnection
879 $this->handleSessionLoss();
880 if ( $this->reconnect() ) {
881 $msg = __METHOD__ . ": lost connection to {$this->getServer()}; reconnected";
882 $this->connLogger->warning( $msg );
883 $this->queryLogger->warning(
884 "$msg:\n" . ( new RuntimeException() )->getTraceAsString() );
886 if ( !$recoverable ) {
887 # Callers may catch the exception and continue to use the DB
888 $this->reportQueryError( $lastError, $lastErrno, $sql, $fname );
889 } else {
890 # Should be safe to silently retry the query
891 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
893 } else {
894 $msg = __METHOD__ . ": lost connection to {$this->getServer()} permanently";
895 $this->connLogger->error( $msg );
899 if ( false === $ret ) {
900 # Deadlocks cause the entire transaction to abort, not just the statement.
901 # http://dev.mysql.com/doc/refman/5.7/en/innodb-error-handling.html
902 # https://www.postgresql.org/docs/9.1/static/explicit-locking.html
903 if ( $this->wasDeadlock() ) {
904 if ( $this->explicitTrxActive() || $priorWritesPending ) {
905 $tempIgnore = false; // not recoverable
907 # Update state tracking to reflect transaction loss
908 $this->handleSessionLoss();
911 $this->reportQueryError(
912 $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
915 $res = $this->resultObject( $ret );
917 return $res;
920 private function doProfiledQuery( $sql, $commentedSql, $isWrite, $fname ) {
921 $isMaster = !is_null( $this->getLBInfo( 'master' ) );
922 # generalizeSQL() will probably cut down the query to reasonable
923 # logging size most of the time. The substr is really just a sanity check.
924 if ( $isMaster ) {
925 $queryProf = 'query-m: ' . substr( self::generalizeSQL( $sql ), 0, 255 );
926 } else {
927 $queryProf = 'query: ' . substr( self::generalizeSQL( $sql ), 0, 255 );
930 # Include query transaction state
931 $queryProf .= $this->mTrxShortId ? " [TRX#{$this->mTrxShortId}]" : "";
933 $startTime = microtime( true );
934 if ( $this->profiler ) {
935 call_user_func( [ $this->profiler, 'profileIn' ], $queryProf );
937 $ret = $this->doQuery( $commentedSql );
938 if ( $this->profiler ) {
939 call_user_func( [ $this->profiler, 'profileOut' ], $queryProf );
941 $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
943 unset( $queryProfSection ); // profile out (if set)
945 if ( $ret !== false ) {
946 $this->lastPing = $startTime;
947 if ( $isWrite && $this->mTrxLevel ) {
948 $this->updateTrxWriteQueryTime( $sql, $queryRuntime );
949 $this->mTrxWriteCallers[] = $fname;
953 if ( $sql === self::PING_QUERY ) {
954 $this->mRTTEstimate = $queryRuntime;
957 $this->trxProfiler->recordQueryCompletion(
958 $queryProf, $startTime, $isWrite, $this->affectedRows()
960 $this->queryLogger->debug( $sql, [
961 'method' => $fname,
962 'master' => $isMaster,
963 'runtime' => $queryRuntime,
964 ] );
966 return $ret;
970 * Update the estimated run-time of a query, not counting large row lock times
972 * LoadBalancer can be set to rollback transactions that will create huge replication
973 * lag. It bases this estimate off of pendingWriteQueryDuration(). Certain simple
974 * queries, like inserting a row can take a long time due to row locking. This method
975 * uses some simple heuristics to discount those cases.
977 * @param string $sql A SQL write query
978 * @param float $runtime Total runtime, including RTT
980 private function updateTrxWriteQueryTime( $sql, $runtime ) {
981 // Whether this is indicative of replica DB runtime (except for RBR or ws_repl)
982 $indicativeOfReplicaRuntime = true;
983 if ( $runtime > self::SLOW_WRITE_SEC ) {
984 $verb = $this->getQueryVerb( $sql );
985 // insert(), upsert(), replace() are fast unless bulky in size or blocked on locks
986 if ( $verb === 'INSERT' ) {
987 $indicativeOfReplicaRuntime = $this->affectedRows() > self::SMALL_WRITE_ROWS;
988 } elseif ( $verb === 'REPLACE' ) {
989 $indicativeOfReplicaRuntime = $this->affectedRows() > self::SMALL_WRITE_ROWS / 2;
993 $this->mTrxWriteDuration += $runtime;
994 $this->mTrxWriteQueryCount += 1;
995 if ( $indicativeOfReplicaRuntime ) {
996 $this->mTrxWriteAdjDuration += $runtime;
997 $this->mTrxWriteAdjQueryCount += 1;
1001 private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
1002 # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
1003 # Dropped connections also mean that named locks are automatically released.
1004 # Only allow error suppression in autocommit mode or when the lost transaction
1005 # didn't matter anyway (aside from DBO_TRX snapshot loss).
1006 if ( $this->mNamedLocksHeld ) {
1007 return false; // possible critical section violation
1008 } elseif ( $sql === 'COMMIT' ) {
1009 return !$priorWritesPending; // nothing written anyway? (T127428)
1010 } elseif ( $sql === 'ROLLBACK' ) {
1011 return true; // transaction lost...which is also what was requested :)
1012 } elseif ( $this->explicitTrxActive() ) {
1013 return false; // don't drop atomocity
1014 } elseif ( $priorWritesPending ) {
1015 return false; // prior writes lost from implicit transaction
1018 return true;
1021 private function handleSessionLoss() {
1022 $this->mTrxLevel = 0;
1023 $this->mTrxIdleCallbacks = []; // bug 65263
1024 $this->mTrxPreCommitCallbacks = []; // bug 65263
1025 $this->mSessionTempTables = [];
1026 $this->mNamedLocksHeld = [];
1027 try {
1028 // Handle callbacks in mTrxEndCallbacks
1029 $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
1030 $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
1031 return null;
1032 } catch ( Exception $e ) {
1033 // Already logged; move on...
1034 return $e;
1038 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
1039 if ( $this->ignoreErrors() || $tempIgnore ) {
1040 $this->queryLogger->debug( "SQL ERROR (ignored): $error\n" );
1041 } else {
1042 $sql1line = mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 );
1043 $this->queryLogger->error(
1044 "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
1045 $this->getLogContext( [
1046 'method' => __METHOD__,
1047 'errno' => $errno,
1048 'error' => $error,
1049 'sql1line' => $sql1line,
1050 'fname' => $fname,
1053 $this->queryLogger->debug( "SQL ERROR: " . $error . "\n" );
1054 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
1058 public function freeResult( $res ) {
1061 public function selectField(
1062 $table, $var, $cond = '', $fname = __METHOD__, $options = []
1064 if ( $var === '*' ) { // sanity
1065 throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
1068 if ( !is_array( $options ) ) {
1069 $options = [ $options ];
1072 $options['LIMIT'] = 1;
1074 $res = $this->select( $table, $var, $cond, $fname, $options );
1075 if ( $res === false || !$this->numRows( $res ) ) {
1076 return false;
1079 $row = $this->fetchRow( $res );
1081 if ( $row !== false ) {
1082 return reset( $row );
1083 } else {
1084 return false;
1088 public function selectFieldValues(
1089 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
1091 if ( $var === '*' ) { // sanity
1092 throw new DBUnexpectedError( $this, "Cannot use a * field" );
1093 } elseif ( !is_string( $var ) ) { // sanity
1094 throw new DBUnexpectedError( $this, "Cannot use an array of fields" );
1097 if ( !is_array( $options ) ) {
1098 $options = [ $options ];
1101 $res = $this->select( $table, $var, $cond, $fname, $options, $join_conds );
1102 if ( $res === false ) {
1103 return false;
1106 $values = [];
1107 foreach ( $res as $row ) {
1108 $values[] = $row->$var;
1111 return $values;
1115 * Returns an optional USE INDEX clause to go after the table, and a
1116 * string to go at the end of the query.
1118 * @param array $options Associative array of options to be turned into
1119 * an SQL query, valid keys are listed in the function.
1120 * @return array
1121 * @see Database::select()
1123 protected function makeSelectOptions( $options ) {
1124 $preLimitTail = $postLimitTail = '';
1125 $startOpts = '';
1127 $noKeyOptions = [];
1129 foreach ( $options as $key => $option ) {
1130 if ( is_numeric( $key ) ) {
1131 $noKeyOptions[$option] = true;
1135 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1137 $preLimitTail .= $this->makeOrderBy( $options );
1139 // if (isset($options['LIMIT'])) {
1140 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
1141 // isset($options['OFFSET']) ? $options['OFFSET']
1142 // : false);
1143 // }
1145 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1146 $postLimitTail .= ' FOR UPDATE';
1149 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1150 $postLimitTail .= ' LOCK IN SHARE MODE';
1153 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1154 $startOpts .= 'DISTINCT';
1157 # Various MySQL extensions
1158 if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
1159 $startOpts .= ' /*! STRAIGHT_JOIN */';
1162 if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) {
1163 $startOpts .= ' HIGH_PRIORITY';
1166 if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
1167 $startOpts .= ' SQL_BIG_RESULT';
1170 if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
1171 $startOpts .= ' SQL_BUFFER_RESULT';
1174 if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
1175 $startOpts .= ' SQL_SMALL_RESULT';
1178 if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
1179 $startOpts .= ' SQL_CALC_FOUND_ROWS';
1182 if ( isset( $noKeyOptions['SQL_CACHE'] ) ) {
1183 $startOpts .= ' SQL_CACHE';
1186 if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) {
1187 $startOpts .= ' SQL_NO_CACHE';
1190 if ( isset( $options['USE INDEX'] ) && is_string( $options['USE INDEX'] ) ) {
1191 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1192 } else {
1193 $useIndex = '';
1195 if ( isset( $options['IGNORE INDEX'] ) && is_string( $options['IGNORE INDEX'] ) ) {
1196 $ignoreIndex = $this->ignoreIndexClause( $options['IGNORE INDEX'] );
1197 } else {
1198 $ignoreIndex = '';
1201 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1205 * Returns an optional GROUP BY with an optional HAVING
1207 * @param array $options Associative array of options
1208 * @return string
1209 * @see Database::select()
1210 * @since 1.21
1212 protected function makeGroupByWithHaving( $options ) {
1213 $sql = '';
1214 if ( isset( $options['GROUP BY'] ) ) {
1215 $gb = is_array( $options['GROUP BY'] )
1216 ? implode( ',', $options['GROUP BY'] )
1217 : $options['GROUP BY'];
1218 $sql .= ' GROUP BY ' . $gb;
1220 if ( isset( $options['HAVING'] ) ) {
1221 $having = is_array( $options['HAVING'] )
1222 ? $this->makeList( $options['HAVING'], self::LIST_AND )
1223 : $options['HAVING'];
1224 $sql .= ' HAVING ' . $having;
1227 return $sql;
1231 * Returns an optional ORDER BY
1233 * @param array $options Associative array of options
1234 * @return string
1235 * @see Database::select()
1236 * @since 1.21
1238 protected function makeOrderBy( $options ) {
1239 if ( isset( $options['ORDER BY'] ) ) {
1240 $ob = is_array( $options['ORDER BY'] )
1241 ? implode( ',', $options['ORDER BY'] )
1242 : $options['ORDER BY'];
1244 return ' ORDER BY ' . $ob;
1247 return '';
1250 public function select( $table, $vars, $conds = '', $fname = __METHOD__,
1251 $options = [], $join_conds = [] ) {
1252 $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
1254 return $this->query( $sql, $fname );
1257 public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__,
1258 $options = [], $join_conds = []
1260 if ( is_array( $vars ) ) {
1261 $vars = implode( ',', $this->fieldNamesWithAlias( $vars ) );
1264 $options = (array)$options;
1265 $useIndexes = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
1266 ? $options['USE INDEX']
1267 : [];
1268 $ignoreIndexes = (
1269 isset( $options['IGNORE INDEX'] ) &&
1270 is_array( $options['IGNORE INDEX'] )
1272 ? $options['IGNORE INDEX']
1273 : [];
1275 if ( is_array( $table ) ) {
1276 $from = ' FROM ' .
1277 $this->tableNamesWithIndexClauseOrJOIN(
1278 $table, $useIndexes, $ignoreIndexes, $join_conds );
1279 } elseif ( $table != '' ) {
1280 if ( $table[0] == ' ' ) {
1281 $from = ' FROM ' . $table;
1282 } else {
1283 $from = ' FROM ' .
1284 $this->tableNamesWithIndexClauseOrJOIN(
1285 [ $table ], $useIndexes, $ignoreIndexes, [] );
1287 } else {
1288 $from = '';
1291 list( $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ) =
1292 $this->makeSelectOptions( $options );
1294 if ( !empty( $conds ) ) {
1295 if ( is_array( $conds ) ) {
1296 $conds = $this->makeList( $conds, self::LIST_AND );
1298 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex " .
1299 "WHERE $conds $preLimitTail";
1300 } else {
1301 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex $preLimitTail";
1304 if ( isset( $options['LIMIT'] ) ) {
1305 $sql = $this->limitResult( $sql, $options['LIMIT'],
1306 isset( $options['OFFSET'] ) ? $options['OFFSET'] : false );
1308 $sql = "$sql $postLimitTail";
1310 if ( isset( $options['EXPLAIN'] ) ) {
1311 $sql = 'EXPLAIN ' . $sql;
1314 return $sql;
1317 public function selectRow( $table, $vars, $conds, $fname = __METHOD__,
1318 $options = [], $join_conds = []
1320 $options = (array)$options;
1321 $options['LIMIT'] = 1;
1322 $res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
1324 if ( $res === false ) {
1325 return false;
1328 if ( !$this->numRows( $res ) ) {
1329 return false;
1332 $obj = $this->fetchObject( $res );
1334 return $obj;
1337 public function estimateRowCount(
1338 $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = []
1340 $rows = 0;
1341 $res = $this->select( $table, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options );
1343 if ( $res ) {
1344 $row = $this->fetchRow( $res );
1345 $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
1348 return $rows;
1351 public function selectRowCount(
1352 $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
1354 $rows = 0;
1355 $sql = $this->selectSQLText( $tables, '1', $conds, $fname, $options, $join_conds );
1356 $res = $this->query( "SELECT COUNT(*) AS rowcount FROM ($sql) tmp_count", $fname );
1358 if ( $res ) {
1359 $row = $this->fetchRow( $res );
1360 $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
1363 return $rows;
1367 * Removes most variables from an SQL query and replaces them with X or N for numbers.
1368 * It's only slightly flawed. Don't use for anything important.
1370 * @param string $sql A SQL Query
1372 * @return string
1374 protected static function generalizeSQL( $sql ) {
1375 # This does the same as the regexp below would do, but in such a way
1376 # as to avoid crashing php on some large strings.
1377 # $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql );
1379 $sql = str_replace( "\\\\", '', $sql );
1380 $sql = str_replace( "\\'", '', $sql );
1381 $sql = str_replace( "\\\"", '', $sql );
1382 $sql = preg_replace( "/'.*'/s", "'X'", $sql );
1383 $sql = preg_replace( '/".*"/s', "'X'", $sql );
1385 # All newlines, tabs, etc replaced by single space
1386 $sql = preg_replace( '/\s+/', ' ', $sql );
1388 # All numbers => N,
1389 # except the ones surrounded by characters, e.g. l10n
1390 $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
1391 $sql = preg_replace( '/(?<![a-zA-Z])-?\d+(?![a-zA-Z])/s', 'N', $sql );
1393 return $sql;
1396 public function fieldExists( $table, $field, $fname = __METHOD__ ) {
1397 $info = $this->fieldInfo( $table, $field );
1399 return (bool)$info;
1402 public function indexExists( $table, $index, $fname = __METHOD__ ) {
1403 if ( !$this->tableExists( $table ) ) {
1404 return null;
1407 $info = $this->indexInfo( $table, $index, $fname );
1408 if ( is_null( $info ) ) {
1409 return null;
1410 } else {
1411 return $info !== false;
1415 public function tableExists( $table, $fname = __METHOD__ ) {
1416 $tableRaw = $this->tableName( $table, 'raw' );
1417 if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
1418 return true; // already known to exist
1421 $table = $this->tableName( $table );
1422 $old = $this->ignoreErrors( true );
1423 $res = $this->query( "SELECT 1 FROM $table LIMIT 1", $fname );
1424 $this->ignoreErrors( $old );
1426 return (bool)$res;
1429 public function indexUnique( $table, $index ) {
1430 $indexInfo = $this->indexInfo( $table, $index );
1432 if ( !$indexInfo ) {
1433 return null;
1436 return !$indexInfo[0]->Non_unique;
1440 * Helper for Database::insert().
1442 * @param array $options
1443 * @return string
1445 protected function makeInsertOptions( $options ) {
1446 return implode( ' ', $options );
1449 public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
1450 # No rows to insert, easy just return now
1451 if ( !count( $a ) ) {
1452 return true;
1455 $table = $this->tableName( $table );
1457 if ( !is_array( $options ) ) {
1458 $options = [ $options ];
1461 $fh = null;
1462 if ( isset( $options['fileHandle'] ) ) {
1463 $fh = $options['fileHandle'];
1465 $options = $this->makeInsertOptions( $options );
1467 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
1468 $multi = true;
1469 $keys = array_keys( $a[0] );
1470 } else {
1471 $multi = false;
1472 $keys = array_keys( $a );
1475 $sql = 'INSERT ' . $options .
1476 " INTO $table (" . implode( ',', $keys ) . ') VALUES ';
1478 if ( $multi ) {
1479 $first = true;
1480 foreach ( $a as $row ) {
1481 if ( $first ) {
1482 $first = false;
1483 } else {
1484 $sql .= ',';
1486 $sql .= '(' . $this->makeList( $row ) . ')';
1488 } else {
1489 $sql .= '(' . $this->makeList( $a ) . ')';
1492 if ( $fh !== null && false === fwrite( $fh, $sql ) ) {
1493 return false;
1494 } elseif ( $fh !== null ) {
1495 return true;
1498 return (bool)$this->query( $sql, $fname );
1502 * Make UPDATE options array for Database::makeUpdateOptions
1504 * @param array $options
1505 * @return array
1507 protected function makeUpdateOptionsArray( $options ) {
1508 if ( !is_array( $options ) ) {
1509 $options = [ $options ];
1512 $opts = [];
1514 if ( in_array( 'IGNORE', $options ) ) {
1515 $opts[] = 'IGNORE';
1518 return $opts;
1522 * Make UPDATE options for the Database::update function
1524 * @param array $options The options passed to Database::update
1525 * @return string
1527 protected function makeUpdateOptions( $options ) {
1528 $opts = $this->makeUpdateOptionsArray( $options );
1530 return implode( ' ', $opts );
1533 public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
1534 $table = $this->tableName( $table );
1535 $opts = $this->makeUpdateOptions( $options );
1536 $sql = "UPDATE $opts $table SET " . $this->makeList( $values, self::LIST_SET );
1538 if ( $conds !== [] && $conds !== '*' ) {
1539 $sql .= " WHERE " . $this->makeList( $conds, self::LIST_AND );
1542 return $this->query( $sql, $fname );
1545 public function makeList( $a, $mode = self::LIST_COMMA ) {
1546 if ( !is_array( $a ) ) {
1547 throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
1550 $first = true;
1551 $list = '';
1553 foreach ( $a as $field => $value ) {
1554 if ( !$first ) {
1555 if ( $mode == self::LIST_AND ) {
1556 $list .= ' AND ';
1557 } elseif ( $mode == self::LIST_OR ) {
1558 $list .= ' OR ';
1559 } else {
1560 $list .= ',';
1562 } else {
1563 $first = false;
1566 if ( ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_numeric( $field ) ) {
1567 $list .= "($value)";
1568 } elseif ( $mode == self::LIST_SET && is_numeric( $field ) ) {
1569 $list .= "$value";
1570 } elseif (
1571 ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_array( $value )
1573 // Remove null from array to be handled separately if found
1574 $includeNull = false;
1575 foreach ( array_keys( $value, null, true ) as $nullKey ) {
1576 $includeNull = true;
1577 unset( $value[$nullKey] );
1579 if ( count( $value ) == 0 && !$includeNull ) {
1580 throw new InvalidArgumentException(
1581 __METHOD__ . ": empty input for field $field" );
1582 } elseif ( count( $value ) == 0 ) {
1583 // only check if $field is null
1584 $list .= "$field IS NULL";
1585 } else {
1586 // IN clause contains at least one valid element
1587 if ( $includeNull ) {
1588 // Group subconditions to ensure correct precedence
1589 $list .= '(';
1591 if ( count( $value ) == 1 ) {
1592 // Special-case single values, as IN isn't terribly efficient
1593 // Don't necessarily assume the single key is 0; we don't
1594 // enforce linear numeric ordering on other arrays here.
1595 $value = array_values( $value )[0];
1596 $list .= $field . " = " . $this->addQuotes( $value );
1597 } else {
1598 $list .= $field . " IN (" . $this->makeList( $value ) . ") ";
1600 // if null present in array, append IS NULL
1601 if ( $includeNull ) {
1602 $list .= " OR $field IS NULL)";
1605 } elseif ( $value === null ) {
1606 if ( $mode == self::LIST_AND || $mode == self::LIST_OR ) {
1607 $list .= "$field IS ";
1608 } elseif ( $mode == self::LIST_SET ) {
1609 $list .= "$field = ";
1611 $list .= 'NULL';
1612 } else {
1613 if (
1614 $mode == self::LIST_AND || $mode == self::LIST_OR || $mode == self::LIST_SET
1616 $list .= "$field = ";
1618 $list .= $mode == self::LIST_NAMES ? $value : $this->addQuotes( $value );
1622 return $list;
1625 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
1626 $conds = [];
1628 foreach ( $data as $base => $sub ) {
1629 if ( count( $sub ) ) {
1630 $conds[] = $this->makeList(
1631 [ $baseKey => $base, $subKey => array_keys( $sub ) ],
1632 self::LIST_AND );
1636 if ( $conds ) {
1637 return $this->makeList( $conds, self::LIST_OR );
1638 } else {
1639 // Nothing to search for...
1640 return false;
1644 public function aggregateValue( $valuedata, $valuename = 'value' ) {
1645 return $valuename;
1648 public function bitNot( $field ) {
1649 return "(~$field)";
1652 public function bitAnd( $fieldLeft, $fieldRight ) {
1653 return "($fieldLeft & $fieldRight)";
1656 public function bitOr( $fieldLeft, $fieldRight ) {
1657 return "($fieldLeft | $fieldRight)";
1660 public function buildConcat( $stringList ) {
1661 return 'CONCAT(' . implode( ',', $stringList ) . ')';
1664 public function buildGroupConcatField(
1665 $delim, $table, $field, $conds = '', $join_conds = []
1667 $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')';
1669 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1672 public function buildStringCast( $field ) {
1673 return $field;
1676 public function selectDB( $db ) {
1677 # Stub. Shouldn't cause serious problems if it's not overridden, but
1678 # if your database engine supports a concept similar to MySQL's
1679 # databases you may as well.
1680 $this->mDBname = $db;
1682 return true;
1685 public function getDBname() {
1686 return $this->mDBname;
1689 public function getServer() {
1690 return $this->mServer;
1693 public function tableName( $name, $format = 'quoted' ) {
1694 # Skip the entire process when we have a string quoted on both ends.
1695 # Note that we check the end so that we will still quote any use of
1696 # use of `database`.table. But won't break things if someone wants
1697 # to query a database table with a dot in the name.
1698 if ( $this->isQuotedIdentifier( $name ) ) {
1699 return $name;
1702 # Lets test for any bits of text that should never show up in a table
1703 # name. Basically anything like JOIN or ON which are actually part of
1704 # SQL queries, but may end up inside of the table value to combine
1705 # sql. Such as how the API is doing.
1706 # Note that we use a whitespace test rather than a \b test to avoid
1707 # any remote case where a word like on may be inside of a table name
1708 # surrounded by symbols which may be considered word breaks.
1709 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
1710 return $name;
1713 # Split database and table into proper variables.
1714 # We reverse the explode so that database.table and table both output
1715 # the correct table.
1716 $dbDetails = explode( '.', $name, 3 );
1717 if ( count( $dbDetails ) == 3 ) {
1718 list( $database, $schema, $table ) = $dbDetails;
1719 # We don't want any prefix added in this case
1720 $prefix = '';
1721 } elseif ( count( $dbDetails ) == 2 ) {
1722 list( $database, $table ) = $dbDetails;
1723 # We don't want any prefix added in this case
1724 $prefix = '';
1725 # In dbs that support it, $database may actually be the schema
1726 # but that doesn't affect any of the functionality here
1727 $schema = '';
1728 } else {
1729 list( $table ) = $dbDetails;
1730 if ( isset( $this->tableAliases[$table] ) ) {
1731 $database = $this->tableAliases[$table]['dbname'];
1732 $schema = is_string( $this->tableAliases[$table]['schema'] )
1733 ? $this->tableAliases[$table]['schema']
1734 : $this->mSchema;
1735 $prefix = is_string( $this->tableAliases[$table]['prefix'] )
1736 ? $this->tableAliases[$table]['prefix']
1737 : $this->mTablePrefix;
1738 } else {
1739 $database = '';
1740 $schema = $this->mSchema; # Default schema
1741 $prefix = $this->mTablePrefix; # Default prefix
1745 # Quote $table and apply the prefix if not quoted.
1746 # $tableName might be empty if this is called from Database::replaceVars()
1747 $tableName = "{$prefix}{$table}";
1748 if ( $format === 'quoted'
1749 && !$this->isQuotedIdentifier( $tableName )
1750 && $tableName !== ''
1752 $tableName = $this->addIdentifierQuotes( $tableName );
1755 # Quote $schema and $database and merge them with the table name if needed
1756 $tableName = $this->prependDatabaseOrSchema( $schema, $tableName, $format );
1757 $tableName = $this->prependDatabaseOrSchema( $database, $tableName, $format );
1759 return $tableName;
1763 * @param string|null $namespace Database or schema
1764 * @param string $relation Name of table, view, sequence, etc...
1765 * @param string $format One of (raw, quoted)
1766 * @return string Relation name with quoted and merged $namespace as needed
1768 private function prependDatabaseOrSchema( $namespace, $relation, $format ) {
1769 if ( strlen( $namespace ) ) {
1770 if ( $format === 'quoted' && !$this->isQuotedIdentifier( $namespace ) ) {
1771 $namespace = $this->addIdentifierQuotes( $namespace );
1773 $relation = $namespace . '.' . $relation;
1776 return $relation;
1779 public function tableNames() {
1780 $inArray = func_get_args();
1781 $retVal = [];
1783 foreach ( $inArray as $name ) {
1784 $retVal[$name] = $this->tableName( $name );
1787 return $retVal;
1790 public function tableNamesN() {
1791 $inArray = func_get_args();
1792 $retVal = [];
1794 foreach ( $inArray as $name ) {
1795 $retVal[] = $this->tableName( $name );
1798 return $retVal;
1802 * Get an aliased table name
1803 * e.g. tableName AS newTableName
1805 * @param string $name Table name, see tableName()
1806 * @param string|bool $alias Alias (optional)
1807 * @return string SQL name for aliased table. Will not alias a table to its own name
1809 protected function tableNameWithAlias( $name, $alias = false ) {
1810 if ( !$alias || $alias == $name ) {
1811 return $this->tableName( $name );
1812 } else {
1813 return $this->tableName( $name ) . ' ' . $this->addIdentifierQuotes( $alias );
1818 * Gets an array of aliased table names
1820 * @param array $tables [ [alias] => table ]
1821 * @return string[] See tableNameWithAlias()
1823 protected function tableNamesWithAlias( $tables ) {
1824 $retval = [];
1825 foreach ( $tables as $alias => $table ) {
1826 if ( is_numeric( $alias ) ) {
1827 $alias = $table;
1829 $retval[] = $this->tableNameWithAlias( $table, $alias );
1832 return $retval;
1836 * Get an aliased field name
1837 * e.g. fieldName AS newFieldName
1839 * @param string $name Field name
1840 * @param string|bool $alias Alias (optional)
1841 * @return string SQL name for aliased field. Will not alias a field to its own name
1843 protected function fieldNameWithAlias( $name, $alias = false ) {
1844 if ( !$alias || (string)$alias === (string)$name ) {
1845 return $name;
1846 } else {
1847 return $name . ' AS ' . $this->addIdentifierQuotes( $alias ); // PostgreSQL needs AS
1852 * Gets an array of aliased field names
1854 * @param array $fields [ [alias] => field ]
1855 * @return string[] See fieldNameWithAlias()
1857 protected function fieldNamesWithAlias( $fields ) {
1858 $retval = [];
1859 foreach ( $fields as $alias => $field ) {
1860 if ( is_numeric( $alias ) ) {
1861 $alias = $field;
1863 $retval[] = $this->fieldNameWithAlias( $field, $alias );
1866 return $retval;
1870 * Get the aliased table name clause for a FROM clause
1871 * which might have a JOIN and/or USE INDEX or IGNORE INDEX clause
1873 * @param array $tables ( [alias] => table )
1874 * @param array $use_index Same as for select()
1875 * @param array $ignore_index Same as for select()
1876 * @param array $join_conds Same as for select()
1877 * @return string
1879 protected function tableNamesWithIndexClauseOrJOIN(
1880 $tables, $use_index = [], $ignore_index = [], $join_conds = []
1882 $ret = [];
1883 $retJOIN = [];
1884 $use_index = (array)$use_index;
1885 $ignore_index = (array)$ignore_index;
1886 $join_conds = (array)$join_conds;
1888 foreach ( $tables as $alias => $table ) {
1889 if ( !is_string( $alias ) ) {
1890 // No alias? Set it equal to the table name
1891 $alias = $table;
1893 // Is there a JOIN clause for this table?
1894 if ( isset( $join_conds[$alias] ) ) {
1895 list( $joinType, $conds ) = $join_conds[$alias];
1896 $tableClause = $joinType;
1897 $tableClause .= ' ' . $this->tableNameWithAlias( $table, $alias );
1898 if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
1899 $use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
1900 if ( $use != '' ) {
1901 $tableClause .= ' ' . $use;
1904 if ( isset( $ignore_index[$alias] ) ) { // has IGNORE INDEX?
1905 $ignore = $this->ignoreIndexClause(
1906 implode( ',', (array)$ignore_index[$alias] ) );
1907 if ( $ignore != '' ) {
1908 $tableClause .= ' ' . $ignore;
1911 $on = $this->makeList( (array)$conds, self::LIST_AND );
1912 if ( $on != '' ) {
1913 $tableClause .= ' ON (' . $on . ')';
1916 $retJOIN[] = $tableClause;
1917 } elseif ( isset( $use_index[$alias] ) ) {
1918 // Is there an INDEX clause for this table?
1919 $tableClause = $this->tableNameWithAlias( $table, $alias );
1920 $tableClause .= ' ' . $this->useIndexClause(
1921 implode( ',', (array)$use_index[$alias] )
1924 $ret[] = $tableClause;
1925 } elseif ( isset( $ignore_index[$alias] ) ) {
1926 // Is there an INDEX clause for this table?
1927 $tableClause = $this->tableNameWithAlias( $table, $alias );
1928 $tableClause .= ' ' . $this->ignoreIndexClause(
1929 implode( ',', (array)$ignore_index[$alias] )
1932 $ret[] = $tableClause;
1933 } else {
1934 $tableClause = $this->tableNameWithAlias( $table, $alias );
1936 $ret[] = $tableClause;
1940 // We can't separate explicit JOIN clauses with ',', use ' ' for those
1941 $implicitJoins = !empty( $ret ) ? implode( ',', $ret ) : "";
1942 $explicitJoins = !empty( $retJOIN ) ? implode( ' ', $retJOIN ) : "";
1944 // Compile our final table clause
1945 return implode( ' ', [ $implicitJoins, $explicitJoins ] );
1949 * Get the name of an index in a given table.
1951 * @param string $index
1952 * @return string
1954 protected function indexName( $index ) {
1955 return $index;
1958 public function addQuotes( $s ) {
1959 if ( $s instanceof Blob ) {
1960 $s = $s->fetch();
1962 if ( $s === null ) {
1963 return 'NULL';
1964 } elseif ( is_bool( $s ) ) {
1965 return (int)$s;
1966 } else {
1967 # This will also quote numeric values. This should be harmless,
1968 # and protects against weird problems that occur when they really
1969 # _are_ strings such as article titles and string->number->string
1970 # conversion is not 1:1.
1971 return "'" . $this->strencode( $s ) . "'";
1976 * Quotes an identifier using `backticks` or "double quotes" depending on the database type.
1977 * MySQL uses `backticks` while basically everything else uses double quotes.
1978 * Since MySQL is the odd one out here the double quotes are our generic
1979 * and we implement backticks in DatabaseMysql.
1981 * @param string $s
1982 * @return string
1984 public function addIdentifierQuotes( $s ) {
1985 return '"' . str_replace( '"', '""', $s ) . '"';
1989 * Returns if the given identifier looks quoted or not according to
1990 * the database convention for quoting identifiers .
1992 * @note Do not use this to determine if untrusted input is safe.
1993 * A malicious user can trick this function.
1994 * @param string $name
1995 * @return bool
1997 public function isQuotedIdentifier( $name ) {
1998 return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
2002 * @param string $s
2003 * @return string
2005 protected function escapeLikeInternal( $s ) {
2006 return addcslashes( $s, '\%_' );
2009 public function buildLike() {
2010 $params = func_get_args();
2012 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
2013 $params = $params[0];
2016 $s = '';
2018 foreach ( $params as $value ) {
2019 if ( $value instanceof LikeMatch ) {
2020 $s .= $value->toString();
2021 } else {
2022 $s .= $this->escapeLikeInternal( $value );
2026 return " LIKE {$this->addQuotes( $s )} ";
2029 public function anyChar() {
2030 return new LikeMatch( '_' );
2033 public function anyString() {
2034 return new LikeMatch( '%' );
2037 public function nextSequenceValue( $seqName ) {
2038 return null;
2042 * USE INDEX clause. Unlikely to be useful for anything but MySQL. This
2043 * is only needed because a) MySQL must be as efficient as possible due to
2044 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2045 * which index to pick. Anyway, other databases might have different
2046 * indexes on a given table. So don't bother overriding this unless you're
2047 * MySQL.
2048 * @param string $index
2049 * @return string
2051 public function useIndexClause( $index ) {
2052 return '';
2056 * IGNORE INDEX clause. Unlikely to be useful for anything but MySQL. This
2057 * is only needed because a) MySQL must be as efficient as possible due to
2058 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2059 * which index to pick. Anyway, other databases might have different
2060 * indexes on a given table. So don't bother overriding this unless you're
2061 * MySQL.
2062 * @param string $index
2063 * @return string
2065 public function ignoreIndexClause( $index ) {
2066 return '';
2069 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
2070 $quotedTable = $this->tableName( $table );
2072 if ( count( $rows ) == 0 ) {
2073 return;
2076 # Single row case
2077 if ( !is_array( reset( $rows ) ) ) {
2078 $rows = [ $rows ];
2081 // @FXIME: this is not atomic, but a trx would break affectedRows()
2082 foreach ( $rows as $row ) {
2083 # Delete rows which collide
2084 if ( $uniqueIndexes ) {
2085 $sql = "DELETE FROM $quotedTable WHERE ";
2086 $first = true;
2087 foreach ( $uniqueIndexes as $index ) {
2088 if ( $first ) {
2089 $first = false;
2090 $sql .= '( ';
2091 } else {
2092 $sql .= ' ) OR ( ';
2094 if ( is_array( $index ) ) {
2095 $first2 = true;
2096 foreach ( $index as $col ) {
2097 if ( $first2 ) {
2098 $first2 = false;
2099 } else {
2100 $sql .= ' AND ';
2102 $sql .= $col . '=' . $this->addQuotes( $row[$col] );
2104 } else {
2105 $sql .= $index . '=' . $this->addQuotes( $row[$index] );
2108 $sql .= ' )';
2109 $this->query( $sql, $fname );
2112 # Now insert the row
2113 $this->insert( $table, $row, $fname );
2118 * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
2119 * statement.
2121 * @param string $table Table name
2122 * @param array|string $rows Row(s) to insert
2123 * @param string $fname Caller function name
2125 * @return ResultWrapper
2127 protected function nativeReplace( $table, $rows, $fname ) {
2128 $table = $this->tableName( $table );
2130 # Single row case
2131 if ( !is_array( reset( $rows ) ) ) {
2132 $rows = [ $rows ];
2135 $sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) . ') VALUES ';
2136 $first = true;
2138 foreach ( $rows as $row ) {
2139 if ( $first ) {
2140 $first = false;
2141 } else {
2142 $sql .= ',';
2145 $sql .= '(' . $this->makeList( $row ) . ')';
2148 return $this->query( $sql, $fname );
2151 public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
2152 $fname = __METHOD__
2154 if ( !count( $rows ) ) {
2155 return true; // nothing to do
2158 if ( !is_array( reset( $rows ) ) ) {
2159 $rows = [ $rows ];
2162 if ( count( $uniqueIndexes ) ) {
2163 $clauses = []; // list WHERE clauses that each identify a single row
2164 foreach ( $rows as $row ) {
2165 foreach ( $uniqueIndexes as $index ) {
2166 $index = is_array( $index ) ? $index : [ $index ]; // columns
2167 $rowKey = []; // unique key to this row
2168 foreach ( $index as $column ) {
2169 $rowKey[$column] = $row[$column];
2171 $clauses[] = $this->makeList( $rowKey, self::LIST_AND );
2174 $where = [ $this->makeList( $clauses, self::LIST_OR ) ];
2175 } else {
2176 $where = false;
2179 $useTrx = !$this->mTrxLevel;
2180 if ( $useTrx ) {
2181 $this->begin( $fname, self::TRANSACTION_INTERNAL );
2183 try {
2184 # Update any existing conflicting row(s)
2185 if ( $where !== false ) {
2186 $ok = $this->update( $table, $set, $where, $fname );
2187 } else {
2188 $ok = true;
2190 # Now insert any non-conflicting row(s)
2191 $ok = $this->insert( $table, $rows, $fname, [ 'IGNORE' ] ) && $ok;
2192 } catch ( Exception $e ) {
2193 if ( $useTrx ) {
2194 $this->rollback( $fname, self::FLUSHING_INTERNAL );
2196 throw $e;
2198 if ( $useTrx ) {
2199 $this->commit( $fname, self::FLUSHING_INTERNAL );
2202 return $ok;
2205 public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
2206 $fname = __METHOD__
2208 if ( !$conds ) {
2209 throw new DBUnexpectedError( $this, __METHOD__ . ' called with empty $conds' );
2212 $delTable = $this->tableName( $delTable );
2213 $joinTable = $this->tableName( $joinTable );
2214 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
2215 if ( $conds != '*' ) {
2216 $sql .= 'WHERE ' . $this->makeList( $conds, self::LIST_AND );
2218 $sql .= ')';
2220 $this->query( $sql, $fname );
2223 public function textFieldSize( $table, $field ) {
2224 $table = $this->tableName( $table );
2225 $sql = "SHOW COLUMNS FROM $table LIKE \"$field\";";
2226 $res = $this->query( $sql, __METHOD__ );
2227 $row = $this->fetchObject( $res );
2229 $m = [];
2231 if ( preg_match( '/\((.*)\)/', $row->Type, $m ) ) {
2232 $size = $m[1];
2233 } else {
2234 $size = -1;
2237 return $size;
2240 public function delete( $table, $conds, $fname = __METHOD__ ) {
2241 if ( !$conds ) {
2242 throw new DBUnexpectedError( $this, __METHOD__ . ' called with no conditions' );
2245 $table = $this->tableName( $table );
2246 $sql = "DELETE FROM $table";
2248 if ( $conds != '*' ) {
2249 if ( is_array( $conds ) ) {
2250 $conds = $this->makeList( $conds, self::LIST_AND );
2252 $sql .= ' WHERE ' . $conds;
2255 return $this->query( $sql, $fname );
2258 public function insertSelect(
2259 $destTable, $srcTable, $varMap, $conds,
2260 $fname = __METHOD__, $insertOptions = [], $selectOptions = []
2262 if ( $this->cliMode ) {
2263 // For massive migrations with downtime, we don't want to select everything
2264 // into memory and OOM, so do all this native on the server side if possible.
2265 return $this->nativeInsertSelect(
2266 $destTable,
2267 $srcTable,
2268 $varMap,
2269 $conds,
2270 $fname,
2271 $insertOptions,
2272 $selectOptions
2276 // For web requests, do a locking SELECT and then INSERT. This puts the SELECT burden
2277 // on only the master (without needing row-based-replication). It also makes it easy to
2278 // know how big the INSERT is going to be.
2279 $fields = [];
2280 foreach ( $varMap as $dstColumn => $sourceColumnOrSql ) {
2281 $fields[] = $this->fieldNameWithAlias( $sourceColumnOrSql, $dstColumn );
2283 $selectOptions[] = 'FOR UPDATE';
2284 $res = $this->select( $srcTable, implode( ',', $fields ), $conds, $fname, $selectOptions );
2285 if ( !$res ) {
2286 return false;
2289 $rows = [];
2290 foreach ( $res as $row ) {
2291 $rows[] = (array)$row;
2294 return $this->insert( $destTable, $rows, $fname, $insertOptions );
2297 protected function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds,
2298 $fname = __METHOD__,
2299 $insertOptions = [], $selectOptions = []
2301 $destTable = $this->tableName( $destTable );
2303 if ( !is_array( $insertOptions ) ) {
2304 $insertOptions = [ $insertOptions ];
2307 $insertOptions = $this->makeInsertOptions( $insertOptions );
2309 if ( !is_array( $selectOptions ) ) {
2310 $selectOptions = [ $selectOptions ];
2313 list( $startOpts, $useIndex, $tailOpts, $ignoreIndex ) = $this->makeSelectOptions(
2314 $selectOptions );
2316 if ( is_array( $srcTable ) ) {
2317 $srcTable = implode( ',', array_map( [ &$this, 'tableName' ], $srcTable ) );
2318 } else {
2319 $srcTable = $this->tableName( $srcTable );
2322 $sql = "INSERT $insertOptions" .
2323 " INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
2324 " SELECT $startOpts " . implode( ',', $varMap ) .
2325 " FROM $srcTable $useIndex $ignoreIndex ";
2327 if ( $conds != '*' ) {
2328 if ( is_array( $conds ) ) {
2329 $conds = $this->makeList( $conds, self::LIST_AND );
2331 $sql .= " WHERE $conds";
2334 $sql .= " $tailOpts";
2336 return $this->query( $sql, $fname );
2340 * Construct a LIMIT query with optional offset. This is used for query
2341 * pages. The SQL should be adjusted so that only the first $limit rows
2342 * are returned. If $offset is provided as well, then the first $offset
2343 * rows should be discarded, and the next $limit rows should be returned.
2344 * If the result of the query is not ordered, then the rows to be returned
2345 * are theoretically arbitrary.
2347 * $sql is expected to be a SELECT, if that makes a difference.
2349 * The version provided by default works in MySQL and SQLite. It will very
2350 * likely need to be overridden for most other DBMSes.
2352 * @param string $sql SQL query we will append the limit too
2353 * @param int $limit The SQL limit
2354 * @param int|bool $offset The SQL offset (default false)
2355 * @throws DBUnexpectedError
2356 * @return string
2358 public function limitResult( $sql, $limit, $offset = false ) {
2359 if ( !is_numeric( $limit ) ) {
2360 throw new DBUnexpectedError( $this,
2361 "Invalid non-numeric limit passed to limitResult()\n" );
2364 return "$sql LIMIT "
2365 . ( ( is_numeric( $offset ) && $offset != 0 ) ? "{$offset}," : "" )
2366 . "{$limit} ";
2369 public function unionSupportsOrderAndLimit() {
2370 return true; // True for almost every DB supported
2373 public function unionQueries( $sqls, $all ) {
2374 $glue = $all ? ') UNION ALL (' : ') UNION (';
2376 return '(' . implode( $glue, $sqls ) . ')';
2379 public function conditional( $cond, $trueVal, $falseVal ) {
2380 if ( is_array( $cond ) ) {
2381 $cond = $this->makeList( $cond, self::LIST_AND );
2384 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
2387 public function strreplace( $orig, $old, $new ) {
2388 return "REPLACE({$orig}, {$old}, {$new})";
2391 public function getServerUptime() {
2392 return 0;
2395 public function wasDeadlock() {
2396 return false;
2399 public function wasLockTimeout() {
2400 return false;
2403 public function wasErrorReissuable() {
2404 return false;
2407 public function wasReadOnlyError() {
2408 return false;
2412 * Do not use this method outside of Database/DBError classes
2414 * @param integer|string $errno
2415 * @return bool Whether the given query error was a connection drop
2417 public function wasConnectionError( $errno ) {
2418 return false;
2421 public function deadlockLoop() {
2422 $args = func_get_args();
2423 $function = array_shift( $args );
2424 $tries = self::DEADLOCK_TRIES;
2426 $this->begin( __METHOD__ );
2428 $retVal = null;
2429 /** @var Exception $e */
2430 $e = null;
2431 do {
2432 try {
2433 $retVal = call_user_func_array( $function, $args );
2434 break;
2435 } catch ( DBQueryError $e ) {
2436 if ( $this->wasDeadlock() ) {
2437 // Retry after a randomized delay
2438 usleep( mt_rand( self::DEADLOCK_DELAY_MIN, self::DEADLOCK_DELAY_MAX ) );
2439 } else {
2440 // Throw the error back up
2441 throw $e;
2444 } while ( --$tries > 0 );
2446 if ( $tries <= 0 ) {
2447 // Too many deadlocks; give up
2448 $this->rollback( __METHOD__ );
2449 throw $e;
2450 } else {
2451 $this->commit( __METHOD__ );
2453 return $retVal;
2457 public function masterPosWait( DBMasterPos $pos, $timeout ) {
2458 # Real waits are implemented in the subclass.
2459 return 0;
2462 public function getReplicaPos() {
2463 # Stub
2464 return false;
2467 public function getMasterPos() {
2468 # Stub
2469 return false;
2472 public function serverIsReadOnly() {
2473 return false;
2476 final public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
2477 if ( !$this->mTrxLevel ) {
2478 throw new DBUnexpectedError( $this, "No transaction is active." );
2480 $this->mTrxEndCallbacks[] = [ $callback, $fname ];
2483 final public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
2484 $this->mTrxIdleCallbacks[] = [ $callback, $fname ];
2485 if ( !$this->mTrxLevel ) {
2486 $this->runOnTransactionIdleCallbacks( self::TRIGGER_IDLE );
2490 final public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
2491 if ( $this->mTrxLevel ) {
2492 $this->mTrxPreCommitCallbacks[] = [ $callback, $fname ];
2493 } else {
2494 // If no transaction is active, then make one for this callback
2495 $this->startAtomic( __METHOD__ );
2496 try {
2497 call_user_func( $callback );
2498 $this->endAtomic( __METHOD__ );
2499 } catch ( Exception $e ) {
2500 $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
2501 throw $e;
2506 final public function setTransactionListener( $name, callable $callback = null ) {
2507 if ( $callback ) {
2508 $this->mTrxRecurringCallbacks[$name] = $callback;
2509 } else {
2510 unset( $this->mTrxRecurringCallbacks[$name] );
2515 * Whether to disable running of post-COMMIT/ROLLBACK callbacks
2517 * This method should not be used outside of Database/LoadBalancer
2519 * @param bool $suppress
2520 * @since 1.28
2522 final public function setTrxEndCallbackSuppression( $suppress ) {
2523 $this->mTrxEndCallbacksSuppressed = $suppress;
2527 * Actually run and consume any "on transaction idle/resolution" callbacks.
2529 * This method should not be used outside of Database/LoadBalancer
2531 * @param integer $trigger IDatabase::TRIGGER_* constant
2532 * @since 1.20
2533 * @throws Exception
2535 public function runOnTransactionIdleCallbacks( $trigger ) {
2536 if ( $this->mTrxEndCallbacksSuppressed ) {
2537 return;
2540 $autoTrx = $this->getFlag( self::DBO_TRX ); // automatic begin() enabled?
2541 /** @var Exception $e */
2542 $e = null; // first exception
2543 do { // callbacks may add callbacks :)
2544 $callbacks = array_merge(
2545 $this->mTrxIdleCallbacks,
2546 $this->mTrxEndCallbacks // include "transaction resolution" callbacks
2548 $this->mTrxIdleCallbacks = []; // consumed (and recursion guard)
2549 $this->mTrxEndCallbacks = []; // consumed (recursion guard)
2550 foreach ( $callbacks as $callback ) {
2551 try {
2552 list( $phpCallback ) = $callback;
2553 $this->clearFlag( self::DBO_TRX ); // make each query its own transaction
2554 call_user_func_array( $phpCallback, [ $trigger ] );
2555 if ( $autoTrx ) {
2556 $this->setFlag( self::DBO_TRX ); // restore automatic begin()
2557 } else {
2558 $this->clearFlag( self::DBO_TRX ); // restore auto-commit
2560 } catch ( Exception $ex ) {
2561 call_user_func( $this->errorLogger, $ex );
2562 $e = $e ?: $ex;
2563 // Some callbacks may use startAtomic/endAtomic, so make sure
2564 // their transactions are ended so other callbacks don't fail
2565 if ( $this->trxLevel() ) {
2566 $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
2570 } while ( count( $this->mTrxIdleCallbacks ) );
2572 if ( $e instanceof Exception ) {
2573 throw $e; // re-throw any first exception
2578 * Actually run and consume any "on transaction pre-commit" callbacks.
2580 * This method should not be used outside of Database/LoadBalancer
2582 * @since 1.22
2583 * @throws Exception
2585 public function runOnTransactionPreCommitCallbacks() {
2586 $e = null; // first exception
2587 do { // callbacks may add callbacks :)
2588 $callbacks = $this->mTrxPreCommitCallbacks;
2589 $this->mTrxPreCommitCallbacks = []; // consumed (and recursion guard)
2590 foreach ( $callbacks as $callback ) {
2591 try {
2592 list( $phpCallback ) = $callback;
2593 call_user_func( $phpCallback );
2594 } catch ( Exception $ex ) {
2595 call_user_func( $this->errorLogger, $ex );
2596 $e = $e ?: $ex;
2599 } while ( count( $this->mTrxPreCommitCallbacks ) );
2601 if ( $e instanceof Exception ) {
2602 throw $e; // re-throw any first exception
2607 * Actually run any "transaction listener" callbacks.
2609 * This method should not be used outside of Database/LoadBalancer
2611 * @param integer $trigger IDatabase::TRIGGER_* constant
2612 * @throws Exception
2613 * @since 1.20
2615 public function runTransactionListenerCallbacks( $trigger ) {
2616 if ( $this->mTrxEndCallbacksSuppressed ) {
2617 return;
2620 /** @var Exception $e */
2621 $e = null; // first exception
2623 foreach ( $this->mTrxRecurringCallbacks as $phpCallback ) {
2624 try {
2625 $phpCallback( $trigger, $this );
2626 } catch ( Exception $ex ) {
2627 call_user_func( $this->errorLogger, $ex );
2628 $e = $e ?: $ex;
2632 if ( $e instanceof Exception ) {
2633 throw $e; // re-throw any first exception
2637 final public function startAtomic( $fname = __METHOD__ ) {
2638 if ( !$this->mTrxLevel ) {
2639 $this->begin( $fname, self::TRANSACTION_INTERNAL );
2640 // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
2641 // in all changes being in one transaction to keep requests transactional.
2642 if ( !$this->getFlag( self::DBO_TRX ) ) {
2643 $this->mTrxAutomaticAtomic = true;
2647 $this->mTrxAtomicLevels[] = $fname;
2650 final public function endAtomic( $fname = __METHOD__ ) {
2651 if ( !$this->mTrxLevel ) {
2652 throw new DBUnexpectedError( $this, "No atomic transaction is open (got $fname)." );
2654 if ( !$this->mTrxAtomicLevels ||
2655 array_pop( $this->mTrxAtomicLevels ) !== $fname
2657 throw new DBUnexpectedError( $this, "Invalid atomic section ended (got $fname)." );
2660 if ( !$this->mTrxAtomicLevels && $this->mTrxAutomaticAtomic ) {
2661 $this->commit( $fname, self::FLUSHING_INTERNAL );
2665 final public function doAtomicSection( $fname, callable $callback ) {
2666 $this->startAtomic( $fname );
2667 try {
2668 $res = call_user_func_array( $callback, [ $this, $fname ] );
2669 } catch ( Exception $e ) {
2670 $this->rollback( $fname, self::FLUSHING_INTERNAL );
2671 throw $e;
2673 $this->endAtomic( $fname );
2675 return $res;
2678 final public function begin( $fname = __METHOD__, $mode = self::TRANSACTION_EXPLICIT ) {
2679 // Protect against mismatched atomic section, transaction nesting, and snapshot loss
2680 if ( $this->mTrxLevel ) {
2681 if ( $this->mTrxAtomicLevels ) {
2682 $levels = implode( ', ', $this->mTrxAtomicLevels );
2683 $msg = "$fname: Got explicit BEGIN while atomic section(s) $levels are open.";
2684 throw new DBUnexpectedError( $this, $msg );
2685 } elseif ( !$this->mTrxAutomatic ) {
2686 $msg = "$fname: Explicit transaction already active (from {$this->mTrxFname}).";
2687 throw new DBUnexpectedError( $this, $msg );
2688 } else {
2689 // @TODO: make this an exception at some point
2690 $msg = "$fname: Implicit transaction already active (from {$this->mTrxFname}).";
2691 $this->queryLogger->error( $msg );
2692 return; // join the main transaction set
2694 } elseif ( $this->getFlag( self::DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) {
2695 // @TODO: make this an exception at some point
2696 $msg = "$fname: Implicit transaction expected (DBO_TRX set).";
2697 $this->queryLogger->error( $msg );
2698 return; // let any writes be in the main transaction
2701 // Avoid fatals if close() was called
2702 $this->assertOpen();
2704 $this->doBegin( $fname );
2705 $this->mTrxTimestamp = microtime( true );
2706 $this->mTrxFname = $fname;
2707 $this->mTrxDoneWrites = false;
2708 $this->mTrxAutomaticAtomic = false;
2709 $this->mTrxAtomicLevels = [];
2710 $this->mTrxShortId = sprintf( '%06x', mt_rand( 0, 0xffffff ) );
2711 $this->mTrxWriteDuration = 0.0;
2712 $this->mTrxWriteQueryCount = 0;
2713 $this->mTrxWriteAdjDuration = 0.0;
2714 $this->mTrxWriteAdjQueryCount = 0;
2715 $this->mTrxWriteCallers = [];
2716 // First SELECT after BEGIN will establish the snapshot in REPEATABLE-READ.
2717 // Get an estimate of the replica DB lag before then, treating estimate staleness
2718 // as lag itself just to be safe
2719 $status = $this->getApproximateLagStatus();
2720 $this->mTrxReplicaLag = $status['lag'] + ( microtime( true ) - $status['since'] );
2721 // T147697: make explicitTrxActive() return true until begin() finishes. This way, no
2722 // caller will think its OK to muck around with the transaction just because startAtomic()
2723 // has not yet completed (e.g. setting mTrxAtomicLevels).
2724 $this->mTrxAutomatic = ( $mode === self::TRANSACTION_INTERNAL );
2728 * Issues the BEGIN command to the database server.
2730 * @see Database::begin()
2731 * @param string $fname
2733 protected function doBegin( $fname ) {
2734 $this->query( 'BEGIN', $fname );
2735 $this->mTrxLevel = 1;
2738 final public function commit( $fname = __METHOD__, $flush = '' ) {
2739 if ( $this->mTrxLevel && $this->mTrxAtomicLevels ) {
2740 // There are still atomic sections open. This cannot be ignored
2741 $levels = implode( ', ', $this->mTrxAtomicLevels );
2742 throw new DBUnexpectedError(
2743 $this,
2744 "$fname: Got COMMIT while atomic sections $levels are still open."
2748 if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
2749 if ( !$this->mTrxLevel ) {
2750 return; // nothing to do
2751 } elseif ( !$this->mTrxAutomatic ) {
2752 throw new DBUnexpectedError(
2753 $this,
2754 "$fname: Flushing an explicit transaction, getting out of sync."
2757 } else {
2758 if ( !$this->mTrxLevel ) {
2759 $this->queryLogger->error(
2760 "$fname: No transaction to commit, something got out of sync." );
2761 return; // nothing to do
2762 } elseif ( $this->mTrxAutomatic ) {
2763 // @TODO: make this an exception at some point
2764 $msg = "$fname: Explicit commit of implicit transaction.";
2765 $this->queryLogger->error( $msg );
2766 return; // wait for the main transaction set commit round
2770 // Avoid fatals if close() was called
2771 $this->assertOpen();
2773 $this->runOnTransactionPreCommitCallbacks();
2774 $writeTime = $this->pendingWriteQueryDuration( self::ESTIMATE_DB_APPLY );
2775 $this->doCommit( $fname );
2776 if ( $this->mTrxDoneWrites ) {
2777 $this->mLastWriteTime = microtime( true );
2778 $this->trxProfiler->transactionWritingOut(
2779 $this->mServer, $this->mDBname, $this->mTrxShortId, $writeTime );
2782 $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
2783 $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
2787 * Issues the COMMIT command to the database server.
2789 * @see Database::commit()
2790 * @param string $fname
2792 protected function doCommit( $fname ) {
2793 if ( $this->mTrxLevel ) {
2794 $this->query( 'COMMIT', $fname );
2795 $this->mTrxLevel = 0;
2799 final public function rollback( $fname = __METHOD__, $flush = '' ) {
2800 if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
2801 if ( !$this->mTrxLevel ) {
2802 return; // nothing to do
2804 } else {
2805 if ( !$this->mTrxLevel ) {
2806 $this->queryLogger->error(
2807 "$fname: No transaction to rollback, something got out of sync." );
2808 return; // nothing to do
2809 } elseif ( $this->getFlag( self::DBO_TRX ) ) {
2810 throw new DBUnexpectedError(
2811 $this,
2812 "$fname: Expected mass rollback of all peer databases (DBO_TRX set)."
2817 // Avoid fatals if close() was called
2818 $this->assertOpen();
2820 $this->doRollback( $fname );
2821 $this->mTrxAtomicLevels = [];
2822 if ( $this->mTrxDoneWrites ) {
2823 $this->trxProfiler->transactionWritingOut(
2824 $this->mServer, $this->mDBname, $this->mTrxShortId );
2827 $this->mTrxIdleCallbacks = []; // clear
2828 $this->mTrxPreCommitCallbacks = []; // clear
2829 $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
2830 $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
2834 * Issues the ROLLBACK command to the database server.
2836 * @see Database::rollback()
2837 * @param string $fname
2839 protected function doRollback( $fname ) {
2840 if ( $this->mTrxLevel ) {
2841 # Disconnects cause rollback anyway, so ignore those errors
2842 $ignoreErrors = true;
2843 $this->query( 'ROLLBACK', $fname, $ignoreErrors );
2844 $this->mTrxLevel = 0;
2848 public function flushSnapshot( $fname = __METHOD__ ) {
2849 if ( $this->writesOrCallbacksPending() || $this->explicitTrxActive() ) {
2850 // This only flushes transactions to clear snapshots, not to write data
2851 $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
2852 throw new DBUnexpectedError(
2853 $this,
2854 "$fname: Cannot flush snapshot because writes are pending ($fnames)."
2858 $this->commit( $fname, self::FLUSHING_INTERNAL );
2861 public function explicitTrxActive() {
2862 return $this->mTrxLevel && ( $this->mTrxAtomicLevels || !$this->mTrxAutomatic );
2866 * Creates a new table with structure copied from existing table
2867 * Note that unlike most database abstraction functions, this function does not
2868 * automatically append database prefix, because it works at a lower
2869 * abstraction level.
2870 * The table names passed to this function shall not be quoted (this
2871 * function calls addIdentifierQuotes when needed).
2873 * @param string $oldName Name of table whose structure should be copied
2874 * @param string $newName Name of table to be created
2875 * @param bool $temporary Whether the new table should be temporary
2876 * @param string $fname Calling function name
2877 * @throws RuntimeException
2878 * @return bool True if operation was successful
2880 public function duplicateTableStructure( $oldName, $newName, $temporary = false,
2881 $fname = __METHOD__
2883 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2886 public function listTables( $prefix = null, $fname = __METHOD__ ) {
2887 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2890 public function listViews( $prefix = null, $fname = __METHOD__ ) {
2891 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2894 public function timestamp( $ts = 0 ) {
2895 $t = new ConvertibleTimestamp( $ts );
2896 // Let errors bubble up to avoid putting garbage in the DB
2897 return $t->getTimestamp( TS_MW );
2900 public function timestampOrNull( $ts = null ) {
2901 if ( is_null( $ts ) ) {
2902 return null;
2903 } else {
2904 return $this->timestamp( $ts );
2909 * Take the result from a query, and wrap it in a ResultWrapper if
2910 * necessary. Boolean values are passed through as is, to indicate success
2911 * of write queries or failure.
2913 * Once upon a time, Database::query() returned a bare MySQL result
2914 * resource, and it was necessary to call this function to convert it to
2915 * a wrapper. Nowadays, raw database objects are never exposed to external
2916 * callers, so this is unnecessary in external code.
2918 * @param bool|ResultWrapper|resource|object $result
2919 * @return bool|ResultWrapper
2921 protected function resultObject( $result ) {
2922 if ( !$result ) {
2923 return false;
2924 } elseif ( $result instanceof ResultWrapper ) {
2925 return $result;
2926 } elseif ( $result === true ) {
2927 // Successful write query
2928 return $result;
2929 } else {
2930 return new ResultWrapper( $this, $result );
2934 public function ping( &$rtt = null ) {
2935 // Avoid hitting the server if it was hit recently
2936 if ( $this->isOpen() && ( microtime( true ) - $this->lastPing ) < self::PING_TTL ) {
2937 if ( !func_num_args() || $this->mRTTEstimate > 0 ) {
2938 $rtt = $this->mRTTEstimate;
2939 return true; // don't care about $rtt
2943 // This will reconnect if possible or return false if not
2944 $this->clearFlag( self::DBO_TRX, self::REMEMBER_PRIOR );
2945 $ok = ( $this->query( self::PING_QUERY, __METHOD__, true ) !== false );
2946 $this->restoreFlags( self::RESTORE_PRIOR );
2948 if ( $ok ) {
2949 $rtt = $this->mRTTEstimate;
2952 return $ok;
2956 * @return bool
2958 protected function reconnect() {
2959 $this->closeConnection();
2960 $this->mOpened = false;
2961 $this->mConn = false;
2962 try {
2963 $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
2964 $this->lastPing = microtime( true );
2965 $ok = true;
2966 } catch ( DBConnectionError $e ) {
2967 $ok = false;
2970 return $ok;
2973 public function getSessionLagStatus() {
2974 return $this->getTransactionLagStatus() ?: $this->getApproximateLagStatus();
2978 * Get the replica DB lag when the current transaction started
2980 * This is useful when transactions might use snapshot isolation
2981 * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
2982 * is this lag plus transaction duration. If they don't, it is still
2983 * safe to be pessimistic. This returns null if there is no transaction.
2985 * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
2986 * @since 1.27
2988 protected function getTransactionLagStatus() {
2989 return $this->mTrxLevel
2990 ? [ 'lag' => $this->mTrxReplicaLag, 'since' => $this->trxTimestamp() ]
2991 : null;
2995 * Get a replica DB lag estimate for this server
2997 * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate)
2998 * @since 1.27
3000 protected function getApproximateLagStatus() {
3001 return [
3002 'lag' => $this->getLBInfo( 'replica' ) ? $this->getLag() : 0,
3003 'since' => microtime( true )
3008 * Merge the result of getSessionLagStatus() for several DBs
3009 * using the most pessimistic values to estimate the lag of
3010 * any data derived from them in combination
3012 * This is information is useful for caching modules
3014 * @see WANObjectCache::set()
3015 * @see WANObjectCache::getWithSetCallback()
3017 * @param IDatabase $db1
3018 * @param IDatabase ...
3019 * @return array Map of values:
3020 * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped)
3021 * - since: oldest UNIX timestamp of any of the DB lag estimates
3022 * - pending: whether any of the DBs have uncommitted changes
3023 * @since 1.27
3025 public static function getCacheSetOptions( IDatabase $db1 ) {
3026 $res = [ 'lag' => 0, 'since' => INF, 'pending' => false ];
3027 foreach ( func_get_args() as $db ) {
3028 /** @var IDatabase $db */
3029 $status = $db->getSessionLagStatus();
3030 if ( $status['lag'] === false ) {
3031 $res['lag'] = false;
3032 } elseif ( $res['lag'] !== false ) {
3033 $res['lag'] = max( $res['lag'], $status['lag'] );
3035 $res['since'] = min( $res['since'], $status['since'] );
3036 $res['pending'] = $res['pending'] ?: $db->writesPending();
3039 return $res;
3042 public function getLag() {
3043 return 0;
3046 public function maxListLen() {
3047 return 0;
3050 public function encodeBlob( $b ) {
3051 return $b;
3054 public function decodeBlob( $b ) {
3055 if ( $b instanceof Blob ) {
3056 $b = $b->fetch();
3058 return $b;
3061 public function setSessionOptions( array $options ) {
3064 public function sourceFile(
3065 $filename,
3066 callable $lineCallback = null,
3067 callable $resultCallback = null,
3068 $fname = false,
3069 callable $inputCallback = null
3071 MediaWiki\suppressWarnings();
3072 $fp = fopen( $filename, 'r' );
3073 MediaWiki\restoreWarnings();
3075 if ( false === $fp ) {
3076 throw new RuntimeException( "Could not open \"{$filename}\".\n" );
3079 if ( !$fname ) {
3080 $fname = __METHOD__ . "( $filename )";
3083 try {
3084 $error = $this->sourceStream(
3085 $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
3086 } catch ( Exception $e ) {
3087 fclose( $fp );
3088 throw $e;
3091 fclose( $fp );
3093 return $error;
3096 public function setSchemaVars( $vars ) {
3097 $this->mSchemaVars = $vars;
3100 public function sourceStream(
3101 $fp,
3102 callable $lineCallback = null,
3103 callable $resultCallback = null,
3104 $fname = __METHOD__,
3105 callable $inputCallback = null
3107 $cmd = '';
3109 while ( !feof( $fp ) ) {
3110 if ( $lineCallback ) {
3111 call_user_func( $lineCallback );
3114 $line = trim( fgets( $fp ) );
3116 if ( $line == '' ) {
3117 continue;
3120 if ( '-' == $line[0] && '-' == $line[1] ) {
3121 continue;
3124 if ( $cmd != '' ) {
3125 $cmd .= ' ';
3128 $done = $this->streamStatementEnd( $cmd, $line );
3130 $cmd .= "$line\n";
3132 if ( $done || feof( $fp ) ) {
3133 $cmd = $this->replaceVars( $cmd );
3135 if ( !$inputCallback || call_user_func( $inputCallback, $cmd ) ) {
3136 $res = $this->query( $cmd, $fname );
3138 if ( $resultCallback ) {
3139 call_user_func( $resultCallback, $res, $this );
3142 if ( false === $res ) {
3143 $err = $this->lastError();
3145 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
3148 $cmd = '';
3152 return true;
3156 * Called by sourceStream() to check if we've reached a statement end
3158 * @param string &$sql SQL assembled so far
3159 * @param string &$newLine New line about to be added to $sql
3160 * @return bool Whether $newLine contains end of the statement
3162 public function streamStatementEnd( &$sql, &$newLine ) {
3163 if ( $this->delimiter ) {
3164 $prev = $newLine;
3165 $newLine = preg_replace(
3166 '/' . preg_quote( $this->delimiter, '/' ) . '$/', '', $newLine );
3167 if ( $newLine != $prev ) {
3168 return true;
3172 return false;
3176 * Database independent variable replacement. Replaces a set of variables
3177 * in an SQL statement with their contents as given by $this->getSchemaVars().
3179 * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables.
3181 * - '{$var}' should be used for text and is passed through the database's
3182 * addQuotes method.
3183 * - `{$var}` should be used for identifiers (e.g. table and database names).
3184 * It is passed through the database's addIdentifierQuotes method which
3185 * can be overridden if the database uses something other than backticks.
3186 * - / *_* / or / *$wgDBprefix* / passes the name that follows through the
3187 * database's tableName method.
3188 * - / *i* / passes the name that follows through the database's indexName method.
3189 * - In all other cases, / *$var* / is left unencoded. Except for table options,
3190 * its use should be avoided. In 1.24 and older, string encoding was applied.
3192 * @param string $ins SQL statement to replace variables in
3193 * @return string The new SQL statement with variables replaced
3195 protected function replaceVars( $ins ) {
3196 $vars = $this->getSchemaVars();
3197 return preg_replace_callback(
3199 /\* (\$wgDBprefix|[_i]) \*/ (\w*) | # 1-2. tableName, indexName
3200 \'\{\$ (\w+) }\' | # 3. addQuotes
3201 `\{\$ (\w+) }` | # 4. addIdentifierQuotes
3202 /\*\$ (\w+) \*/ # 5. leave unencoded
3203 !x',
3204 function ( $m ) use ( $vars ) {
3205 // Note: Because of <https://bugs.php.net/bug.php?id=51881>,
3206 // check for both nonexistent keys *and* the empty string.
3207 if ( isset( $m[1] ) && $m[1] !== '' ) {
3208 if ( $m[1] === 'i' ) {
3209 return $this->indexName( $m[2] );
3210 } else {
3211 return $this->tableName( $m[2] );
3213 } elseif ( isset( $m[3] ) && $m[3] !== '' && array_key_exists( $m[3], $vars ) ) {
3214 return $this->addQuotes( $vars[$m[3]] );
3215 } elseif ( isset( $m[4] ) && $m[4] !== '' && array_key_exists( $m[4], $vars ) ) {
3216 return $this->addIdentifierQuotes( $vars[$m[4]] );
3217 } elseif ( isset( $m[5] ) && $m[5] !== '' && array_key_exists( $m[5], $vars ) ) {
3218 return $vars[$m[5]];
3219 } else {
3220 return $m[0];
3223 $ins
3228 * Get schema variables. If none have been set via setSchemaVars(), then
3229 * use some defaults from the current object.
3231 * @return array
3233 protected function getSchemaVars() {
3234 if ( $this->mSchemaVars ) {
3235 return $this->mSchemaVars;
3236 } else {
3237 return $this->getDefaultSchemaVars();
3242 * Get schema variables to use if none have been set via setSchemaVars().
3244 * Override this in derived classes to provide variables for tables.sql
3245 * and SQL patch files.
3247 * @return array
3249 protected function getDefaultSchemaVars() {
3250 return [];
3253 public function lockIsFree( $lockName, $method ) {
3254 return true;
3257 public function lock( $lockName, $method, $timeout = 5 ) {
3258 $this->mNamedLocksHeld[$lockName] = 1;
3260 return true;
3263 public function unlock( $lockName, $method ) {
3264 unset( $this->mNamedLocksHeld[$lockName] );
3266 return true;
3269 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
3270 if ( $this->writesOrCallbacksPending() ) {
3271 // This only flushes transactions to clear snapshots, not to write data
3272 $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
3273 throw new DBUnexpectedError(
3274 $this,
3275 "$fname: Cannot flush pre-lock snapshot because writes are pending ($fnames)."
3279 if ( !$this->lock( $lockKey, $fname, $timeout ) ) {
3280 return null;
3283 $unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) {
3284 if ( $this->trxLevel() ) {
3285 // There is a good chance an exception was thrown, causing any early return
3286 // from the caller. Let any error handler get a chance to issue rollback().
3287 // If there isn't one, let the error bubble up and trigger server-side rollback.
3288 $this->onTransactionResolution(
3289 function () use ( $lockKey, $fname ) {
3290 $this->unlock( $lockKey, $fname );
3292 $fname
3294 } else {
3295 $this->unlock( $lockKey, $fname );
3297 } );
3299 $this->commit( $fname, self::FLUSHING_INTERNAL );
3301 return $unlocker;
3304 public function namedLocksEnqueue() {
3305 return false;
3309 * Lock specific tables
3311 * @param array $read Array of tables to lock for read access
3312 * @param array $write Array of tables to lock for write access
3313 * @param string $method Name of caller
3314 * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY
3315 * @return bool
3317 public function lockTables( $read, $write, $method, $lowPriority = true ) {
3318 return true;
3322 * Unlock specific tables
3324 * @param string $method The caller
3325 * @return bool
3327 public function unlockTables( $method ) {
3328 return true;
3332 * Delete a table
3333 * @param string $tableName
3334 * @param string $fName
3335 * @return bool|ResultWrapper
3336 * @since 1.18
3338 public function dropTable( $tableName, $fName = __METHOD__ ) {
3339 if ( !$this->tableExists( $tableName, $fName ) ) {
3340 return false;
3342 $sql = "DROP TABLE " . $this->tableName( $tableName ) . " CASCADE";
3344 return $this->query( $sql, $fName );
3347 public function getInfinity() {
3348 return 'infinity';
3351 public function encodeExpiry( $expiry ) {
3352 return ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() )
3353 ? $this->getInfinity()
3354 : $this->timestamp( $expiry );
3357 public function decodeExpiry( $expiry, $format = TS_MW ) {
3358 if ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() ) {
3359 return 'infinity';
3362 return ConvertibleTimestamp::convert( $format, $expiry );
3365 public function setBigSelects( $value = true ) {
3366 // no-op
3369 public function isReadOnly() {
3370 return ( $this->getReadOnlyReason() !== false );
3374 * @return string|bool Reason this DB is read-only or false if it is not
3376 protected function getReadOnlyReason() {
3377 $reason = $this->getLBInfo( 'readOnlyReason' );
3379 return is_string( $reason ) ? $reason : false;
3382 public function setTableAliases( array $aliases ) {
3383 $this->tableAliases = $aliases;
3387 * @return bool Whether a DB user is required to access the DB
3388 * @since 1.28
3390 protected function requiresDatabaseUser() {
3391 return true;
3395 * Get the underlying binding handle, mConn
3397 * Makes sure that mConn is set (disconnects and ping() failure can unset it).
3398 * This catches broken callers than catch and ignore disconnection exceptions.
3399 * Unlike checking isOpen(), this is safe to call inside of open().
3401 * @return resource|object
3402 * @throws DBUnexpectedError
3403 * @since 1.26
3405 protected function getBindingHandle() {
3406 if ( !$this->mConn ) {
3407 throw new DBUnexpectedError(
3408 $this,
3409 'DB connection was already closed or the connection dropped.'
3413 return $this->mConn;
3417 * @since 1.19
3418 * @return string
3420 public function __toString() {
3421 return (string)$this->mConn;
3425 * Make sure that copies do not share the same client binding handle
3426 * @throws DBConnectionError
3428 public function __clone() {
3429 $this->connLogger->warning(
3430 "Cloning " . get_class( $this ) . " is not recomended; forking connection:\n" .
3431 ( new RuntimeException() )->getTraceAsString()
3434 if ( $this->isOpen() ) {
3435 // Open a new connection resource without messing with the old one
3436 $this->mOpened = false;
3437 $this->mConn = false;
3438 $this->mTrxEndCallbacks = []; // don't copy
3439 $this->handleSessionLoss(); // no trx or locks anymore
3440 $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
3441 $this->lastPing = microtime( true );
3446 * Called by serialize. Throw an exception when DB connection is serialized.
3447 * This causes problems on some database engines because the connection is
3448 * not restored on unserialize.
3450 public function __sleep() {
3451 throw new RuntimeException( 'Database serialization may cause problems, since ' .
3452 'the connection is not restored on wakeup.' );
3456 * Run a few simple sanity checks and close dangling connections
3458 public function __destruct() {
3459 if ( $this->mTrxLevel && $this->mTrxDoneWrites ) {
3460 trigger_error( "Uncommitted DB writes (transaction from {$this->mTrxFname})." );
3463 $danglingWriters = $this->pendingWriteAndCallbackCallers();
3464 if ( $danglingWriters ) {
3465 $fnames = implode( ', ', $danglingWriters );
3466 trigger_error( "DB transaction writes or callbacks still pending ($fnames)." );
3469 if ( $this->mConn ) {
3470 // Avoid connection leaks for sanity. Normally, resources close at script completion.
3471 // The connection might already be closed in zend/hhvm by now, so suppress warnings.
3472 \MediaWiki\suppressWarnings();
3473 $this->closeConnection();
3474 \MediaWiki\restoreWarnings();
3475 $this->mConn = false;
3476 $this->mOpened = false;
3481 class_alias( 'Database', 'DatabaseBase' );