Move Blob class to Rdbms namespaces
[mediawiki.git] / includes / libs / rdbms / database / Database.php
blob9d800a29c89a658906cbd34fc45621107646d7aa
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;
29 use Wikimedia\Rdbms\TransactionProfiler;
30 use Wikimedia\Rdbms\LikeMatch;
31 use Wikimedia\Rdbms\DatabaseDomain;
32 use Wikimedia\Rdbms\DBMasterPos;
33 use Wikimedia\Rdbms\Blob;
35 /**
36 * Relational database abstraction object
38 * @ingroup Database
39 * @since 1.28
41 abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAwareInterface {
42 /** Number of times to re-try an operation in case of deadlock */
43 const DEADLOCK_TRIES = 4;
44 /** Minimum time to wait before retry, in microseconds */
45 const DEADLOCK_DELAY_MIN = 500000;
46 /** Maximum time to wait before retry */
47 const DEADLOCK_DELAY_MAX = 1500000;
49 /** How long before it is worth doing a dummy query to test the connection */
50 const PING_TTL = 1.0;
51 const PING_QUERY = 'SELECT 1 AS ping';
53 const TINY_WRITE_SEC = .010;
54 const SLOW_WRITE_SEC = .500;
55 const SMALL_WRITE_ROWS = 100;
57 /** @var string SQL query */
58 protected $mLastQuery = '';
59 /** @var float|bool UNIX timestamp of last write query */
60 protected $mLastWriteTime = false;
61 /** @var string|bool */
62 protected $mPHPError = false;
63 /** @var string */
64 protected $mServer;
65 /** @var string */
66 protected $mUser;
67 /** @var string */
68 protected $mPassword;
69 /** @var string */
70 protected $mDBname;
71 /** @var array[] $aliases Map of (table => (dbname, schema, prefix) map) */
72 protected $tableAliases = [];
73 /** @var bool Whether this PHP instance is for a CLI script */
74 protected $cliMode;
75 /** @var string Agent name for query profiling */
76 protected $agent;
78 /** @var BagOStuff APC cache */
79 protected $srvCache;
80 /** @var LoggerInterface */
81 protected $connLogger;
82 /** @var LoggerInterface */
83 protected $queryLogger;
84 /** @var callback Error logging callback */
85 protected $errorLogger;
87 /** @var resource|null Database connection */
88 protected $mConn = null;
89 /** @var bool */
90 protected $mOpened = false;
92 /** @var array[] List of (callable, method name) */
93 protected $mTrxIdleCallbacks = [];
94 /** @var array[] List of (callable, method name) */
95 protected $mTrxPreCommitCallbacks = [];
96 /** @var array[] List of (callable, method name) */
97 protected $mTrxEndCallbacks = [];
98 /** @var callable[] Map of (name => callable) */
99 protected $mTrxRecurringCallbacks = [];
100 /** @var bool Whether to suppress triggering of transaction end callbacks */
101 protected $mTrxEndCallbacksSuppressed = false;
103 /** @var string */
104 protected $mTablePrefix = '';
105 /** @var string */
106 protected $mSchema = '';
107 /** @var integer */
108 protected $mFlags;
109 /** @var array */
110 protected $mLBInfo = [];
111 /** @var bool|null */
112 protected $mDefaultBigSelects = null;
113 /** @var array|bool */
114 protected $mSchemaVars = false;
115 /** @var array */
116 protected $mSessionVars = [];
117 /** @var array|null */
118 protected $preparedArgs;
119 /** @var string|bool|null Stashed value of html_errors INI setting */
120 protected $htmlErrors;
121 /** @var string */
122 protected $delimiter = ';';
123 /** @var DatabaseDomain */
124 protected $currentDomain;
127 * Either 1 if a transaction is active or 0 otherwise.
128 * The other Trx fields may not be meaningfull if this is 0.
130 * @var int
132 protected $mTrxLevel = 0;
134 * Either a short hexidecimal string if a transaction is active or ""
136 * @var string
137 * @see Database::mTrxLevel
139 protected $mTrxShortId = '';
141 * The UNIX time that the transaction started. Callers can assume that if
142 * snapshot isolation is used, then the data is *at least* up to date to that
143 * point (possibly more up-to-date since the first SELECT defines the snapshot).
145 * @var float|null
146 * @see Database::mTrxLevel
148 private $mTrxTimestamp = null;
149 /** @var float Lag estimate at the time of BEGIN */
150 private $mTrxReplicaLag = null;
152 * Remembers the function name given for starting the most recent transaction via begin().
153 * Used to provide additional context for error reporting.
155 * @var string
156 * @see Database::mTrxLevel
158 private $mTrxFname = null;
160 * Record if possible write queries were done in the last transaction started
162 * @var bool
163 * @see Database::mTrxLevel
165 private $mTrxDoneWrites = false;
167 * Record if the current transaction was started implicitly due to DBO_TRX being set.
169 * @var bool
170 * @see Database::mTrxLevel
172 private $mTrxAutomatic = false;
174 * Array of levels of atomicity within transactions
176 * @var array
178 private $mTrxAtomicLevels = [];
180 * Record if the current transaction was started implicitly by Database::startAtomic
182 * @var bool
184 private $mTrxAutomaticAtomic = false;
186 * Track the write query callers of the current transaction
188 * @var string[]
190 private $mTrxWriteCallers = [];
192 * @var float Seconds spent in write queries for the current transaction
194 private $mTrxWriteDuration = 0.0;
196 * @var integer Number of write queries for the current transaction
198 private $mTrxWriteQueryCount = 0;
200 * @var float Like mTrxWriteQueryCount but excludes lock-bound, easy to replicate, queries
202 private $mTrxWriteAdjDuration = 0.0;
204 * @var integer Number of write queries counted in mTrxWriteAdjDuration
206 private $mTrxWriteAdjQueryCount = 0;
208 * @var float RTT time estimate
210 private $mRTTEstimate = 0.0;
212 /** @var array Map of (name => 1) for locks obtained via lock() */
213 private $mNamedLocksHeld = [];
214 /** @var array Map of (table name => 1) for TEMPORARY tables */
215 protected $mSessionTempTables = [];
217 /** @var IDatabase|null Lazy handle to the master DB this server replicates from */
218 private $lazyMasterHandle;
220 /** @var float UNIX timestamp */
221 protected $lastPing = 0.0;
223 /** @var int[] Prior mFlags values */
224 private $priorFlags = [];
226 /** @var object|string Class name or object With profileIn/profileOut methods */
227 protected $profiler;
228 /** @var TransactionProfiler */
229 protected $trxProfiler;
232 * Constructor and database handle and attempt to connect to the DB server
234 * IDatabase classes should not be constructed directly in external
235 * code. Database::factory() should be used instead.
237 * @param array $params Parameters passed from Database::factory()
239 function __construct( array $params ) {
240 $server = $params['host'];
241 $user = $params['user'];
242 $password = $params['password'];
243 $dbName = $params['dbname'];
245 $this->mSchema = $params['schema'];
246 $this->mTablePrefix = $params['tablePrefix'];
248 $this->cliMode = $params['cliMode'];
249 // Agent name is added to SQL queries in a comment, so make sure it can't break out
250 $this->agent = str_replace( '/', '-', $params['agent'] );
252 $this->mFlags = $params['flags'];
253 if ( $this->mFlags & self::DBO_DEFAULT ) {
254 if ( $this->cliMode ) {
255 $this->mFlags &= ~self::DBO_TRX;
256 } else {
257 $this->mFlags |= self::DBO_TRX;
261 $this->mSessionVars = $params['variables'];
263 $this->srvCache = isset( $params['srvCache'] )
264 ? $params['srvCache']
265 : new HashBagOStuff();
267 $this->profiler = $params['profiler'];
268 $this->trxProfiler = $params['trxProfiler'];
269 $this->connLogger = $params['connLogger'];
270 $this->queryLogger = $params['queryLogger'];
271 $this->errorLogger = $params['errorLogger'];
273 // Set initial dummy domain until open() sets the final DB/prefix
274 $this->currentDomain = DatabaseDomain::newUnspecified();
276 if ( $user ) {
277 $this->open( $server, $user, $password, $dbName );
278 } elseif ( $this->requiresDatabaseUser() ) {
279 throw new InvalidArgumentException( "No database user provided." );
282 // Set the domain object after open() sets the relevant fields
283 if ( $this->mDBname != '' ) {
284 // Domains with server scope but a table prefix are not used by IDatabase classes
285 $this->currentDomain = new DatabaseDomain( $this->mDBname, null, $this->mTablePrefix );
290 * Construct a Database subclass instance given a database type and parameters
292 * This also connects to the database immediately upon object construction
294 * @param string $dbType A possible DB type (sqlite, mysql, postgres)
295 * @param array $p Parameter map with keys:
296 * - host : The hostname of the DB server
297 * - user : The name of the database user the client operates under
298 * - password : The password for the database user
299 * - dbname : The name of the database to use where queries do not specify one.
300 * The database must exist or an error might be thrown. Setting this to the empty string
301 * will avoid any such errors and make the handle have no implicit database scope. This is
302 * useful for queries like SHOW STATUS, CREATE DATABASE, or DROP DATABASE. Note that a
303 * "database" in Postgres is rougly equivalent to an entire MySQL server. This the domain
304 * in which user names and such are defined, e.g. users are database-specific in Postgres.
305 * - schema : The database schema to use (if supported). A "schema" in Postgres is roughly
306 * equivalent to a "database" in MySQL. Note that MySQL and SQLite do not use schemas.
307 * - tablePrefix : Optional table prefix that is implicitly added on to all table names
308 * recognized in queries. This can be used in place of schemas for handle site farms.
309 * - flags : Optional bitfield of DBO_* constants that define connection, protocol,
310 * buffering, and transaction behavior. It is STRONGLY adviced to leave the DBO_DEFAULT
311 * flag in place UNLESS this this database simply acts as a key/value store.
312 * - driver: Optional name of a specific DB client driver. For MySQL, there is the old
313 * 'mysql' driver and the newer 'mysqli' driver.
314 * - variables: Optional map of session variables to set after connecting. This can be
315 * used to adjust lock timeouts or encoding modes and the like.
316 * - connLogger: Optional PSR-3 logger interface instance.
317 * - queryLogger: Optional PSR-3 logger interface instance.
318 * - profiler: Optional class name or object with profileIn()/profileOut() methods.
319 * These will be called in query(), using a simplified version of the SQL that also
320 * includes the agent as a SQL comment.
321 * - trxProfiler: Optional TransactionProfiler instance.
322 * - errorLogger: Optional callback that takes an Exception and logs it.
323 * - cliMode: Whether to consider the execution context that of a CLI script.
324 * - agent: Optional name used to identify the end-user in query profiling/logging.
325 * - srvCache: Optional BagOStuff instance to an APC-style cache.
326 * @return Database|null If the database driver or extension cannot be found
327 * @throws InvalidArgumentException If the database driver or extension cannot be found
328 * @since 1.18
330 final public static function factory( $dbType, $p = [] ) {
331 static $canonicalDBTypes = [
332 'mysql' => [ 'mysqli', 'mysql' ],
333 'postgres' => [],
334 'sqlite' => [],
335 'oracle' => [],
336 'mssql' => [],
339 $driver = false;
340 $dbType = strtolower( $dbType );
341 if ( isset( $canonicalDBTypes[$dbType] ) && $canonicalDBTypes[$dbType] ) {
342 $possibleDrivers = $canonicalDBTypes[$dbType];
343 if ( !empty( $p['driver'] ) ) {
344 if ( in_array( $p['driver'], $possibleDrivers ) ) {
345 $driver = $p['driver'];
346 } else {
347 throw new InvalidArgumentException( __METHOD__ .
348 " type '$dbType' does not support driver '{$p['driver']}'" );
350 } else {
351 foreach ( $possibleDrivers as $posDriver ) {
352 if ( extension_loaded( $posDriver ) ) {
353 $driver = $posDriver;
354 break;
358 } else {
359 $driver = $dbType;
361 if ( $driver === false || $driver === '' ) {
362 throw new InvalidArgumentException( __METHOD__ .
363 " no viable database extension found for type '$dbType'" );
366 $class = 'Database' . ucfirst( $driver );
367 if ( class_exists( $class ) && is_subclass_of( $class, 'IDatabase' ) ) {
368 // Resolve some defaults for b/c
369 $p['host'] = isset( $p['host'] ) ? $p['host'] : false;
370 $p['user'] = isset( $p['user'] ) ? $p['user'] : false;
371 $p['password'] = isset( $p['password'] ) ? $p['password'] : false;
372 $p['dbname'] = isset( $p['dbname'] ) ? $p['dbname'] : false;
373 $p['flags'] = isset( $p['flags'] ) ? $p['flags'] : 0;
374 $p['variables'] = isset( $p['variables'] ) ? $p['variables'] : [];
375 $p['tablePrefix'] = isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : '';
376 $p['schema'] = isset( $p['schema'] ) ? $p['schema'] : '';
377 $p['cliMode'] = isset( $p['cliMode'] ) ? $p['cliMode'] : ( PHP_SAPI === 'cli' );
378 $p['agent'] = isset( $p['agent'] ) ? $p['agent'] : '';
379 if ( !isset( $p['connLogger'] ) ) {
380 $p['connLogger'] = new \Psr\Log\NullLogger();
382 if ( !isset( $p['queryLogger'] ) ) {
383 $p['queryLogger'] = new \Psr\Log\NullLogger();
385 $p['profiler'] = isset( $p['profiler'] ) ? $p['profiler'] : null;
386 if ( !isset( $p['trxProfiler'] ) ) {
387 $p['trxProfiler'] = new TransactionProfiler();
389 if ( !isset( $p['errorLogger'] ) ) {
390 $p['errorLogger'] = function ( Exception $e ) {
391 trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
395 $conn = new $class( $p );
396 } else {
397 $conn = null;
400 return $conn;
403 public function setLogger( LoggerInterface $logger ) {
404 $this->queryLogger = $logger;
407 public function getServerInfo() {
408 return $this->getServerVersion();
411 public function bufferResults( $buffer = null ) {
412 $res = !$this->getFlag( self::DBO_NOBUFFER );
413 if ( $buffer !== null ) {
414 $buffer
415 ? $this->clearFlag( self::DBO_NOBUFFER )
416 : $this->setFlag( self::DBO_NOBUFFER );
419 return $res;
423 * Turns on (false) or off (true) the automatic generation and sending
424 * of a "we're sorry, but there has been a database error" page on
425 * database errors. Default is on (false). When turned off, the
426 * code should use lastErrno() and lastError() to handle the
427 * situation as appropriate.
429 * Do not use this function outside of the Database classes.
431 * @param null|bool $ignoreErrors
432 * @return bool The previous value of the flag.
434 protected function ignoreErrors( $ignoreErrors = null ) {
435 $res = $this->getFlag( self::DBO_IGNORE );
436 if ( $ignoreErrors !== null ) {
437 $ignoreErrors
438 ? $this->setFlag( self::DBO_IGNORE )
439 : $this->clearFlag( self::DBO_IGNORE );
442 return $res;
445 public function trxLevel() {
446 return $this->mTrxLevel;
449 public function trxTimestamp() {
450 return $this->mTrxLevel ? $this->mTrxTimestamp : null;
453 public function tablePrefix( $prefix = null ) {
454 $old = $this->mTablePrefix;
455 if ( $prefix !== null ) {
456 $this->mTablePrefix = $prefix;
457 $this->currentDomain = ( $this->mDBname != '' )
458 ? new DatabaseDomain( $this->mDBname, null, $this->mTablePrefix )
459 : DatabaseDomain::newUnspecified();
462 return $old;
465 public function dbSchema( $schema = null ) {
466 $old = $this->mSchema;
467 if ( $schema !== null ) {
468 $this->mSchema = $schema;
471 return $old;
474 public function getLBInfo( $name = null ) {
475 if ( is_null( $name ) ) {
476 return $this->mLBInfo;
477 } else {
478 if ( array_key_exists( $name, $this->mLBInfo ) ) {
479 return $this->mLBInfo[$name];
480 } else {
481 return null;
486 public function setLBInfo( $name, $value = null ) {
487 if ( is_null( $value ) ) {
488 $this->mLBInfo = $name;
489 } else {
490 $this->mLBInfo[$name] = $value;
494 public function setLazyMasterHandle( IDatabase $conn ) {
495 $this->lazyMasterHandle = $conn;
499 * @return IDatabase|null
500 * @see setLazyMasterHandle()
501 * @since 1.27
503 protected function getLazyMasterHandle() {
504 return $this->lazyMasterHandle;
507 public function implicitGroupby() {
508 return true;
511 public function implicitOrderby() {
512 return true;
515 public function lastQuery() {
516 return $this->mLastQuery;
519 public function doneWrites() {
520 return (bool)$this->mLastWriteTime;
523 public function lastDoneWrites() {
524 return $this->mLastWriteTime ?: false;
527 public function writesPending() {
528 return $this->mTrxLevel && $this->mTrxDoneWrites;
531 public function writesOrCallbacksPending() {
532 return $this->mTrxLevel && (
533 $this->mTrxDoneWrites || $this->mTrxIdleCallbacks || $this->mTrxPreCommitCallbacks
537 public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
538 if ( !$this->mTrxLevel ) {
539 return false;
540 } elseif ( !$this->mTrxDoneWrites ) {
541 return 0.0;
544 switch ( $type ) {
545 case self::ESTIMATE_DB_APPLY:
546 $this->ping( $rtt );
547 $rttAdjTotal = $this->mTrxWriteAdjQueryCount * $rtt;
548 $applyTime = max( $this->mTrxWriteAdjDuration - $rttAdjTotal, 0 );
549 // For omitted queries, make them count as something at least
550 $omitted = $this->mTrxWriteQueryCount - $this->mTrxWriteAdjQueryCount;
551 $applyTime += self::TINY_WRITE_SEC * $omitted;
553 return $applyTime;
554 default: // everything
555 return $this->mTrxWriteDuration;
559 public function pendingWriteCallers() {
560 return $this->mTrxLevel ? $this->mTrxWriteCallers : [];
563 protected function pendingWriteAndCallbackCallers() {
564 if ( !$this->mTrxLevel ) {
565 return [];
568 $fnames = $this->mTrxWriteCallers;
569 foreach ( [
570 $this->mTrxIdleCallbacks,
571 $this->mTrxPreCommitCallbacks,
572 $this->mTrxEndCallbacks
573 ] as $callbacks ) {
574 foreach ( $callbacks as $callback ) {
575 $fnames[] = $callback[1];
579 return $fnames;
582 public function isOpen() {
583 return $this->mOpened;
586 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
587 if ( $remember === self::REMEMBER_PRIOR ) {
588 array_push( $this->priorFlags, $this->mFlags );
590 $this->mFlags |= $flag;
593 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
594 if ( $remember === self::REMEMBER_PRIOR ) {
595 array_push( $this->priorFlags, $this->mFlags );
597 $this->mFlags &= ~$flag;
600 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
601 if ( !$this->priorFlags ) {
602 return;
605 if ( $state === self::RESTORE_INITIAL ) {
606 $this->mFlags = reset( $this->priorFlags );
607 $this->priorFlags = [];
608 } else {
609 $this->mFlags = array_pop( $this->priorFlags );
613 public function getFlag( $flag ) {
614 return !!( $this->mFlags & $flag );
618 * @param string $name Class field name
619 * @return mixed
620 * @deprecated Since 1.28
622 public function getProperty( $name ) {
623 return $this->$name;
626 public function getDomainID() {
627 return $this->currentDomain->getId();
630 final public function getWikiID() {
631 return $this->getDomainID();
635 * Get information about an index into an object
636 * @param string $table Table name
637 * @param string $index Index name
638 * @param string $fname Calling function name
639 * @return mixed Database-specific index description class or false if the index does not exist
641 abstract function indexInfo( $table, $index, $fname = __METHOD__ );
644 * Wrapper for addslashes()
646 * @param string $s String to be slashed.
647 * @return string Slashed string.
649 abstract function strencode( $s );
651 protected function installErrorHandler() {
652 $this->mPHPError = false;
653 $this->htmlErrors = ini_set( 'html_errors', '0' );
654 set_error_handler( [ $this, 'connectionErrorLogger' ] );
658 * @return bool|string
660 protected function restoreErrorHandler() {
661 restore_error_handler();
662 if ( $this->htmlErrors !== false ) {
663 ini_set( 'html_errors', $this->htmlErrors );
666 return $this->getLastPHPError();
670 * @return string|bool Last PHP error for this DB (typically connection errors)
672 protected function getLastPHPError() {
673 if ( $this->mPHPError ) {
674 $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError );
675 $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
677 return $error;
680 return false;
684 * This method should not be used outside of Database classes
686 * @param int $errno
687 * @param string $errstr
689 public function connectionErrorLogger( $errno, $errstr ) {
690 $this->mPHPError = $errstr;
694 * Create a log context to pass to PSR-3 logger functions.
696 * @param array $extras Additional data to add to context
697 * @return array
699 protected function getLogContext( array $extras = [] ) {
700 return array_merge(
702 'db_server' => $this->mServer,
703 'db_name' => $this->mDBname,
704 'db_user' => $this->mUser,
706 $extras
710 public function close() {
711 if ( $this->mConn ) {
712 if ( $this->trxLevel() ) {
713 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
716 $closed = $this->closeConnection();
717 $this->mConn = false;
718 } elseif ( $this->mTrxIdleCallbacks || $this->mTrxEndCallbacks ) { // sanity
719 throw new RuntimeException( "Transaction callbacks still pending." );
720 } else {
721 $closed = true;
723 $this->mOpened = false;
725 return $closed;
729 * Make sure isOpen() returns true as a sanity check
731 * @throws DBUnexpectedError
733 protected function assertOpen() {
734 if ( !$this->isOpen() ) {
735 throw new DBUnexpectedError( $this, "DB connection was already closed." );
740 * Closes underlying database connection
741 * @since 1.20
742 * @return bool Whether connection was closed successfully
744 abstract protected function closeConnection();
746 public function reportConnectionError( $error = 'Unknown error' ) {
747 $myError = $this->lastError();
748 if ( $myError ) {
749 $error = $myError;
752 # New method
753 throw new DBConnectionError( $this, $error );
757 * The DBMS-dependent part of query()
759 * @param string $sql SQL query.
760 * @return ResultWrapper|bool Result object to feed to fetchObject,
761 * fetchRow, ...; or false on failure
763 abstract protected function doQuery( $sql );
766 * Determine whether a query writes to the DB.
767 * Should return true if unsure.
769 * @param string $sql
770 * @return bool
772 protected function isWriteQuery( $sql ) {
773 return !preg_match(
774 '/^(?:SELECT|BEGIN|ROLLBACK|COMMIT|SET|SHOW|EXPLAIN|\(SELECT)\b/i', $sql );
778 * @param $sql
779 * @return string|null
781 protected function getQueryVerb( $sql ) {
782 return preg_match( '/^\s*([a-z]+)/i', $sql, $m ) ? strtoupper( $m[1] ) : null;
786 * Determine whether a SQL statement is sensitive to isolation level.
787 * A SQL statement is considered transactable if its result could vary
788 * depending on the transaction isolation level. Operational commands
789 * such as 'SET' and 'SHOW' are not considered to be transactable.
791 * @param string $sql
792 * @return bool
794 protected function isTransactableQuery( $sql ) {
795 return !in_array(
796 $this->getQueryVerb( $sql ),
797 [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET', 'CREATE', 'ALTER' ],
798 true
803 * @param string $sql A SQL query
804 * @return bool Whether $sql is SQL for creating/dropping a new TEMPORARY table
806 protected function registerTempTableOperation( $sql ) {
807 if ( preg_match(
808 '/^CREATE\s+TEMPORARY\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?[`"\']?(\w+)[`"\']?/i',
809 $sql,
810 $matches
811 ) ) {
812 $this->mSessionTempTables[$matches[1]] = 1;
814 return true;
815 } elseif ( preg_match(
816 '/^DROP\s+(?:TEMPORARY\s+)?TABLE\s+(?:IF\s+EXISTS\s+)?[`"\']?(\w+)[`"\']?/i',
817 $sql,
818 $matches
819 ) ) {
820 unset( $this->mSessionTempTables[$matches[1]] );
822 return true;
823 } elseif ( preg_match(
824 '/^(?:INSERT\s+(?:\w+\s+)?INTO|UPDATE|DELETE\s+FROM)\s+[`"\']?(\w+)[`"\']?/i',
825 $sql,
826 $matches
827 ) ) {
828 return isset( $this->mSessionTempTables[$matches[1]] );
831 return false;
834 public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
835 $priorWritesPending = $this->writesOrCallbacksPending();
836 $this->mLastQuery = $sql;
838 $isWrite = $this->isWriteQuery( $sql ) && !$this->registerTempTableOperation( $sql );
839 if ( $isWrite ) {
840 $reason = $this->getReadOnlyReason();
841 if ( $reason !== false ) {
842 throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
844 # Set a flag indicating that writes have been done
845 $this->mLastWriteTime = microtime( true );
848 // Add trace comment to the begin of the sql string, right after the operator.
849 // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (bug 42598)
850 $commentedSql = preg_replace( '/\s|$/', " /* $fname {$this->agent} */ ", $sql, 1 );
852 # Start implicit transactions that wrap the request if DBO_TRX is enabled
853 if ( !$this->mTrxLevel && $this->getFlag( self::DBO_TRX )
854 && $this->isTransactableQuery( $sql )
856 $this->begin( __METHOD__ . " ($fname)", self::TRANSACTION_INTERNAL );
857 $this->mTrxAutomatic = true;
860 # Keep track of whether the transaction has write queries pending
861 if ( $this->mTrxLevel && !$this->mTrxDoneWrites && $isWrite ) {
862 $this->mTrxDoneWrites = true;
863 $this->trxProfiler->transactionWritingIn(
864 $this->mServer, $this->mDBname, $this->mTrxShortId );
867 if ( $this->getFlag( self::DBO_DEBUG ) ) {
868 $this->queryLogger->debug( "{$this->mDBname} {$commentedSql}" );
871 # Avoid fatals if close() was called
872 $this->assertOpen();
874 # Send the query to the server
875 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
877 # Try reconnecting if the connection was lost
878 if ( false === $ret && $this->wasErrorReissuable() ) {
879 $recoverable = $this->canRecoverFromDisconnect( $sql, $priorWritesPending );
880 # Stash the last error values before anything might clear them
881 $lastError = $this->lastError();
882 $lastErrno = $this->lastErrno();
883 # Update state tracking to reflect transaction loss due to disconnection
884 $this->handleSessionLoss();
885 if ( $this->reconnect() ) {
886 $msg = __METHOD__ . ": lost connection to {$this->getServer()}; reconnected";
887 $this->connLogger->warning( $msg );
888 $this->queryLogger->warning(
889 "$msg:\n" . ( new RuntimeException() )->getTraceAsString() );
891 if ( !$recoverable ) {
892 # Callers may catch the exception and continue to use the DB
893 $this->reportQueryError( $lastError, $lastErrno, $sql, $fname );
894 } else {
895 # Should be safe to silently retry the query
896 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
898 } else {
899 $msg = __METHOD__ . ": lost connection to {$this->getServer()} permanently";
900 $this->connLogger->error( $msg );
904 if ( false === $ret ) {
905 # Deadlocks cause the entire transaction to abort, not just the statement.
906 # https://dev.mysql.com/doc/refman/5.7/en/innodb-error-handling.html
907 # https://www.postgresql.org/docs/9.1/static/explicit-locking.html
908 if ( $this->wasDeadlock() ) {
909 if ( $this->explicitTrxActive() || $priorWritesPending ) {
910 $tempIgnore = false; // not recoverable
912 # Update state tracking to reflect transaction loss
913 $this->handleSessionLoss();
916 $this->reportQueryError(
917 $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
920 $res = $this->resultObject( $ret );
922 return $res;
925 private function doProfiledQuery( $sql, $commentedSql, $isWrite, $fname ) {
926 $isMaster = !is_null( $this->getLBInfo( 'master' ) );
927 # generalizeSQL() will probably cut down the query to reasonable
928 # logging size most of the time. The substr is really just a sanity check.
929 if ( $isMaster ) {
930 $queryProf = 'query-m: ' . substr( self::generalizeSQL( $sql ), 0, 255 );
931 } else {
932 $queryProf = 'query: ' . substr( self::generalizeSQL( $sql ), 0, 255 );
935 # Include query transaction state
936 $queryProf .= $this->mTrxShortId ? " [TRX#{$this->mTrxShortId}]" : "";
938 $startTime = microtime( true );
939 if ( $this->profiler ) {
940 call_user_func( [ $this->profiler, 'profileIn' ], $queryProf );
942 $ret = $this->doQuery( $commentedSql );
943 if ( $this->profiler ) {
944 call_user_func( [ $this->profiler, 'profileOut' ], $queryProf );
946 $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
948 unset( $queryProfSection ); // profile out (if set)
950 if ( $ret !== false ) {
951 $this->lastPing = $startTime;
952 if ( $isWrite && $this->mTrxLevel ) {
953 $this->updateTrxWriteQueryTime( $sql, $queryRuntime );
954 $this->mTrxWriteCallers[] = $fname;
958 if ( $sql === self::PING_QUERY ) {
959 $this->mRTTEstimate = $queryRuntime;
962 $this->trxProfiler->recordQueryCompletion(
963 $queryProf, $startTime, $isWrite, $this->affectedRows()
965 $this->queryLogger->debug( $sql, [
966 'method' => $fname,
967 'master' => $isMaster,
968 'runtime' => $queryRuntime,
969 ] );
971 return $ret;
975 * Update the estimated run-time of a query, not counting large row lock times
977 * LoadBalancer can be set to rollback transactions that will create huge replication
978 * lag. It bases this estimate off of pendingWriteQueryDuration(). Certain simple
979 * queries, like inserting a row can take a long time due to row locking. This method
980 * uses some simple heuristics to discount those cases.
982 * @param string $sql A SQL write query
983 * @param float $runtime Total runtime, including RTT
985 private function updateTrxWriteQueryTime( $sql, $runtime ) {
986 // Whether this is indicative of replica DB runtime (except for RBR or ws_repl)
987 $indicativeOfReplicaRuntime = true;
988 if ( $runtime > self::SLOW_WRITE_SEC ) {
989 $verb = $this->getQueryVerb( $sql );
990 // insert(), upsert(), replace() are fast unless bulky in size or blocked on locks
991 if ( $verb === 'INSERT' ) {
992 $indicativeOfReplicaRuntime = $this->affectedRows() > self::SMALL_WRITE_ROWS;
993 } elseif ( $verb === 'REPLACE' ) {
994 $indicativeOfReplicaRuntime = $this->affectedRows() > self::SMALL_WRITE_ROWS / 2;
998 $this->mTrxWriteDuration += $runtime;
999 $this->mTrxWriteQueryCount += 1;
1000 if ( $indicativeOfReplicaRuntime ) {
1001 $this->mTrxWriteAdjDuration += $runtime;
1002 $this->mTrxWriteAdjQueryCount += 1;
1006 private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
1007 # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
1008 # Dropped connections also mean that named locks are automatically released.
1009 # Only allow error suppression in autocommit mode or when the lost transaction
1010 # didn't matter anyway (aside from DBO_TRX snapshot loss).
1011 if ( $this->mNamedLocksHeld ) {
1012 return false; // possible critical section violation
1013 } elseif ( $sql === 'COMMIT' ) {
1014 return !$priorWritesPending; // nothing written anyway? (T127428)
1015 } elseif ( $sql === 'ROLLBACK' ) {
1016 return true; // transaction lost...which is also what was requested :)
1017 } elseif ( $this->explicitTrxActive() ) {
1018 return false; // don't drop atomocity
1019 } elseif ( $priorWritesPending ) {
1020 return false; // prior writes lost from implicit transaction
1023 return true;
1026 private function handleSessionLoss() {
1027 $this->mTrxLevel = 0;
1028 $this->mTrxIdleCallbacks = []; // bug 65263
1029 $this->mTrxPreCommitCallbacks = []; // bug 65263
1030 $this->mSessionTempTables = [];
1031 $this->mNamedLocksHeld = [];
1032 try {
1033 // Handle callbacks in mTrxEndCallbacks
1034 $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
1035 $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
1036 return null;
1037 } catch ( Exception $e ) {
1038 // Already logged; move on...
1039 return $e;
1043 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
1044 if ( $this->ignoreErrors() || $tempIgnore ) {
1045 $this->queryLogger->debug( "SQL ERROR (ignored): $error\n" );
1046 } else {
1047 $sql1line = mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 );
1048 $this->queryLogger->error(
1049 "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
1050 $this->getLogContext( [
1051 'method' => __METHOD__,
1052 'errno' => $errno,
1053 'error' => $error,
1054 'sql1line' => $sql1line,
1055 'fname' => $fname,
1058 $this->queryLogger->debug( "SQL ERROR: " . $error . "\n" );
1059 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
1063 public function freeResult( $res ) {
1066 public function selectField(
1067 $table, $var, $cond = '', $fname = __METHOD__, $options = []
1069 if ( $var === '*' ) { // sanity
1070 throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
1073 if ( !is_array( $options ) ) {
1074 $options = [ $options ];
1077 $options['LIMIT'] = 1;
1079 $res = $this->select( $table, $var, $cond, $fname, $options );
1080 if ( $res === false || !$this->numRows( $res ) ) {
1081 return false;
1084 $row = $this->fetchRow( $res );
1086 if ( $row !== false ) {
1087 return reset( $row );
1088 } else {
1089 return false;
1093 public function selectFieldValues(
1094 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
1096 if ( $var === '*' ) { // sanity
1097 throw new DBUnexpectedError( $this, "Cannot use a * field" );
1098 } elseif ( !is_string( $var ) ) { // sanity
1099 throw new DBUnexpectedError( $this, "Cannot use an array of fields" );
1102 if ( !is_array( $options ) ) {
1103 $options = [ $options ];
1106 $res = $this->select( $table, $var, $cond, $fname, $options, $join_conds );
1107 if ( $res === false ) {
1108 return false;
1111 $values = [];
1112 foreach ( $res as $row ) {
1113 $values[] = $row->$var;
1116 return $values;
1120 * Returns an optional USE INDEX clause to go after the table, and a
1121 * string to go at the end of the query.
1123 * @param array $options Associative array of options to be turned into
1124 * an SQL query, valid keys are listed in the function.
1125 * @return array
1126 * @see Database::select()
1128 protected function makeSelectOptions( $options ) {
1129 $preLimitTail = $postLimitTail = '';
1130 $startOpts = '';
1132 $noKeyOptions = [];
1134 foreach ( $options as $key => $option ) {
1135 if ( is_numeric( $key ) ) {
1136 $noKeyOptions[$option] = true;
1140 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1142 $preLimitTail .= $this->makeOrderBy( $options );
1144 // if (isset($options['LIMIT'])) {
1145 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
1146 // isset($options['OFFSET']) ? $options['OFFSET']
1147 // : false);
1148 // }
1150 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1151 $postLimitTail .= ' FOR UPDATE';
1154 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1155 $postLimitTail .= ' LOCK IN SHARE MODE';
1158 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1159 $startOpts .= 'DISTINCT';
1162 # Various MySQL extensions
1163 if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
1164 $startOpts .= ' /*! STRAIGHT_JOIN */';
1167 if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) {
1168 $startOpts .= ' HIGH_PRIORITY';
1171 if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
1172 $startOpts .= ' SQL_BIG_RESULT';
1175 if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
1176 $startOpts .= ' SQL_BUFFER_RESULT';
1179 if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
1180 $startOpts .= ' SQL_SMALL_RESULT';
1183 if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
1184 $startOpts .= ' SQL_CALC_FOUND_ROWS';
1187 if ( isset( $noKeyOptions['SQL_CACHE'] ) ) {
1188 $startOpts .= ' SQL_CACHE';
1191 if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) {
1192 $startOpts .= ' SQL_NO_CACHE';
1195 if ( isset( $options['USE INDEX'] ) && is_string( $options['USE INDEX'] ) ) {
1196 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1197 } else {
1198 $useIndex = '';
1200 if ( isset( $options['IGNORE INDEX'] ) && is_string( $options['IGNORE INDEX'] ) ) {
1201 $ignoreIndex = $this->ignoreIndexClause( $options['IGNORE INDEX'] );
1202 } else {
1203 $ignoreIndex = '';
1206 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1210 * Returns an optional GROUP BY with an optional HAVING
1212 * @param array $options Associative array of options
1213 * @return string
1214 * @see Database::select()
1215 * @since 1.21
1217 protected function makeGroupByWithHaving( $options ) {
1218 $sql = '';
1219 if ( isset( $options['GROUP BY'] ) ) {
1220 $gb = is_array( $options['GROUP BY'] )
1221 ? implode( ',', $options['GROUP BY'] )
1222 : $options['GROUP BY'];
1223 $sql .= ' GROUP BY ' . $gb;
1225 if ( isset( $options['HAVING'] ) ) {
1226 $having = is_array( $options['HAVING'] )
1227 ? $this->makeList( $options['HAVING'], self::LIST_AND )
1228 : $options['HAVING'];
1229 $sql .= ' HAVING ' . $having;
1232 return $sql;
1236 * Returns an optional ORDER BY
1238 * @param array $options Associative array of options
1239 * @return string
1240 * @see Database::select()
1241 * @since 1.21
1243 protected function makeOrderBy( $options ) {
1244 if ( isset( $options['ORDER BY'] ) ) {
1245 $ob = is_array( $options['ORDER BY'] )
1246 ? implode( ',', $options['ORDER BY'] )
1247 : $options['ORDER BY'];
1249 return ' ORDER BY ' . $ob;
1252 return '';
1255 public function select( $table, $vars, $conds = '', $fname = __METHOD__,
1256 $options = [], $join_conds = [] ) {
1257 $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
1259 return $this->query( $sql, $fname );
1262 public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__,
1263 $options = [], $join_conds = []
1265 if ( is_array( $vars ) ) {
1266 $vars = implode( ',', $this->fieldNamesWithAlias( $vars ) );
1269 $options = (array)$options;
1270 $useIndexes = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
1271 ? $options['USE INDEX']
1272 : [];
1273 $ignoreIndexes = (
1274 isset( $options['IGNORE INDEX'] ) &&
1275 is_array( $options['IGNORE INDEX'] )
1277 ? $options['IGNORE INDEX']
1278 : [];
1280 if ( is_array( $table ) ) {
1281 $from = ' FROM ' .
1282 $this->tableNamesWithIndexClauseOrJOIN(
1283 $table, $useIndexes, $ignoreIndexes, $join_conds );
1284 } elseif ( $table != '' ) {
1285 if ( $table[0] == ' ' ) {
1286 $from = ' FROM ' . $table;
1287 } else {
1288 $from = ' FROM ' .
1289 $this->tableNamesWithIndexClauseOrJOIN(
1290 [ $table ], $useIndexes, $ignoreIndexes, [] );
1292 } else {
1293 $from = '';
1296 list( $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ) =
1297 $this->makeSelectOptions( $options );
1299 if ( !empty( $conds ) ) {
1300 if ( is_array( $conds ) ) {
1301 $conds = $this->makeList( $conds, self::LIST_AND );
1303 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex " .
1304 "WHERE $conds $preLimitTail";
1305 } else {
1306 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex $preLimitTail";
1309 if ( isset( $options['LIMIT'] ) ) {
1310 $sql = $this->limitResult( $sql, $options['LIMIT'],
1311 isset( $options['OFFSET'] ) ? $options['OFFSET'] : false );
1313 $sql = "$sql $postLimitTail";
1315 if ( isset( $options['EXPLAIN'] ) ) {
1316 $sql = 'EXPLAIN ' . $sql;
1319 return $sql;
1322 public function selectRow( $table, $vars, $conds, $fname = __METHOD__,
1323 $options = [], $join_conds = []
1325 $options = (array)$options;
1326 $options['LIMIT'] = 1;
1327 $res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
1329 if ( $res === false ) {
1330 return false;
1333 if ( !$this->numRows( $res ) ) {
1334 return false;
1337 $obj = $this->fetchObject( $res );
1339 return $obj;
1342 public function estimateRowCount(
1343 $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = []
1345 $rows = 0;
1346 $res = $this->select( $table, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options );
1348 if ( $res ) {
1349 $row = $this->fetchRow( $res );
1350 $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
1353 return $rows;
1356 public function selectRowCount(
1357 $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
1359 $rows = 0;
1360 $sql = $this->selectSQLText( $tables, '1', $conds, $fname, $options, $join_conds );
1361 $res = $this->query( "SELECT COUNT(*) AS rowcount FROM ($sql) tmp_count", $fname );
1363 if ( $res ) {
1364 $row = $this->fetchRow( $res );
1365 $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
1368 return $rows;
1372 * Removes most variables from an SQL query and replaces them with X or N for numbers.
1373 * It's only slightly flawed. Don't use for anything important.
1375 * @param string $sql A SQL Query
1377 * @return string
1379 protected static function generalizeSQL( $sql ) {
1380 # This does the same as the regexp below would do, but in such a way
1381 # as to avoid crashing php on some large strings.
1382 # $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql );
1384 $sql = str_replace( "\\\\", '', $sql );
1385 $sql = str_replace( "\\'", '', $sql );
1386 $sql = str_replace( "\\\"", '', $sql );
1387 $sql = preg_replace( "/'.*'/s", "'X'", $sql );
1388 $sql = preg_replace( '/".*"/s', "'X'", $sql );
1390 # All newlines, tabs, etc replaced by single space
1391 $sql = preg_replace( '/\s+/', ' ', $sql );
1393 # All numbers => N,
1394 # except the ones surrounded by characters, e.g. l10n
1395 $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
1396 $sql = preg_replace( '/(?<![a-zA-Z])-?\d+(?![a-zA-Z])/s', 'N', $sql );
1398 return $sql;
1401 public function fieldExists( $table, $field, $fname = __METHOD__ ) {
1402 $info = $this->fieldInfo( $table, $field );
1404 return (bool)$info;
1407 public function indexExists( $table, $index, $fname = __METHOD__ ) {
1408 if ( !$this->tableExists( $table ) ) {
1409 return null;
1412 $info = $this->indexInfo( $table, $index, $fname );
1413 if ( is_null( $info ) ) {
1414 return null;
1415 } else {
1416 return $info !== false;
1420 public function tableExists( $table, $fname = __METHOD__ ) {
1421 $tableRaw = $this->tableName( $table, 'raw' );
1422 if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
1423 return true; // already known to exist
1426 $table = $this->tableName( $table );
1427 $ignoreErrors = true;
1428 $res = $this->query( "SELECT 1 FROM $table LIMIT 1", $fname, $ignoreErrors );
1430 return (bool)$res;
1433 public function indexUnique( $table, $index ) {
1434 $indexInfo = $this->indexInfo( $table, $index );
1436 if ( !$indexInfo ) {
1437 return null;
1440 return !$indexInfo[0]->Non_unique;
1444 * Helper for Database::insert().
1446 * @param array $options
1447 * @return string
1449 protected function makeInsertOptions( $options ) {
1450 return implode( ' ', $options );
1453 public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
1454 # No rows to insert, easy just return now
1455 if ( !count( $a ) ) {
1456 return true;
1459 $table = $this->tableName( $table );
1461 if ( !is_array( $options ) ) {
1462 $options = [ $options ];
1465 $fh = null;
1466 if ( isset( $options['fileHandle'] ) ) {
1467 $fh = $options['fileHandle'];
1469 $options = $this->makeInsertOptions( $options );
1471 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
1472 $multi = true;
1473 $keys = array_keys( $a[0] );
1474 } else {
1475 $multi = false;
1476 $keys = array_keys( $a );
1479 $sql = 'INSERT ' . $options .
1480 " INTO $table (" . implode( ',', $keys ) . ') VALUES ';
1482 if ( $multi ) {
1483 $first = true;
1484 foreach ( $a as $row ) {
1485 if ( $first ) {
1486 $first = false;
1487 } else {
1488 $sql .= ',';
1490 $sql .= '(' . $this->makeList( $row ) . ')';
1492 } else {
1493 $sql .= '(' . $this->makeList( $a ) . ')';
1496 if ( $fh !== null && false === fwrite( $fh, $sql ) ) {
1497 return false;
1498 } elseif ( $fh !== null ) {
1499 return true;
1502 return (bool)$this->query( $sql, $fname );
1506 * Make UPDATE options array for Database::makeUpdateOptions
1508 * @param array $options
1509 * @return array
1511 protected function makeUpdateOptionsArray( $options ) {
1512 if ( !is_array( $options ) ) {
1513 $options = [ $options ];
1516 $opts = [];
1518 if ( in_array( 'IGNORE', $options ) ) {
1519 $opts[] = 'IGNORE';
1522 return $opts;
1526 * Make UPDATE options for the Database::update function
1528 * @param array $options The options passed to Database::update
1529 * @return string
1531 protected function makeUpdateOptions( $options ) {
1532 $opts = $this->makeUpdateOptionsArray( $options );
1534 return implode( ' ', $opts );
1537 public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
1538 $table = $this->tableName( $table );
1539 $opts = $this->makeUpdateOptions( $options );
1540 $sql = "UPDATE $opts $table SET " . $this->makeList( $values, self::LIST_SET );
1542 if ( $conds !== [] && $conds !== '*' ) {
1543 $sql .= " WHERE " . $this->makeList( $conds, self::LIST_AND );
1546 return $this->query( $sql, $fname );
1549 public function makeList( $a, $mode = self::LIST_COMMA ) {
1550 if ( !is_array( $a ) ) {
1551 throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
1554 $first = true;
1555 $list = '';
1557 foreach ( $a as $field => $value ) {
1558 if ( !$first ) {
1559 if ( $mode == self::LIST_AND ) {
1560 $list .= ' AND ';
1561 } elseif ( $mode == self::LIST_OR ) {
1562 $list .= ' OR ';
1563 } else {
1564 $list .= ',';
1566 } else {
1567 $first = false;
1570 if ( ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_numeric( $field ) ) {
1571 $list .= "($value)";
1572 } elseif ( $mode == self::LIST_SET && is_numeric( $field ) ) {
1573 $list .= "$value";
1574 } elseif (
1575 ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_array( $value )
1577 // Remove null from array to be handled separately if found
1578 $includeNull = false;
1579 foreach ( array_keys( $value, null, true ) as $nullKey ) {
1580 $includeNull = true;
1581 unset( $value[$nullKey] );
1583 if ( count( $value ) == 0 && !$includeNull ) {
1584 throw new InvalidArgumentException(
1585 __METHOD__ . ": empty input for field $field" );
1586 } elseif ( count( $value ) == 0 ) {
1587 // only check if $field is null
1588 $list .= "$field IS NULL";
1589 } else {
1590 // IN clause contains at least one valid element
1591 if ( $includeNull ) {
1592 // Group subconditions to ensure correct precedence
1593 $list .= '(';
1595 if ( count( $value ) == 1 ) {
1596 // Special-case single values, as IN isn't terribly efficient
1597 // Don't necessarily assume the single key is 0; we don't
1598 // enforce linear numeric ordering on other arrays here.
1599 $value = array_values( $value )[0];
1600 $list .= $field . " = " . $this->addQuotes( $value );
1601 } else {
1602 $list .= $field . " IN (" . $this->makeList( $value ) . ") ";
1604 // if null present in array, append IS NULL
1605 if ( $includeNull ) {
1606 $list .= " OR $field IS NULL)";
1609 } elseif ( $value === null ) {
1610 if ( $mode == self::LIST_AND || $mode == self::LIST_OR ) {
1611 $list .= "$field IS ";
1612 } elseif ( $mode == self::LIST_SET ) {
1613 $list .= "$field = ";
1615 $list .= 'NULL';
1616 } else {
1617 if (
1618 $mode == self::LIST_AND || $mode == self::LIST_OR || $mode == self::LIST_SET
1620 $list .= "$field = ";
1622 $list .= $mode == self::LIST_NAMES ? $value : $this->addQuotes( $value );
1626 return $list;
1629 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
1630 $conds = [];
1632 foreach ( $data as $base => $sub ) {
1633 if ( count( $sub ) ) {
1634 $conds[] = $this->makeList(
1635 [ $baseKey => $base, $subKey => array_keys( $sub ) ],
1636 self::LIST_AND );
1640 if ( $conds ) {
1641 return $this->makeList( $conds, self::LIST_OR );
1642 } else {
1643 // Nothing to search for...
1644 return false;
1648 public function aggregateValue( $valuedata, $valuename = 'value' ) {
1649 return $valuename;
1652 public function bitNot( $field ) {
1653 return "(~$field)";
1656 public function bitAnd( $fieldLeft, $fieldRight ) {
1657 return "($fieldLeft & $fieldRight)";
1660 public function bitOr( $fieldLeft, $fieldRight ) {
1661 return "($fieldLeft | $fieldRight)";
1664 public function buildConcat( $stringList ) {
1665 return 'CONCAT(' . implode( ',', $stringList ) . ')';
1668 public function buildGroupConcatField(
1669 $delim, $table, $field, $conds = '', $join_conds = []
1671 $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')';
1673 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1676 public function buildStringCast( $field ) {
1677 return $field;
1680 public function selectDB( $db ) {
1681 # Stub. Shouldn't cause serious problems if it's not overridden, but
1682 # if your database engine supports a concept similar to MySQL's
1683 # databases you may as well.
1684 $this->mDBname = $db;
1686 return true;
1689 public function getDBname() {
1690 return $this->mDBname;
1693 public function getServer() {
1694 return $this->mServer;
1697 public function tableName( $name, $format = 'quoted' ) {
1698 # Skip the entire process when we have a string quoted on both ends.
1699 # Note that we check the end so that we will still quote any use of
1700 # use of `database`.table. But won't break things if someone wants
1701 # to query a database table with a dot in the name.
1702 if ( $this->isQuotedIdentifier( $name ) ) {
1703 return $name;
1706 # Lets test for any bits of text that should never show up in a table
1707 # name. Basically anything like JOIN or ON which are actually part of
1708 # SQL queries, but may end up inside of the table value to combine
1709 # sql. Such as how the API is doing.
1710 # Note that we use a whitespace test rather than a \b test to avoid
1711 # any remote case where a word like on may be inside of a table name
1712 # surrounded by symbols which may be considered word breaks.
1713 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
1714 return $name;
1717 # Split database and table into proper variables.
1718 # We reverse the explode so that database.table and table both output
1719 # the correct table.
1720 $dbDetails = explode( '.', $name, 3 );
1721 if ( count( $dbDetails ) == 3 ) {
1722 list( $database, $schema, $table ) = $dbDetails;
1723 # We don't want any prefix added in this case
1724 $prefix = '';
1725 } elseif ( count( $dbDetails ) == 2 ) {
1726 list( $database, $table ) = $dbDetails;
1727 # We don't want any prefix added in this case
1728 $prefix = '';
1729 # In dbs that support it, $database may actually be the schema
1730 # but that doesn't affect any of the functionality here
1731 $schema = '';
1732 } else {
1733 list( $table ) = $dbDetails;
1734 if ( isset( $this->tableAliases[$table] ) ) {
1735 $database = $this->tableAliases[$table]['dbname'];
1736 $schema = is_string( $this->tableAliases[$table]['schema'] )
1737 ? $this->tableAliases[$table]['schema']
1738 : $this->mSchema;
1739 $prefix = is_string( $this->tableAliases[$table]['prefix'] )
1740 ? $this->tableAliases[$table]['prefix']
1741 : $this->mTablePrefix;
1742 } else {
1743 $database = '';
1744 $schema = $this->mSchema; # Default schema
1745 $prefix = $this->mTablePrefix; # Default prefix
1749 # Quote $table and apply the prefix if not quoted.
1750 # $tableName might be empty if this is called from Database::replaceVars()
1751 $tableName = "{$prefix}{$table}";
1752 if ( $format === 'quoted'
1753 && !$this->isQuotedIdentifier( $tableName )
1754 && $tableName !== ''
1756 $tableName = $this->addIdentifierQuotes( $tableName );
1759 # Quote $schema and $database and merge them with the table name if needed
1760 $tableName = $this->prependDatabaseOrSchema( $schema, $tableName, $format );
1761 $tableName = $this->prependDatabaseOrSchema( $database, $tableName, $format );
1763 return $tableName;
1767 * @param string|null $namespace Database or schema
1768 * @param string $relation Name of table, view, sequence, etc...
1769 * @param string $format One of (raw, quoted)
1770 * @return string Relation name with quoted and merged $namespace as needed
1772 private function prependDatabaseOrSchema( $namespace, $relation, $format ) {
1773 if ( strlen( $namespace ) ) {
1774 if ( $format === 'quoted' && !$this->isQuotedIdentifier( $namespace ) ) {
1775 $namespace = $this->addIdentifierQuotes( $namespace );
1777 $relation = $namespace . '.' . $relation;
1780 return $relation;
1783 public function tableNames() {
1784 $inArray = func_get_args();
1785 $retVal = [];
1787 foreach ( $inArray as $name ) {
1788 $retVal[$name] = $this->tableName( $name );
1791 return $retVal;
1794 public function tableNamesN() {
1795 $inArray = func_get_args();
1796 $retVal = [];
1798 foreach ( $inArray as $name ) {
1799 $retVal[] = $this->tableName( $name );
1802 return $retVal;
1806 * Get an aliased table name
1807 * e.g. tableName AS newTableName
1809 * @param string $name Table name, see tableName()
1810 * @param string|bool $alias Alias (optional)
1811 * @return string SQL name for aliased table. Will not alias a table to its own name
1813 protected function tableNameWithAlias( $name, $alias = false ) {
1814 if ( !$alias || $alias == $name ) {
1815 return $this->tableName( $name );
1816 } else {
1817 return $this->tableName( $name ) . ' ' . $this->addIdentifierQuotes( $alias );
1822 * Gets an array of aliased table names
1824 * @param array $tables [ [alias] => table ]
1825 * @return string[] See tableNameWithAlias()
1827 protected function tableNamesWithAlias( $tables ) {
1828 $retval = [];
1829 foreach ( $tables as $alias => $table ) {
1830 if ( is_numeric( $alias ) ) {
1831 $alias = $table;
1833 $retval[] = $this->tableNameWithAlias( $table, $alias );
1836 return $retval;
1840 * Get an aliased field name
1841 * e.g. fieldName AS newFieldName
1843 * @param string $name Field name
1844 * @param string|bool $alias Alias (optional)
1845 * @return string SQL name for aliased field. Will not alias a field to its own name
1847 protected function fieldNameWithAlias( $name, $alias = false ) {
1848 if ( !$alias || (string)$alias === (string)$name ) {
1849 return $name;
1850 } else {
1851 return $name . ' AS ' . $this->addIdentifierQuotes( $alias ); // PostgreSQL needs AS
1856 * Gets an array of aliased field names
1858 * @param array $fields [ [alias] => field ]
1859 * @return string[] See fieldNameWithAlias()
1861 protected function fieldNamesWithAlias( $fields ) {
1862 $retval = [];
1863 foreach ( $fields as $alias => $field ) {
1864 if ( is_numeric( $alias ) ) {
1865 $alias = $field;
1867 $retval[] = $this->fieldNameWithAlias( $field, $alias );
1870 return $retval;
1874 * Get the aliased table name clause for a FROM clause
1875 * which might have a JOIN and/or USE INDEX or IGNORE INDEX clause
1877 * @param array $tables ( [alias] => table )
1878 * @param array $use_index Same as for select()
1879 * @param array $ignore_index Same as for select()
1880 * @param array $join_conds Same as for select()
1881 * @return string
1883 protected function tableNamesWithIndexClauseOrJOIN(
1884 $tables, $use_index = [], $ignore_index = [], $join_conds = []
1886 $ret = [];
1887 $retJOIN = [];
1888 $use_index = (array)$use_index;
1889 $ignore_index = (array)$ignore_index;
1890 $join_conds = (array)$join_conds;
1892 foreach ( $tables as $alias => $table ) {
1893 if ( !is_string( $alias ) ) {
1894 // No alias? Set it equal to the table name
1895 $alias = $table;
1897 // Is there a JOIN clause for this table?
1898 if ( isset( $join_conds[$alias] ) ) {
1899 list( $joinType, $conds ) = $join_conds[$alias];
1900 $tableClause = $joinType;
1901 $tableClause .= ' ' . $this->tableNameWithAlias( $table, $alias );
1902 if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
1903 $use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
1904 if ( $use != '' ) {
1905 $tableClause .= ' ' . $use;
1908 if ( isset( $ignore_index[$alias] ) ) { // has IGNORE INDEX?
1909 $ignore = $this->ignoreIndexClause(
1910 implode( ',', (array)$ignore_index[$alias] ) );
1911 if ( $ignore != '' ) {
1912 $tableClause .= ' ' . $ignore;
1915 $on = $this->makeList( (array)$conds, self::LIST_AND );
1916 if ( $on != '' ) {
1917 $tableClause .= ' ON (' . $on . ')';
1920 $retJOIN[] = $tableClause;
1921 } elseif ( isset( $use_index[$alias] ) ) {
1922 // Is there an INDEX clause for this table?
1923 $tableClause = $this->tableNameWithAlias( $table, $alias );
1924 $tableClause .= ' ' . $this->useIndexClause(
1925 implode( ',', (array)$use_index[$alias] )
1928 $ret[] = $tableClause;
1929 } elseif ( isset( $ignore_index[$alias] ) ) {
1930 // Is there an INDEX clause for this table?
1931 $tableClause = $this->tableNameWithAlias( $table, $alias );
1932 $tableClause .= ' ' . $this->ignoreIndexClause(
1933 implode( ',', (array)$ignore_index[$alias] )
1936 $ret[] = $tableClause;
1937 } else {
1938 $tableClause = $this->tableNameWithAlias( $table, $alias );
1940 $ret[] = $tableClause;
1944 // We can't separate explicit JOIN clauses with ',', use ' ' for those
1945 $implicitJoins = !empty( $ret ) ? implode( ',', $ret ) : "";
1946 $explicitJoins = !empty( $retJOIN ) ? implode( ' ', $retJOIN ) : "";
1948 // Compile our final table clause
1949 return implode( ' ', [ $implicitJoins, $explicitJoins ] );
1953 * Get the name of an index in a given table.
1955 * @param string $index
1956 * @return string
1958 protected function indexName( $index ) {
1959 return $index;
1962 public function addQuotes( $s ) {
1963 if ( $s instanceof Blob ) {
1964 $s = $s->fetch();
1966 if ( $s === null ) {
1967 return 'NULL';
1968 } elseif ( is_bool( $s ) ) {
1969 return (int)$s;
1970 } else {
1971 # This will also quote numeric values. This should be harmless,
1972 # and protects against weird problems that occur when they really
1973 # _are_ strings such as article titles and string->number->string
1974 # conversion is not 1:1.
1975 return "'" . $this->strencode( $s ) . "'";
1980 * Quotes an identifier using `backticks` or "double quotes" depending on the database type.
1981 * MySQL uses `backticks` while basically everything else uses double quotes.
1982 * Since MySQL is the odd one out here the double quotes are our generic
1983 * and we implement backticks in DatabaseMysql.
1985 * @param string $s
1986 * @return string
1988 public function addIdentifierQuotes( $s ) {
1989 return '"' . str_replace( '"', '""', $s ) . '"';
1993 * Returns if the given identifier looks quoted or not according to
1994 * the database convention for quoting identifiers .
1996 * @note Do not use this to determine if untrusted input is safe.
1997 * A malicious user can trick this function.
1998 * @param string $name
1999 * @return bool
2001 public function isQuotedIdentifier( $name ) {
2002 return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
2006 * @param string $s
2007 * @return string
2009 protected function escapeLikeInternal( $s ) {
2010 return addcslashes( $s, '\%_' );
2013 public function buildLike() {
2014 $params = func_get_args();
2016 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
2017 $params = $params[0];
2020 $s = '';
2022 foreach ( $params as $value ) {
2023 if ( $value instanceof LikeMatch ) {
2024 $s .= $value->toString();
2025 } else {
2026 $s .= $this->escapeLikeInternal( $value );
2030 return " LIKE {$this->addQuotes( $s )} ";
2033 public function anyChar() {
2034 return new LikeMatch( '_' );
2037 public function anyString() {
2038 return new LikeMatch( '%' );
2041 public function nextSequenceValue( $seqName ) {
2042 return null;
2046 * USE INDEX clause. Unlikely to be useful for anything but MySQL. This
2047 * is only needed because a) MySQL must be as efficient as possible due to
2048 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2049 * which index to pick. Anyway, other databases might have different
2050 * indexes on a given table. So don't bother overriding this unless you're
2051 * MySQL.
2052 * @param string $index
2053 * @return string
2055 public function useIndexClause( $index ) {
2056 return '';
2060 * IGNORE INDEX clause. Unlikely to be useful for anything but MySQL. This
2061 * is only needed because a) MySQL must be as efficient as possible due to
2062 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2063 * which index to pick. Anyway, other databases might have different
2064 * indexes on a given table. So don't bother overriding this unless you're
2065 * MySQL.
2066 * @param string $index
2067 * @return string
2069 public function ignoreIndexClause( $index ) {
2070 return '';
2073 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
2074 $quotedTable = $this->tableName( $table );
2076 if ( count( $rows ) == 0 ) {
2077 return;
2080 # Single row case
2081 if ( !is_array( reset( $rows ) ) ) {
2082 $rows = [ $rows ];
2085 // @FXIME: this is not atomic, but a trx would break affectedRows()
2086 foreach ( $rows as $row ) {
2087 # Delete rows which collide
2088 if ( $uniqueIndexes ) {
2089 $sql = "DELETE FROM $quotedTable WHERE ";
2090 $first = true;
2091 foreach ( $uniqueIndexes as $index ) {
2092 if ( $first ) {
2093 $first = false;
2094 $sql .= '( ';
2095 } else {
2096 $sql .= ' ) OR ( ';
2098 if ( is_array( $index ) ) {
2099 $first2 = true;
2100 foreach ( $index as $col ) {
2101 if ( $first2 ) {
2102 $first2 = false;
2103 } else {
2104 $sql .= ' AND ';
2106 $sql .= $col . '=' . $this->addQuotes( $row[$col] );
2108 } else {
2109 $sql .= $index . '=' . $this->addQuotes( $row[$index] );
2112 $sql .= ' )';
2113 $this->query( $sql, $fname );
2116 # Now insert the row
2117 $this->insert( $table, $row, $fname );
2122 * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
2123 * statement.
2125 * @param string $table Table name
2126 * @param array|string $rows Row(s) to insert
2127 * @param string $fname Caller function name
2129 * @return ResultWrapper
2131 protected function nativeReplace( $table, $rows, $fname ) {
2132 $table = $this->tableName( $table );
2134 # Single row case
2135 if ( !is_array( reset( $rows ) ) ) {
2136 $rows = [ $rows ];
2139 $sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) . ') VALUES ';
2140 $first = true;
2142 foreach ( $rows as $row ) {
2143 if ( $first ) {
2144 $first = false;
2145 } else {
2146 $sql .= ',';
2149 $sql .= '(' . $this->makeList( $row ) . ')';
2152 return $this->query( $sql, $fname );
2155 public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
2156 $fname = __METHOD__
2158 if ( !count( $rows ) ) {
2159 return true; // nothing to do
2162 if ( !is_array( reset( $rows ) ) ) {
2163 $rows = [ $rows ];
2166 if ( count( $uniqueIndexes ) ) {
2167 $clauses = []; // list WHERE clauses that each identify a single row
2168 foreach ( $rows as $row ) {
2169 foreach ( $uniqueIndexes as $index ) {
2170 $index = is_array( $index ) ? $index : [ $index ]; // columns
2171 $rowKey = []; // unique key to this row
2172 foreach ( $index as $column ) {
2173 $rowKey[$column] = $row[$column];
2175 $clauses[] = $this->makeList( $rowKey, self::LIST_AND );
2178 $where = [ $this->makeList( $clauses, self::LIST_OR ) ];
2179 } else {
2180 $where = false;
2183 $useTrx = !$this->mTrxLevel;
2184 if ( $useTrx ) {
2185 $this->begin( $fname, self::TRANSACTION_INTERNAL );
2187 try {
2188 # Update any existing conflicting row(s)
2189 if ( $where !== false ) {
2190 $ok = $this->update( $table, $set, $where, $fname );
2191 } else {
2192 $ok = true;
2194 # Now insert any non-conflicting row(s)
2195 $ok = $this->insert( $table, $rows, $fname, [ 'IGNORE' ] ) && $ok;
2196 } catch ( Exception $e ) {
2197 if ( $useTrx ) {
2198 $this->rollback( $fname, self::FLUSHING_INTERNAL );
2200 throw $e;
2202 if ( $useTrx ) {
2203 $this->commit( $fname, self::FLUSHING_INTERNAL );
2206 return $ok;
2209 public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
2210 $fname = __METHOD__
2212 if ( !$conds ) {
2213 throw new DBUnexpectedError( $this, __METHOD__ . ' called with empty $conds' );
2216 $delTable = $this->tableName( $delTable );
2217 $joinTable = $this->tableName( $joinTable );
2218 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
2219 if ( $conds != '*' ) {
2220 $sql .= 'WHERE ' . $this->makeList( $conds, self::LIST_AND );
2222 $sql .= ')';
2224 $this->query( $sql, $fname );
2227 public function textFieldSize( $table, $field ) {
2228 $table = $this->tableName( $table );
2229 $sql = "SHOW COLUMNS FROM $table LIKE \"$field\";";
2230 $res = $this->query( $sql, __METHOD__ );
2231 $row = $this->fetchObject( $res );
2233 $m = [];
2235 if ( preg_match( '/\((.*)\)/', $row->Type, $m ) ) {
2236 $size = $m[1];
2237 } else {
2238 $size = -1;
2241 return $size;
2244 public function delete( $table, $conds, $fname = __METHOD__ ) {
2245 if ( !$conds ) {
2246 throw new DBUnexpectedError( $this, __METHOD__ . ' called with no conditions' );
2249 $table = $this->tableName( $table );
2250 $sql = "DELETE FROM $table";
2252 if ( $conds != '*' ) {
2253 if ( is_array( $conds ) ) {
2254 $conds = $this->makeList( $conds, self::LIST_AND );
2256 $sql .= ' WHERE ' . $conds;
2259 return $this->query( $sql, $fname );
2262 public function insertSelect(
2263 $destTable, $srcTable, $varMap, $conds,
2264 $fname = __METHOD__, $insertOptions = [], $selectOptions = []
2266 if ( $this->cliMode ) {
2267 // For massive migrations with downtime, we don't want to select everything
2268 // into memory and OOM, so do all this native on the server side if possible.
2269 return $this->nativeInsertSelect(
2270 $destTable,
2271 $srcTable,
2272 $varMap,
2273 $conds,
2274 $fname,
2275 $insertOptions,
2276 $selectOptions
2280 // For web requests, do a locking SELECT and then INSERT. This puts the SELECT burden
2281 // on only the master (without needing row-based-replication). It also makes it easy to
2282 // know how big the INSERT is going to be.
2283 $fields = [];
2284 foreach ( $varMap as $dstColumn => $sourceColumnOrSql ) {
2285 $fields[] = $this->fieldNameWithAlias( $sourceColumnOrSql, $dstColumn );
2287 $selectOptions[] = 'FOR UPDATE';
2288 $res = $this->select( $srcTable, implode( ',', $fields ), $conds, $fname, $selectOptions );
2289 if ( !$res ) {
2290 return false;
2293 $rows = [];
2294 foreach ( $res as $row ) {
2295 $rows[] = (array)$row;
2298 return $this->insert( $destTable, $rows, $fname, $insertOptions );
2301 protected function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds,
2302 $fname = __METHOD__,
2303 $insertOptions = [], $selectOptions = []
2305 $destTable = $this->tableName( $destTable );
2307 if ( !is_array( $insertOptions ) ) {
2308 $insertOptions = [ $insertOptions ];
2311 $insertOptions = $this->makeInsertOptions( $insertOptions );
2313 if ( !is_array( $selectOptions ) ) {
2314 $selectOptions = [ $selectOptions ];
2317 list( $startOpts, $useIndex, $tailOpts, $ignoreIndex ) = $this->makeSelectOptions(
2318 $selectOptions );
2320 if ( is_array( $srcTable ) ) {
2321 $srcTable = implode( ',', array_map( [ $this, 'tableName' ], $srcTable ) );
2322 } else {
2323 $srcTable = $this->tableName( $srcTable );
2326 $sql = "INSERT $insertOptions" .
2327 " INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
2328 " SELECT $startOpts " . implode( ',', $varMap ) .
2329 " FROM $srcTable $useIndex $ignoreIndex ";
2331 if ( $conds != '*' ) {
2332 if ( is_array( $conds ) ) {
2333 $conds = $this->makeList( $conds, self::LIST_AND );
2335 $sql .= " WHERE $conds";
2338 $sql .= " $tailOpts";
2340 return $this->query( $sql, $fname );
2344 * Construct a LIMIT query with optional offset. This is used for query
2345 * pages. The SQL should be adjusted so that only the first $limit rows
2346 * are returned. If $offset is provided as well, then the first $offset
2347 * rows should be discarded, and the next $limit rows should be returned.
2348 * If the result of the query is not ordered, then the rows to be returned
2349 * are theoretically arbitrary.
2351 * $sql is expected to be a SELECT, if that makes a difference.
2353 * The version provided by default works in MySQL and SQLite. It will very
2354 * likely need to be overridden for most other DBMSes.
2356 * @param string $sql SQL query we will append the limit too
2357 * @param int $limit The SQL limit
2358 * @param int|bool $offset The SQL offset (default false)
2359 * @throws DBUnexpectedError
2360 * @return string
2362 public function limitResult( $sql, $limit, $offset = false ) {
2363 if ( !is_numeric( $limit ) ) {
2364 throw new DBUnexpectedError( $this,
2365 "Invalid non-numeric limit passed to limitResult()\n" );
2368 return "$sql LIMIT "
2369 . ( ( is_numeric( $offset ) && $offset != 0 ) ? "{$offset}," : "" )
2370 . "{$limit} ";
2373 public function unionSupportsOrderAndLimit() {
2374 return true; // True for almost every DB supported
2377 public function unionQueries( $sqls, $all ) {
2378 $glue = $all ? ') UNION ALL (' : ') UNION (';
2380 return '(' . implode( $glue, $sqls ) . ')';
2383 public function conditional( $cond, $trueVal, $falseVal ) {
2384 if ( is_array( $cond ) ) {
2385 $cond = $this->makeList( $cond, self::LIST_AND );
2388 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
2391 public function strreplace( $orig, $old, $new ) {
2392 return "REPLACE({$orig}, {$old}, {$new})";
2395 public function getServerUptime() {
2396 return 0;
2399 public function wasDeadlock() {
2400 return false;
2403 public function wasLockTimeout() {
2404 return false;
2407 public function wasErrorReissuable() {
2408 return false;
2411 public function wasReadOnlyError() {
2412 return false;
2416 * Do not use this method outside of Database/DBError classes
2418 * @param integer|string $errno
2419 * @return bool Whether the given query error was a connection drop
2421 public function wasConnectionError( $errno ) {
2422 return false;
2425 public function deadlockLoop() {
2426 $args = func_get_args();
2427 $function = array_shift( $args );
2428 $tries = self::DEADLOCK_TRIES;
2430 $this->begin( __METHOD__ );
2432 $retVal = null;
2433 /** @var Exception $e */
2434 $e = null;
2435 do {
2436 try {
2437 $retVal = call_user_func_array( $function, $args );
2438 break;
2439 } catch ( DBQueryError $e ) {
2440 if ( $this->wasDeadlock() ) {
2441 // Retry after a randomized delay
2442 usleep( mt_rand( self::DEADLOCK_DELAY_MIN, self::DEADLOCK_DELAY_MAX ) );
2443 } else {
2444 // Throw the error back up
2445 throw $e;
2448 } while ( --$tries > 0 );
2450 if ( $tries <= 0 ) {
2451 // Too many deadlocks; give up
2452 $this->rollback( __METHOD__ );
2453 throw $e;
2454 } else {
2455 $this->commit( __METHOD__ );
2457 return $retVal;
2461 public function masterPosWait( DBMasterPos $pos, $timeout ) {
2462 # Real waits are implemented in the subclass.
2463 return 0;
2466 public function getReplicaPos() {
2467 # Stub
2468 return false;
2471 public function getMasterPos() {
2472 # Stub
2473 return false;
2476 public function serverIsReadOnly() {
2477 return false;
2480 final public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
2481 if ( !$this->mTrxLevel ) {
2482 throw new DBUnexpectedError( $this, "No transaction is active." );
2484 $this->mTrxEndCallbacks[] = [ $callback, $fname ];
2487 final public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
2488 $this->mTrxIdleCallbacks[] = [ $callback, $fname ];
2489 if ( !$this->mTrxLevel ) {
2490 $this->runOnTransactionIdleCallbacks( self::TRIGGER_IDLE );
2494 final public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
2495 if ( $this->mTrxLevel ) {
2496 $this->mTrxPreCommitCallbacks[] = [ $callback, $fname ];
2497 } else {
2498 // If no transaction is active, then make one for this callback
2499 $this->startAtomic( __METHOD__ );
2500 try {
2501 call_user_func( $callback );
2502 $this->endAtomic( __METHOD__ );
2503 } catch ( Exception $e ) {
2504 $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
2505 throw $e;
2510 final public function setTransactionListener( $name, callable $callback = null ) {
2511 if ( $callback ) {
2512 $this->mTrxRecurringCallbacks[$name] = $callback;
2513 } else {
2514 unset( $this->mTrxRecurringCallbacks[$name] );
2519 * Whether to disable running of post-COMMIT/ROLLBACK callbacks
2521 * This method should not be used outside of Database/LoadBalancer
2523 * @param bool $suppress
2524 * @since 1.28
2526 final public function setTrxEndCallbackSuppression( $suppress ) {
2527 $this->mTrxEndCallbacksSuppressed = $suppress;
2531 * Actually run and consume any "on transaction idle/resolution" callbacks.
2533 * This method should not be used outside of Database/LoadBalancer
2535 * @param integer $trigger IDatabase::TRIGGER_* constant
2536 * @since 1.20
2537 * @throws Exception
2539 public function runOnTransactionIdleCallbacks( $trigger ) {
2540 if ( $this->mTrxEndCallbacksSuppressed ) {
2541 return;
2544 $autoTrx = $this->getFlag( self::DBO_TRX ); // automatic begin() enabled?
2545 /** @var Exception $e */
2546 $e = null; // first exception
2547 do { // callbacks may add callbacks :)
2548 $callbacks = array_merge(
2549 $this->mTrxIdleCallbacks,
2550 $this->mTrxEndCallbacks // include "transaction resolution" callbacks
2552 $this->mTrxIdleCallbacks = []; // consumed (and recursion guard)
2553 $this->mTrxEndCallbacks = []; // consumed (recursion guard)
2554 foreach ( $callbacks as $callback ) {
2555 try {
2556 list( $phpCallback ) = $callback;
2557 $this->clearFlag( self::DBO_TRX ); // make each query its own transaction
2558 call_user_func_array( $phpCallback, [ $trigger ] );
2559 if ( $autoTrx ) {
2560 $this->setFlag( self::DBO_TRX ); // restore automatic begin()
2561 } else {
2562 $this->clearFlag( self::DBO_TRX ); // restore auto-commit
2564 } catch ( Exception $ex ) {
2565 call_user_func( $this->errorLogger, $ex );
2566 $e = $e ?: $ex;
2567 // Some callbacks may use startAtomic/endAtomic, so make sure
2568 // their transactions are ended so other callbacks don't fail
2569 if ( $this->trxLevel() ) {
2570 $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
2574 } while ( count( $this->mTrxIdleCallbacks ) );
2576 if ( $e instanceof Exception ) {
2577 throw $e; // re-throw any first exception
2582 * Actually run and consume any "on transaction pre-commit" callbacks.
2584 * This method should not be used outside of Database/LoadBalancer
2586 * @since 1.22
2587 * @throws Exception
2589 public function runOnTransactionPreCommitCallbacks() {
2590 $e = null; // first exception
2591 do { // callbacks may add callbacks :)
2592 $callbacks = $this->mTrxPreCommitCallbacks;
2593 $this->mTrxPreCommitCallbacks = []; // consumed (and recursion guard)
2594 foreach ( $callbacks as $callback ) {
2595 try {
2596 list( $phpCallback ) = $callback;
2597 call_user_func( $phpCallback );
2598 } catch ( Exception $ex ) {
2599 call_user_func( $this->errorLogger, $ex );
2600 $e = $e ?: $ex;
2603 } while ( count( $this->mTrxPreCommitCallbacks ) );
2605 if ( $e instanceof Exception ) {
2606 throw $e; // re-throw any first exception
2611 * Actually run any "transaction listener" callbacks.
2613 * This method should not be used outside of Database/LoadBalancer
2615 * @param integer $trigger IDatabase::TRIGGER_* constant
2616 * @throws Exception
2617 * @since 1.20
2619 public function runTransactionListenerCallbacks( $trigger ) {
2620 if ( $this->mTrxEndCallbacksSuppressed ) {
2621 return;
2624 /** @var Exception $e */
2625 $e = null; // first exception
2627 foreach ( $this->mTrxRecurringCallbacks as $phpCallback ) {
2628 try {
2629 $phpCallback( $trigger, $this );
2630 } catch ( Exception $ex ) {
2631 call_user_func( $this->errorLogger, $ex );
2632 $e = $e ?: $ex;
2636 if ( $e instanceof Exception ) {
2637 throw $e; // re-throw any first exception
2641 final public function startAtomic( $fname = __METHOD__ ) {
2642 if ( !$this->mTrxLevel ) {
2643 $this->begin( $fname, self::TRANSACTION_INTERNAL );
2644 // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
2645 // in all changes being in one transaction to keep requests transactional.
2646 if ( !$this->getFlag( self::DBO_TRX ) ) {
2647 $this->mTrxAutomaticAtomic = true;
2651 $this->mTrxAtomicLevels[] = $fname;
2654 final public function endAtomic( $fname = __METHOD__ ) {
2655 if ( !$this->mTrxLevel ) {
2656 throw new DBUnexpectedError( $this, "No atomic transaction is open (got $fname)." );
2658 if ( !$this->mTrxAtomicLevels ||
2659 array_pop( $this->mTrxAtomicLevels ) !== $fname
2661 throw new DBUnexpectedError( $this, "Invalid atomic section ended (got $fname)." );
2664 if ( !$this->mTrxAtomicLevels && $this->mTrxAutomaticAtomic ) {
2665 $this->commit( $fname, self::FLUSHING_INTERNAL );
2669 final public function doAtomicSection( $fname, callable $callback ) {
2670 $this->startAtomic( $fname );
2671 try {
2672 $res = call_user_func_array( $callback, [ $this, $fname ] );
2673 } catch ( Exception $e ) {
2674 $this->rollback( $fname, self::FLUSHING_INTERNAL );
2675 throw $e;
2677 $this->endAtomic( $fname );
2679 return $res;
2682 final public function begin( $fname = __METHOD__, $mode = self::TRANSACTION_EXPLICIT ) {
2683 // Protect against mismatched atomic section, transaction nesting, and snapshot loss
2684 if ( $this->mTrxLevel ) {
2685 if ( $this->mTrxAtomicLevels ) {
2686 $levels = implode( ', ', $this->mTrxAtomicLevels );
2687 $msg = "$fname: Got explicit BEGIN while atomic section(s) $levels are open.";
2688 throw new DBUnexpectedError( $this, $msg );
2689 } elseif ( !$this->mTrxAutomatic ) {
2690 $msg = "$fname: Explicit transaction already active (from {$this->mTrxFname}).";
2691 throw new DBUnexpectedError( $this, $msg );
2692 } else {
2693 // @TODO: make this an exception at some point
2694 $msg = "$fname: Implicit transaction already active (from {$this->mTrxFname}).";
2695 $this->queryLogger->error( $msg );
2696 return; // join the main transaction set
2698 } elseif ( $this->getFlag( self::DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) {
2699 // @TODO: make this an exception at some point
2700 $msg = "$fname: Implicit transaction expected (DBO_TRX set).";
2701 $this->queryLogger->error( $msg );
2702 return; // let any writes be in the main transaction
2705 // Avoid fatals if close() was called
2706 $this->assertOpen();
2708 $this->doBegin( $fname );
2709 $this->mTrxTimestamp = microtime( true );
2710 $this->mTrxFname = $fname;
2711 $this->mTrxDoneWrites = false;
2712 $this->mTrxAutomaticAtomic = false;
2713 $this->mTrxAtomicLevels = [];
2714 $this->mTrxShortId = sprintf( '%06x', mt_rand( 0, 0xffffff ) );
2715 $this->mTrxWriteDuration = 0.0;
2716 $this->mTrxWriteQueryCount = 0;
2717 $this->mTrxWriteAdjDuration = 0.0;
2718 $this->mTrxWriteAdjQueryCount = 0;
2719 $this->mTrxWriteCallers = [];
2720 // First SELECT after BEGIN will establish the snapshot in REPEATABLE-READ.
2721 // Get an estimate of the replica DB lag before then, treating estimate staleness
2722 // as lag itself just to be safe
2723 $status = $this->getApproximateLagStatus();
2724 $this->mTrxReplicaLag = $status['lag'] + ( microtime( true ) - $status['since'] );
2725 // T147697: make explicitTrxActive() return true until begin() finishes. This way, no
2726 // caller will think its OK to muck around with the transaction just because startAtomic()
2727 // has not yet completed (e.g. setting mTrxAtomicLevels).
2728 $this->mTrxAutomatic = ( $mode === self::TRANSACTION_INTERNAL );
2732 * Issues the BEGIN command to the database server.
2734 * @see Database::begin()
2735 * @param string $fname
2737 protected function doBegin( $fname ) {
2738 $this->query( 'BEGIN', $fname );
2739 $this->mTrxLevel = 1;
2742 final public function commit( $fname = __METHOD__, $flush = '' ) {
2743 if ( $this->mTrxLevel && $this->mTrxAtomicLevels ) {
2744 // There are still atomic sections open. This cannot be ignored
2745 $levels = implode( ', ', $this->mTrxAtomicLevels );
2746 throw new DBUnexpectedError(
2747 $this,
2748 "$fname: Got COMMIT while atomic sections $levels are still open."
2752 if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
2753 if ( !$this->mTrxLevel ) {
2754 return; // nothing to do
2755 } elseif ( !$this->mTrxAutomatic ) {
2756 throw new DBUnexpectedError(
2757 $this,
2758 "$fname: Flushing an explicit transaction, getting out of sync."
2761 } else {
2762 if ( !$this->mTrxLevel ) {
2763 $this->queryLogger->error(
2764 "$fname: No transaction to commit, something got out of sync." );
2765 return; // nothing to do
2766 } elseif ( $this->mTrxAutomatic ) {
2767 // @TODO: make this an exception at some point
2768 $msg = "$fname: Explicit commit of implicit transaction.";
2769 $this->queryLogger->error( $msg );
2770 return; // wait for the main transaction set commit round
2774 // Avoid fatals if close() was called
2775 $this->assertOpen();
2777 $this->runOnTransactionPreCommitCallbacks();
2778 $writeTime = $this->pendingWriteQueryDuration( self::ESTIMATE_DB_APPLY );
2779 $this->doCommit( $fname );
2780 if ( $this->mTrxDoneWrites ) {
2781 $this->mLastWriteTime = microtime( true );
2782 $this->trxProfiler->transactionWritingOut(
2783 $this->mServer, $this->mDBname, $this->mTrxShortId, $writeTime );
2786 $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
2787 $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
2791 * Issues the COMMIT command to the database server.
2793 * @see Database::commit()
2794 * @param string $fname
2796 protected function doCommit( $fname ) {
2797 if ( $this->mTrxLevel ) {
2798 $this->query( 'COMMIT', $fname );
2799 $this->mTrxLevel = 0;
2803 final public function rollback( $fname = __METHOD__, $flush = '' ) {
2804 if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
2805 if ( !$this->mTrxLevel ) {
2806 return; // nothing to do
2808 } else {
2809 if ( !$this->mTrxLevel ) {
2810 $this->queryLogger->error(
2811 "$fname: No transaction to rollback, something got out of sync." );
2812 return; // nothing to do
2813 } elseif ( $this->getFlag( self::DBO_TRX ) ) {
2814 throw new DBUnexpectedError(
2815 $this,
2816 "$fname: Expected mass rollback of all peer databases (DBO_TRX set)."
2821 // Avoid fatals if close() was called
2822 $this->assertOpen();
2824 $this->doRollback( $fname );
2825 $this->mTrxAtomicLevels = [];
2826 if ( $this->mTrxDoneWrites ) {
2827 $this->trxProfiler->transactionWritingOut(
2828 $this->mServer, $this->mDBname, $this->mTrxShortId );
2831 $this->mTrxIdleCallbacks = []; // clear
2832 $this->mTrxPreCommitCallbacks = []; // clear
2833 $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
2834 $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
2838 * Issues the ROLLBACK command to the database server.
2840 * @see Database::rollback()
2841 * @param string $fname
2843 protected function doRollback( $fname ) {
2844 if ( $this->mTrxLevel ) {
2845 # Disconnects cause rollback anyway, so ignore those errors
2846 $ignoreErrors = true;
2847 $this->query( 'ROLLBACK', $fname, $ignoreErrors );
2848 $this->mTrxLevel = 0;
2852 public function flushSnapshot( $fname = __METHOD__ ) {
2853 if ( $this->writesOrCallbacksPending() || $this->explicitTrxActive() ) {
2854 // This only flushes transactions to clear snapshots, not to write data
2855 $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
2856 throw new DBUnexpectedError(
2857 $this,
2858 "$fname: Cannot flush snapshot because writes are pending ($fnames)."
2862 $this->commit( $fname, self::FLUSHING_INTERNAL );
2865 public function explicitTrxActive() {
2866 return $this->mTrxLevel && ( $this->mTrxAtomicLevels || !$this->mTrxAutomatic );
2869 public function duplicateTableStructure(
2870 $oldName, $newName, $temporary = false, $fname = __METHOD__
2872 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2875 public function listTables( $prefix = null, $fname = __METHOD__ ) {
2876 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2879 public function listViews( $prefix = null, $fname = __METHOD__ ) {
2880 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2883 public function timestamp( $ts = 0 ) {
2884 $t = new ConvertibleTimestamp( $ts );
2885 // Let errors bubble up to avoid putting garbage in the DB
2886 return $t->getTimestamp( TS_MW );
2889 public function timestampOrNull( $ts = null ) {
2890 if ( is_null( $ts ) ) {
2891 return null;
2892 } else {
2893 return $this->timestamp( $ts );
2898 * Take the result from a query, and wrap it in a ResultWrapper if
2899 * necessary. Boolean values are passed through as is, to indicate success
2900 * of write queries or failure.
2902 * Once upon a time, Database::query() returned a bare MySQL result
2903 * resource, and it was necessary to call this function to convert it to
2904 * a wrapper. Nowadays, raw database objects are never exposed to external
2905 * callers, so this is unnecessary in external code.
2907 * @param bool|ResultWrapper|resource|object $result
2908 * @return bool|ResultWrapper
2910 protected function resultObject( $result ) {
2911 if ( !$result ) {
2912 return false;
2913 } elseif ( $result instanceof ResultWrapper ) {
2914 return $result;
2915 } elseif ( $result === true ) {
2916 // Successful write query
2917 return $result;
2918 } else {
2919 return new ResultWrapper( $this, $result );
2923 public function ping( &$rtt = null ) {
2924 // Avoid hitting the server if it was hit recently
2925 if ( $this->isOpen() && ( microtime( true ) - $this->lastPing ) < self::PING_TTL ) {
2926 if ( !func_num_args() || $this->mRTTEstimate > 0 ) {
2927 $rtt = $this->mRTTEstimate;
2928 return true; // don't care about $rtt
2932 // This will reconnect if possible or return false if not
2933 $this->clearFlag( self::DBO_TRX, self::REMEMBER_PRIOR );
2934 $ok = ( $this->query( self::PING_QUERY, __METHOD__, true ) !== false );
2935 $this->restoreFlags( self::RESTORE_PRIOR );
2937 if ( $ok ) {
2938 $rtt = $this->mRTTEstimate;
2941 return $ok;
2945 * @return bool
2947 protected function reconnect() {
2948 $this->closeConnection();
2949 $this->mOpened = false;
2950 $this->mConn = false;
2951 try {
2952 $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
2953 $this->lastPing = microtime( true );
2954 $ok = true;
2955 } catch ( DBConnectionError $e ) {
2956 $ok = false;
2959 return $ok;
2962 public function getSessionLagStatus() {
2963 return $this->getTransactionLagStatus() ?: $this->getApproximateLagStatus();
2967 * Get the replica DB lag when the current transaction started
2969 * This is useful when transactions might use snapshot isolation
2970 * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
2971 * is this lag plus transaction duration. If they don't, it is still
2972 * safe to be pessimistic. This returns null if there is no transaction.
2974 * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
2975 * @since 1.27
2977 protected function getTransactionLagStatus() {
2978 return $this->mTrxLevel
2979 ? [ 'lag' => $this->mTrxReplicaLag, 'since' => $this->trxTimestamp() ]
2980 : null;
2984 * Get a replica DB lag estimate for this server
2986 * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate)
2987 * @since 1.27
2989 protected function getApproximateLagStatus() {
2990 return [
2991 'lag' => $this->getLBInfo( 'replica' ) ? $this->getLag() : 0,
2992 'since' => microtime( true )
2997 * Merge the result of getSessionLagStatus() for several DBs
2998 * using the most pessimistic values to estimate the lag of
2999 * any data derived from them in combination
3001 * This is information is useful for caching modules
3003 * @see WANObjectCache::set()
3004 * @see WANObjectCache::getWithSetCallback()
3006 * @param IDatabase $db1
3007 * @param IDatabase ...
3008 * @return array Map of values:
3009 * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped)
3010 * - since: oldest UNIX timestamp of any of the DB lag estimates
3011 * - pending: whether any of the DBs have uncommitted changes
3012 * @since 1.27
3014 public static function getCacheSetOptions( IDatabase $db1 ) {
3015 $res = [ 'lag' => 0, 'since' => INF, 'pending' => false ];
3016 foreach ( func_get_args() as $db ) {
3017 /** @var IDatabase $db */
3018 $status = $db->getSessionLagStatus();
3019 if ( $status['lag'] === false ) {
3020 $res['lag'] = false;
3021 } elseif ( $res['lag'] !== false ) {
3022 $res['lag'] = max( $res['lag'], $status['lag'] );
3024 $res['since'] = min( $res['since'], $status['since'] );
3025 $res['pending'] = $res['pending'] ?: $db->writesPending();
3028 return $res;
3031 public function getLag() {
3032 return 0;
3035 public function maxListLen() {
3036 return 0;
3039 public function encodeBlob( $b ) {
3040 return $b;
3043 public function decodeBlob( $b ) {
3044 if ( $b instanceof Blob ) {
3045 $b = $b->fetch();
3047 return $b;
3050 public function setSessionOptions( array $options ) {
3053 public function sourceFile(
3054 $filename,
3055 callable $lineCallback = null,
3056 callable $resultCallback = null,
3057 $fname = false,
3058 callable $inputCallback = null
3060 MediaWiki\suppressWarnings();
3061 $fp = fopen( $filename, 'r' );
3062 MediaWiki\restoreWarnings();
3064 if ( false === $fp ) {
3065 throw new RuntimeException( "Could not open \"{$filename}\".\n" );
3068 if ( !$fname ) {
3069 $fname = __METHOD__ . "( $filename )";
3072 try {
3073 $error = $this->sourceStream(
3074 $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
3075 } catch ( Exception $e ) {
3076 fclose( $fp );
3077 throw $e;
3080 fclose( $fp );
3082 return $error;
3085 public function setSchemaVars( $vars ) {
3086 $this->mSchemaVars = $vars;
3089 public function sourceStream(
3090 $fp,
3091 callable $lineCallback = null,
3092 callable $resultCallback = null,
3093 $fname = __METHOD__,
3094 callable $inputCallback = null
3096 $cmd = '';
3098 while ( !feof( $fp ) ) {
3099 if ( $lineCallback ) {
3100 call_user_func( $lineCallback );
3103 $line = trim( fgets( $fp ) );
3105 if ( $line == '' ) {
3106 continue;
3109 if ( '-' == $line[0] && '-' == $line[1] ) {
3110 continue;
3113 if ( $cmd != '' ) {
3114 $cmd .= ' ';
3117 $done = $this->streamStatementEnd( $cmd, $line );
3119 $cmd .= "$line\n";
3121 if ( $done || feof( $fp ) ) {
3122 $cmd = $this->replaceVars( $cmd );
3124 if ( !$inputCallback || call_user_func( $inputCallback, $cmd ) ) {
3125 $res = $this->query( $cmd, $fname );
3127 if ( $resultCallback ) {
3128 call_user_func( $resultCallback, $res, $this );
3131 if ( false === $res ) {
3132 $err = $this->lastError();
3134 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
3137 $cmd = '';
3141 return true;
3145 * Called by sourceStream() to check if we've reached a statement end
3147 * @param string &$sql SQL assembled so far
3148 * @param string &$newLine New line about to be added to $sql
3149 * @return bool Whether $newLine contains end of the statement
3151 public function streamStatementEnd( &$sql, &$newLine ) {
3152 if ( $this->delimiter ) {
3153 $prev = $newLine;
3154 $newLine = preg_replace(
3155 '/' . preg_quote( $this->delimiter, '/' ) . '$/', '', $newLine );
3156 if ( $newLine != $prev ) {
3157 return true;
3161 return false;
3165 * Database independent variable replacement. Replaces a set of variables
3166 * in an SQL statement with their contents as given by $this->getSchemaVars().
3168 * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables.
3170 * - '{$var}' should be used for text and is passed through the database's
3171 * addQuotes method.
3172 * - `{$var}` should be used for identifiers (e.g. table and database names).
3173 * It is passed through the database's addIdentifierQuotes method which
3174 * can be overridden if the database uses something other than backticks.
3175 * - / *_* / or / *$wgDBprefix* / passes the name that follows through the
3176 * database's tableName method.
3177 * - / *i* / passes the name that follows through the database's indexName method.
3178 * - In all other cases, / *$var* / is left unencoded. Except for table options,
3179 * its use should be avoided. In 1.24 and older, string encoding was applied.
3181 * @param string $ins SQL statement to replace variables in
3182 * @return string The new SQL statement with variables replaced
3184 protected function replaceVars( $ins ) {
3185 $vars = $this->getSchemaVars();
3186 return preg_replace_callback(
3188 /\* (\$wgDBprefix|[_i]) \*/ (\w*) | # 1-2. tableName, indexName
3189 \'\{\$ (\w+) }\' | # 3. addQuotes
3190 `\{\$ (\w+) }` | # 4. addIdentifierQuotes
3191 /\*\$ (\w+) \*/ # 5. leave unencoded
3192 !x',
3193 function ( $m ) use ( $vars ) {
3194 // Note: Because of <https://bugs.php.net/bug.php?id=51881>,
3195 // check for both nonexistent keys *and* the empty string.
3196 if ( isset( $m[1] ) && $m[1] !== '' ) {
3197 if ( $m[1] === 'i' ) {
3198 return $this->indexName( $m[2] );
3199 } else {
3200 return $this->tableName( $m[2] );
3202 } elseif ( isset( $m[3] ) && $m[3] !== '' && array_key_exists( $m[3], $vars ) ) {
3203 return $this->addQuotes( $vars[$m[3]] );
3204 } elseif ( isset( $m[4] ) && $m[4] !== '' && array_key_exists( $m[4], $vars ) ) {
3205 return $this->addIdentifierQuotes( $vars[$m[4]] );
3206 } elseif ( isset( $m[5] ) && $m[5] !== '' && array_key_exists( $m[5], $vars ) ) {
3207 return $vars[$m[5]];
3208 } else {
3209 return $m[0];
3212 $ins
3217 * Get schema variables. If none have been set via setSchemaVars(), then
3218 * use some defaults from the current object.
3220 * @return array
3222 protected function getSchemaVars() {
3223 if ( $this->mSchemaVars ) {
3224 return $this->mSchemaVars;
3225 } else {
3226 return $this->getDefaultSchemaVars();
3231 * Get schema variables to use if none have been set via setSchemaVars().
3233 * Override this in derived classes to provide variables for tables.sql
3234 * and SQL patch files.
3236 * @return array
3238 protected function getDefaultSchemaVars() {
3239 return [];
3242 public function lockIsFree( $lockName, $method ) {
3243 return true;
3246 public function lock( $lockName, $method, $timeout = 5 ) {
3247 $this->mNamedLocksHeld[$lockName] = 1;
3249 return true;
3252 public function unlock( $lockName, $method ) {
3253 unset( $this->mNamedLocksHeld[$lockName] );
3255 return true;
3258 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
3259 if ( $this->writesOrCallbacksPending() ) {
3260 // This only flushes transactions to clear snapshots, not to write data
3261 $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
3262 throw new DBUnexpectedError(
3263 $this,
3264 "$fname: Cannot flush pre-lock snapshot because writes are pending ($fnames)."
3268 if ( !$this->lock( $lockKey, $fname, $timeout ) ) {
3269 return null;
3272 $unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) {
3273 if ( $this->trxLevel() ) {
3274 // There is a good chance an exception was thrown, causing any early return
3275 // from the caller. Let any error handler get a chance to issue rollback().
3276 // If there isn't one, let the error bubble up and trigger server-side rollback.
3277 $this->onTransactionResolution(
3278 function () use ( $lockKey, $fname ) {
3279 $this->unlock( $lockKey, $fname );
3281 $fname
3283 } else {
3284 $this->unlock( $lockKey, $fname );
3286 } );
3288 $this->commit( $fname, self::FLUSHING_INTERNAL );
3290 return $unlocker;
3293 public function namedLocksEnqueue() {
3294 return false;
3298 * Lock specific tables
3300 * @param array $read Array of tables to lock for read access
3301 * @param array $write Array of tables to lock for write access
3302 * @param string $method Name of caller
3303 * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY
3304 * @return bool
3306 public function lockTables( $read, $write, $method, $lowPriority = true ) {
3307 return true;
3311 * Unlock specific tables
3313 * @param string $method The caller
3314 * @return bool
3316 public function unlockTables( $method ) {
3317 return true;
3321 * Delete a table
3322 * @param string $tableName
3323 * @param string $fName
3324 * @return bool|ResultWrapper
3325 * @since 1.18
3327 public function dropTable( $tableName, $fName = __METHOD__ ) {
3328 if ( !$this->tableExists( $tableName, $fName ) ) {
3329 return false;
3331 $sql = "DROP TABLE " . $this->tableName( $tableName ) . " CASCADE";
3333 return $this->query( $sql, $fName );
3336 public function getInfinity() {
3337 return 'infinity';
3340 public function encodeExpiry( $expiry ) {
3341 return ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() )
3342 ? $this->getInfinity()
3343 : $this->timestamp( $expiry );
3346 public function decodeExpiry( $expiry, $format = TS_MW ) {
3347 if ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() ) {
3348 return 'infinity';
3351 return ConvertibleTimestamp::convert( $format, $expiry );
3354 public function setBigSelects( $value = true ) {
3355 // no-op
3358 public function isReadOnly() {
3359 return ( $this->getReadOnlyReason() !== false );
3363 * @return string|bool Reason this DB is read-only or false if it is not
3365 protected function getReadOnlyReason() {
3366 $reason = $this->getLBInfo( 'readOnlyReason' );
3368 return is_string( $reason ) ? $reason : false;
3371 public function setTableAliases( array $aliases ) {
3372 $this->tableAliases = $aliases;
3376 * @return bool Whether a DB user is required to access the DB
3377 * @since 1.28
3379 protected function requiresDatabaseUser() {
3380 return true;
3384 * Get the underlying binding handle, mConn
3386 * Makes sure that mConn is set (disconnects and ping() failure can unset it).
3387 * This catches broken callers than catch and ignore disconnection exceptions.
3388 * Unlike checking isOpen(), this is safe to call inside of open().
3390 * @return resource|object
3391 * @throws DBUnexpectedError
3392 * @since 1.26
3394 protected function getBindingHandle() {
3395 if ( !$this->mConn ) {
3396 throw new DBUnexpectedError(
3397 $this,
3398 'DB connection was already closed or the connection dropped.'
3402 return $this->mConn;
3406 * @since 1.19
3407 * @return string
3409 public function __toString() {
3410 return (string)$this->mConn;
3414 * Make sure that copies do not share the same client binding handle
3415 * @throws DBConnectionError
3417 public function __clone() {
3418 $this->connLogger->warning(
3419 "Cloning " . get_class( $this ) . " is not recomended; forking connection:\n" .
3420 ( new RuntimeException() )->getTraceAsString()
3423 if ( $this->isOpen() ) {
3424 // Open a new connection resource without messing with the old one
3425 $this->mOpened = false;
3426 $this->mConn = false;
3427 $this->mTrxEndCallbacks = []; // don't copy
3428 $this->handleSessionLoss(); // no trx or locks anymore
3429 $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
3430 $this->lastPing = microtime( true );
3435 * Called by serialize. Throw an exception when DB connection is serialized.
3436 * This causes problems on some database engines because the connection is
3437 * not restored on unserialize.
3439 public function __sleep() {
3440 throw new RuntimeException( 'Database serialization may cause problems, since ' .
3441 'the connection is not restored on wakeup.' );
3445 * Run a few simple sanity checks and close dangling connections
3447 public function __destruct() {
3448 if ( $this->mTrxLevel && $this->mTrxDoneWrites ) {
3449 trigger_error( "Uncommitted DB writes (transaction from {$this->mTrxFname})." );
3452 $danglingWriters = $this->pendingWriteAndCallbackCallers();
3453 if ( $danglingWriters ) {
3454 $fnames = implode( ', ', $danglingWriters );
3455 trigger_error( "DB transaction writes or callbacks still pending ($fnames)." );
3458 if ( $this->mConn ) {
3459 // Avoid connection leaks for sanity. Normally, resources close at script completion.
3460 // The connection might already be closed in zend/hhvm by now, so suppress warnings.
3461 \MediaWiki\suppressWarnings();
3462 $this->closeConnection();
3463 \MediaWiki\restoreWarnings();
3464 $this->mConn = false;
3465 $this->mOpened = false;
3470 class_alias( 'Database', 'DatabaseBase' );