MediaWiki  master
Database.php
Go to the documentation of this file.
1 <?php
26 namespace Wikimedia\Rdbms;
27 
28 use BagOStuff;
29 use Exception;
30 use HashBagOStuff;
31 use InvalidArgumentException;
32 use LogicException;
33 use Psr\Log\LoggerAwareInterface;
34 use Psr\Log\LoggerInterface;
35 use Psr\Log\NullLogger;
36 use RuntimeException;
37 use Throwable;
38 use UnexpectedValueException;
39 use Wikimedia\AtEase\AtEase;
40 use Wikimedia\ScopedCallback;
41 use Wikimedia\Timestamp\ConvertibleTimestamp;
42 
49 abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAwareInterface {
51  protected $srvCache;
53  protected $connLogger;
55  protected $queryLogger;
57  protected $replLogger;
59  protected $errorLogger;
61  protected $deprecationLogger;
63  protected $profiler;
65  protected $trxProfiler;
66 
68  protected $currentDomain;
69 
71  protected $conn;
72 
75 
77  protected $server;
79  protected $user;
81  protected $password;
83  protected $cliMode;
85  protected $agent;
87  protected $topologyRole;
91  protected $connectionParams;
96 
98  protected $flags;
100  protected $lbInfo = [];
102  protected $delimiter = ';';
104  protected $tableAliases = [];
106  protected $indexAliases = [];
108  protected $schemaVars;
109 
111  private $htmlErrors;
113  private $priorFlags = [];
114 
116  protected $sessionNamedLocks = [];
118  protected $sessionTempTables = [];
120  protected $sessionDirtyTempTables = [];
121 
123  private $trxShortId = '';
125  private $trxStatus = self::STATUS_TRX_NONE;
131  private $trxTimestamp = null;
133  private $trxReplicaLag = null;
135  private $trxFname = null;
137  private $trxDoneWrites = false;
139  private $trxAutomatic = false;
141  private $trxAtomicCounter = 0;
143  private $trxAtomicLevels = [];
145  private $trxAutomaticAtomic = false;
147  private $trxWriteCallers = [];
149  private $trxWriteDuration = 0.0;
151  private $trxWriteQueryCount = 0;
155  private $trxWriteAdjDuration = 0.0;
159  private $trxIdleCallbacks = [];
166  private $trxEndCallbacks = [];
172  private $trxEndCallbacksSuppressed = false;
173 
175  protected $affectedRowCount;
176 
178  private $lastPing = 0.0;
180  private $lastQuery = '';
182  private $lastWriteTime = false;
184  private $lastPhpError = false;
186  private $lastRoundTripEstimate = 0.0;
187 
189  private $ownerId;
190 
192  public const ATTR_DB_IS_FILE = 'db-is-file';
194  public const ATTR_DB_LEVEL_LOCKING = 'db-level-locking';
196  public const ATTR_SCHEMAS_AS_TABLE_GROUPS = 'supports-schemas';
197 
199  public const NEW_UNCONNECTED = 0;
201  public const NEW_CONNECTED = 1;
202 
204  public const STATUS_TRX_ERROR = 1;
206  public const STATUS_TRX_OK = 2;
208  public const STATUS_TRX_NONE = 3;
209 
211  private static $NOT_APPLICABLE = 'n/a';
213  private static $SAVEPOINT_PREFIX = 'wikimedia_rdbms_atomic';
214 
216  private static $TEMP_NORMAL = 1;
218  private static $TEMP_PSEUDO_PERMANENT = 2;
219 
221  private static $DEADLOCK_TRIES = 4;
223  private static $DEADLOCK_DELAY_MIN = 500000;
225  private static $DEADLOCK_DELAY_MAX = 1500000;
226 
228  private static $PING_TTL = 1.0;
230  private static $PING_QUERY = 'SELECT 1 AS ping';
231 
233  private static $TINY_WRITE_SEC = 0.010;
235  private static $SLOW_WRITE_SEC = 0.500;
237  private static $SMALL_WRITE_ROWS = 100;
238 
240  protected static $MUTABLE_FLAGS = [
241  'DBO_DEBUG',
242  'DBO_NOBUFFER',
243  'DBO_TRX',
244  'DBO_DDLMODE',
245  ];
247  protected static $DBO_MUTABLE = (
249  );
250 
255  public function __construct( array $params ) {
256  $this->connectionParams = [
257  'host' => strlen( $params['host'] ) ? $params['host'] : null,
258  'user' => strlen( $params['user'] ) ? $params['user'] : null,
259  'dbname' => strlen( $params['dbname'] ) ? $params['dbname'] : null,
260  'schema' => strlen( $params['schema'] ) ? $params['schema'] : null,
261  'password' => is_string( $params['password'] ) ? $params['password'] : null,
262  'tablePrefix' => (string)$params['tablePrefix']
263  ];
264 
265  $this->lbInfo = $params['lbInfo'] ?? [];
266  $this->lazyMasterHandle = $params['lazyMasterHandle'] ?? null;
267  $this->connectionVariables = $params['variables'] ?? [];
268 
269  $this->flags = (int)$params['flags'];
270  $this->cliMode = (bool)$params['cliMode'];
271  $this->agent = (string)$params['agent'];
272  $this->topologyRole = (string)$params['topologyRole'];
273  $this->topologyRootMaster = (string)$params['topologicalMaster'];
274  $this->nonNativeInsertSelectBatchSize = $params['nonNativeInsertSelectBatchSize'] ?? 10000;
275 
276  $this->srvCache = $params['srvCache'];
277  $this->profiler = is_callable( $params['profiler'] ) ? $params['profiler'] : null;
278  $this->trxProfiler = $params['trxProfiler'];
279  $this->connLogger = $params['connLogger'];
280  $this->queryLogger = $params['queryLogger'];
281  $this->replLogger = $params['replLogger'];
282  $this->errorLogger = $params['errorLogger'];
283  $this->deprecationLogger = $params['deprecationLogger'];
284 
285  // Set initial dummy domain until open() sets the final DB/prefix
286  $this->currentDomain = new DatabaseDomain(
287  $params['dbname'] != '' ? $params['dbname'] : null,
288  $params['schema'] != '' ? $params['schema'] : null,
289  $params['tablePrefix']
290  );
291 
292  $this->ownerId = $params['ownerId'] ?? null;
293  }
294 
303  final public function initConnection() {
304  if ( $this->isOpen() ) {
305  throw new LogicException( __METHOD__ . ': already connected' );
306  }
307  // Establish the connection
308  $this->doInitConnection();
309  }
310 
317  protected function doInitConnection() {
318  $this->open(
319  $this->connectionParams['host'],
320  $this->connectionParams['user'],
321  $this->connectionParams['password'],
322  $this->connectionParams['dbname'],
323  $this->connectionParams['schema'],
324  $this->connectionParams['tablePrefix']
325  );
326  }
327 
339  abstract protected function open( $server, $user, $password, $dbName, $schema, $tablePrefix );
340 
394  final public static function factory( $type, $params = [], $connect = self::NEW_CONNECTED ) {
395  $class = self::getClass( $type, $params['driver'] ?? null );
396 
397  if ( class_exists( $class ) && is_subclass_of( $class, IDatabase::class ) ) {
398  $params += [
399  // Default configuration
400  'host' => null,
401  'user' => null,
402  'password' => null,
403  'dbname' => null,
404  'schema' => null,
405  'tablePrefix' => '',
406  'flags' => 0,
407  'variables' => [],
408  'lbInfo' => [],
409  'cliMode' => ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ),
410  'agent' => '',
411  'ownerId' => null,
412  'topologyRole' => null,
413  'topologicalMaster' => null,
414  // Objects and callbacks
415  'lazyMasterHandle' => $params['lazyMasterHandle'] ?? null,
416  'srvCache' => $params['srvCache'] ?? new HashBagOStuff(),
417  'profiler' => $params['profiler'] ?? null,
418  'trxProfiler' => $params['trxProfiler'] ?? new TransactionProfiler(),
419  'connLogger' => $params['connLogger'] ?? new NullLogger(),
420  'queryLogger' => $params['queryLogger'] ?? new NullLogger(),
421  'replLogger' => $params['replLogger'] ?? new NullLogger(),
422  'errorLogger' => $params['errorLogger'] ?? function ( Throwable $e ) {
423  trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
424  },
425  'deprecationLogger' => $params['deprecationLogger'] ?? function ( $msg ) {
426  trigger_error( $msg, E_USER_DEPRECATED );
427  }
428  ];
429 
431  $conn = new $class( $params );
432  if ( $connect === self::NEW_CONNECTED ) {
433  $conn->initConnection();
434  }
435  } else {
436  $conn = null;
437  }
438 
439  return $conn;
440  }
441 
449  final public static function attributesFromType( $dbType, $driver = null ) {
450  static $defaults = [
451  self::ATTR_DB_IS_FILE => false,
452  self::ATTR_DB_LEVEL_LOCKING => false,
453  self::ATTR_SCHEMAS_AS_TABLE_GROUPS => false
454  ];
455 
456  $class = self::getClass( $dbType, $driver );
457 
458  return call_user_func( [ $class, 'getAttributes' ] ) + $defaults;
459  }
460 
467  private static function getClass( $dbType, $driver = null ) {
468  // For database types with built-in support, the below maps type to IDatabase
469  // implementations. For types with multiple driver implementations (PHP extensions),
470  // an array can be used, keyed by extension name. In case of an array, the
471  // optional 'driver' parameter can be used to force a specific driver. Otherwise,
472  // we auto-detect the first available driver. For types without built-in support,
473  // an class named "Database<Type>" us used, eg. DatabaseFoo for type 'foo'.
474  static $builtinTypes = [
475  'mysql' => [ 'mysqli' => DatabaseMysqli::class ],
476  'sqlite' => DatabaseSqlite::class,
477  'postgres' => DatabasePostgres::class,
478  ];
479 
480  $dbType = strtolower( $dbType );
481  $class = false;
482 
483  if ( isset( $builtinTypes[$dbType] ) ) {
484  $possibleDrivers = $builtinTypes[$dbType];
485  if ( is_string( $possibleDrivers ) ) {
486  $class = $possibleDrivers;
487  } elseif ( (string)$driver !== '' ) {
488  if ( !isset( $possibleDrivers[$driver] ) ) {
489  throw new InvalidArgumentException( __METHOD__ .
490  " type '$dbType' does not support driver '{$driver}'" );
491  }
492 
493  $class = $possibleDrivers[$driver];
494  } else {
495  foreach ( $possibleDrivers as $posDriver => $possibleClass ) {
496  if ( extension_loaded( $posDriver ) ) {
497  $class = $possibleClass;
498  break;
499  }
500  }
501  }
502  } else {
503  $class = 'Database' . ucfirst( $dbType );
504  }
505 
506  if ( $class === false ) {
507  throw new InvalidArgumentException( __METHOD__ .
508  " no viable database extension found for type '$dbType'" );
509  }
510 
511  return $class;
512  }
513 
518  protected static function getAttributes() {
519  return [];
520  }
521 
529  public function setLogger( LoggerInterface $logger ) {
530  $this->queryLogger = $logger;
531  }
532 
533  public function getServerInfo() {
534  return $this->getServerVersion();
535  }
536 
537  public function getTopologyRole() {
538  return $this->topologyRole;
539  }
540 
541  public function getTopologyRootMaster() {
543  }
544 
545  final public function trxLevel() {
546  return ( $this->trxShortId != '' ) ? 1 : 0;
547  }
548 
549  public function trxTimestamp() {
550  return $this->trxLevel() ? $this->trxTimestamp : null;
551  }
552 
557  public function trxStatus() {
558  return $this->trxStatus;
559  }
560 
561  public function tablePrefix( $prefix = null ) {
562  $old = $this->currentDomain->getTablePrefix();
563  if ( $prefix !== null ) {
564  $this->currentDomain = new DatabaseDomain(
565  $this->currentDomain->getDatabase(),
566  $this->currentDomain->getSchema(),
567  $prefix
568  );
569  }
570 
571  return $old;
572  }
573 
574  public function dbSchema( $schema = null ) {
575  if ( strlen( $schema ) && $this->getDBname() === null ) {
576  throw new DBUnexpectedError( $this, "Cannot set schema to '$schema'; no database set" );
577  }
578 
579  $old = $this->currentDomain->getSchema();
580  if ( $schema !== null ) {
581  $this->currentDomain = new DatabaseDomain(
582  $this->currentDomain->getDatabase(),
583  // DatabaseDomain uses null for unspecified schemas
584  strlen( $schema ) ? $schema : null,
585  $this->currentDomain->getTablePrefix()
586  );
587  }
588 
589  return (string)$old;
590  }
591 
595  protected function relationSchemaQualifier() {
596  return $this->dbSchema();
597  }
598 
599  public function getLBInfo( $name = null ) {
600  if ( $name === null ) {
601  return $this->lbInfo;
602  }
603 
604  if ( array_key_exists( $name, $this->lbInfo ) ) {
605  return $this->lbInfo[$name];
606  }
607 
608  return null;
609  }
610 
611  public function setLBInfo( $nameOrArray, $value = null ) {
612  if ( is_array( $nameOrArray ) ) {
613  $this->lbInfo = $nameOrArray;
614  } elseif ( is_string( $nameOrArray ) ) {
615  if ( $value !== null ) {
616  $this->lbInfo[$nameOrArray] = $value;
617  } else {
618  unset( $this->lbInfo[$nameOrArray] );
619  }
620  } else {
621  throw new InvalidArgumentException( "Got non-string key" );
622  }
623  }
624 
631  protected function getLazyMasterHandle() {
633  }
634 
635  public function implicitOrderby() {
636  return true;
637  }
638 
639  public function lastQuery() {
640  return $this->lastQuery;
641  }
642 
643  public function lastDoneWrites() {
644  return $this->lastWriteTime ?: false;
645  }
646 
647  public function writesPending() {
648  return $this->trxLevel() && $this->trxDoneWrites;
649  }
650 
651  public function writesOrCallbacksPending() {
652  return $this->trxLevel() && (
653  $this->trxDoneWrites ||
654  $this->trxIdleCallbacks ||
655  $this->trxPreCommitCallbacks ||
656  $this->trxEndCallbacks ||
658  );
659  }
660 
661  public function preCommitCallbacksPending() {
662  return $this->trxLevel() && $this->trxPreCommitCallbacks;
663  }
664 
668  final protected function getTransactionRoundId() {
669  // If transaction round participation is enabled, see if one is active
670  if ( $this->getFlag( self::DBO_TRX ) ) {
671  $id = $this->getLBInfo( self::LB_TRX_ROUND_ID );
672 
673  return is_string( $id ) ? $id : null;
674  }
675 
676  return null;
677  }
678 
679  public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
680  if ( !$this->trxLevel() ) {
681  return false;
682  } elseif ( !$this->trxDoneWrites ) {
683  return 0.0;
684  }
685 
686  switch ( $type ) {
687  case self::ESTIMATE_DB_APPLY:
688  return $this->pingAndCalculateLastTrxApplyTime();
689  default: // everything
691  }
692  }
693 
697  private function pingAndCalculateLastTrxApplyTime() {
698  $this->ping( $rtt );
699 
700  $rttAdjTotal = $this->trxWriteAdjQueryCount * $rtt;
701  $applyTime = max( $this->trxWriteAdjDuration - $rttAdjTotal, 0 );
702  // For omitted queries, make them count as something at least
703  $omitted = $this->trxWriteQueryCount - $this->trxWriteAdjQueryCount;
704  $applyTime += self::$TINY_WRITE_SEC * $omitted;
705 
706  return $applyTime;
707  }
708 
709  public function pendingWriteCallers() {
710  return $this->trxLevel() ? $this->trxWriteCallers : [];
711  }
712 
713  public function pendingWriteRowsAffected() {
715  }
716 
725  public function pendingWriteAndCallbackCallers() {
726  $fnames = $this->pendingWriteCallers();
727  foreach ( [
728  $this->trxIdleCallbacks,
729  $this->trxPreCommitCallbacks,
730  $this->trxEndCallbacks,
731  $this->trxSectionCancelCallbacks
732  ] as $callbacks ) {
733  foreach ( $callbacks as $callback ) {
734  $fnames[] = $callback[1];
735  }
736  }
737 
738  return $fnames;
739  }
740 
744  private function flatAtomicSectionList() {
745  return array_reduce( $this->trxAtomicLevels, function ( $accum, $v ) {
746  return $accum === null ? $v[0] : "$accum, " . $v[0];
747  } );
748  }
749 
750  public function isOpen() {
751  return (bool)$this->conn;
752  }
753 
754  public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
755  if ( $flag & ~static::$DBO_MUTABLE ) {
756  throw new DBUnexpectedError(
757  $this,
758  "Got $flag (allowed: " . implode( ', ', static::$MUTABLE_FLAGS ) . ')'
759  );
760  }
761 
762  if ( $remember === self::REMEMBER_PRIOR ) {
763  array_push( $this->priorFlags, $this->flags );
764  }
765 
766  $this->flags |= $flag;
767  }
768 
769  public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
770  if ( $flag & ~static::$DBO_MUTABLE ) {
771  throw new DBUnexpectedError(
772  $this,
773  "Got $flag (allowed: " . implode( ', ', static::$MUTABLE_FLAGS ) . ')'
774  );
775  }
776 
777  if ( $remember === self::REMEMBER_PRIOR ) {
778  array_push( $this->priorFlags, $this->flags );
779  }
780 
781  $this->flags &= ~$flag;
782  }
783 
784  public function restoreFlags( $state = self::RESTORE_PRIOR ) {
785  if ( !$this->priorFlags ) {
786  return;
787  }
788 
789  if ( $state === self::RESTORE_INITIAL ) {
790  $this->flags = reset( $this->priorFlags );
791  $this->priorFlags = [];
792  } else {
793  $this->flags = array_pop( $this->priorFlags );
794  }
795  }
796 
797  public function getFlag( $flag ) {
798  return ( ( $this->flags & $flag ) === $flag );
799  }
800 
801  public function getDomainID() {
802  return $this->currentDomain->getId();
803  }
804 
812  abstract public function indexInfo( $table, $index, $fname = __METHOD__ );
813 
820  abstract public function strencode( $s );
821 
825  protected function installErrorHandler() {
826  $this->lastPhpError = false;
827  $this->htmlErrors = ini_set( 'html_errors', '0' );
828  set_error_handler( [ $this, 'connectionErrorLogger' ] );
829  }
830 
836  protected function restoreErrorHandler() {
837  restore_error_handler();
838  if ( $this->htmlErrors !== false ) {
839  ini_set( 'html_errors', $this->htmlErrors );
840  }
841 
842  return $this->getLastPHPError();
843  }
844 
848  protected function getLastPHPError() {
849  if ( $this->lastPhpError ) {
850  $error = preg_replace( '!\[<a.*</a>\]!', '', $this->lastPhpError );
851  $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
852 
853  return $error;
854  }
855 
856  return false;
857  }
858 
866  public function connectionErrorLogger( $errno, $errstr ) {
867  $this->lastPhpError = $errstr;
868  }
869 
876  protected function getLogContext( array $extras = [] ) {
877  return array_merge(
878  [
879  'db_server' => $this->server,
880  'db_name' => $this->getDBname(),
881  'db_user' => $this->user,
882  ],
883  $extras
884  );
885  }
886 
887  final public function close( $fname = __METHOD__, $owner = null ) {
888  $error = null; // error to throw after disconnecting
889 
890  $wasOpen = (bool)$this->conn;
891  // This should mostly do nothing if the connection is already closed
892  if ( $this->conn ) {
893  // Roll back any dangling transaction first
894  if ( $this->trxLevel() ) {
895  if ( $this->trxAtomicLevels ) {
896  // Cannot let incomplete atomic sections be committed
897  $levels = $this->flatAtomicSectionList();
898  $error = "$fname: atomic sections $levels are still open";
899  } elseif ( $this->trxAutomatic ) {
900  // Only the connection manager can commit non-empty DBO_TRX transactions
901  // (empty ones we can silently roll back)
902  if ( $this->writesOrCallbacksPending() ) {
903  $error = "$fname: " .
904  "expected mass rollback of all peer transactions (DBO_TRX set)";
905  }
906  } else {
907  // Manual transactions should have been committed or rolled
908  // back, even if empty.
909  $error = "$fname: transaction is still open (from {$this->trxFname})";
910  }
911 
912  if ( $this->trxEndCallbacksSuppressed && $error === null ) {
913  $error = "$fname: callbacks are suppressed; cannot properly commit";
914  }
915 
916  // Rollback the changes and run any callbacks as needed
917  $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
918  }
919 
920  // Close the actual connection in the binding handle
921  $closed = $this->closeConnection();
922  } else {
923  $closed = true; // already closed; nothing to do
924  }
925 
926  $this->conn = null;
927 
928  // Log or throw any unexpected errors after having disconnected
929  if ( $error !== null ) {
930  // T217819, T231443: if this is probably just LoadBalancer trying to recover from
931  // errors and shutdown, then log any problems and move on since the request has to
932  // end one way or another. Throwing errors is not very useful at some point.
933  if ( $this->ownerId !== null && $owner === $this->ownerId ) {
934  $this->queryLogger->error( $error );
935  } else {
936  throw new DBUnexpectedError( $this, $error );
937  }
938  }
939 
940  // Note that various subclasses call close() at the start of open(), which itself is
941  // called by replaceLostConnection(). In that case, just because onTransactionResolution()
942  // callbacks are pending does not mean that an exception should be thrown. Rather, they
943  // will be executed after the reconnection step.
944  if ( $wasOpen ) {
945  // Sanity check that no callbacks are dangling
946  $fnames = $this->pendingWriteAndCallbackCallers();
947  if ( $fnames ) {
948  throw new RuntimeException(
949  "Transaction callbacks are still pending: " . implode( ', ', $fnames )
950  );
951  }
952  }
953 
954  return $closed;
955  }
956 
965  final protected function assertHasConnectionHandle() {
966  if ( !$this->isOpen() ) {
967  throw new DBUnexpectedError( $this, "DB connection was already closed" );
968  }
969  }
970 
976  protected function assertIsWritableMaster() {
977  $info = $this->getReadOnlyReason();
978  if ( $info ) {
979  list( $reason, $source ) = $info;
980  if ( $source === 'role' ) {
981  throw new DBReadOnlyRoleError( $this, "Database is read-only: $reason" );
982  } else {
983  throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
984  }
985  }
986  }
987 
993  abstract protected function closeConnection();
994 
1024  abstract protected function doQuery( $sql );
1025 
1043  protected function isWriteQuery( $sql, $flags ) {
1044  if (
1045  $this->fieldHasBit( $flags, self::QUERY_CHANGE_ROWS ) ||
1046  $this->fieldHasBit( $flags, self::QUERY_CHANGE_SCHEMA )
1047  ) {
1048  return true;
1049  } elseif ( $this->fieldHasBit( $flags, self::QUERY_CHANGE_NONE ) ) {
1050  return false;
1051  }
1052  // BEGIN and COMMIT queries are considered read queries here.
1053  // Database backends and drivers (MySQL, MariaDB, php-mysqli) generally
1054  // treat these as write queries, in that their results have "affected rows"
1055  // as meta data as from writes, instead of "num rows" as from reads.
1056  // But, we treat them as read queries because when reading data (from
1057  // either replica or master) we use transactions to enable repeatable-read
1058  // snapshots, which ensures we get consistent results from the same snapshot
1059  // for all queries within a request. Use cases:
1060  // - Treating these as writes would trigger ChronologyProtector (see method doc).
1061  // - We use this method to reject writes to replicas, but we need to allow
1062  // use of transactions on replicas for read snapshots. This is fine given
1063  // that transactions by themselves don't make changes, only actual writes
1064  // within the transaction matter, which we still detect.
1065  return !preg_match(
1066  '/^\s*(?:SELECT|BEGIN|ROLLBACK|COMMIT|SAVEPOINT|RELEASE|SET|SHOW|EXPLAIN|USE|\(SELECT)\b/i',
1067  $sql
1068  );
1069  }
1070 
1075  protected function getQueryVerb( $sql ) {
1076  return preg_match( '/^\s*([a-z]+)/i', $sql, $m ) ? strtoupper( $m[1] ) : null;
1077  }
1078 
1092  protected function isTransactableQuery( $sql ) {
1093  return !in_array(
1094  $this->getQueryVerb( $sql ),
1095  [ 'BEGIN', 'ROLLBACK', 'COMMIT', 'SET', 'SHOW', 'CREATE', 'ALTER', 'USE', 'SHOW' ],
1096  true
1097  );
1098  }
1099 
1108  protected function getTempTableWrites( $sql, $pseudoPermanent ) {
1109  // Regexes for basic queries that can create/change/drop temporary tables.
1110  // For simplicity, this only looks for tables with sane, alphanumeric, names;
1111  // temporary tables only need simple programming names anyway.
1112  static $regexes = null;
1113  if ( $regexes === null ) {
1114  // Regex with a group for quoted table 0 and a group for quoted tables 1..N
1115  $qts = '(\w+|`\w+`|\'\w+\'|"\w+")(?:\s*,\s*(\w+|`\w+`|\'\w+\'|"\w+"))*';
1116  // Regex to get query verb, table 0, and tables 1..N
1117  $regexes = [
1118  // DML write queries
1119  "/^(INSERT|REPLACE)\s+(?:\w+\s+)*?INTO\s+$qts/i",
1120  "/^(UPDATE)(?:\s+OR\s+\w+|\s+IGNORE|\s+ONLY)?\s+$qts/i",
1121  "/^(DELETE)\s+(?:\w+\s+)*?FROM(?:\s+ONLY)?\s+$qts/i",
1122  // DDL write queries
1123  "/^(CREATE)\s+TEMPORARY\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?\s+$qts/i",
1124  "/^(DROP)\s+(?:TEMPORARY\s+)?TABLE(?:\s+IF\s+EXISTS)?\s+$qts/i",
1125  "/^(TRUNCATE)\s+(?:TEMPORARY\s+)?TABLE\s+$qts/i",
1126  "/^(ALTER)\s+TABLE\s+$qts/i"
1127  ];
1128  }
1129 
1130  $queryVerb = null;
1131  $queryTables = [];
1132  foreach ( $regexes as $regex ) {
1133  if ( preg_match( $regex, $sql, $m, PREG_UNMATCHED_AS_NULL ) ) {
1134  $queryVerb = $m[1];
1135  $queryTables[] = trim( $m[2], "\"'`" );
1136  foreach ( ( $m[3] ?? [] ) as $quotedTable ) {
1137  $queryTables[] = trim( $quotedTable, "\"'`" );
1138  }
1139  break;
1140  }
1141  }
1142 
1143  $tempTableChanges = [];
1144  foreach ( $queryTables as $table ) {
1145  if ( $queryVerb === 'CREATE' ) {
1146  // Record the type of temporary table being created
1147  $tableType = $pseudoPermanent ? self::$TEMP_PSEUDO_PERMANENT : self::$TEMP_NORMAL;
1148  } else {
1149  $tableType = $this->sessionTempTables[$table] ?? null;
1150  }
1151 
1152  if ( $tableType !== null ) {
1153  $tempTableChanges[] = [ $tableType, $queryVerb, $table ];
1154  }
1155  }
1156 
1157  return $tempTableChanges;
1158  }
1159 
1164  protected function registerTempWrites( $ret, array $changes ) {
1165  if ( $ret === false ) {
1166  return;
1167  }
1168 
1169  foreach ( $changes as list( $tmpTableType, $verb, $table ) ) {
1170  switch ( $verb ) {
1171  case 'CREATE':
1172  $this->sessionTempTables[$table] = $tmpTableType;
1173  break;
1174  case 'DROP':
1175  unset( $this->sessionTempTables[$table] );
1176  unset( $this->sessionDirtyTempTables[$table] );
1177  break;
1178  case 'TRUNCATE':
1179  unset( $this->sessionDirtyTempTables[$table] );
1180  break;
1181  default:
1182  $this->sessionDirtyTempTables[$table] = 1;
1183  break;
1184  }
1185  }
1186  }
1187 
1195  protected function isPristineTemporaryTable( $table ) {
1196  $rawTable = $this->tableName( $table, 'raw' );
1197 
1198  return (
1199  isset( $this->sessionTempTables[$rawTable] ) &&
1200  !isset( $this->sessionDirtyTempTables[$rawTable] )
1201  );
1202  }
1203 
1204  public function query( $sql, $fname = __METHOD__, $flags = self::QUERY_NORMAL ) {
1205  $flags = (int)$flags; // b/c; this field used to be a bool
1206  // Sanity check that the SQL query is appropriate in the current context and is
1207  // allowed for an outside caller (e.g. does not break transaction/session tracking).
1208  $this->assertQueryIsCurrentlyAllowed( $sql, $fname );
1209 
1210  // Send the query to the server and fetch any corresponding errors
1211  list( $ret, $err, $errno, $unignorable ) = $this->executeQuery( $sql, $fname, $flags );
1212  if ( $ret === false ) {
1213  $ignoreErrors = $this->fieldHasBit( $flags, self::QUERY_SILENCE_ERRORS );
1214  // Throw an error unless both the ignore flag was set and a rollback is not needed
1215  $this->reportQueryError( $err, $errno, $sql, $fname, $ignoreErrors && !$unignorable );
1216  }
1217 
1218  return $this->resultObject( $ret );
1219  }
1220 
1241  final protected function executeQuery( $sql, $fname, $flags ) {
1242  $this->assertHasConnectionHandle();
1243 
1244  $priorTransaction = $this->trxLevel();
1245 
1246  if ( $this->isWriteQuery( $sql, $flags ) ) {
1247  // In theory, non-persistent writes are allowed in read-only mode, but due to things
1248  // like https://bugs.mysql.com/bug.php?id=33669 that might not work anyway...
1249  $this->assertIsWritableMaster();
1250  // Do not treat temporary table writes as "meaningful writes" since they are only
1251  // visible to one session and are not permanent. Profile them as reads. Integration
1252  // tests can override this behavior via $flags.
1253  $pseudoPermanent = $this->fieldHasBit( $flags, self::QUERY_PSEUDO_PERMANENT );
1254  $tempTableChanges = $this->getTempTableWrites( $sql, $pseudoPermanent );
1255  $isPermWrite = !$tempTableChanges;
1256  foreach ( $tempTableChanges as list( $tmpType ) ) {
1257  $isPermWrite = $isPermWrite || ( $tmpType !== self::$TEMP_NORMAL );
1258  }
1259  // DBConnRef uses QUERY_REPLICA_ROLE to enforce the replica role for raw SQL queries
1260  if ( $isPermWrite && $this->fieldHasBit( $flags, self::QUERY_REPLICA_ROLE ) ) {
1261  throw new DBReadOnlyRoleError( $this, "Cannot write; target role is DB_REPLICA" );
1262  }
1263  } else {
1264  // No permanent writes in this query
1265  $isPermWrite = false;
1266  // No temporary tables written to either
1267  $tempTableChanges = [];
1268  }
1269 
1270  // Add trace comment to the begin of the sql string, right after the operator.
1271  // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (T44598).
1272  $encAgent = str_replace( '/', '-', $this->agent );
1273  $commentedSql = preg_replace( '/\s|$/', " /* $fname $encAgent */ ", $sql, 1 );
1274 
1275  // Send the query to the server and fetch any corresponding errors.
1276  // This also doubles as a "ping" to see if the connection was dropped.
1277  list( $ret, $err, $errno, $recoverableSR, $recoverableCL, $reconnected ) =
1278  $this->executeQueryAttempt( $sql, $commentedSql, $isPermWrite, $fname, $flags );
1279 
1280  // Check if the query failed due to a recoverable connection loss
1281  $allowRetry = !$this->fieldHasBit( $flags, self::QUERY_NO_RETRY );
1282  if ( $ret === false && $recoverableCL && $reconnected && $allowRetry ) {
1283  // Silently resend the query to the server since it is safe and possible
1284  list( $ret, $err, $errno, $recoverableSR, $recoverableCL ) =
1285  $this->executeQueryAttempt( $sql, $commentedSql, $isPermWrite, $fname, $flags );
1286  }
1287 
1288  // Register creation and dropping of temporary tables
1289  $this->registerTempWrites( $ret, $tempTableChanges );
1290 
1291  $corruptedTrx = false;
1292 
1293  if ( $ret === false ) {
1294  if ( $priorTransaction ) {
1295  if ( $recoverableSR ) {
1296  # We're ignoring an error that caused just the current query to be aborted.
1297  # But log the cause so we can log a deprecation notice if a caller actually
1298  # does ignore it.
1299  $this->trxStatusIgnoredCause = [ $err, $errno, $fname ];
1300  } elseif ( !$recoverableCL ) {
1301  # Either the query was aborted or all queries after BEGIN where aborted.
1302  # In the first case, the only options going forward are (a) ROLLBACK, or
1303  # (b) ROLLBACK TO SAVEPOINT (if one was set). If the later case, the only
1304  # option is ROLLBACK, since the snapshots would have been released.
1305  $corruptedTrx = true; // cannot recover
1306  $this->trxStatus = self::STATUS_TRX_ERROR;
1307  $this->trxStatusCause = $this->getQueryException( $err, $errno, $sql, $fname );
1308  $this->trxStatusIgnoredCause = null;
1309  }
1310  }
1311  }
1312 
1313  return [ $ret, $err, $errno, $corruptedTrx ];
1314  }
1315 
1334  private function executeQueryAttempt( $sql, $commentedSql, $isPermWrite, $fname, $flags ) {
1335  $priorWritesPending = $this->writesOrCallbacksPending();
1336 
1337  if ( ( $flags & self::QUERY_IGNORE_DBO_TRX ) == 0 ) {
1338  $this->beginIfImplied( $sql, $fname );
1339  }
1340 
1341  // Keep track of whether the transaction has write queries pending
1342  if ( $isPermWrite ) {
1343  $this->lastWriteTime = microtime( true );
1344  if ( $this->trxLevel() && !$this->trxDoneWrites ) {
1345  $this->trxDoneWrites = true;
1346  $this->trxProfiler->transactionWritingIn(
1347  $this->server, $this->getDomainID(), $this->trxShortId );
1348  }
1349  }
1350 
1351  $prefix = $this->topologyRole ? 'query-m: ' : 'query: ';
1352  $generalizedSql = new GeneralizedSql( $sql, $this->trxShortId, $prefix );
1353 
1354  $startTime = microtime( true );
1355  $ps = $this->profiler
1356  ? ( $this->profiler )( $generalizedSql->stringify() )
1357  : null;
1358  $this->affectedRowCount = null;
1359  $this->lastQuery = $sql;
1360  $ret = $this->doQuery( $commentedSql );
1361  $lastError = $this->lastError();
1362  $lastErrno = $this->lastErrno();
1363 
1364  $this->affectedRowCount = $this->affectedRows();
1365  unset( $ps ); // profile out (if set)
1366  $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
1367 
1368  $recoverableSR = false; // recoverable statement rollback?
1369  $recoverableCL = false; // recoverable connection loss?
1370  $reconnected = false; // reconnection both attempted and succeeded?
1371 
1372  if ( $ret !== false ) {
1373  $this->lastPing = $startTime;
1374  if ( $isPermWrite && $this->trxLevel() ) {
1375  $this->updateTrxWriteQueryTime( $sql, $queryRuntime, $this->affectedRows() );
1376  $this->trxWriteCallers[] = $fname;
1377  }
1378  } elseif ( $this->wasConnectionError( $lastErrno ) ) {
1379  # Check if no meaningful session state was lost
1380  $recoverableCL = $this->canRecoverFromDisconnect( $sql, $priorWritesPending );
1381  # Update session state tracking and try to restore the connection
1382  $reconnected = $this->replaceLostConnection( __METHOD__ );
1383  } else {
1384  # Check if only the last query was rolled back
1385  $recoverableSR = $this->wasKnownStatementRollbackError();
1386  }
1387 
1388  if ( $sql === self::$PING_QUERY ) {
1389  $this->lastRoundTripEstimate = $queryRuntime;
1390  }
1391 
1392  $this->trxProfiler->recordQueryCompletion(
1393  $generalizedSql,
1394  $startTime,
1395  $isPermWrite,
1396  $isPermWrite ? $this->affectedRows() : $this->numRows( $ret )
1397  );
1398 
1399  // Avoid the overhead of logging calls unless debug mode is enabled
1400  if ( $this->getFlag( self::DBO_DEBUG ) ) {
1401  $this->queryLogger->debug(
1402  "{method} [{runtime}s] {db_host}: {sql}",
1403  [
1404  'method' => $fname,
1405  'db_host' => $this->getServer(),
1406  'sql' => $sql,
1407  'domain' => $this->getDomainID(),
1408  'runtime' => round( $queryRuntime, 3 )
1409  ]
1410  );
1411  }
1412 
1413  return [ $ret, $lastError, $lastErrno, $recoverableSR, $recoverableCL, $reconnected ];
1414  }
1415 
1422  private function beginIfImplied( $sql, $fname ) {
1423  if (
1424  !$this->trxLevel() &&
1425  $this->getFlag( self::DBO_TRX ) &&
1426  $this->isTransactableQuery( $sql )
1427  ) {
1428  $this->begin( __METHOD__ . " ($fname)", self::TRANSACTION_INTERNAL );
1429  $this->trxAutomatic = true;
1430  }
1431  }
1432 
1445  private function updateTrxWriteQueryTime( $sql, $runtime, $affected ) {
1446  // Whether this is indicative of replica DB runtime (except for RBR or ws_repl)
1447  $indicativeOfReplicaRuntime = true;
1448  if ( $runtime > self::$SLOW_WRITE_SEC ) {
1449  $verb = $this->getQueryVerb( $sql );
1450  // insert(), upsert(), replace() are fast unless bulky in size or blocked on locks
1451  if ( $verb === 'INSERT' ) {
1452  $indicativeOfReplicaRuntime = $this->affectedRows() > self::$SMALL_WRITE_ROWS;
1453  } elseif ( $verb === 'REPLACE' ) {
1454  $indicativeOfReplicaRuntime = $this->affectedRows() > self::$SMALL_WRITE_ROWS / 2;
1455  }
1456  }
1457 
1458  $this->trxWriteDuration += $runtime;
1459  $this->trxWriteQueryCount += 1;
1460  $this->trxWriteAffectedRows += $affected;
1461  if ( $indicativeOfReplicaRuntime ) {
1462  $this->trxWriteAdjDuration += $runtime;
1463  $this->trxWriteAdjQueryCount += 1;
1464  }
1465  }
1466 
1474  private function assertQueryIsCurrentlyAllowed( $sql, $fname ) {
1475  $verb = $this->getQueryVerb( $sql );
1476  if ( $verb === 'USE' ) {
1477  throw new DBUnexpectedError( $this, "Got USE query; use selectDomain() instead" );
1478  }
1479 
1480  if ( $verb === 'ROLLBACK' ) { // transaction/savepoint
1481  return;
1482  }
1483 
1484  if ( $this->trxStatus < self::STATUS_TRX_OK ) {
1485  throw new DBTransactionStateError(
1486  $this,
1487  "Cannot execute query from $fname while transaction status is ERROR",
1488  [],
1489  $this->trxStatusCause
1490  );
1491  } elseif ( $this->trxStatus === self::STATUS_TRX_OK && $this->trxStatusIgnoredCause ) {
1492  list( $iLastError, $iLastErrno, $iFname ) = $this->trxStatusIgnoredCause;
1493  call_user_func( $this->deprecationLogger,
1494  "Caller from $fname ignored an error originally raised from $iFname: " .
1495  "[$iLastErrno] $iLastError"
1496  );
1497  $this->trxStatusIgnoredCause = null;
1498  }
1499  }
1500 
1501  public function assertNoOpenTransactions() {
1502  if ( $this->explicitTrxActive() ) {
1503  throw new DBTransactionError(
1504  $this,
1505  "Explicit transaction still active. A caller may have caught an error. "
1506  . "Open transactions: " . $this->flatAtomicSectionList()
1507  );
1508  }
1509  }
1510 
1520  private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
1521  # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
1522  # Dropped connections also mean that named locks are automatically released.
1523  # Only allow error suppression in autocommit mode or when the lost transaction
1524  # didn't matter anyway (aside from DBO_TRX snapshot loss).
1525  if ( $this->sessionNamedLocks ) {
1526  return false; // possible critical section violation
1527  } elseif ( $this->sessionTempTables ) {
1528  return false; // tables might be queried latter
1529  } elseif ( $sql === 'COMMIT' ) {
1530  return !$priorWritesPending; // nothing written anyway? (T127428)
1531  } elseif ( $sql === 'ROLLBACK' ) {
1532  return true; // transaction lost...which is also what was requested :)
1533  } elseif ( $this->explicitTrxActive() ) {
1534  return false; // don't drop atomicity and explicit snapshots
1535  } elseif ( $priorWritesPending ) {
1536  return false; // prior writes lost from implicit transaction
1537  }
1538 
1539  return true;
1540  }
1541 
1545  private function handleSessionLossPreconnect() {
1546  // Clean up tracking of session-level things...
1547  // https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html
1548  // https://www.postgresql.org/docs/9.2/static/sql-createtable.html (ignoring ON COMMIT)
1549  $this->sessionTempTables = [];
1550  $this->sessionDirtyTempTables = [];
1551  // https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
1552  // https://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1553  $this->sessionNamedLocks = [];
1554  // Session loss implies transaction loss
1555  $oldTrxShortId = $this->consumeTrxShortId();
1556  $this->trxAtomicCounter = 0;
1557  $this->trxIdleCallbacks = []; // T67263; transaction already lost
1558  $this->trxPreCommitCallbacks = []; // T67263; transaction already lost
1559  // Clear additional subclass fields
1561  // @note: leave trxRecurringCallbacks in place
1562  if ( $this->trxDoneWrites ) {
1563  $this->trxProfiler->transactionWritingOut(
1564  $this->server,
1565  $this->getDomainID(),
1566  $oldTrxShortId,
1567  $this->pendingWriteQueryDuration( self::ESTIMATE_TOTAL ),
1568  $this->trxWriteAffectedRows
1569  );
1570  }
1571  }
1572 
1576  protected function doHandleSessionLossPreconnect() {
1577  // no-op
1578  }
1579 
1583  private function handleSessionLossPostconnect() {
1584  try {
1585  // Handle callbacks in trxEndCallbacks, e.g. onTransactionResolution().
1586  // If callback suppression is set then the array will remain unhandled.
1587  $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
1588  } catch ( Throwable $ex ) {
1589  // Already logged; move on...
1590  }
1591  try {
1592  // Handle callbacks in trxRecurringCallbacks, e.g. setTransactionListener()
1593  $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
1594  } catch ( Throwable $ex ) {
1595  // Already logged; move on...
1596  }
1597  }
1598 
1604  private function consumeTrxShortId() {
1605  $old = $this->trxShortId;
1606  $this->trxShortId = '';
1607 
1608  return $old;
1609  }
1610 
1621  protected function wasQueryTimeout( $error, $errno ) {
1622  return false;
1623  }
1624 
1636  public function reportQueryError( $error, $errno, $sql, $fname, $ignore = false ) {
1637  if ( $ignore ) {
1638  $this->queryLogger->debug( "SQL ERROR (ignored): $error" );
1639  } else {
1640  throw $this->getQueryExceptionAndLog( $error, $errno, $sql, $fname );
1641  }
1642  }
1643 
1651  private function getQueryExceptionAndLog( $error, $errno, $sql, $fname ) {
1652  $this->queryLogger->error(
1653  "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
1654  $this->getLogContext( [
1655  'method' => __METHOD__,
1656  'errno' => $errno,
1657  'error' => $error,
1658  'sql1line' => mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 ),
1659  'fname' => $fname,
1660  'exception' => new RuntimeException()
1661  ] )
1662  );
1663  return $this->getQueryException( $error, $errno, $sql, $fname );
1664  }
1665 
1673  private function getQueryException( $error, $errno, $sql, $fname ) {
1674  if ( $this->wasQueryTimeout( $error, $errno ) ) {
1675  return new DBQueryTimeoutError( $this, $error, $errno, $sql, $fname );
1676  } elseif ( $this->wasConnectionError( $errno ) ) {
1677  return new DBQueryDisconnectedError( $this, $error, $errno, $sql, $fname );
1678  } else {
1679  return new DBQueryError( $this, $error, $errno, $sql, $fname );
1680  }
1681  }
1682 
1687  final protected function newExceptionAfterConnectError( $error ) {
1688  // Connection was not fully initialized and is not safe for use
1689  $this->conn = null;
1690 
1691  $this->connLogger->error(
1692  "Error connecting to {db_server} as user {db_user}: {error}",
1693  $this->getLogContext( [
1694  'error' => $error,
1695  'exception' => new RuntimeException()
1696  ] )
1697  );
1698 
1699  return new DBConnectionError( $this, $error );
1700  }
1701 
1702  public function freeResult( $res ) {
1703  }
1704 
1705  public function newSelectQueryBuilder() {
1706  return new SelectQueryBuilder( $this );
1707  }
1708 
1709  public function selectField(
1710  $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
1711  ) {
1712  if ( $var === '*' ) { // sanity
1713  throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
1714  }
1715 
1716  $options = $this->normalizeOptions( $options );
1717  $options['LIMIT'] = 1;
1718 
1719  $res = $this->select( $table, $var, $cond, $fname, $options, $join_conds );
1720  if ( $res === false ) {
1721  throw new DBUnexpectedError( $this, "Got false from select()" );
1722  }
1723 
1724  $row = $this->fetchRow( $res );
1725  if ( $row === false ) {
1726  return false;
1727  }
1728 
1729  return reset( $row );
1730  }
1731 
1732  public function selectFieldValues(
1733  $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
1734  ) {
1735  if ( $var === '*' ) { // sanity
1736  throw new DBUnexpectedError( $this, "Cannot use a * field" );
1737  } elseif ( !is_string( $var ) ) { // sanity
1738  throw new DBUnexpectedError( $this, "Cannot use an array of fields" );
1739  }
1740 
1741  $options = $this->normalizeOptions( $options );
1742  $res = $this->select( $table, [ 'value' => $var ], $cond, $fname, $options, $join_conds );
1743  if ( $res === false ) {
1744  throw new DBUnexpectedError( $this, "Got false from select()" );
1745  }
1746 
1747  $values = [];
1748  foreach ( $res as $row ) {
1749  $values[] = $row->value;
1750  }
1751 
1752  return $values;
1753  }
1754 
1765  protected function makeSelectOptions( array $options ) {
1766  $preLimitTail = $postLimitTail = '';
1767  $startOpts = '';
1768 
1769  $noKeyOptions = [];
1770 
1771  foreach ( $options as $key => $option ) {
1772  if ( is_numeric( $key ) ) {
1773  $noKeyOptions[$option] = true;
1774  }
1775  }
1776 
1777  $preLimitTail .= $this->makeGroupByWithHaving( $options );
1778 
1779  $preLimitTail .= $this->makeOrderBy( $options );
1780 
1781  if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1782  $postLimitTail .= ' FOR UPDATE';
1783  }
1784 
1785  if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1786  $postLimitTail .= ' LOCK IN SHARE MODE';
1787  }
1788 
1789  if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1790  $startOpts .= 'DISTINCT';
1791  }
1792 
1793  # Various MySQL extensions
1794  if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
1795  $startOpts .= ' /*! STRAIGHT_JOIN */';
1796  }
1797 
1798  if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
1799  $startOpts .= ' SQL_BIG_RESULT';
1800  }
1801 
1802  if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
1803  $startOpts .= ' SQL_BUFFER_RESULT';
1804  }
1805 
1806  if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
1807  $startOpts .= ' SQL_SMALL_RESULT';
1808  }
1809 
1810  if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
1811  $startOpts .= ' SQL_CALC_FOUND_ROWS';
1812  }
1813 
1814  if ( isset( $options['USE INDEX'] ) && is_string( $options['USE INDEX'] ) ) {
1815  $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1816  } else {
1817  $useIndex = '';
1818  }
1819  if ( isset( $options['IGNORE INDEX'] ) && is_string( $options['IGNORE INDEX'] ) ) {
1820  $ignoreIndex = $this->ignoreIndexClause( $options['IGNORE INDEX'] );
1821  } else {
1822  $ignoreIndex = '';
1823  }
1824 
1825  return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1826  }
1827 
1836  protected function makeGroupByWithHaving( $options ) {
1837  $sql = '';
1838  if ( isset( $options['GROUP BY'] ) ) {
1839  $gb = is_array( $options['GROUP BY'] )
1840  ? implode( ',', $options['GROUP BY'] )
1841  : $options['GROUP BY'];
1842  $sql .= ' GROUP BY ' . $gb;
1843  }
1844  if ( isset( $options['HAVING'] ) ) {
1845  $having = is_array( $options['HAVING'] )
1846  ? $this->makeList( $options['HAVING'], self::LIST_AND )
1847  : $options['HAVING'];
1848  $sql .= ' HAVING ' . $having;
1849  }
1850 
1851  return $sql;
1852  }
1853 
1862  protected function makeOrderBy( $options ) {
1863  if ( isset( $options['ORDER BY'] ) ) {
1864  $ob = is_array( $options['ORDER BY'] )
1865  ? implode( ',', $options['ORDER BY'] )
1866  : $options['ORDER BY'];
1867 
1868  return ' ORDER BY ' . $ob;
1869  }
1870 
1871  return '';
1872  }
1873 
1874  public function select(
1875  $table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
1876  ) {
1877  $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
1878 
1879  return $this->query( $sql, $fname, self::QUERY_CHANGE_NONE );
1880  }
1881 
1882  public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__,
1883  $options = [], $join_conds = []
1884  ) {
1885  if ( is_array( $vars ) ) {
1886  $fields = implode( ',', $this->fieldNamesWithAlias( $vars ) );
1887  } else {
1888  $fields = $vars;
1889  }
1890 
1891  $options = (array)$options;
1892  $useIndexes = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
1893  ? $options['USE INDEX']
1894  : [];
1895  $ignoreIndexes = (
1896  isset( $options['IGNORE INDEX'] ) &&
1897  is_array( $options['IGNORE INDEX'] )
1898  )
1899  ? $options['IGNORE INDEX']
1900  : [];
1901 
1902  if (
1903  $this->selectOptionsIncludeLocking( $options ) &&
1904  $this->selectFieldsOrOptionsAggregate( $vars, $options )
1905  ) {
1906  // Some DB types (e.g. postgres) disallow FOR UPDATE with aggregate
1907  // functions. Discourage use of such queries to encourage compatibility.
1908  call_user_func(
1909  $this->deprecationLogger,
1910  __METHOD__ . ": aggregation used with a locking SELECT ($fname)"
1911  );
1912  }
1913 
1914  if ( is_array( $table ) ) {
1915  if ( count( $table ) === 0 ) {
1916  $from = '';
1917  } else {
1918  $from = ' FROM ' .
1920  $table, $useIndexes, $ignoreIndexes, $join_conds );
1921  }
1922  } elseif ( $table != '' ) {
1923  $from = ' FROM ' .
1925  [ $table ], $useIndexes, $ignoreIndexes, [] );
1926  } else {
1927  $from = '';
1928  }
1929 
1930  list( $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ) =
1931  $this->makeSelectOptions( $options );
1932 
1933  if ( is_array( $conds ) ) {
1934  $conds = $this->makeList( $conds, self::LIST_AND );
1935  }
1936 
1937  if ( $conds === null || $conds === false ) {
1938  $this->queryLogger->warning(
1939  __METHOD__
1940  . ' called from '
1941  . $fname
1942  . ' with incorrect parameters: $conds must be a string or an array'
1943  );
1944  $conds = '';
1945  }
1946 
1947  if ( $conds === '' || $conds === '*' ) {
1948  $sql = "SELECT $startOpts $fields $from $useIndex $ignoreIndex $preLimitTail";
1949  } elseif ( is_string( $conds ) ) {
1950  $sql = "SELECT $startOpts $fields $from $useIndex $ignoreIndex " .
1951  "WHERE $conds $preLimitTail";
1952  } else {
1953  throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
1954  }
1955 
1956  if ( isset( $options['LIMIT'] ) ) {
1957  $sql = $this->limitResult( $sql, $options['LIMIT'],
1958  $options['OFFSET'] ?? false );
1959  }
1960  $sql = "$sql $postLimitTail";
1961 
1962  if ( isset( $options['EXPLAIN'] ) ) {
1963  $sql = 'EXPLAIN ' . $sql;
1964  }
1965 
1966  return $sql;
1967  }
1968 
1969  public function selectRow( $table, $vars, $conds, $fname = __METHOD__,
1970  $options = [], $join_conds = []
1971  ) {
1972  $options = (array)$options;
1973  $options['LIMIT'] = 1;
1974 
1975  $res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
1976  if ( $res === false ) {
1977  throw new DBUnexpectedError( $this, "Got false from select()" );
1978  }
1979 
1980  if ( !$this->numRows( $res ) ) {
1981  return false;
1982  }
1983 
1984  return $this->fetchObject( $res );
1985  }
1986 
1987  public function estimateRowCount(
1988  $tables, $var = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
1989  ) {
1990  $conds = $this->normalizeConditions( $conds, $fname );
1991  $column = $this->extractSingleFieldFromList( $var );
1992  if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
1993  $conds[] = "$column IS NOT NULL";
1994  }
1995 
1996  $res = $this->select(
1997  $tables, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options, $join_conds
1998  );
1999  $row = $res ? $this->fetchRow( $res ) : [];
2000 
2001  return isset( $row['rowcount'] ) ? (int)$row['rowcount'] : 0;
2002  }
2003 
2004  public function selectRowCount(
2005  $tables, $var = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
2006  ) {
2007  $conds = $this->normalizeConditions( $conds, $fname );
2008  $column = $this->extractSingleFieldFromList( $var );
2009  if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
2010  $conds[] = "$column IS NOT NULL";
2011  }
2012 
2013  $res = $this->select(
2014  [
2015  'tmp_count' => $this->buildSelectSubquery(
2016  $tables,
2017  '1',
2018  $conds,
2019  $fname,
2020  $options,
2021  $join_conds
2022  )
2023  ],
2024  [ 'rowcount' => 'COUNT(*)' ],
2025  [],
2026  $fname
2027  );
2028  $row = $res ? $this->fetchRow( $res ) : [];
2029 
2030  return isset( $row['rowcount'] ) ? (int)$row['rowcount'] : 0;
2031  }
2032 
2037  private function selectOptionsIncludeLocking( $options ) {
2038  $options = (array)$options;
2039  foreach ( [ 'FOR UPDATE', 'LOCK IN SHARE MODE' ] as $lock ) {
2040  if ( in_array( $lock, $options, true ) ) {
2041  return true;
2042  }
2043  }
2044 
2045  return false;
2046  }
2047 
2053  private function selectFieldsOrOptionsAggregate( $fields, $options ) {
2054  foreach ( (array)$options as $key => $value ) {
2055  if ( is_string( $key ) ) {
2056  if ( preg_match( '/^(?:GROUP BY|HAVING)$/i', $key ) ) {
2057  return true;
2058  }
2059  } elseif ( is_string( $value ) ) {
2060  if ( preg_match( '/^(?:DISTINCT|DISTINCTROW)$/i', $value ) ) {
2061  return true;
2062  }
2063  }
2064  }
2065 
2066  $regex = '/^(?:COUNT|MIN|MAX|SUM|GROUP_CONCAT|LISTAGG|ARRAY_AGG)\s*\\(/i';
2067  foreach ( (array)$fields as $field ) {
2068  if ( is_string( $field ) && preg_match( $regex, $field ) ) {
2069  return true;
2070  }
2071  }
2072 
2073  return false;
2074  }
2075 
2081  final protected function normalizeRowArray( array $rowOrRows ) {
2082  if ( !$rowOrRows ) {
2083  $rows = [];
2084  } elseif ( isset( $rowOrRows[0] ) ) {
2085  $rows = $rowOrRows;
2086  } else {
2087  $rows = [ $rowOrRows ];
2088  }
2089 
2090  foreach ( $rows as $row ) {
2091  if ( !is_array( $row ) ) {
2092  throw new DBUnexpectedError( $this, "Got non-array in row array" );
2093  } elseif ( !$row ) {
2094  throw new DBUnexpectedError( $this, "Got empty array in row array" );
2095  }
2096  }
2097 
2098  return $rows;
2099  }
2100 
2107  final protected function normalizeConditions( $conds, $fname ) {
2108  if ( $conds === null || $conds === false ) {
2109  $this->queryLogger->warning(
2110  __METHOD__
2111  . ' called from '
2112  . $fname
2113  . ' with incorrect parameters: $conds must be a string or an array'
2114  );
2115  return [];
2116  } elseif ( $conds === '' ) {
2117  return [];
2118  }
2119 
2120  return is_array( $conds ) ? $conds : [ $conds ];
2121  }
2122 
2128  final protected function normalizeUpsertKeys( $uniqueKeys ) {
2129  if ( is_string( $uniqueKeys ) ) {
2130  return [ [ $uniqueKeys ] ];
2131  }
2132 
2133  if ( !is_array( $uniqueKeys ) || !$uniqueKeys ) {
2134  throw new DBUnexpectedError( $this, 'Invalid or empty unique key array' );
2135  }
2136 
2137  $oldStyle = false;
2138  $uniqueColumnSets = [];
2139  foreach ( $uniqueKeys as $i => $uniqueKey ) {
2140  if ( !is_int( $i ) ) {
2141  throw new DBUnexpectedError( $this, 'Unique key array should be a list' );
2142  } elseif ( is_string( $uniqueKey ) ) {
2143  $oldStyle = true;
2144  $uniqueColumnSets[] = [ $uniqueKey ];
2145  } elseif ( is_array( $uniqueKey ) && $uniqueKey ) {
2146  $uniqueColumnSets[] = $uniqueKey;
2147  } else {
2148  throw new DBUnexpectedError( $this, 'Invalid unique key array entry' );
2149  }
2150  }
2151 
2152  if ( count( $uniqueColumnSets ) > 1 ) {
2153  // If an existing row conflicts with new row X on key A and new row Y on key B,
2154  // it is not well defined how many UPDATEs should apply to the existing row and
2155  // in what order the new rows are checked
2156  $this->queryLogger->warning(
2157  __METHOD__ . " called with multiple unique keys",
2158  [ 'exception' => new RuntimeException() ]
2159  );
2160  }
2161 
2162  if ( $oldStyle ) {
2163  // Passing a list of strings for single-column unique keys is too
2164  // easily confused with passing the columns of composite unique key
2165  $this->queryLogger->warning(
2166  __METHOD__ . " called with deprecated parameter style: " .
2167  "the unique key array should be a string or array of string arrays",
2168  [ 'exception' => new RuntimeException() ]
2169  );
2170  }
2171 
2172  return $uniqueColumnSets;
2173  }
2174 
2180  final protected function normalizeOptions( $options ) {
2181  if ( is_array( $options ) ) {
2182  return $options;
2183  } elseif ( is_string( $options ) ) {
2184  return ( $options === '' ) ? [] : [ $options ];
2185  } else {
2186  throw new DBUnexpectedError( $this, __METHOD__ . ': expected string or array' );
2187  }
2188  }
2189 
2196  final protected function isFlagInOptions( $option, array $options ) {
2197  foreach ( array_keys( $options, $option, true ) as $k ) {
2198  if ( is_int( $k ) ) {
2199  return true;
2200  }
2201  }
2202 
2203  return false;
2204  }
2205 
2210  final protected function extractSingleFieldFromList( $var ) {
2211  if ( is_array( $var ) ) {
2212  if ( !$var ) {
2213  $column = null;
2214  } elseif ( count( $var ) == 1 ) {
2215  $column = $var[0] ?? reset( $var );
2216  } else {
2217  throw new DBUnexpectedError( $this, __METHOD__ . ': got multiple columns' );
2218  }
2219  } else {
2220  $column = $var;
2221  }
2222 
2223  return $column;
2224  }
2225 
2226  public function lockForUpdate(
2227  $table, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
2228  ) {
2229  if ( !$this->trxLevel() && !$this->getFlag( self::DBO_TRX ) ) {
2230  throw new DBUnexpectedError(
2231  $this,
2232  __METHOD__ . ': no transaction is active nor is DBO_TRX set'
2233  );
2234  }
2235 
2236  $options = (array)$options;
2237  $options[] = 'FOR UPDATE';
2238 
2239  return $this->selectRowCount( $table, '*', $conds, $fname, $options, $join_conds );
2240  }
2241 
2242  public function fieldExists( $table, $field, $fname = __METHOD__ ) {
2243  $info = $this->fieldInfo( $table, $field );
2244 
2245  return (bool)$info;
2246  }
2247 
2248  public function indexExists( $table, $index, $fname = __METHOD__ ) {
2249  if ( !$this->tableExists( $table ) ) {
2250  return null;
2251  }
2252 
2253  $info = $this->indexInfo( $table, $index, $fname );
2254  if ( $info === null ) {
2255  return null;
2256  } else {
2257  return $info !== false;
2258  }
2259  }
2260 
2261  abstract public function tableExists( $table, $fname = __METHOD__ );
2262 
2263  public function indexUnique( $table, $index ) {
2264  $indexInfo = $this->indexInfo( $table, $index );
2265 
2266  if ( !$indexInfo ) {
2267  return null;
2268  }
2269 
2270  return !$indexInfo[0]->Non_unique;
2271  }
2272 
2273  public function insert( $table, $rows, $fname = __METHOD__, $options = [] ) {
2274  $rows = $this->normalizeRowArray( $rows );
2275  if ( !$rows ) {
2276  return true;
2277  }
2278 
2279  $options = $this->normalizeOptions( $options );
2280  if ( $this->isFlagInOptions( 'IGNORE', $options ) ) {
2281  $this->doInsertNonConflicting( $table, $rows, $fname );
2282  } else {
2283  $this->doInsert( $table, $rows, $fname );
2284  }
2285 
2286  return true;
2287  }
2288 
2296  protected function doInsert( $table, array $rows, $fname ) {
2297  $encTable = $this->tableName( $table );
2298  list( $sqlColumns, $sqlTuples ) = $this->makeInsertLists( $rows );
2299 
2300  $sql = "INSERT INTO $encTable ($sqlColumns) VALUES $sqlTuples";
2301 
2302  $this->query( $sql, $fname, self::QUERY_CHANGE_ROWS );
2303  }
2304 
2312  protected function doInsertNonConflicting( $table, array $rows, $fname ) {
2313  $encTable = $this->tableName( $table );
2314  list( $sqlColumns, $sqlTuples ) = $this->makeInsertLists( $rows );
2315  list( $sqlVerb, $sqlOpts ) = $this->makeInsertNonConflictingVerbAndOptions();
2316 
2317  $sql = rtrim( "$sqlVerb $encTable ($sqlColumns) VALUES $sqlTuples $sqlOpts" );
2318 
2319  $this->query( $sql, $fname, self::QUERY_CHANGE_ROWS );
2320  }
2321 
2327  return [ 'INSERT IGNORE INTO', '' ];
2328  }
2329 
2340  protected function makeInsertLists( array $rows ) {
2341  $firstRow = $rows[0];
2342  if ( !is_array( $firstRow ) || !$firstRow ) {
2343  throw new DBUnexpectedError( $this, 'Got an empty row list or empty row' );
2344  }
2345  // List of columns that define the value tuple ordering
2346  $tupleColumns = array_keys( $firstRow );
2347 
2348  $valueTuples = [];
2349  foreach ( $rows as $row ) {
2350  $rowColumns = array_keys( $row );
2351  // VALUES(...) requires a uniform correspondance of (column => value)
2352  if ( $rowColumns !== $tupleColumns ) {
2353  throw new DBUnexpectedError(
2354  $this,
2355  'Got row columns (' . implode( ', ', $rowColumns ) . ') ' .
2356  'instead of expected (' . implode( ', ', $tupleColumns ) . ')'
2357  );
2358  }
2359  // Make the value tuple that defines this row
2360  $valueTuples[] = '(' . $this->makeList( $row, self::LIST_COMMA ) . ')';
2361  }
2362 
2363  return [
2364  $this->makeList( $tupleColumns, self::LIST_NAMES ),
2365  implode( ',', $valueTuples )
2366  ];
2367  }
2368 
2375  protected function makeUpdateOptionsArray( $options ) {
2376  $options = $this->normalizeOptions( $options );
2377 
2378  $opts = [];
2379 
2380  if ( in_array( 'IGNORE', $options ) ) {
2381  $opts[] = 'IGNORE';
2382  }
2383 
2384  return $opts;
2385  }
2386 
2393  protected function makeUpdateOptions( $options ) {
2394  $opts = $this->makeUpdateOptionsArray( $options );
2395 
2396  return implode( ' ', $opts );
2397  }
2398 
2399  public function update( $table, $set, $conds, $fname = __METHOD__, $options = [] ) {
2400  $this->assertConditionIsNotEmpty( $conds, __METHOD__, true );
2401  $table = $this->tableName( $table );
2402  $opts = $this->makeUpdateOptions( $options );
2403  $sql = "UPDATE $opts $table SET " . $this->makeList( $set, self::LIST_SET );
2404 
2405  if ( $conds && $conds !== IDatabase::ALL_ROWS ) {
2406  if ( is_array( $conds ) ) {
2407  $conds = $this->makeList( $conds, self::LIST_AND );
2408  }
2409  $sql .= ' WHERE ' . $conds;
2410  }
2411 
2412  $this->query( $sql, $fname, self::QUERY_CHANGE_ROWS );
2413 
2414  return true;
2415  }
2416 
2417  public function makeList( array $a, $mode = self::LIST_COMMA ) {
2418  $first = true;
2419  $list = '';
2420 
2421  foreach ( $a as $field => $value ) {
2422  if ( !$first ) {
2423  if ( $mode == self::LIST_AND ) {
2424  $list .= ' AND ';
2425  } elseif ( $mode == self::LIST_OR ) {
2426  $list .= ' OR ';
2427  } else {
2428  $list .= ',';
2429  }
2430  } else {
2431  $first = false;
2432  }
2433 
2434  if ( ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_numeric( $field ) ) {
2435  $list .= "($value)";
2436  } elseif ( $mode == self::LIST_SET && is_numeric( $field ) ) {
2437  $list .= "$value";
2438  } elseif (
2439  ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_array( $value )
2440  ) {
2441  // Remove null from array to be handled separately if found
2442  $includeNull = false;
2443  foreach ( array_keys( $value, null, true ) as $nullKey ) {
2444  $includeNull = true;
2445  unset( $value[$nullKey] );
2446  }
2447  if ( count( $value ) == 0 && !$includeNull ) {
2448  throw new InvalidArgumentException(
2449  __METHOD__ . ": empty input for field $field" );
2450  } elseif ( count( $value ) == 0 ) {
2451  // only check if $field is null
2452  $list .= "$field IS NULL";
2453  } else {
2454  // IN clause contains at least one valid element
2455  if ( $includeNull ) {
2456  // Group subconditions to ensure correct precedence
2457  $list .= '(';
2458  }
2459  if ( count( $value ) == 1 ) {
2460  // Special-case single values, as IN isn't terribly efficient
2461  // Don't necessarily assume the single key is 0; we don't
2462  // enforce linear numeric ordering on other arrays here.
2463  $value = array_values( $value )[0];
2464  $list .= $field . " = " . $this->addQuotes( $value );
2465  } else {
2466  $list .= $field . " IN (" . $this->makeList( $value ) . ") ";
2467  }
2468  // if null present in array, append IS NULL
2469  if ( $includeNull ) {
2470  $list .= " OR $field IS NULL)";
2471  }
2472  }
2473  } elseif ( $value === null ) {
2474  if ( $mode == self::LIST_AND || $mode == self::LIST_OR ) {
2475  $list .= "$field IS ";
2476  } elseif ( $mode == self::LIST_SET ) {
2477  $list .= "$field = ";
2478  }
2479  $list .= 'NULL';
2480  } else {
2481  if (
2482  $mode == self::LIST_AND || $mode == self::LIST_OR || $mode == self::LIST_SET
2483  ) {
2484  $list .= "$field = ";
2485  }
2486  $list .= $mode == self::LIST_NAMES ? $value : $this->addQuotes( $value );
2487  }
2488  }
2489 
2490  return $list;
2491  }
2492 
2493  public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
2494  $conds = [];
2495 
2496  foreach ( $data as $base => $sub ) {
2497  if ( count( $sub ) ) {
2498  $conds[] = $this->makeList(
2499  [ $baseKey => $base, $subKey => array_map( 'strval', array_keys( $sub ) ) ],
2500  self::LIST_AND );
2501  }
2502  }
2503 
2504  if ( $conds ) {
2505  return $this->makeList( $conds, self::LIST_OR );
2506  } else {
2507  // Nothing to search for...
2508  return false;
2509  }
2510  }
2511 
2512  public function aggregateValue( $valuedata, $valuename = 'value' ) {
2513  return $valuename;
2514  }
2515 
2516  public function bitNot( $field ) {
2517  return "(~$field)";
2518  }
2519 
2520  public function bitAnd( $fieldLeft, $fieldRight ) {
2521  return "($fieldLeft & $fieldRight)";
2522  }
2523 
2524  public function bitOr( $fieldLeft, $fieldRight ) {
2525  return "($fieldLeft | $fieldRight)";
2526  }
2527 
2528  public function buildConcat( $stringList ) {
2529  return 'CONCAT(' . implode( ',', $stringList ) . ')';
2530  }
2531 
2532  public function buildGroupConcatField(
2533  $delim, $table, $field, $conds = '', $join_conds = []
2534  ) {
2535  $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')';
2536 
2537  return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
2538  }
2539 
2540  public function buildGreatest( $fields, $values ) {
2541  return $this->buildSuperlative( 'GREATEST', $fields, $values );
2542  }
2543 
2544  public function buildLeast( $fields, $values ) {
2545  return $this->buildSuperlative( 'LEAST', $fields, $values );
2546  }
2547 
2563  protected function buildSuperlative( $sqlfunc, $fields, $values ) {
2564  $fields = is_array( $fields ) ? $fields : [ $fields ];
2565  $values = is_array( $values ) ? $values : [ $values ];
2566 
2567  $encValues = [];
2568  foreach ( $fields as $alias => $field ) {
2569  if ( is_int( $alias ) ) {
2570  $encValues[] = $this->addIdentifierQuotes( $field );
2571  } else {
2572  $encValues[] = $field; // expression
2573  }
2574  }
2575  foreach ( $values as $value ) {
2576  if ( is_int( $value ) || is_float( $value ) ) {
2577  $encValues[] = $value;
2578  } elseif ( is_string( $value ) ) {
2579  $encValues[] = $this->addQuotes( $value );
2580  } elseif ( $value === null ) {
2581  throw new DBUnexpectedError( $this, 'Null value in superlative' );
2582  } else {
2583  throw new DBUnexpectedError( $this, 'Unexpected value type in superlative' );
2584  }
2585  }
2586 
2587  return $sqlfunc . '(' . implode( ',', $encValues ) . ')';
2588  }
2589 
2590  public function buildSubstring( $input, $startPosition, $length = null ) {
2591  $this->assertBuildSubstringParams( $startPosition, $length );
2592  $functionBody = "$input FROM $startPosition";
2593  if ( $length !== null ) {
2594  $functionBody .= " FOR $length";
2595  }
2596  return 'SUBSTRING(' . $functionBody . ')';
2597  }
2598 
2611  protected function assertBuildSubstringParams( $startPosition, $length ) {
2612  if ( !is_int( $startPosition ) || $startPosition <= 0 ) {
2613  throw new InvalidArgumentException(
2614  '$startPosition must be a positive integer'
2615  );
2616  }
2617  if ( !( is_int( $length ) && $length >= 0 || $length === null ) ) {
2618  throw new InvalidArgumentException(
2619  '$length must be null or an integer greater than or equal to 0'
2620  );
2621  }
2622  }
2623 
2637  protected function assertConditionIsNotEmpty( $conds, string $fname, bool $deprecate ) {
2638  $isCondValid = ( is_string( $conds ) || is_array( $conds ) ) && $conds;
2639  if ( !$isCondValid ) {
2640  if ( $deprecate ) {
2641  wfDeprecated( $fname . ' called with empty $conds', '1.35', false, 3 );
2642  } else {
2643  throw new DBUnexpectedError( $this, $fname . ' called with empty conditions' );
2644  }
2645  }
2646  }
2647 
2648  public function buildStringCast( $field ) {
2649  // In theory this should work for any standards-compliant
2650  // SQL implementation, although it may not be the best way to do it.
2651  return "CAST( $field AS CHARACTER )";
2652  }
2653 
2654  public function buildIntegerCast( $field ) {
2655  return 'CAST( ' . $field . ' AS INTEGER )';
2656  }
2657 
2658  public function buildSelectSubquery(
2659  $table, $vars, $conds = '', $fname = __METHOD__,
2660  $options = [], $join_conds = []
2661  ) {
2662  return new Subquery(
2663  $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds )
2664  );
2665  }
2666 
2667  public function databasesAreIndependent() {
2668  return false;
2669  }
2670 
2671  final public function selectDB( $db ) {
2672  $this->selectDomain( new DatabaseDomain(
2673  $db,
2674  $this->currentDomain->getSchema(),
2675  $this->currentDomain->getTablePrefix()
2676  ) );
2677 
2678  return true;
2679  }
2680 
2681  final public function selectDomain( $domain ) {
2682  $this->doSelectDomain( DatabaseDomain::newFromId( $domain ) );
2683  }
2684 
2691  protected function doSelectDomain( DatabaseDomain $domain ) {
2692  $this->currentDomain = $domain;
2693  }
2694 
2695  public function getDBname() {
2696  return $this->currentDomain->getDatabase();
2697  }
2698 
2699  public function getServer() {
2700  return $this->server;
2701  }
2702 
2703  public function tableName( $name, $format = 'quoted' ) {
2704  if ( $name instanceof Subquery ) {
2705  throw new DBUnexpectedError(
2706  $this,
2707  __METHOD__ . ': got Subquery instance when expecting a string'
2708  );
2709  }
2710 
2711  # Skip the entire process when we have a string quoted on both ends.
2712  # Note that we check the end so that we will still quote any use of
2713  # use of `database`.table. But won't break things if someone wants
2714  # to query a database table with a dot in the name.
2715  if ( $this->isQuotedIdentifier( $name ) ) {
2716  return $name;
2717  }
2718 
2719  # Lets test for any bits of text that should never show up in a table
2720  # name. Basically anything like JOIN or ON which are actually part of
2721  # SQL queries, but may end up inside of the table value to combine
2722  # sql. Such as how the API is doing.
2723  # Note that we use a whitespace test rather than a \b test to avoid
2724  # any remote case where a word like on may be inside of a table name
2725  # surrounded by symbols which may be considered word breaks.
2726  if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
2727  $this->queryLogger->warning(
2728  __METHOD__ . ": use of subqueries is not supported this way",
2729  [ 'exception' => new RuntimeException() ]
2730  );
2731 
2732  return $name;
2733  }
2734 
2735  # Split database and table into proper variables.
2736  list( $database, $schema, $prefix, $table ) = $this->qualifiedTableComponents( $name );
2737 
2738  # Quote $table and apply the prefix if not quoted.
2739  # $tableName might be empty if this is called from Database::replaceVars()
2740  $tableName = "{$prefix}{$table}";
2741  if ( $format === 'quoted'
2742  && !$this->isQuotedIdentifier( $tableName )
2743  && $tableName !== ''
2744  ) {
2745  $tableName = $this->addIdentifierQuotes( $tableName );
2746  }
2747 
2748  # Quote $schema and $database and merge them with the table name if needed
2749  $tableName = $this->prependDatabaseOrSchema( $schema, $tableName, $format );
2750  $tableName = $this->prependDatabaseOrSchema( $database, $tableName, $format );
2751 
2752  return $tableName;
2753  }
2754 
2761  protected function qualifiedTableComponents( $name ) {
2762  # We reverse the explode so that database.table and table both output the correct table.
2763  $dbDetails = explode( '.', $name, 3 );
2764  if ( count( $dbDetails ) == 3 ) {
2765  list( $database, $schema, $table ) = $dbDetails;
2766  # We don't want any prefix added in this case
2767  $prefix = '';
2768  } elseif ( count( $dbDetails ) == 2 ) {
2769  list( $database, $table ) = $dbDetails;
2770  # We don't want any prefix added in this case
2771  $prefix = '';
2772  # In dbs that support it, $database may actually be the schema
2773  # but that doesn't affect any of the functionality here
2774  $schema = '';
2775  } else {
2776  list( $table ) = $dbDetails;
2777  if ( isset( $this->tableAliases[$table] ) ) {
2778  $database = $this->tableAliases[$table]['dbname'];
2779  $schema = is_string( $this->tableAliases[$table]['schema'] )
2780  ? $this->tableAliases[$table]['schema']
2781  : $this->relationSchemaQualifier();
2782  $prefix = is_string( $this->tableAliases[$table]['prefix'] )
2783  ? $this->tableAliases[$table]['prefix']
2784  : $this->tablePrefix();
2785  } else {
2786  $database = '';
2787  $schema = $this->relationSchemaQualifier(); # Default schema
2788  $prefix = $this->tablePrefix(); # Default prefix
2789  }
2790  }
2791 
2792  return [ $database, $schema, $prefix, $table ];
2793  }
2794 
2801  private function prependDatabaseOrSchema( $namespace, $relation, $format ) {
2802  if ( strlen( $namespace ) ) {
2803  if ( $format === 'quoted' && !$this->isQuotedIdentifier( $namespace ) ) {
2804  $namespace = $this->addIdentifierQuotes( $namespace );
2805  }
2806  $relation = $namespace . '.' . $relation;
2807  }
2808 
2809  return $relation;
2810  }
2811 
2812  public function tableNames( ...$tables ) {
2813  $retVal = [];
2814 
2815  foreach ( $tables as $name ) {
2816  $retVal[$name] = $this->tableName( $name );
2817  }
2818 
2819  return $retVal;
2820  }
2821 
2822  public function tableNamesN( ...$tables ) {
2823  $retVal = [];
2824 
2825  foreach ( $tables as $name ) {
2826  $retVal[] = $this->tableName( $name );
2827  }
2828 
2829  return $retVal;
2830  }
2831 
2843  protected function tableNameWithAlias( $table, $alias = false ) {
2844  if ( is_string( $table ) ) {
2845  $quotedTable = $this->tableName( $table );
2846  } elseif ( $table instanceof Subquery ) {
2847  $quotedTable = (string)$table;
2848  } else {
2849  throw new InvalidArgumentException( "Table must be a string or Subquery" );
2850  }
2851 
2852  if ( $alias === false || $alias === $table ) {
2853  if ( $table instanceof Subquery ) {
2854  throw new InvalidArgumentException( "Subquery table missing alias" );
2855  }
2856 
2857  return $quotedTable;
2858  } else {
2859  return $quotedTable . ' ' . $this->addIdentifierQuotes( $alias );
2860  }
2861  }
2862 
2871  protected function fieldNameWithAlias( $name, $alias = false ) {
2872  if ( !$alias || (string)$alias === (string)$name ) {
2873  return $name;
2874  } else {
2875  return $name . ' AS ' . $this->addIdentifierQuotes( $alias ); // PostgreSQL needs AS
2876  }
2877  }
2878 
2885  protected function fieldNamesWithAlias( $fields ) {
2886  $retval = [];
2887  foreach ( $fields as $alias => $field ) {
2888  if ( is_numeric( $alias ) ) {
2889  $alias = $field;
2890  }
2891  $retval[] = $this->fieldNameWithAlias( $field, $alias );
2892  }
2893 
2894  return $retval;
2895  }
2896 
2908  $tables, $use_index = [], $ignore_index = [], $join_conds = []
2909  ) {
2910  $ret = [];
2911  $retJOIN = [];
2912  $use_index = (array)$use_index;
2913  $ignore_index = (array)$ignore_index;
2914  $join_conds = (array)$join_conds;
2915 
2916  foreach ( $tables as $alias => $table ) {
2917  if ( !is_string( $alias ) ) {
2918  // No alias? Set it equal to the table name
2919  $alias = $table;
2920  }
2921 
2922  if ( is_array( $table ) ) {
2923  // A parenthesized group
2924  if ( count( $table ) > 1 ) {
2925  $joinedTable = '(' .
2927  $table, $use_index, $ignore_index, $join_conds ) . ')';
2928  } else {
2929  // Degenerate case
2930  $innerTable = reset( $table );
2931  $innerAlias = key( $table );
2932  $joinedTable = $this->tableNameWithAlias(
2933  $innerTable,
2934  is_string( $innerAlias ) ? $innerAlias : $innerTable
2935  );
2936  }
2937  } else {
2938  $joinedTable = $this->tableNameWithAlias( $table, $alias );
2939  }
2940 
2941  // Is there a JOIN clause for this table?
2942  if ( isset( $join_conds[$alias] ) ) {
2943  list( $joinType, $conds ) = $join_conds[$alias];
2944  $tableClause = $joinType;
2945  $tableClause .= ' ' . $joinedTable;
2946  if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
2947  $use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
2948  if ( $use != '' ) {
2949  $tableClause .= ' ' . $use;
2950  }
2951  }
2952  if ( isset( $ignore_index[$alias] ) ) { // has IGNORE INDEX?
2953  $ignore = $this->ignoreIndexClause(
2954  implode( ',', (array)$ignore_index[$alias] ) );
2955  if ( $ignore != '' ) {
2956  $tableClause .= ' ' . $ignore;
2957  }
2958  }
2959  $on = $this->makeList( (array)$conds, self::LIST_AND );
2960  if ( $on != '' ) {
2961  $tableClause .= ' ON (' . $on . ')';
2962  }
2963 
2964  $retJOIN[] = $tableClause;
2965  } elseif ( isset( $use_index[$alias] ) ) {
2966  // Is there an INDEX clause for this table?
2967  $tableClause = $joinedTable;
2968  $tableClause .= ' ' . $this->useIndexClause(
2969  implode( ',', (array)$use_index[$alias] )
2970  );
2971 
2972  $ret[] = $tableClause;
2973  } elseif ( isset( $ignore_index[$alias] ) ) {
2974  // Is there an INDEX clause for this table?
2975  $tableClause = $joinedTable;
2976  $tableClause .= ' ' . $this->ignoreIndexClause(
2977  implode( ',', (array)$ignore_index[$alias] )
2978  );
2979 
2980  $ret[] = $tableClause;
2981  } else {
2982  $tableClause = $joinedTable;
2983 
2984  $ret[] = $tableClause;
2985  }
2986  }
2987 
2988  // We can't separate explicit JOIN clauses with ',', use ' ' for those
2989  $implicitJoins = implode( ',', $ret );
2990  $explicitJoins = implode( ' ', $retJOIN );
2991 
2992  // Compile our final table clause
2993  return implode( ' ', [ $implicitJoins, $explicitJoins ] );
2994  }
2995 
3002  protected function indexName( $index ) {
3003  return $this->indexAliases[$index] ?? $index;
3004  }
3005 
3006  public function addQuotes( $s ) {
3007  if ( $s instanceof Blob ) {
3008  $s = $s->fetch();
3009  }
3010  if ( $s === null ) {
3011  return 'NULL';
3012  } elseif ( is_bool( $s ) ) {
3013  return (string)(int)$s;
3014  } elseif ( is_int( $s ) ) {
3015  return (string)$s;
3016  } else {
3017  return "'" . $this->strencode( $s ) . "'";
3018  }
3019  }
3020 
3021  public function addIdentifierQuotes( $s ) {
3022  return '"' . str_replace( '"', '""', $s ) . '"';
3023  }
3024 
3034  public function isQuotedIdentifier( $name ) {
3035  return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
3036  }
3037 
3043  protected function escapeLikeInternal( $s, $escapeChar = '`' ) {
3044  return str_replace( [ $escapeChar, '%', '_' ],
3045  [ "{$escapeChar}{$escapeChar}", "{$escapeChar}%", "{$escapeChar}_" ],
3046  $s );
3047  }
3048 
3049  public function buildLike( $param, ...$params ) {
3050  if ( is_array( $param ) ) {
3051  $params = $param;
3052  } else {
3053  $params = func_get_args();
3054  }
3055 
3056  $s = '';
3057 
3058  // We use ` instead of \ as the default LIKE escape character, since addQuotes()
3059  // may escape backslashes, creating problems of double escaping. The `
3060  // character has good cross-DBMS compatibility, avoiding special operators
3061  // in MS SQL like ^ and %
3062  $escapeChar = '`';
3063 
3064  foreach ( $params as $value ) {
3065  if ( $value instanceof LikeMatch ) {
3066  $s .= $value->toString();
3067  } else {
3068  $s .= $this->escapeLikeInternal( $value, $escapeChar );
3069  }
3070  }
3071 
3072  return ' LIKE ' .
3073  $this->addQuotes( $s ) . ' ESCAPE ' . $this->addQuotes( $escapeChar ) . ' ';
3074  }
3075 
3076  public function anyChar() {
3077  return new LikeMatch( '_' );
3078  }
3079 
3080  public function anyString() {
3081  return new LikeMatch( '%' );
3082  }
3083 
3084  public function nextSequenceValue( $seqName ) {
3085  return null;
3086  }
3087 
3098  public function useIndexClause( $index ) {
3099  return '';
3100  }
3101 
3112  public function ignoreIndexClause( $index ) {
3113  return '';
3114  }
3115 
3116  public function replace( $table, $uniqueKeys, $rows, $fname = __METHOD__ ) {
3117  $rows = $this->normalizeRowArray( $rows );
3118  if ( !$rows ) {
3119  return;
3120  }
3121 
3122  if ( $uniqueKeys ) {
3123  $uniqueKeys = $this->normalizeUpsertKeys( $uniqueKeys );
3124  $this->doReplace( $table, $uniqueKeys, $rows, $fname );
3125  } else {
3126  $this->queryLogger->warning(
3127  __METHOD__ . " called with no unique keys",
3128  [ 'exception' => new RuntimeException() ]
3129  );
3130  $this->doInsert( $table, $rows, $fname );
3131  }
3132  }
3133 
3142  protected function doReplace( $table, array $uniqueKeys, array $rows, $fname ) {
3143  $affectedRowCount = 0;
3144  $this->startAtomic( $fname, self::ATOMIC_CANCELABLE );
3145  try {
3146  foreach ( $rows as $row ) {
3147  // Delete any conflicting rows (including ones inserted from $rows)
3148  $sqlCondition = $this->makeConditionCollidesUponKeys( [ $row ], $uniqueKeys );
3149  $this->delete( $table, [ $sqlCondition ], $fname );
3150  $affectedRowCount += $this->affectedRows();
3151  // Now insert the row
3152  $this->insert( $table, $row, $fname );
3153  $affectedRowCount += $this->affectedRows();
3154  }
3155  $this->endAtomic( $fname );
3156  } catch ( Throwable $e ) {
3157  $this->cancelAtomic( $fname );
3158  throw $e;
3159  }
3160  $this->affectedRowCount = $affectedRowCount;
3161  }
3162 
3168  private function makeConditionCollidesUponKey( array $rows, array $uniqueKey ) {
3169  if ( !$rows ) {
3170  throw new DBUnexpectedError( $this, "Empty row array" );
3171  } elseif ( !$uniqueKey ) {
3172  throw new DBUnexpectedError( $this, "Empty unique key array" );
3173  }
3174 
3175  if ( count( $uniqueKey ) == 1 ) {
3176  // Use a simple IN(...) clause
3177  $column = reset( $uniqueKey );
3178  $values = array_column( $rows, $column );
3179  if ( count( $values ) !== count( $rows ) ) {
3180  throw new DBUnexpectedError( $this, "Missing values for unique key ($column)" );
3181  }
3182 
3183  return $this->makeList( [ $column => $values ], self::LIST_AND );
3184  }
3185 
3186  $disjunctions = [];
3187  foreach ( $rows as $row ) {
3188  $rowKeyMap = array_intersect_key( $row, array_flip( $uniqueKey ) );
3189  if ( count( $rowKeyMap ) != count( $uniqueKey ) ) {
3190  throw new DBUnexpectedError(
3191  $this,
3192  "Missing values for unique key (" . implode( ',', $uniqueKey ) . ")"
3193  );
3194  }
3195  $disjunctions[] = $this->makeList( $rowKeyMap, self::LIST_AND );
3196  }
3197 
3198  return count( $disjunctions ) > 1
3199  ? $this->makeList( $disjunctions, self::LIST_OR )
3200  : $disjunctions[0];
3201  }
3202 
3209  final protected function makeConditionCollidesUponKeys( array $rows, array $uniqueKeys ) {
3210  if ( !$uniqueKeys ) {
3211  throw new DBUnexpectedError( $this, "Empty unique key array" );
3212  }
3213 
3214  $disjunctions = [];
3215  foreach ( $uniqueKeys as $uniqueKey ) {
3216  $disjunctions[] = $this->makeConditionCollidesUponKey( $rows, $uniqueKey );
3217  }
3218 
3219  return count( $disjunctions ) > 1
3220  ? $this->makeList( $disjunctions, self::LIST_OR )
3221  : $disjunctions[0];
3222  }
3223 
3224  public function upsert( $table, array $rows, $uniqueKeys, array $set, $fname = __METHOD__ ) {
3225  $rows = $this->normalizeRowArray( $rows );
3226  if ( !$rows ) {
3227  return true;
3228  }
3229 
3230  if ( $uniqueKeys ) {
3231  $uniqueKeys = $this->normalizeUpsertKeys( $uniqueKeys );
3232  $this->doUpsert( $table, $rows, $uniqueKeys, $set, $fname );
3233  } else {
3234  $this->queryLogger->warning(
3235  __METHOD__ . " called with no unique keys",
3236  [ 'exception' => new RuntimeException() ]
3237  );
3238  $this->doInsert( $table, $rows, $fname );
3239  }
3240 
3241  return true;
3242  }
3243 
3253  protected function doUpsert( $table, array $rows, array $uniqueKeys, array $set, $fname ) {
3254  $affectedRowCount = 0;
3255  $this->startAtomic( $fname, self::ATOMIC_CANCELABLE );
3256  try {
3257  foreach ( $rows as $row ) {
3258  // Update any existing conflicting rows (including ones inserted from $rows)
3259  $sqlConditions = $this->makeConditionCollidesUponKeys( [ $row ], $uniqueKeys );
3260  $this->update( $table, $set, [ $sqlConditions ], $fname );
3261  $rowsUpdated = $this->affectedRows();
3262  $affectedRowCount += $rowsUpdated;
3263  if ( $rowsUpdated <= 0 ) {
3264  // Now insert the row if there are no conflicts
3265  $this->insert( $table, $row, $fname );
3266  $affectedRowCount += $this->affectedRows();
3267  }
3268  }
3269  $this->endAtomic( $fname );
3270  } catch ( Throwable $e ) {
3271  $this->cancelAtomic( $fname );
3272  throw $e;
3273  }
3274  $this->affectedRowCount = $affectedRowCount;
3275  }
3276 
3277  public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
3278  $fname = __METHOD__
3279  ) {
3280  if ( !$conds ) {
3281  throw new DBUnexpectedError( $this, __METHOD__ . ' called with empty $conds' );
3282  }
3283 
3284  $delTable = $this->tableName( $delTable );
3285  $joinTable = $this->tableName( $joinTable );
3286  $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
3287  if ( $conds != '*' ) {
3288  $sql .= 'WHERE ' . $this->makeList( $conds, self::LIST_AND );
3289  }
3290  $sql .= ')';
3291 
3292  $this->query( $sql, $fname, self::QUERY_CHANGE_ROWS );
3293  }
3294 
3295  public function textFieldSize( $table, $field ) {
3296  $table = $this->tableName( $table );
3297  $sql = "SHOW COLUMNS FROM $table LIKE \"$field\"";
3298  $res = $this->query( $sql, __METHOD__, self::QUERY_CHANGE_NONE );
3299  $row = $this->fetchObject( $res );
3300 
3301  $m = [];
3302 
3303  if ( preg_match( '/\((.*)\)/', $row->Type, $m ) ) {
3304  $size = $m[1];
3305  } else {
3306  $size = -1;
3307  }
3308 
3309  return $size;
3310  }
3311 
3312  public function delete( $table, $conds, $fname = __METHOD__ ) {
3313  $this->assertConditionIsNotEmpty( $conds, __METHOD__, false );
3314 
3315  $table = $this->tableName( $table );
3316  $sql = "DELETE FROM $table";
3317 
3318  if ( $conds !== IDatabase::ALL_ROWS ) {
3319  if ( is_array( $conds ) ) {
3320  $conds = $this->makeList( $conds, self::LIST_AND );
3321  }
3322  $sql .= ' WHERE ' . $conds;
3323  }
3324 
3325  $this->query( $sql, $fname, self::QUERY_CHANGE_ROWS );
3326 
3327  return true;
3328  }
3329 
3330  final public function insertSelect(
3331  $destTable,
3332  $srcTable,
3333  $varMap,
3334  $conds,
3335  $fname = __METHOD__,
3336  $insertOptions = [],
3337  $selectOptions = [],
3338  $selectJoinConds = []
3339  ) {
3340  static $hints = [ 'NO_AUTO_COLUMNS' ];
3341 
3342  $insertOptions = $this->normalizeOptions( $insertOptions );
3343  $selectOptions = $this->normalizeOptions( $selectOptions );
3344 
3345  if ( $this->cliMode && $this->isInsertSelectSafe( $insertOptions, $selectOptions ) ) {
3346  // For massive migrations with downtime, we don't want to select everything
3347  // into memory and OOM, so do all this native on the server side if possible.
3348  $this->doInsertSelectNative(
3349  $destTable,
3350  $srcTable,
3351  $varMap,
3352  $conds,
3353  $fname,
3354  array_diff( $insertOptions, $hints ),
3355  $selectOptions,
3356  $selectJoinConds
3357  );
3358  } else {
3359  $this->doInsertSelectGeneric(
3360  $destTable,
3361  $srcTable,
3362  $varMap,
3363  $conds,
3364  $fname,
3365  array_diff( $insertOptions, $hints ),
3366  $selectOptions,
3367  $selectJoinConds
3368  );
3369  }
3370 
3371  return true;
3372  }
3373 
3380  protected function isInsertSelectSafe( array $insertOptions, array $selectOptions ) {
3381  return true;
3382  }
3383 
3398  protected function doInsertSelectGeneric(
3399  $destTable,
3400  $srcTable,
3401  array $varMap,
3402  $conds,
3403  $fname,
3404  array $insertOptions,
3405  array $selectOptions,
3406  $selectJoinConds
3407  ) {
3408  // For web requests, do a locking SELECT and then INSERT. This puts the SELECT burden
3409  // on only the master (without needing row-based-replication). It also makes it easy to
3410  // know how big the INSERT is going to be.
3411  $fields = [];
3412  foreach ( $varMap as $dstColumn => $sourceColumnOrSql ) {
3413  $fields[] = $this->fieldNameWithAlias( $sourceColumnOrSql, $dstColumn );
3414  }
3415  $res = $this->select(
3416  $srcTable,
3417  implode( ',', $fields ),
3418  $conds,
3419  $fname,
3420  array_merge( $selectOptions, [ 'FOR UPDATE' ] ),
3421  $selectJoinConds
3422  );
3423  if ( !$res ) {
3424  return;
3425  }
3426 
3427  $affectedRowCount = 0;
3428  $this->startAtomic( $fname, self::ATOMIC_CANCELABLE );
3429  try {
3430  $rows = [];
3431  foreach ( $res as $row ) {
3432  $rows[] = (array)$row;
3433  }
3434  // Avoid inserts that are too huge
3435  $rowBatches = array_chunk( $rows, $this->nonNativeInsertSelectBatchSize );
3436  foreach ( $rowBatches as $rows ) {
3437  $this->insert( $destTable, $rows, $fname, $insertOptions );
3438  $affectedRowCount += $this->affectedRows();
3439  }
3440  } catch ( Throwable $e ) {
3441  $this->cancelAtomic( $fname );
3442  throw $e;
3443  }
3444  $this->endAtomic( $fname );
3445  $this->affectedRowCount = $affectedRowCount;
3446  }
3447 
3463  protected function doInsertSelectNative(
3464  $destTable,
3465  $srcTable,
3466  array $varMap,
3467  $conds,
3468  $fname,
3469  array $insertOptions,
3470  array $selectOptions,
3471  $selectJoinConds
3472  ) {
3473  list( $sqlVerb, $sqlOpts ) = $this->isFlagInOptions( 'IGNORE', $insertOptions )
3475  : [ 'INSERT INTO', '' ];
3476  $encDstTable = $this->tableName( $destTable );
3477  $sqlDstColumns = implode( ',', array_keys( $varMap ) );
3478  $selectSql = $this->selectSQLText(
3479  $srcTable,
3480  array_values( $varMap ),
3481  $conds,
3482  $fname,
3483  $selectOptions,
3484  $selectJoinConds
3485  );
3486 
3487  $sql = rtrim( "$sqlVerb $encDstTable ($sqlDstColumns) $selectSql $sqlOpts" );
3488 
3489  $this->query( $sql, $fname, self::QUERY_CHANGE_ROWS );
3490  }
3491 
3492  public function limitResult( $sql, $limit, $offset = false ) {
3493  if ( !is_numeric( $limit ) ) {
3494  throw new DBUnexpectedError(
3495  $this,
3496  "Invalid non-numeric limit passed to " . __METHOD__
3497  );
3498  }
3499  // This version works in MySQL and SQLite. It will very likely need to be
3500  // overridden for most other RDBMS subclasses.
3501  return "$sql LIMIT "
3502  . ( ( is_numeric( $offset ) && $offset != 0 ) ? "{$offset}," : "" )
3503  . "{$limit} ";
3504  }
3505 
3506  public function unionSupportsOrderAndLimit() {
3507  return true; // True for almost every DB supported
3508  }
3509 
3510  public function unionQueries( $sqls, $all ) {
3511  $glue = $all ? ') UNION ALL (' : ') UNION (';
3512 
3513  return '(' . implode( $glue, $sqls ) . ')';
3514  }
3515 
3517  $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__,
3518  $options = [], $join_conds = []
3519  ) {
3520  // First, build the Cartesian product of $permute_conds
3521  $conds = [ [] ];
3522  foreach ( $permute_conds as $field => $values ) {
3523  if ( !$values ) {
3524  // Skip empty $values
3525  continue;
3526  }
3527  $values = array_unique( $values ); // For sanity
3528  $newConds = [];
3529  foreach ( $conds as $cond ) {
3530  foreach ( $values as $value ) {
3531  $cond[$field] = $value;
3532  $newConds[] = $cond; // Arrays are by-value, not by-reference, so this works
3533  }
3534  }
3535  $conds = $newConds;
3536  }
3537 
3538  $extra_conds = $extra_conds === '' ? [] : (array)$extra_conds;
3539 
3540  // If there's just one condition and no subordering, hand off to
3541  // selectSQLText directly.
3542  if ( count( $conds ) === 1 &&
3543  ( !isset( $options['INNER ORDER BY'] ) || !$this->unionSupportsOrderAndLimit() )
3544  ) {
3545  return $this->selectSQLText(
3546  $table, $vars, $conds[0] + $extra_conds, $fname, $options, $join_conds
3547  );
3548  }
3549 
3550  // Otherwise, we need to pull out the order and limit to apply after
3551  // the union. Then build the SQL queries for each set of conditions in
3552  // $conds. Then union them together (using UNION ALL, because the
3553  // product *should* already be distinct).
3554  $orderBy = $this->makeOrderBy( $options );
3555  $limit = $options['LIMIT'] ?? null;
3556  $offset = $options['OFFSET'] ?? false;
3557  $all = empty( $options['NOTALL'] ) && !in_array( 'NOTALL', $options );
3558  if ( !$this->unionSupportsOrderAndLimit() ) {
3559  unset( $options['ORDER BY'], $options['LIMIT'], $options['OFFSET'] );
3560  } else {
3561  if ( array_key_exists( 'INNER ORDER BY', $options ) ) {
3562  $options['ORDER BY'] = $options['INNER ORDER BY'];
3563  }
3564  if ( $limit !== null && is_numeric( $offset ) && $offset != 0 ) {
3565  // We need to increase the limit by the offset rather than
3566  // using the offset directly, otherwise it'll skip incorrectly
3567  // in the subqueries.
3568  $options['LIMIT'] = $limit + $offset;
3569  unset( $options['OFFSET'] );
3570  }
3571  }
3572 
3573  $sqls = [];
3574  foreach ( $conds as $cond ) {
3575  $sqls[] = $this->selectSQLText(
3576  $table, $vars, $cond + $extra_conds, $fname, $options, $join_conds
3577  );
3578  }
3579  $sql = $this->unionQueries( $sqls, $all ) . $orderBy;
3580  if ( $limit !== null ) {
3581  $sql = $this->limitResult( $sql, $limit, $offset );
3582  }
3583 
3584  return $sql;
3585  }
3586 
3587  public function conditional( $cond, $trueVal, $falseVal ) {
3588  if ( is_array( $cond ) ) {
3589  $cond = $this->makeList( $cond, self::LIST_AND );
3590  }
3591 
3592  return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
3593  }
3594 
3595  public function strreplace( $orig, $old, $new ) {
3596  return "REPLACE({$orig}, {$old}, {$new})";
3597  }
3598 
3599  public function getServerUptime() {
3600  return 0;
3601  }
3602 
3603  public function wasDeadlock() {
3604  return false;
3605  }
3606 
3607  public function wasLockTimeout() {
3608  return false;
3609  }
3610 
3611  public function wasConnectionLoss() {
3612  return $this->wasConnectionError( $this->lastErrno() );
3613  }
3614 
3615  public function wasReadOnlyError() {
3616  return false;
3617  }
3618 
3619  public function wasErrorReissuable() {
3620  return (
3621  $this->wasDeadlock() ||
3622  $this->wasLockTimeout() ||
3623  $this->wasConnectionLoss()
3624  );
3625  }
3626 
3633  public function wasConnectionError( $errno ) {
3634  return false;
3635  }
3636 
3643  protected function wasKnownStatementRollbackError() {
3644  return false; // don't know; it could have caused a transaction rollback
3645  }
3646 
3647  public function deadlockLoop( ...$args ) {
3648  $function = array_shift( $args );
3649  $tries = self::$DEADLOCK_TRIES;
3650 
3651  $this->begin( __METHOD__ );
3652 
3653  $retVal = null;
3655  $e = null;
3656  do {
3657  try {
3658  $retVal = $function( ...$args );
3659  break;
3660  } catch ( DBQueryError $e ) {
3661  if ( $this->wasDeadlock() ) {
3662  // Retry after a randomized delay
3663  usleep( mt_rand( self::$DEADLOCK_DELAY_MIN, self::$DEADLOCK_DELAY_MAX ) );
3664  } else {
3665  // Throw the error back up
3666  throw $e;
3667  }
3668  }
3669  } while ( --$tries > 0 );
3670 
3671  if ( $tries <= 0 ) {
3672  // Too many deadlocks; give up
3673  $this->rollback( __METHOD__ );
3674  throw $e;
3675  } else {
3676  $this->commit( __METHOD__ );
3677 
3678  return $retVal;
3679  }
3680  }
3681 
3682  public function masterPosWait( DBMasterPos $pos, $timeout ) {
3683  # Real waits are implemented in the subclass.
3684  return 0;
3685  }
3686 
3687  public function getReplicaPos() {
3688  # Stub
3689  return false;
3690  }
3691 
3692  public function getMasterPos() {
3693  # Stub
3694  return false;
3695  }
3696 
3697  public function serverIsReadOnly() {
3698  return false;
3699  }
3700 
3701  final public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
3702  if ( !$this->trxLevel() ) {
3703  throw new DBUnexpectedError( $this, "No transaction is active" );
3704  }
3705  $this->trxEndCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
3706  }
3707 
3708  final public function onTransactionCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
3709  if ( !$this->trxLevel() && $this->getTransactionRoundId() ) {
3710  // Start an implicit transaction similar to how query() does
3711  $this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
3712  $this->trxAutomatic = true;
3713  }
3714 
3715  $this->trxIdleCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
3716  if ( !$this->trxLevel() ) {
3717  $this->runOnTransactionIdleCallbacks( self::TRIGGER_IDLE );
3718  }
3719  }
3720 
3721  final public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
3722  $this->onTransactionCommitOrIdle( $callback, $fname );
3723  }
3724 
3725  final public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
3726  if ( !$this->trxLevel() && $this->getTransactionRoundId() ) {
3727  // Start an implicit transaction similar to how query() does
3728  $this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
3729  $this->trxAutomatic = true;
3730  }
3731 
3732  if ( $this->trxLevel() ) {
3733  $this->trxPreCommitCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
3734  } else {
3735  // No transaction is active nor will start implicitly, so make one for this callback
3736  $this->startAtomic( __METHOD__, self::ATOMIC_CANCELABLE );
3737  try {
3738  $callback( $this );
3739  $this->endAtomic( __METHOD__ );
3740  } catch ( Throwable $e ) {
3741  $this->cancelAtomic( __METHOD__ );
3742  throw $e;
3743  }
3744  }
3745  }
3746 
3747  final public function onAtomicSectionCancel( callable $callback, $fname = __METHOD__ ) {
3748  if ( !$this->trxLevel() || !$this->trxAtomicLevels ) {
3749  throw new DBUnexpectedError( $this, "No atomic section is open (got $fname)" );
3750  }
3751  $this->trxSectionCancelCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
3752  }
3753 
3757  private function currentAtomicSectionId() {
3758  if ( $this->trxLevel() && $this->trxAtomicLevels ) {
3759  $levelInfo = end( $this->trxAtomicLevels );
3760 
3761  return $levelInfo[1];
3762  }
3763 
3764  return null;
3765  }
3766 
3775  ) {
3776  foreach ( $this->trxPreCommitCallbacks as $key => $info ) {
3777  if ( $info[2] === $old ) {
3778  $this->trxPreCommitCallbacks[$key][2] = $new;
3779  }
3780  }
3781  foreach ( $this->trxIdleCallbacks as $key => $info ) {
3782  if ( $info[2] === $old ) {
3783  $this->trxIdleCallbacks[$key][2] = $new;
3784  }
3785  }
3786  foreach ( $this->trxEndCallbacks as $key => $info ) {
3787  if ( $info[2] === $old ) {
3788  $this->trxEndCallbacks[$key][2] = $new;
3789  }
3790  }
3791  foreach ( $this->trxSectionCancelCallbacks as $key => $info ) {
3792  if ( $info[2] === $old ) {
3793  $this->trxSectionCancelCallbacks[$key][2] = $new;
3794  }
3795  }
3796  }
3797 
3817  private function modifyCallbacksForCancel(
3818  array $sectionIds, AtomicSectionIdentifier $newSectionId = null
3819  ) {
3820  // Cancel the "on commit" callbacks owned by this savepoint
3821  $this->trxIdleCallbacks = array_filter(
3822  $this->trxIdleCallbacks,
3823  function ( $entry ) use ( $sectionIds ) {
3824  return !in_array( $entry[2], $sectionIds, true );
3825  }
3826  );
3827  $this->trxPreCommitCallbacks = array_filter(
3828  $this->trxPreCommitCallbacks,
3829  function ( $entry ) use ( $sectionIds ) {
3830  return !in_array( $entry[2], $sectionIds, true );
3831  }
3832  );
3833  // Make "on resolution" callbacks owned by this savepoint to perceive a rollback
3834  foreach ( $this->trxEndCallbacks as $key => $entry ) {
3835  if ( in_array( $entry[2], $sectionIds, true ) ) {
3836  $callback = $entry[0];
3837  $this->trxEndCallbacks[$key][0] = function () use ( $callback ) {
3838  return $callback( self::TRIGGER_ROLLBACK, $this );
3839  };
3840  // This "on resolution" callback no longer belongs to a section.
3841  $this->trxEndCallbacks[$key][2] = null;
3842  }
3843  }
3844  // Hoist callback ownership for section cancel callbacks to the new top section
3845  foreach ( $this->trxSectionCancelCallbacks as $key => $entry ) {
3846  if ( in_array( $entry[2], $sectionIds, true ) ) {
3847  $this->trxSectionCancelCallbacks[$key][2] = $newSectionId;
3848  }
3849  }
3850  }
3851 
3852  final public function setTransactionListener( $name, callable $callback = null ) {
3853  if ( $callback ) {
3854  $this->trxRecurringCallbacks[$name] = $callback;
3855  } else {
3856  unset( $this->trxRecurringCallbacks[$name] );
3857  }
3858  }
3859 
3868  final public function setTrxEndCallbackSuppression( $suppress ) {
3869  $this->trxEndCallbacksSuppressed = $suppress;
3870  }
3871 
3882  public function runOnTransactionIdleCallbacks( $trigger ) {
3883  if ( $this->trxLevel() ) { // sanity
3884  throw new DBUnexpectedError( $this, __METHOD__ . ': a transaction is still open' );
3885  }
3886 
3887  if ( $this->trxEndCallbacksSuppressed ) {
3888  return 0;
3889  }
3890 
3891  $count = 0;
3892  $autoTrx = $this->getFlag( self::DBO_TRX ); // automatic begin() enabled?
3894  $e = null; // first exception
3895  do { // callbacks may add callbacks :)
3896  $callbacks = array_merge(
3897  $this->trxIdleCallbacks,
3898  $this->trxEndCallbacks // include "transaction resolution" callbacks
3899  );
3900  $this->trxIdleCallbacks = []; // consumed (and recursion guard)
3901  $this->trxEndCallbacks = []; // consumed (recursion guard)
3902 
3903  // Only run trxSectionCancelCallbacks on rollback, not commit.
3904  // But always consume them.
3905  if ( $trigger === self::TRIGGER_ROLLBACK ) {
3906  $callbacks = array_merge( $callbacks, $this->trxSectionCancelCallbacks );
3907  }
3908  $this->trxSectionCancelCallbacks = []; // consumed (recursion guard)
3909 
3910  foreach ( $callbacks as $callback ) {
3911  ++$count;
3912  list( $phpCallback ) = $callback;
3913  $this->clearFlag( self::DBO_TRX ); // make each query its own transaction
3914  try {
3915  call_user_func( $phpCallback, $trigger, $this );
3916  } catch ( Throwable $ex ) {
3917  call_user_func( $this->errorLogger, $ex );
3918  $e = $e ?: $ex;
3919  // Some callbacks may use startAtomic/endAtomic, so make sure
3920  // their transactions are ended so other callbacks don't fail
3921  if ( $this->trxLevel() ) {
3922  $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
3923  }
3924  } finally {
3925  if ( $autoTrx ) {
3926  $this->setFlag( self::DBO_TRX ); // restore automatic begin()
3927  } else {
3928  $this->clearFlag( self::DBO_TRX ); // restore auto-commit
3929  }
3930  }
3931  }
3932  } while ( count( $this->trxIdleCallbacks ) );
3933 
3934  if ( $e instanceof Throwable ) {
3935  throw $e; // re-throw any first exception
3936  }
3937 
3938  return $count;
3939  }
3940 
3951  $count = 0;
3952 
3953  $e = null; // first exception
3954  do { // callbacks may add callbacks :)
3955  $callbacks = $this->trxPreCommitCallbacks;
3956  $this->trxPreCommitCallbacks = []; // consumed (and recursion guard)
3957  foreach ( $callbacks as $callback ) {
3958  try {
3959  ++$count;
3960  list( $phpCallback ) = $callback;
3961  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable
3962  $phpCallback( $this );
3963  } catch ( Throwable $ex ) {
3964  ( $this->errorLogger )( $ex );
3965  $e = $e ?: $ex;
3966  }
3967  }
3968  } while ( count( $this->trxPreCommitCallbacks ) );
3969 
3970  if ( $e instanceof Throwable ) {
3971  throw $e; // re-throw any first exception
3972  }
3973 
3974  return $count;
3975  }
3976 
3985  $trigger, array $sectionIds = null
3986  ) {
3988  $e = null; // first exception
3989 
3990  $notCancelled = [];
3991  do {
3992  $callbacks = $this->trxSectionCancelCallbacks;
3993  $this->trxSectionCancelCallbacks = []; // consumed (recursion guard)
3994  foreach ( $callbacks as $entry ) {
3995  if ( $sectionIds === null || in_array( $entry[2], $sectionIds, true ) ) {
3996  try {
3997  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable
3998  $entry[0]( $trigger, $this );
3999  } catch ( Throwable $ex ) {
4000  ( $this->errorLogger )( $ex );
4001  $e = $e ?: $ex;
4002  }
4003  } else {
4004  $notCancelled[] = $entry;
4005  }
4006  }
4007  } while ( count( $this->trxSectionCancelCallbacks ) );
4008  $this->trxSectionCancelCallbacks = $notCancelled;
4009 
4010  if ( $e !== null ) {
4011  throw $e; // re-throw any first Throwable
4012  }
4013  }
4014 
4024  public function runTransactionListenerCallbacks( $trigger ) {
4025  if ( $this->trxEndCallbacksSuppressed ) {
4026  return;
4027  }
4028 
4030  $e = null; // first exception
4031 
4032  foreach ( $this->trxRecurringCallbacks as $phpCallback ) {
4033  try {
4034  $phpCallback( $trigger, $this );
4035  } catch ( Throwable $ex ) {
4036  ( $this->errorLogger )( $ex );
4037  $e = $e ?: $ex;
4038  }
4039  }
4040 
4041  if ( $e instanceof Throwable ) {
4042  throw $e; // re-throw any first exception
4043  }
4044  }
4045 
4056  protected function doSavepoint( $identifier, $fname ) {
4057  $sql = 'SAVEPOINT ' . $this->addIdentifierQuotes( $identifier );
4058  $this->query( $sql, $fname, self::QUERY_CHANGE_TRX );
4059  }
4060 
4071  protected function doReleaseSavepoint( $identifier, $fname ) {
4072  $sql = 'RELEASE SAVEPOINT ' . $this->addIdentifierQuotes( $identifier );
4073  $this->query( $sql, $fname, self::QUERY_CHANGE_TRX );
4074  }
4075 
4086  protected function doRollbackToSavepoint( $identifier, $fname ) {
4087  $sql = 'ROLLBACK TO SAVEPOINT ' . $this->addIdentifierQuotes( $identifier );
4088  $this->query( $sql, $fname, self::QUERY_CHANGE_TRX );
4089  }
4090 
4095  private function nextSavepointId( $fname ) {
4096  $savepointId = self::$SAVEPOINT_PREFIX . ++$this->trxAtomicCounter;
4097  if ( strlen( $savepointId ) > 30 ) {
4098  // 30 == Oracle's identifier length limit (pre 12c)
4099  // With a 22 character prefix, that puts the highest number at 99999999.
4100  throw new DBUnexpectedError(
4101  $this,
4102  'There have been an excessively large number of atomic sections in a transaction'
4103  . " started by $this->trxFname (at $fname)"
4104  );
4105  }
4106 
4107  return $savepointId;
4108  }
4109 
4110  final public function startAtomic(
4111  $fname = __METHOD__, $cancelable = self::ATOMIC_NOT_CANCELABLE
4112  ) {
4113  $savepointId = $cancelable === self::ATOMIC_CANCELABLE ? self::$NOT_APPLICABLE : null;
4114 
4115  if ( !$this->trxLevel() ) {
4116  $this->begin( $fname, self::TRANSACTION_INTERNAL ); // sets trxAutomatic
4117  // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
4118  // in all changes being in one transaction to keep requests transactional.
4119  if ( $this->getFlag( self::DBO_TRX ) ) {
4120  // Since writes could happen in between the topmost atomic sections as part
4121  // of the transaction, those sections will need savepoints.
4122  $savepointId = $this->nextSavepointId( $fname );
4123  $this->doSavepoint( $savepointId, $fname );
4124  } else {
4125  $this->trxAutomaticAtomic = true;
4126  }
4127  } elseif ( $cancelable === self::ATOMIC_CANCELABLE ) {
4128  $savepointId = $this->nextSavepointId( $fname );
4129  $this->doSavepoint( $savepointId, $fname );
4130  }
4131 
4132  $sectionId = new AtomicSectionIdentifier;
4133  $this->trxAtomicLevels[] = [ $fname, $sectionId, $savepointId ];
4134  $this->queryLogger->debug( 'startAtomic: entering level ' .
4135  ( count( $this->trxAtomicLevels ) - 1 ) . " ($fname)" );
4136 
4137  return $sectionId;
4138  }
4139 
4140  final public function endAtomic( $fname = __METHOD__ ) {
4141  if ( !$this->trxLevel() || !$this->trxAtomicLevels ) {
4142  throw new DBUnexpectedError( $this, "No atomic section is open (got $fname)" );
4143  }
4144 
4145  // Check if the current section matches $fname
4146  $pos = count( $this->trxAtomicLevels ) - 1;
4147  list( $savedFname, $sectionId, $savepointId ) = $this->trxAtomicLevels[$pos];
4148  $this->queryLogger->debug( "endAtomic: leaving level $pos ($fname)" );
4149 
4150  if ( $savedFname !== $fname ) {
4151  throw new DBUnexpectedError(
4152  $this,
4153  "Invalid atomic section ended (got $fname but expected $savedFname)"
4154  );
4155  }
4156 
4157  // Remove the last section (no need to re-index the array)
4158  array_pop( $this->trxAtomicLevels );
4159 
4160  if ( !$this->trxAtomicLevels && $this->trxAutomaticAtomic ) {
4161  $this->commit( $fname, self::FLUSHING_INTERNAL );
4162  } elseif ( $savepointId !== null && $savepointId !== self::$NOT_APPLICABLE ) {
4163  $this->doReleaseSavepoint( $savepointId, $fname );
4164  }
4165 
4166  // Hoist callback ownership for callbacks in the section that just ended;
4167  // all callbacks should have an owner that is present in trxAtomicLevels.
4168  $currentSectionId = $this->currentAtomicSectionId();
4169  if ( $currentSectionId ) {
4170  $this->reassignCallbacksForSection( $sectionId, $currentSectionId );
4171  }
4172  }
4173 
4174  final public function cancelAtomic(
4175  $fname = __METHOD__, AtomicSectionIdentifier $sectionId = null
4176  ) {
4177  if ( !$this->trxLevel() || !$this->trxAtomicLevels ) {
4178  throw new DBUnexpectedError( $this, "No atomic section is open (got $fname)" );
4179  }
4180 
4181  $excisedIds = [];
4182  $newTopSection = $this->currentAtomicSectionId();
4183  try {
4184  $excisedFnames = [];
4185  if ( $sectionId !== null ) {
4186  // Find the (last) section with the given $sectionId
4187  $pos = -1;
4188  foreach ( $this->trxAtomicLevels as $i => list( $asFname, $asId, $spId ) ) {
4189  if ( $asId === $sectionId ) {
4190  $pos = $i;
4191  }
4192  }
4193  if ( $pos < 0 ) {
4194  throw new DBUnexpectedError( $this, "Atomic section not found (for $fname)" );
4195  }
4196  // Remove all descendant sections and re-index the array
4197  $len = count( $this->trxAtomicLevels );
4198  for ( $i = $pos + 1; $i < $len; ++$i ) {
4199  $excisedFnames[] = $this->trxAtomicLevels[$i][0];
4200  $excisedIds[] = $this->trxAtomicLevels[$i][1];
4201  }
4202  $this->trxAtomicLevels = array_slice( $this->trxAtomicLevels, 0, $pos + 1 );
4203  $newTopSection = $this->currentAtomicSectionId();
4204  }
4205 
4206  // Check if the current section matches $fname
4207  $pos = count( $this->trxAtomicLevels ) - 1;
4208  list( $savedFname, $savedSectionId, $savepointId ) = $this->trxAtomicLevels[$pos];
4209 
4210  if ( $excisedFnames ) {
4211  $this->queryLogger->debug( "cancelAtomic: canceling level $pos ($savedFname) " .
4212  "and descendants " . implode( ', ', $excisedFnames ) );
4213  } else {
4214  $this->queryLogger->debug( "cancelAtomic: canceling level $pos ($savedFname)" );
4215  }
4216 
4217  if ( $savedFname !== $fname ) {
4218  throw new DBUnexpectedError(
4219  $this,
4220  "Invalid atomic section ended (got $fname but expected $savedFname)"
4221  );
4222  }
4223 
4224  // Remove the last section (no need to re-index the array)
4225  array_pop( $this->trxAtomicLevels );
4226  $excisedIds[] = $savedSectionId;
4227  $newTopSection = $this->currentAtomicSectionId();
4228 
4229  if ( $savepointId !== null ) {
4230  // Rollback the transaction to the state just before this atomic section
4231  if ( $savepointId === self::$NOT_APPLICABLE ) {
4232  $this->rollback( $fname, self::FLUSHING_INTERNAL );
4233  // Note: rollback() will run trxSectionCancelCallbacks
4234  } else {
4235  $this->doRollbackToSavepoint( $savepointId, $fname );
4236  $this->trxStatus = self::STATUS_TRX_OK; // no exception; recovered
4237  $this->trxStatusIgnoredCause = null;
4238 
4239  // Run trxSectionCancelCallbacks now.
4240  $this->runOnAtomicSectionCancelCallbacks( self::TRIGGER_CANCEL, $excisedIds );
4241  }
4242  } elseif ( $this->trxStatus > self::STATUS_TRX_ERROR ) {
4243  // Put the transaction into an error state if it's not already in one
4244  $this->trxStatus = self::STATUS_TRX_ERROR;
4245  $this->trxStatusCause = new DBUnexpectedError(
4246  $this,
4247  "Uncancelable atomic section canceled (got $fname)"
4248  );
4249  }
4250  } finally {
4251  // Fix up callbacks owned by the sections that were just cancelled.
4252  // All callbacks should have an owner that is present in trxAtomicLevels.
4253  $this->modifyCallbacksForCancel( $excisedIds, $newTopSection );
4254  }
4255 
4256  $this->affectedRowCount = 0; // for the sake of consistency
4257  }
4258 
4259  final public function doAtomicSection(
4260  $fname, callable $callback, $cancelable = self::ATOMIC_NOT_CANCELABLE
4261  ) {
4262  $sectionId = $this->startAtomic( $fname, $cancelable );
4263  try {
4264  $res = $callback( $this, $fname );
4265  } catch ( Throwable $e ) {
4266  $this->cancelAtomic( $fname, $sectionId );
4267 
4268  throw $e;
4269  }
4270  $this->endAtomic( $fname );
4271 
4272  return $res;
4273  }
4274 
4275  final public function begin( $fname = __METHOD__, $mode = self::TRANSACTION_EXPLICIT ) {
4276  static $modes = [ self::TRANSACTION_EXPLICIT, self::TRANSACTION_INTERNAL ];
4277  if ( !in_array( $mode, $modes, true ) ) {
4278  throw new DBUnexpectedError( $this, "$fname: invalid mode parameter '$mode'" );
4279  }
4280 
4281  // Protect against mismatched atomic section, transaction nesting, and snapshot loss
4282  if ( $this->trxLevel() ) {
4283  if ( $this->trxAtomicLevels ) {
4284  $levels = $this->flatAtomicSectionList();
4285  $msg = "$fname: got explicit BEGIN while atomic section(s) $levels are open";
4286  throw new DBUnexpectedError( $this, $msg );
4287  } elseif ( !$this->trxAutomatic ) {
4288  $msg = "$fname: explicit transaction already active (from {$this->trxFname})";
4289  throw new DBUnexpectedError( $this, $msg );
4290  } else {
4291  $msg = "$fname: implicit transaction already active (from {$this->trxFname})";
4292  throw new DBUnexpectedError( $this, $msg );
4293  }
4294  } elseif ( $this->getFlag( self::DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) {
4295  $msg = "$fname: implicit transaction expected (DBO_TRX set)";
4296  throw new DBUnexpectedError( $this, $msg );
4297  }
4298 
4299  $this->assertHasConnectionHandle();
4300 
4301  $this->doBegin( $fname );
4302  $this->trxShortId = sprintf( '%06x', mt_rand( 0, 0xffffff ) );
4303  $this->trxStatus = self::STATUS_TRX_OK;
4304  $this->trxStatusIgnoredCause = null;
4305  $this->trxAtomicCounter = 0;
4306  $this->trxTimestamp = microtime( true );
4307  $this->trxFname = $fname;
4308  $this->trxDoneWrites = false;
4309  $this->trxAutomaticAtomic = false;
4310  $this->trxAtomicLevels = [];
4311  $this->trxWriteDuration = 0.0;
4312  $this->trxWriteQueryCount = 0;
4313  $this->trxWriteAffectedRows = 0;
4314  $this->trxWriteAdjDuration = 0.0;
4315  $this->trxWriteAdjQueryCount = 0;
4316  $this->trxWriteCallers = [];
4317  // First SELECT after BEGIN will establish the snapshot in REPEATABLE-READ.
4318  // Get an estimate of the replication lag before any such queries.
4319  $this->trxReplicaLag = null; // clear cached value first
4320  $this->trxReplicaLag = $this->getApproximateLagStatus()['lag'];
4321  // T147697: make explicitTrxActive() return true until begin() finishes. This way, no
4322  // caller will think its OK to muck around with the transaction just because startAtomic()
4323  // has not yet completed (e.g. setting trxAtomicLevels).
4324  $this->trxAutomatic = ( $mode === self::TRANSACTION_INTERNAL );
4325  }
4326 
4334  protected function doBegin( $fname ) {
4335  $this->query( 'BEGIN', $fname, self::QUERY_CHANGE_TRX );
4336  }
4337 
4338  final public function commit( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
4339  static $modes = [ self::FLUSHING_ONE, self::FLUSHING_ALL_PEERS, self::FLUSHING_INTERNAL ];
4340  if ( !in_array( $flush, $modes, true ) ) {
4341  throw new DBUnexpectedError( $this, "$fname: invalid flush parameter '$flush'" );
4342  }
4343 
4344  if ( $this->trxLevel() && $this->trxAtomicLevels ) {
4345  // There are still atomic sections open; this cannot be ignored
4346  $levels = $this->flatAtomicSectionList();
4347  throw new DBUnexpectedError(
4348  $this,
4349  "$fname: got COMMIT while atomic sections $levels are still open"
4350  );
4351  }
4352 
4353  if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
4354  if ( !$this->trxLevel() ) {
4355  return; // nothing to do
4356  } elseif ( !$this->trxAutomatic ) {
4357  throw new DBUnexpectedError(
4358  $this,
4359  "$fname: flushing an explicit transaction, getting out of sync"
4360  );
4361  }
4362  } elseif ( !$this->trxLevel() ) {
4363  $this->queryLogger->error(
4364  "$fname: no transaction to commit, something got out of sync" );
4365  return; // nothing to do
4366  } elseif ( $this->trxAutomatic ) {
4367  throw new DBUnexpectedError(
4368  $this,
4369  "$fname: expected mass commit of all peer transactions (DBO_TRX set)"
4370  );
4371  }
4372 
4373  $this->assertHasConnectionHandle();
4374 
4376 
4377  $writeTime = $this->pendingWriteQueryDuration( self::ESTIMATE_DB_APPLY );
4378  $this->doCommit( $fname );
4379  $oldTrxShortId = $this->consumeTrxShortId();
4380  $this->trxStatus = self::STATUS_TRX_NONE;
4381 
4382  if ( $this->trxDoneWrites ) {
4383  $this->lastWriteTime = microtime( true );
4384  $this->trxProfiler->transactionWritingOut(
4385  $this->server,
4386  $this->getDomainID(),
4387  $oldTrxShortId,
4388  $writeTime,
4389  $this->trxWriteAffectedRows
4390  );
4391  }
4392 
4393  // With FLUSHING_ALL_PEERS, callbacks will be explicitly run later
4394  if ( $flush !== self::FLUSHING_ALL_PEERS ) {
4395  $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
4396  $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
4397  }
4398  }
4399 
4407  protected function doCommit( $fname ) {
4408  if ( $this->trxLevel() ) {
4409  $this->query( 'COMMIT', $fname, self::QUERY_CHANGE_TRX );
4410  }
4411  }
4412 
4413  final public function rollback( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
4414  $trxActive = $this->trxLevel();
4415 
4416  if ( $flush !== self::FLUSHING_INTERNAL
4417  && $flush !== self::FLUSHING_ALL_PEERS
4418  && $this->getFlag( self::DBO_TRX )
4419  ) {
4420  throw new DBUnexpectedError(
4421  $this,
4422  "$fname: Expected mass rollback of all peer transactions (DBO_TRX set)"
4423  );
4424  }
4425 
4426  if ( $trxActive ) {
4427  $this->assertHasConnectionHandle();
4428 
4429  $this->doRollback( $fname );
4430  $oldTrxShortId = $this->consumeTrxShortId();
4431  $this->trxStatus = self::STATUS_TRX_NONE;
4432  $this->trxAtomicLevels = [];
4433  // Estimate the RTT via a query now that trxStatus is OK
4434  $writeTime = $this->pingAndCalculateLastTrxApplyTime();
4435 
4436  if ( $this->trxDoneWrites ) {
4437  $this->trxProfiler->transactionWritingOut(
4438  $this->server,
4439  $this->getDomainID(),
4440  $oldTrxShortId,
4441  $writeTime,
4442  $this->trxWriteAffectedRows
4443  );
4444  }
4445  }
4446 
4447  // Clear any commit-dependant callbacks. They might even be present
4448  // only due to transaction rounds, with no SQL transaction being active
4449  $this->trxIdleCallbacks = [];
4450  $this->trxPreCommitCallbacks = [];
4451 
4452  // With FLUSHING_ALL_PEERS, callbacks will be explicitly run later
4453  if ( $trxActive && $flush !== self::FLUSHING_ALL_PEERS ) {
4454  try {
4455  $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
4456  } catch ( Throwable $e ) {
4457  // already logged; finish and let LoadBalancer move on during mass-rollback
4458  }
4459  try {
4460  $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
4461  } catch ( Throwable $e ) {
4462  // already logged; let LoadBalancer move on during mass-rollback
4463  }
4464 
4465  $this->affectedRowCount = 0; // for the sake of consistency
4466  }
4467  }
4468 
4476  protected function doRollback( $fname ) {
4477  if ( $this->trxLevel() ) {
4478  # Disconnects cause rollback anyway, so ignore those errors
4479  $this->query( 'ROLLBACK', $fname, self::QUERY_SILENCE_ERRORS | self::QUERY_CHANGE_TRX );
4480  }
4481  }
4482 
4483  public function flushSnapshot( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
4484  if ( $this->explicitTrxActive() ) {
4485  // Committing this transaction would break callers that assume it is still open
4486  throw new DBUnexpectedError(
4487  $this,
4488  "$fname: Cannot flush snapshot; " .
4489  "explicit transaction '{$this->trxFname}' is still open"
4490  );
4491  } elseif ( $this->writesOrCallbacksPending() ) {
4492  // This only flushes transactions to clear snapshots, not to write data
4493  $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
4494  throw new DBUnexpectedError(
4495  $this,
4496  "$fname: Cannot flush snapshot; " .
4497  "writes from transaction {$this->trxFname} are still pending ($fnames)"
4498  );
4499  } elseif (
4500  $this->trxLevel() &&
4501  $this->getTransactionRoundId() &&
4502  $flush !== self::FLUSHING_INTERNAL &&
4503  $flush !== self::FLUSHING_ALL_PEERS
4504  ) {
4505  $this->queryLogger->warning(
4506  "$fname: Expected mass snapshot flush of all peer transactions " .
4507  "in the explicit transactions round '{$this->getTransactionRoundId()}'",
4508  [ 'exception' => new RuntimeException() ]
4509  );
4510  }
4511 
4512  $this->commit( $fname, self::FLUSHING_INTERNAL );
4513  }
4514 
4515  public function explicitTrxActive() {
4516  return $this->trxLevel() && ( $this->trxAtomicLevels || !$this->trxAutomatic );
4517  }
4518 
4519  public function duplicateTableStructure(
4520  $oldName, $newName, $temporary = false, $fname = __METHOD__
4521  ) {
4522  throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
4523  }
4524 
4525  public function listTables( $prefix = null, $fname = __METHOD__ ) {
4526  throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
4527  }
4528 
4529  public function listViews( $prefix = null, $fname = __METHOD__ ) {
4530  throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
4531  }
4532 
4533  public function timestamp( $ts = 0 ) {
4534  $t = new ConvertibleTimestamp( $ts );
4535  // Let errors bubble up to avoid putting garbage in the DB
4536  return $t->getTimestamp( TS_MW );
4537  }
4538 
4539  public function timestampOrNull( $ts = null ) {
4540  if ( $ts === null ) {
4541  return null;
4542  } else {
4543  return $this->timestamp( $ts );
4544  }
4545  }
4546 
4547  public function affectedRows() {
4548  return ( $this->affectedRowCount === null )
4549  ? $this->fetchAffectedRowCount() // default to driver value
4551  }
4552 
4556  abstract protected function fetchAffectedRowCount();
4557 
4570  protected function resultObject( $result ) {
4571  if ( !$result ) {
4572  return false; // failed query
4573  } elseif ( $result instanceof IResultWrapper ) {
4574  return $result;
4575  } elseif ( $result === true ) {
4576  return $result; // successful write query
4577  } else {
4578  return new ResultWrapper( $this, $result );
4579  }
4580  }
4581 
4582  public function ping( &$rtt = null ) {
4583  // Avoid hitting the server if it was hit recently
4584  if ( $this->isOpen() && ( microtime( true ) - $this->lastPing ) < self::$PING_TTL ) {
4585  if ( !func_num_args() || $this->lastRoundTripEstimate > 0 ) {
4587  return true; // don't care about $rtt
4588  }
4589  }
4590 
4591  // This will reconnect if possible or return false if not
4592  $flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_SILENCE_ERRORS | self::QUERY_CHANGE_NONE;
4593  $ok = ( $this->query( self::$PING_QUERY, __METHOD__, $flags ) !== false );
4594  if ( $ok ) {
4596  }
4597 
4598  return $ok;
4599  }
4600 
4607  protected function replaceLostConnection( $fname ) {
4608  $this->closeConnection();
4609  $this->conn = null;
4610 
4611  $this->handleSessionLossPreconnect();
4612 
4613  try {
4614  $this->open(
4615  $this->server,
4616  $this->user,
4617  $this->password,
4618  $this->currentDomain->getDatabase(),
4619  $this->currentDomain->getSchema(),
4620  $this->tablePrefix()
4621  );
4622  $this->lastPing = microtime( true );
4623  $ok = true;
4624 
4625  $this->connLogger->warning(
4626  $fname . ': lost connection to {dbserver}; reconnected',
4627  [
4628  'dbserver' => $this->getServer(),
4629  'exception' => new RuntimeException()
4630  ]
4631  );
4632  } catch ( DBConnectionError $e ) {
4633  $ok = false;
4634 
4635  $this->connLogger->error(
4636  $fname . ': lost connection to {dbserver} permanently',
4637  [ 'dbserver' => $this->getServer() ]
4638  );
4639  }
4640 
4642 
4643  return $ok;
4644  }
4645 
4646  public function getSessionLagStatus() {
4647  return $this->getRecordedTransactionLagStatus() ?: $this->getApproximateLagStatus();
4648  }
4649 
4663  final protected function getRecordedTransactionLagStatus() {
4664  return ( $this->trxLevel() && $this->trxReplicaLag !== null )
4665  ? [ 'lag' => $this->trxReplicaLag, 'since' => $this->trxTimestamp() ]
4666  : null;
4667  }
4668 
4677  protected function getApproximateLagStatus() {
4678  return [
4679  'lag' => ( $this->topologyRole === self::ROLE_STREAMING_REPLICA ) ? $this->getLag() : 0,
4680  'since' => microtime( true )
4681  ];
4682  }
4683 
4703  public static function getCacheSetOptions( IDatabase $db1, IDatabase $db2 = null ) {
4704  $res = [ 'lag' => 0, 'since' => INF, 'pending' => false ];
4705  foreach ( func_get_args() as $db ) {
4707  $status = $db->getSessionLagStatus();
4708  if ( $status['lag'] === false ) {
4709  $res['lag'] = false;
4710  } elseif ( $res['lag'] !== false ) {
4711  $res['lag'] = max( $res['lag'], $status['lag'] );
4712  }
4713  $res['since'] = min( $res['since'], $status['since'] );
4714  $res['pending'] = $res['pending'] ?: $db->writesPending();
4715  }
4716 
4717  return $res;
4718  }
4719 
4720  public function getLag() {
4721  if ( $this->topologyRole === self::ROLE_STREAMING_MASTER ) {
4722  return 0; // this is the master
4723  } elseif ( $this->topologyRole === self::ROLE_STATIC_CLONE ) {
4724  return 0; // static dataset
4725  }
4726 
4727  return $this->doGetLag();
4728  }
4729 
4730  protected function doGetLag() {
4731  return 0;
4732  }
4733 
4734  public function maxListLen() {
4735  return 0;
4736  }
4737 
4738  public function encodeBlob( $b ) {
4739  return $b;
4740  }
4741 
4742  public function decodeBlob( $b ) {
4743  if ( $b instanceof Blob ) {
4744  $b = $b->fetch();
4745  }
4746  return $b;
4747  }
4748 
4749  public function setSessionOptions( array $options ) {
4750  }
4751 
4752  public function sourceFile(
4753  $filename,
4754  callable $lineCallback = null,
4755  callable $resultCallback = null,
4756  $fname = false,
4757  callable $inputCallback = null
4758  ) {
4759  AtEase::suppressWarnings();
4760  $fp = fopen( $filename, 'r' );
4761  AtEase::restoreWarnings();
4762 
4763  if ( $fp === false ) {
4764  throw new RuntimeException( "Could not open \"{$filename}\"" );
4765  }
4766 
4767  if ( !$fname ) {
4768  $fname = __METHOD__ . "( $filename )";
4769  }
4770 
4771  try {
4772  $error = $this->sourceStream(
4773  $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
4774  } catch ( Throwable $e ) {
4775  fclose( $fp );
4776  throw $e;
4777  }
4778 
4779  fclose( $fp );
4780 
4781  return $error;
4782  }
4783 
4784  public function setSchemaVars( $vars ) {
4785  $this->schemaVars = is_array( $vars ) ? $vars : null;
4786  }
4787 
4788  public function sourceStream(
4789  $fp,
4790  callable $lineCallback = null,
4791  callable $resultCallback = null,
4792  $fname = __METHOD__,
4793  callable $inputCallback = null
4794  ) {
4795  $delimiterReset = new ScopedCallback(
4796  function ( $delimiter ) {
4797  $this->delimiter = $delimiter;
4798  },
4799  [ $this->delimiter ]
4800  );
4801  $cmd = '';
4802 
4803  while ( !feof( $fp ) ) {
4804  if ( $lineCallback ) {
4805  call_user_func( $lineCallback );
4806  }
4807 
4808  $line = trim( fgets( $fp ) );
4809 
4810  if ( $line == '' ) {
4811  continue;
4812  }
4813 
4814  if ( $line[0] == '-' && $line[1] == '-' ) {
4815  continue;
4816  }
4817 
4818  if ( $cmd != '' ) {
4819  $cmd .= ' ';
4820  }
4821 
4822  $done = $this->streamStatementEnd( $cmd, $line );
4823 
4824  $cmd .= "$line\n";
4825 
4826  if ( $done || feof( $fp ) ) {
4827  $cmd = $this->replaceVars( $cmd );
4828 
4829  if ( $inputCallback ) {
4830  $callbackResult = $inputCallback( $cmd );
4831 
4832  if ( is_string( $callbackResult ) || !$callbackResult ) {
4833  $cmd = $callbackResult;
4834  }
4835  }
4836 
4837  if ( $cmd ) {
4838  $res = $this->query( $cmd, $fname );
4839 
4840  if ( $resultCallback ) {
4841  $resultCallback( $res, $this );
4842  }
4843 
4844  if ( $res === false ) {
4845  $err = $this->lastError();
4846 
4847  return "Query \"{$cmd}\" failed with error code \"$err\".\n";
4848  }
4849  }
4850  $cmd = '';
4851  }
4852  }
4853 
4854  ScopedCallback::consume( $delimiterReset );
4855  return true;
4856  }
4857 
4865  public function streamStatementEnd( &$sql, &$newLine ) {
4866  if ( $this->delimiter ) {
4867  $prev = $newLine;
4868  $newLine = preg_replace(
4869  '/' . preg_quote( $this->delimiter, '/' ) . '$/', '', $newLine );
4870  if ( $newLine != $prev ) {
4871  return true;
4872  }
4873  }
4874 
4875  return false;
4876  }
4877 
4898  protected function replaceVars( $ins ) {
4899  $vars = $this->getSchemaVars();
4900  return preg_replace_callback(
4901  '!
4902  /\* (\$wgDBprefix|[_i]) \*/ (\w*) | # 1-2. tableName, indexName
4903  \'\{\$ (\w+) }\' | # 3. addQuotes
4904  `\{\$ (\w+) }` | # 4. addIdentifierQuotes
4905  /\*\$ (\w+) \*/ # 5. leave unencoded
4906  !x',
4907  function ( $m ) use ( $vars ) {
4908  // Note: Because of <https://bugs.php.net/bug.php?id=51881>,
4909  // check for both nonexistent keys *and* the empty string.
4910  if ( isset( $m[1] ) && $m[1] !== '' ) {
4911  if ( $m[1] === 'i' ) {
4912  return $this->indexName( $m[2] );
4913  } else {
4914  return $this->tableName( $m[2] );
4915  }
4916  } elseif ( isset( $m[3] ) && $m[3] !== '' && array_key_exists( $m[3], $vars ) ) {
4917  return $this->addQuotes( $vars[$m[3]] );
4918  } elseif ( isset( $m[4] ) && $m[4] !== '' && array_key_exists( $m[4], $vars ) ) {
4919  return $this->addIdentifierQuotes( $vars[$m[4]] );
4920  } elseif ( isset( $m[5] ) && $m[5] !== '' && array_key_exists( $m[5], $vars ) ) {
4921  return $vars[$m[5]];
4922  } else {
4923  return $m[0];
4924  }
4925  },
4926  $ins
4927  );
4928  }
4929 
4936  protected function getSchemaVars() {
4937  return $this->schemaVars ?? $this->getDefaultSchemaVars();
4938  }
4939 
4948  protected function getDefaultSchemaVars() {
4949  return [];
4950  }
4951 
4952  public function lockIsFree( $lockName, $method ) {
4953  // RDBMs methods for checking named locks may or may not count this thread itself.
4954  // In MySQL, IS_FREE_LOCK() returns 0 if the thread already has the lock. This is
4955  // the behavior chosen by the interface for this method.
4956  return !isset( $this->sessionNamedLocks[$lockName] );
4957  }
4958 
4959  public function lock( $lockName, $method, $timeout = 5 ) {
4960  $this->sessionNamedLocks[$lockName] = 1;
4961 
4962  return true;
4963  }
4964 
4965  public function unlock( $lockName, $method ) {
4966  unset( $this->sessionNamedLocks[$lockName] );
4967 
4968  return true;
4969  }
4970 
4971  public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
4972  if ( $this->writesOrCallbacksPending() ) {
4973  // This only flushes transactions to clear snapshots, not to write data
4974  $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
4975  throw new DBUnexpectedError(
4976  $this,
4977  "$fname: Cannot flush pre-lock snapshot; " .
4978  "writes from transaction {$this->trxFname} are still pending ($fnames)"
4979  );
4980  }
4981 
4982  if ( !$this->lock( $lockKey, $fname, $timeout ) ) {
4983  return null;
4984  }
4985 
4986  $unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) {
4987  if ( $this->trxLevel() ) {
4988  // There is a good chance an exception was thrown, causing any early return
4989  // from the caller. Let any error handler get a chance to issue rollback().
4990  // If there isn't one, let the error bubble up and trigger server-side rollback.
4991  $this->onTransactionResolution(
4992  function () use ( $lockKey, $fname ) {
4993  $this->unlock( $lockKey, $fname );
4994  },
4995  $fname
4996  );
4997  } else {
4998  $this->unlock( $lockKey, $fname );
4999  }
5000  } );
5001 
5002  $this->commit( $fname, self::FLUSHING_INTERNAL );
5003 
5004  return $unlocker;
5005  }
5006 
5007  public function namedLocksEnqueue() {
5008  return false;
5009  }
5010 
5012  return true;
5013  }
5014 
5015  final public function lockTables( array $read, array $write, $method ) {
5016  if ( $this->writesOrCallbacksPending() ) {
5017  throw new DBUnexpectedError( $this, "Transaction writes or callbacks still pending" );
5018  }
5019 
5020  if ( $this->tableLocksHaveTransactionScope() ) {
5021  $this->startAtomic( $method );
5022  }
5023 
5024  return $this->doLockTables( $read, $write, $method );
5025  }
5026 
5035  protected function doLockTables( array $read, array $write, $method ) {
5036  return true;
5037  }
5038 
5039  final public function unlockTables( $method ) {
5040  if ( $this->tableLocksHaveTransactionScope() ) {
5041  $this->endAtomic( $method );
5042 
5043  return true; // locks released on COMMIT/ROLLBACK
5044  }
5045 
5046  return $this->doUnlockTables( $method );
5047  }
5048 
5055  protected function doUnlockTables( $method ) {
5056  return true;
5057  }
5058 
5059  public function dropTable( $table, $fname = __METHOD__ ) {
5060  if ( !$this->tableExists( $table, $fname ) ) {
5061  return false;
5062  }
5063 
5064  $this->doDropTable( $table, $fname );
5065 
5066  return true;
5067  }
5068 
5074  protected function doDropTable( $table, $fname ) {
5075  // https://mariadb.com/kb/en/drop-table/
5076  // https://dev.mysql.com/doc/refman/8.0/en/drop-table.html
5077  // https://www.postgresql.org/docs/9.2/sql-truncate.html
5078  $sql = "DROP TABLE " . $this->tableName( $table ) . " CASCADE";
5079  $this->query( $sql, $fname, self::QUERY_IGNORE_DBO_TRX );
5080  }
5081 
5082  public function truncate( $tables, $fname = __METHOD__ ) {
5083  $tables = is_array( $tables ) ? $tables : [ $tables ];
5084 
5085  $tablesTruncate = [];
5086  foreach ( $tables as $table ) {
5087  // Skip TEMPORARY tables with no writes nor sequence updates detected.
5088  // This mostly is an optimization for integration testing.
5089  if ( !$this->isPristineTemporaryTable( $table ) ) {
5090  $tablesTruncate[] = $table;
5091  }
5092  }
5093 
5094  if ( $tablesTruncate ) {
5095  $this->doTruncate( $tablesTruncate, $fname );
5096  }
5097  }
5098 
5104  protected function doTruncate( array $tables, $fname ) {
5105  foreach ( $tables as $table ) {
5106  $sql = "TRUNCATE TABLE " . $this->tableName( $table );
5107  $this->query( $sql, $fname, self::QUERY_CHANGE_SCHEMA );
5108  }
5109  }
5110 
5111  public function getInfinity() {
5112  return 'infinity';
5113  }
5114 
5115  public function encodeExpiry( $expiry ) {
5116  return ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() )
5117  ? $this->getInfinity()
5118  : $this->timestamp( $expiry );
5119  }
5120 
5121  public function decodeExpiry( $expiry, $format = TS_MW ) {
5122  if ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() ) {
5123  return 'infinity';
5124  }
5125 
5126  return ConvertibleTimestamp::convert( $format, $expiry );
5127  }
5128 
5129  public function setBigSelects( $value = true ) {
5130  // no-op
5131  }
5132 
5133  public function isReadOnly() {
5134  return ( $this->getReadOnlyReason() !== false );
5135  }
5136 
5140  protected function getReadOnlyReason() {
5141  if ( $this->topologyRole === self::ROLE_STREAMING_REPLICA ) {
5142  return [ 'Server is configured as a read-only replica database.', 'role' ];
5143  } elseif ( $this->topologyRole === self::ROLE_STATIC_CLONE ) {
5144  return [ 'Server is configured as a read-only static clone database.', 'role' ];
5145  }
5146 
5147  $reason = $this->getLBInfo( self::LB_READ_ONLY_REASON );
5148  if ( is_string( $reason ) ) {
5149  return [ $reason, 'lb' ];
5150  }
5151 
5152  return false;
5153  }
5154 
5155  public function setTableAliases( array $aliases ) {
5156  $this->tableAliases = $aliases;
5157  }
5158 
5159  public function setIndexAliases( array $aliases ) {
5160  $this->indexAliases = $aliases;
5161  }
5162 
5169  final protected function fieldHasBit( $field, $flags ) {
5170  return ( ( $field & $flags ) === $flags );
5171  }
5172 
5184  protected function getBindingHandle() {
5185  if ( !$this->conn ) {
5186  throw new DBUnexpectedError(
5187  $this,
5188  'DB connection was already closed or the connection dropped'
5189  );
5190  }
5191 
5192  return $this->conn;
5193  }
5194 
5195  public function __toString() {
5196  // spl_object_id is PHP >= 7.2
5197  $id = function_exists( 'spl_object_id' )
5198  ? spl_object_id( $this )
5199  : spl_object_hash( $this );
5200 
5201  $description = $this->getType() . ' object #' . $id;
5202  if ( is_resource( $this->conn ) ) {
5203  $description .= ' (' . (string)$this->conn . ')'; // "resource id #<ID>"
5204  } elseif ( is_object( $this->conn ) ) {
5205  // spl_object_id is PHP >= 7.2
5206  $handleId = function_exists( 'spl_object_id' )
5207  ? spl_object_id( $this->conn )
5208  : spl_object_hash( $this->conn );
5209  $description .= " (handle id #$handleId)";
5210  }
5211 
5212  return $description;
5213  }
5214 
5219  public function __clone() {
5220  $this->connLogger->warning(
5221  "Cloning " . static::class . " is not recommended; forking connection",
5222  [ 'exception' => new RuntimeException() ]
5223  );
5224 
5225  if ( $this->isOpen() ) {
5226  // Open a new connection resource without messing with the old one
5227  $this->conn = null;
5228  $this->trxEndCallbacks = []; // don't copy
5229  $this->trxSectionCancelCallbacks = []; // don't copy
5230  $this->handleSessionLossPreconnect(); // no trx or locks anymore
5231  $this->open(
5232  $this->server,
5233  $this->user,
5234  $this->password,
5235  $this->currentDomain->getDatabase(),
5236  $this->currentDomain->getSchema(),
5237  $this->tablePrefix()
5238  );
5239  $this->lastPing = microtime( true );
5240  }
5241  }
5242 
5248  public function __sleep() {
5249  throw new RuntimeException( 'Database serialization may cause problems, since ' .
5250  'the connection is not restored on wakeup' );
5251  }
5252 
5256  public function __destruct() {
5257  if ( $this->trxLevel() && $this->trxDoneWrites ) {
5258  trigger_error( "Uncommitted DB writes (transaction from {$this->trxFname})" );
5259  }
5260 
5261  $danglingWriters = $this->pendingWriteAndCallbackCallers();
5262  if ( $danglingWriters ) {
5263  $fnames = implode( ', ', $danglingWriters );
5264  trigger_error( "DB transaction writes or callbacks still pending ($fnames)" );
5265  }
5266 
5267  if ( $this->conn ) {
5268  // Avoid connection leaks for sanity. Normally, resources close at script completion.
5269  // The connection might already be closed in PHP by now, so suppress warnings.
5270  AtEase::suppressWarnings();
5271  $this->closeConnection();
5272  AtEase::restoreWarnings();
5273  $this->conn = null;
5274  }
5275  }
5276 }
5277 
5281 class_alias( Database::class, 'DatabaseBase' );
5282 
5286 class_alias( Database::class, 'Database' );
Wikimedia\Rdbms\Database\implicitOrderby
implicitOrderby()
Returns true if this database does an implicit order by when the column has an index For example: SEL...
Definition: Database.php:635
Wikimedia\Rdbms\Database\$PING_TTL
static int $PING_TTL
How long before it is worth doing a dummy query to test the connection.
Definition: Database.php:228
Wikimedia\Rdbms\Database\$schemaVars
array null $schemaVars
Current variables use for schema element placeholders.
Definition: Database.php:108
Wikimedia\Rdbms\Database\tablePrefix
tablePrefix( $prefix=null)
Get/set the table prefix.
Definition: Database.php:561
Wikimedia\Rdbms\Database\$trxFname
string $trxFname
Name of the function that start the last transaction.
Definition: Database.php:135
Wikimedia\Rdbms\Database\getLastPHPError
getLastPHPError()
Definition: Database.php:848
Wikimedia\Rdbms\Database\setSessionOptions
setSessionOptions(array $options)
Override database's default behavior.
Definition: Database.php:4749
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:49
Wikimedia\Rdbms\Database\limitResult
limitResult( $sql, $limit, $offset=false)
Construct a LIMIT query with optional offset.
Definition: Database.php:3492
Wikimedia\Rdbms\Database\$trxDoneWrites
bool $trxDoneWrites
Whether possible write queries were done in the last transaction started.
Definition: Database.php:137
Wikimedia\Rdbms\Database\$trxEndCallbacksSuppressed
bool $trxEndCallbacksSuppressed
Whether to suppress triggering of transaction end callbacks.
Definition: Database.php:172
Wikimedia\Rdbms\Database\$DBO_MUTABLE
static int $DBO_MUTABLE
Bit field of all DBO_* flags that can be changed after connection.
Definition: Database.php:247
Wikimedia\Rdbms\Database\makeUpdateOptionsArray
makeUpdateOptionsArray( $options)
Make UPDATE options array for Database::makeUpdateOptions.
Definition: Database.php:2375
Wikimedia\Rdbms\Database\replaceLostConnection
replaceLostConnection( $fname)
Close any existing (dead) database connection and open a new connection.
Definition: Database.php:4607
Wikimedia\Rdbms\Database\$trxWriteAdjQueryCount
int $trxWriteAdjQueryCount
Number of write queries counted in trxWriteAdjDuration.
Definition: Database.php:157
Wikimedia\Rdbms\Database\getBindingHandle
getBindingHandle()
Get the underlying binding connection handle.
Definition: Database.php:5184
Wikimedia\Rdbms\Database\$lastPhpError
string bool $lastPhpError
Definition: Database.php:184
Wikimedia\Rdbms\Database\canRecoverFromDisconnect
canRecoverFromDisconnect( $sql, $priorWritesPending)
Determine whether it is safe to retry queries after a database connection is lost.
Definition: Database.php:1520
Wikimedia\Rdbms\Database\doInsert
doInsert( $table, array $rows, $fname)
Definition: Database.php:2296
Wikimedia\Rdbms\Database\buildGroupConcatField
buildGroupConcatField( $delim, $table, $field, $conds='', $join_conds=[])
Build a GROUP_CONCAT or equivalent statement for a query.
Definition: Database.php:2532
Wikimedia\Rdbms\Database\doInitConnection
doInitConnection()
Actually connect to the database over the wire (or to local files)
Definition: Database.php:317
Wikimedia\Rdbms\Database\$trxReplicaLag
float $trxReplicaLag
Replication lag estimate at the time of BEGIN for the last transaction.
Definition: Database.php:133
Wikimedia\Rdbms\Database\isQuotedIdentifier
isQuotedIdentifier( $name)
Returns if the given identifier looks quoted or not according to the database convention for quoting ...
Definition: Database.php:3034
Wikimedia\Rdbms\IDatabase\getServerVersion
getServerVersion()
A string describing the current software version, like from mysql_get_server_info()
Wikimedia\Rdbms\Database\upsert
upsert( $table, array $rows, $uniqueKeys, array $set, $fname=__METHOD__)
Upsert the given row(s) into a table.
Definition: Database.php:3224
Wikimedia\Rdbms\Database\fieldExists
fieldExists( $table, $field, $fname=__METHOD__)
Determines whether a field exists in a table.
Definition: Database.php:2242
Wikimedia\Rdbms\Database\deadlockLoop
deadlockLoop(... $args)
Perform a deadlock-prone transaction.
Definition: Database.php:3647
Wikimedia\Rdbms\Database\bitOr
bitOr( $fieldLeft, $fieldRight)
Definition: Database.php:2524
Wikimedia\Rdbms\Database\update
update( $table, $set, $conds, $fname=__METHOD__, $options=[])
Update all rows in a table that match a given condition.
Definition: Database.php:2399
Wikimedia\Rdbms\Database\selectRowCount
selectRowCount( $tables, $var=' *', $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Get the number of rows in dataset.
Definition: Database.php:2004
Wikimedia\Rdbms\Database\ignoreIndexClause
ignoreIndexClause( $index)
IGNORE INDEX clause.
Definition: Database.php:3112
Wikimedia\Rdbms\Database\$sessionDirtyTempTables
array $sessionDirtyTempTables
Map of (table name => 1) for current TEMPORARY tables.
Definition: Database.php:120
Wikimedia\Rdbms\Database\trxStatus
trxStatus()
Definition: Database.php:557
Wikimedia\Rdbms\Database\listTables
listTables( $prefix=null, $fname=__METHOD__)
List all tables on the database.
Definition: Database.php:4525
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
Wikimedia\Rdbms\Database\updateTrxWriteQueryTime
updateTrxWriteQueryTime( $sql, $runtime, $affected)
Update the estimated run-time of a query, not counting large row lock times.
Definition: Database.php:1445
Wikimedia\Rdbms\Database\makeWhereFrom2d
makeWhereFrom2d( $data, $baseKey, $subKey)
Build a partial where clause from a 2-d array such as used for LinkBatch.
Definition: Database.php:2493
Wikimedia\Rdbms\Database\$queryLogger
LoggerInterface $queryLogger
Definition: Database.php:55
Wikimedia\Rdbms\DatabaseDomain\newFromId
static newFromId( $domain)
Definition: DatabaseDomain.php:77
Wikimedia\Rdbms\Database\encodeBlob
encodeBlob( $b)
Some DBMSs have a special format for inserting into blob fields, they don't allow simple quoted strin...
Definition: Database.php:4738
Wikimedia\Rdbms\Database\tableNamesN
tableNamesN(... $tables)
Fetch a number of table names into an zero-indexed numerical array This is handy when you need to con...
Definition: Database.php:2822
Wikimedia\Rdbms\Database\getDomainID
getDomainID()
Return the currently selected domain ID.
Definition: Database.php:801
Wikimedia\Rdbms\Database\getClass
static getClass( $dbType, $driver=null)
Definition: Database.php:467
Wikimedia\Rdbms\Database\pingAndCalculateLastTrxApplyTime
pingAndCalculateLastTrxApplyTime()
Definition: Database.php:697
Wikimedia\Rdbms\Database\$topologyRole
string $topologyRole
Replication topology role of the server; one of the class ROLE_* constants.
Definition: Database.php:87
Wikimedia\Rdbms\IDatabase\numRows
numRows( $res)
Get the number of rows in a query result.
Wikimedia\Rdbms\Database\assertHasConnectionHandle
assertHasConnectionHandle()
Make sure there is an open connection handle (alive or not) as a sanity check.
Definition: Database.php:965
Wikimedia\Rdbms\Database\onTransactionPreCommitOrIdle
onTransactionPreCommitOrIdle(callable $callback, $fname=__METHOD__)
Run a callback before the current transaction commits or now if there is none.
Definition: Database.php:3725
Wikimedia\Rdbms\Database\tableLocksHaveTransactionScope
tableLocksHaveTransactionScope()
Checks if table locks acquired by lockTables() are transaction-bound in their scope.
Definition: Database.php:5011
Wikimedia\Rdbms\Database\wasErrorReissuable
wasErrorReissuable()
Determines if the last query error was due to something outside of the query itself.
Definition: Database.php:3619
Wikimedia\Rdbms\Database\deleteJoin
deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname=__METHOD__)
DELETE where the condition is a join.
Definition: Database.php:3277
Wikimedia\Rdbms\Database\setIndexAliases
setIndexAliases(array $aliases)
Convert certain index names to alternative names before querying the DB.
Definition: Database.php:5159
Wikimedia\Rdbms\Database\$password
string $password
Password used to establish the current connection.
Definition: Database.php:81
Wikimedia\Rdbms\Database\decodeBlob
decodeBlob( $b)
Some DBMSs return a special placeholder object representing blob fields in result objects.
Definition: Database.php:4742
Wikimedia\Rdbms\Database\unionConditionPermutations
unionConditionPermutations( $table, $vars, array $permute_conds, $extra_conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Construct a UNION query for permutations of conditions.
Definition: Database.php:3516
Wikimedia\Rdbms\Database\factory
static factory( $type, $params=[], $connect=self::NEW_CONNECTED)
Construct a Database subclass instance given a database type and parameters.
Definition: Database.php:394
Wikimedia\Rdbms\Database\maxListLen
maxListLen()
Return the maximum number of items allowed in a list, or 0 for unlimited.
Definition: Database.php:4734
Wikimedia\Rdbms\Database\selectDomain
selectDomain( $domain)
Set the current domain (database, schema, and table prefix)
Definition: Database.php:2681
Wikimedia\Rdbms\Database\buildIntegerCast
buildIntegerCast( $field)
Definition: Database.php:2654
Wikimedia\Rdbms\Database\getServerUptime
getServerUptime()
Determines how long the server has been up.
Definition: Database.php:3599
Wikimedia\Rdbms\Database\prependDatabaseOrSchema
prependDatabaseOrSchema( $namespace, $relation, $format)
Definition: Database.php:2801
Wikimedia\Rdbms\Database\$delimiter
string $delimiter
Current SQL query delimiter.
Definition: Database.php:102
Wikimedia\Rdbms\Database\__destruct
__destruct()
Run a few simple sanity checks and close dangling connections.
Definition: Database.php:5256
Wikimedia\Rdbms\Database\endAtomic
endAtomic( $fname=__METHOD__)
Ends an atomic section of SQL statements.
Definition: Database.php:4140
Wikimedia\Rdbms\Database\indexName
indexName( $index)
Allows for index remapping in queries where this is not consistent across DBMS.
Definition: Database.php:3002
Wikimedia\Rdbms\Database\executeQuery
executeQuery( $sql, $fname, $flags)
Execute a query, retrying it if there is a recoverable connection loss.
Definition: Database.php:1241
Wikimedia\Rdbms\IDatabase\lastError
lastError()
Get a description of the last error.
DBO_DEBUG
const DBO_DEBUG
Definition: defines.php:9
Wikimedia\Rdbms\Database\setBigSelects
setBigSelects( $value=true)
Allow or deny "big selects" for this session only.
Definition: Database.php:5129
Wikimedia\Rdbms\Database\doDropTable
doDropTable( $table, $fname)
Definition: Database.php:5074
Wikimedia\Rdbms\Database\doReplace
doReplace( $table, array $uniqueKeys, array $rows, $fname)
Definition: Database.php:3142
Wikimedia\Rdbms\DBReadOnlyRoleError
Exception class for attempted DB write access to a DBConnRef with the DB_REPLICA role.
Definition: DBReadOnlyRoleError.php:29
Wikimedia\Rdbms\DBTransactionStateError
Definition: DBTransactionStateError.php:27
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\Database\timestampOrNull
timestampOrNull( $ts=null)
Convert a timestamp in one of the formats accepted by ConvertibleTimestamp to the format used for ins...
Definition: Database.php:4539
Wikimedia\Rdbms\Database\decodeExpiry
decodeExpiry( $expiry, $format=TS_MW)
Decode an expiry time into a DBMS independent format.
Definition: Database.php:5121
Wikimedia\Rdbms\Database\$trxAutomaticAtomic
bool $trxAutomaticAtomic
Whether the current transaction was started implicitly by startAtomic()
Definition: Database.php:145
Wikimedia\Rdbms\Database\initConnection
initConnection()
Initialize the connection to the database over the wire (or to local files)
Definition: Database.php:303
Wikimedia\Rdbms\Database\normalizeConditions
normalizeConditions( $conds, $fname)
Definition: Database.php:2107
Wikimedia\Rdbms\Database\anyString
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.
Definition: Database.php:3080
Wikimedia\Rdbms\Database\unionSupportsOrderAndLimit
unionSupportsOrderAndLimit()
Determine if the RDBMS supports ORDER BY and LIMIT for separate subqueries within UNION.
Definition: Database.php:3506
Wikimedia\Rdbms\DBMasterPos
An object representing a master or replica DB position in a replicated setup.
Definition: DBMasterPos.php:12
Wikimedia\Rdbms\Database\isInsertSelectSafe
isInsertSelectSafe(array $insertOptions, array $selectOptions)
Definition: Database.php:3380
Wikimedia\Rdbms\Database\fieldNameWithAlias
fieldNameWithAlias( $name, $alias=false)
Get an aliased field name e.g.
Definition: Database.php:2871
Wikimedia\Rdbms\Database\assertIsWritableMaster
assertIsWritableMaster()
Make sure that this server is not marked as a replica nor read-only as a sanity check.
Definition: Database.php:976
Wikimedia\Rdbms\Database\normalizeOptions
normalizeOptions( $options)
Definition: Database.php:2180
Wikimedia\Rdbms\Database\getDefaultSchemaVars
getDefaultSchemaVars()
Get schema variables to use if none have been set via setSchemaVars().
Definition: Database.php:4948
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:65
Wikimedia\Rdbms\GeneralizedSql
Lazy-loaded wrapper for simplification and scrubbing of SQL queries for profiling.
Definition: GeneralizedSql.php:30
$s
$s
Definition: mergeMessageFileList.php:185
Wikimedia\Rdbms\Database\extractSingleFieldFromList
extractSingleFieldFromList( $var)
Definition: Database.php:2210
Wikimedia\Rdbms\Database\duplicateTableStructure
duplicateTableStructure( $oldName, $newName, $temporary=false, $fname=__METHOD__)
Creates a new table with structure copied from existing table.
Definition: Database.php:4519
Wikimedia\Rdbms\Database\$trxSectionCancelCallbacks
array[] $trxSectionCancelCallbacks
List of (callable, method name, atomic section id)
Definition: Database.php:168
Wikimedia\Rdbms\Database\doTruncate
doTruncate(array $tables, $fname)
Definition: Database.php:5104
Wikimedia\Rdbms\Database\onTransactionCommitOrIdle
onTransactionCommitOrIdle(callable $callback, $fname=__METHOD__)
Run a callback as soon as there is no transaction pending.
Definition: Database.php:3708
Wikimedia\Rdbms\Database\registerTempWrites
registerTempWrites( $ret, array $changes)
Definition: Database.php:1164
Wikimedia\Rdbms\Database\namedLocksEnqueue
namedLocksEnqueue()
Check to see if a named lock used by lock() use blocking queues.
Definition: Database.php:5007
Wikimedia\Rdbms\Database\buildSubstring
buildSubstring( $input, $startPosition, $length=null)
Definition: Database.php:2590
Wikimedia\Rdbms\ResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: ResultWrapper.php:23
Wikimedia\Rdbms\IDatabase\getSessionLagStatus
getSessionLagStatus()
Get the replica DB lag when the current transaction started or a general lag estimate if not transact...
Wikimedia\Rdbms\Database\tableNamesWithIndexClauseOrJOIN
tableNamesWithIndexClauseOrJOIN( $tables, $use_index=[], $ignore_index=[], $join_conds=[])
Get the aliased table name clause for a FROM clause which might have a JOIN and/or USE INDEX or IGNOR...
Definition: Database.php:2907
Wikimedia\Rdbms\Database\getReplicaPos
getReplicaPos()
Get the replication position of this replica DB.
Definition: Database.php:3687
Wikimedia\Rdbms\Database\getSchemaVars
getSchemaVars()
Get schema variables.
Definition: Database.php:4936
Wikimedia\Rdbms\Database\setLogger
setLogger(LoggerInterface $logger)
Set the PSR-3 logger interface to use for query logging.
Definition: Database.php:529
Wikimedia\Rdbms\Database\__clone
__clone()
Make sure that copies do not share the same client binding handle.
Definition: Database.php:5219
$res
$res
Definition: testCompression.php:57
Wikimedia\Rdbms\Database\cancelAtomic
cancelAtomic( $fname=__METHOD__, AtomicSectionIdentifier $sectionId=null)
Cancel an atomic section of SQL statements.
Definition: Database.php:4174
$base
$base
Definition: generateLocalAutoload.php:11
Wikimedia\Rdbms\Database\getFlag
getFlag( $flag)
Returns a boolean whether the flag $flag is set for this connection.
Definition: Database.php:797
Wikimedia\Rdbms\Database\doInsertSelectGeneric
doInsertSelectGeneric( $destTable, $srcTable, array $varMap, $conds, $fname, array $insertOptions, array $selectOptions, $selectJoinConds)
Implementation of insertSelect() based on select() and insert()
Definition: Database.php:3398
Wikimedia\Rdbms\Database\conditional
conditional( $cond, $trueVal, $falseVal)
Returns an SQL expression for a simple conditional.
Definition: Database.php:3587
Wikimedia\Rdbms\Database\$trxAtomicLevels
array $trxAtomicLevels
List of (name, unique ID, savepoint ID) for each active atomic section level.
Definition: Database.php:143
Wikimedia\Rdbms\Database\$PING_QUERY
static string $PING_QUERY
Dummy SQL query.
Definition: Database.php:230
Wikimedia\Rdbms\Database\handleSessionLossPostconnect
handleSessionLossPostconnect()
Clean things up after session (and thus transaction) loss after reconnect.
Definition: Database.php:1583
Wikimedia\Rdbms\Database\isPristineTemporaryTable
isPristineTemporaryTable( $table)
Check if the table is both a TEMPORARY table and has not yet received CRUD operations.
Definition: Database.php:1195
Wikimedia\Rdbms\Database\$connectionVariables
string[] int[] float[] $connectionVariables
SQL variables values to use for all new connections.
Definition: Database.php:93
Wikimedia\Rdbms\Database\$lastQuery
string $lastQuery
The last SQL query attempted.
Definition: Database.php:180
Wikimedia\Rdbms\Database\$trxStatusIgnoredCause
array null $trxStatusIgnoredCause
Error details of the last statement-only rollback.
Definition: Database.php:129
DBO_TRX
const DBO_TRX
Definition: defines.php:12
Wikimedia\Rdbms\Database\textFieldSize
textFieldSize( $table, $field)
Returns the size of a text field, or -1 for "unlimited".
Definition: Database.php:3295
Wikimedia\Rdbms\Database\buildConcat
buildConcat( $stringList)
Build a concatenation list to feed into a SQL query.
Definition: Database.php:2528
Wikimedia\Rdbms\Database\bitNot
bitNot( $field)
Definition: Database.php:2516
Wikimedia\Rdbms\Database\nextSavepointId
nextSavepointId( $fname)
Definition: Database.php:4095
LIST_AND
const LIST_AND
Definition: Defines.php:48
Wikimedia\Rdbms\Database\$agent
string $agent
Agent name for query profiling.
Definition: Database.php:85
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Wikimedia\Rdbms\Database\__sleep
__sleep()
Called by serialize.
Definition: Database.php:5248
DBO_NOBUFFER
const DBO_NOBUFFER
Definition: defines.php:10
Wikimedia\Rdbms\Database\getTopologyRole
getTopologyRole()
Get the replication topology role of this server.
Definition: Database.php:537
Wikimedia\Rdbms\Database\doBegin
doBegin( $fname)
Issues the BEGIN command to the database server.
Definition: Database.php:4334
Wikimedia\Rdbms\Database\$trxIdleCallbacks
array[] $trxIdleCallbacks
List of (callable, method name, atomic section id)
Definition: Database.php:159
Wikimedia\Rdbms\Database\makeConditionCollidesUponKeys
makeConditionCollidesUponKeys(array $rows, array $uniqueKeys)
Definition: Database.php:3209
Wikimedia\Rdbms\Database\isWriteQuery
isWriteQuery( $sql, $flags)
Determine whether a query writes to the DB.
Definition: Database.php:1043
Wikimedia\Rdbms\Database\isFlagInOptions
isFlagInOptions( $option, array $options)
Definition: Database.php:2196
Wikimedia\Rdbms\Database\clearFlag
clearFlag( $flag, $remember=self::REMEMBER_NOTHING)
Clear a flag for this connection.
Definition: Database.php:769
Wikimedia\Rdbms\Database\getQueryExceptionAndLog
getQueryExceptionAndLog( $error, $errno, $sql, $fname)
Definition: Database.php:1651
Wikimedia\Rdbms\Database\begin
begin( $fname=__METHOD__, $mode=self::TRANSACTION_EXPLICIT)
Begin a transaction.
Definition: Database.php:4275
Wikimedia\Rdbms\IDatabase\fetchObject
fetchObject( $res)
Fetch the next row from the given result object, in object form.
Wikimedia\Rdbms\Database\resultObject
resultObject( $result)
Take a query result and wrap it in an iterable result wrapper if necessary.
Definition: Database.php:4570
Wikimedia\Rdbms\Database\doHandleSessionLossPreconnect
doHandleSessionLossPreconnect()
Reset any additional subclass trx* and session* fields.
Definition: Database.php:1576
Wikimedia\Rdbms\Database\$lastWriteTime
float bool $lastWriteTime
UNIX timestamp of last write query.
Definition: Database.php:182
Wikimedia\Rdbms\Database\doQuery
doQuery( $sql)
Run a query and return a DBMS-dependent wrapper or boolean.
Wikimedia\Rdbms\Database\runOnTransactionIdleCallbacks
runOnTransactionIdleCallbacks( $trigger)
Actually consume and run any "on transaction idle/resolution" callbacks.
Definition: Database.php:3882
Wikimedia\Rdbms\Database\$trxStatusCause
Exception null $trxStatusCause
The last error that caused the status to become STATUS_TRX_ERROR.
Definition: Database.php:127
Wikimedia\Rdbms\Database\close
close( $fname=__METHOD__, $owner=null)
Close the database connection.
Definition: Database.php:887
Wikimedia\Rdbms\Database\replaceVars
replaceVars( $ins)
Database independent variable replacement.
Definition: Database.php:4898
Wikimedia\Rdbms\Database\handleSessionLossPreconnect
handleSessionLossPreconnect()
Clean things up after session (and thus transaction) loss before reconnect.
Definition: Database.php:1545
Wikimedia\Rdbms\Database\doInsertNonConflicting
doInsertNonConflicting( $table, array $rows, $fname)
Definition: Database.php:2312
Wikimedia\Rdbms\Database\$NOT_APPLICABLE
static string $NOT_APPLICABLE
Idiom used when a cancelable atomic section started the transaction.
Definition: Database.php:211
Wikimedia\Rdbms\Database\$trxStatus
int $trxStatus
Transaction status.
Definition: Database.php:125
Wikimedia\Rdbms\Database\runOnTransactionPreCommitCallbacks
runOnTransactionPreCommitCallbacks()
Actually consume and run any "on transaction pre-commit" callbacks.
Definition: Database.php:3950
Wikimedia\Rdbms\Database\wasLockTimeout
wasLockTimeout()
Determines if the last failure was due to a lock timeout.
Definition: Database.php:3607
Wikimedia\Rdbms\Database\$affectedRowCount
integer null $affectedRowCount
Rows affected by the last query to query() or its CRUD wrappers.
Definition: Database.php:175
Wikimedia\Rdbms\Database\$currentDomain
DatabaseDomain $currentDomain
Definition: Database.php:68
LIST_OR
const LIST_OR
Definition: Defines.php:51
Wikimedia\Rdbms\Database\getLag
getLag()
Get the amount of replication lag for this database server.
Definition: Database.php:4720
Wikimedia\Rdbms\Database\$sessionTempTables
array $sessionTempTables
Map of (table name => 1) for current TEMPORARY tables.
Definition: Database.php:118
Wikimedia\Rdbms\Database\buildSelectSubquery
buildSelectSubquery( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Equivalent to IDatabase::selectSQLText() except wraps the result in Subquery.
Definition: Database.php:2658
Wikimedia\Rdbms\Database\$TEMP_PSEUDO_PERMANENT
static int $TEMP_PSEUDO_PERMANENT
Writes to this temporary table effect lastDoneWrites()
Definition: Database.php:218
Wikimedia\Rdbms\Database\assertBuildSubstringParams
assertBuildSubstringParams( $startPosition, $length)
Check type and bounds for parameters to self::buildSubstring()
Definition: Database.php:2611
Wikimedia\Rdbms\Database\isReadOnly
isReadOnly()
Definition: Database.php:5133
Wikimedia\Rdbms\Database\buildStringCast
buildStringCast( $field)
Definition: Database.php:2648
Wikimedia\Rdbms\Database\$lazyMasterHandle
IDatabase null $lazyMasterHandle
Lazy handle to the master DB this server replicates from.
Definition: Database.php:74
Wikimedia\Rdbms\Database\unlockTables
unlockTables( $method)
Unlock all tables locked via lockTables()
Definition: Database.php:5039
Wikimedia\Rdbms\Database\getSessionLagStatus
getSessionLagStatus()
Get the replica DB lag when the current transaction started or a general lag estimate if not transact...
Definition: Database.php:4646
Wikimedia\Rdbms\Database\$trxWriteDuration
float $trxWriteDuration
Seconds spent in write queries for the current transaction.
Definition: Database.php:149
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1030
Wikimedia\Rdbms\Database\doRollbackToSavepoint
doRollbackToSavepoint( $identifier, $fname)
Rollback to a savepoint.
Definition: Database.php:4086
Wikimedia\Rdbms\Database\dropTable
dropTable( $table, $fname=__METHOD__)
Delete a table.
Definition: Database.php:5059
Wikimedia\Rdbms\Database\getTempTableWrites
getTempTableWrites( $sql, $pseudoPermanent)
Definition: Database.php:1108
Wikimedia\Rdbms\Database\doInsertSelectNative
doInsertSelectNative( $destTable, $srcTable, array $varMap, $conds, $fname, array $insertOptions, array $selectOptions, $selectJoinConds)
Native server-side implementation of insertSelect() for situations where we don't want to select ever...
Definition: Database.php:3463
Wikimedia\Rdbms\Database\startAtomic
startAtomic( $fname=__METHOD__, $cancelable=self::ATOMIC_NOT_CANCELABLE)
Begin an atomic section of SQL statements.
Definition: Database.php:4110
Wikimedia\Rdbms\DBQueryTimeoutError
Error thrown when a query times out.
Definition: DBQueryTimeoutError.php:29
Wikimedia\Rdbms\Database\selectOptionsIncludeLocking
selectOptionsIncludeLocking( $options)
Definition: Database.php:2037
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
Wikimedia\Rdbms\Database\getCacheSetOptions
static getCacheSetOptions(IDatabase $db1, IDatabase $db2=null)
Merge the result of getSessionLagStatus() for several DBs using the most pessimistic values to estima...
Definition: Database.php:4703
Wikimedia\Rdbms\Database\open
open( $server, $user, $password, $dbName, $schema, $tablePrefix)
Open a new connection to the database (closing any existing one)
Wikimedia\Rdbms\Database\lock
lock( $lockName, $method, $timeout=5)
Acquire a named lock.
Definition: Database.php:4959
Wikimedia\Rdbms\Database\getTransactionRoundId
getTransactionRoundId()
Definition: Database.php:668
Wikimedia\Rdbms\Database\trxLevel
trxLevel()
Gets the current transaction level.
Definition: Database.php:545
Wikimedia\Rdbms\Database\setFlag
setFlag( $flag, $remember=self::REMEMBER_NOTHING)
Set a flag for this connection.
Definition: Database.php:754
Wikimedia\Rdbms\Database\rollback
rollback( $fname=__METHOD__, $flush=self::FLUSHING_ONE)
Rollback a transaction previously started using begin()
Definition: Database.php:4413
Wikimedia\Rdbms\Database\assertQueryIsCurrentlyAllowed
assertQueryIsCurrentlyAllowed( $sql, $fname)
Error out if the DB is not in a valid state for a query via query()
Definition: Database.php:1474
Wikimedia\Rdbms\Database\setTransactionListener
setTransactionListener( $name, callable $callback=null)
Run a callback after each time any transaction commits or rolls back.
Definition: Database.php:3852
Wikimedia\Rdbms\Database\escapeLikeInternal
escapeLikeInternal( $s, $escapeChar='`')
Definition: Database.php:3043
Wikimedia\Rdbms\Database\$tableAliases
array[] $tableAliases
Current map of (table => (dbname, schema, prefix) map)
Definition: Database.php:104
Wikimedia\Rdbms\Database\reportQueryError
reportQueryError( $error, $errno, $sql, $fname, $ignore=false)
Report a query error.
Definition: Database.php:1636
Wikimedia\Rdbms\Database\$cliMode
bool $cliMode
Whether this PHP instance is for a CLI script.
Definition: Database.php:83
Wikimedia\Rdbms\Database\$trxShortId
string $trxShortId
ID of the active transaction or the empty string otherwise.
Definition: Database.php:123
Wikimedia\Rdbms\Database\isTransactableQuery
isTransactableQuery( $sql)
Determine whether a SQL statement is sensitive to isolation level.
Definition: Database.php:1092
Wikimedia\Rdbms\Database\lockForUpdate
lockForUpdate( $table, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Lock all rows meeting the given conditions/options FOR UPDATE.
Definition: Database.php:2226
LIST_SET
const LIST_SET
Definition: Defines.php:49
Wikimedia\Rdbms\Database\indexExists
indexExists( $table, $index, $fname=__METHOD__)
Determines whether an index exists.
Definition: Database.php:2248
Wikimedia\Rdbms\Database\writesOrCallbacksPending
writesOrCallbacksPending()
Whether there is a transaction open with either possible write queries or unresolved pre-commit/commi...
Definition: Database.php:651
Wikimedia\Rdbms\Database\sourceStream
sourceStream( $fp, callable $lineCallback=null, callable $resultCallback=null, $fname=__METHOD__, callable $inputCallback=null)
Read and execute commands from an open file handle.
Definition: Database.php:4788
Wikimedia\Rdbms\Database\selectSQLText
selectSQLText( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Take the same arguments as IDatabase::select() and return the SQL it would use.
Definition: Database.php:1882
Wikimedia\Rdbms\SelectQueryBuilder
Definition: SelectQueryBuilder.php:5
Wikimedia\Rdbms\Database\makeInsertLists
makeInsertLists(array $rows)
Make SQL lists of columns, row tuples for INSERT/VALUES expressions.
Definition: Database.php:2340
$args
if( $line===false) $args
Definition: mcc.php:124
Wikimedia\Rdbms\Database\$TEMP_NORMAL
static int $TEMP_NORMAL
Writes to this temporary table do not affect lastDoneWrites()
Definition: Database.php:216
Wikimedia\Rdbms\Database\$trxWriteCallers
string[] $trxWriteCallers
Write query callers of the current transaction.
Definition: Database.php:147
Wikimedia\Rdbms\Database\doUpsert
doUpsert( $table, array $rows, array $uniqueKeys, array $set, $fname)
Definition: Database.php:3253
Wikimedia\Rdbms\Database\$lastPing
float $lastPing
UNIX timestamp.
Definition: Database.php:178
Wikimedia\Rdbms\Database\bitAnd
bitAnd( $fieldLeft, $fieldRight)
Definition: Database.php:2520
Wikimedia\Rdbms\Database\buildLike
buildLike( $param,... $params)
LIKE statement wrapper.
Definition: Database.php:3049
Wikimedia\Rdbms\Database\$trxProfiler
TransactionProfiler $trxProfiler
Definition: Database.php:65
Wikimedia\Rdbms\Database\$user
string $user
User that this instance is currently connected under the name of.
Definition: Database.php:79
Wikimedia\Rdbms\Database\assertNoOpenTransactions
assertNoOpenTransactions()
Assert that all explicit transactions or atomic sections have been closed.
Definition: Database.php:1501
Wikimedia\Rdbms\Database\selectDB
selectDB( $db)
Change the current database.
Definition: Database.php:2671
Wikimedia\Rdbms\Database\wasQueryTimeout
wasQueryTimeout( $error, $errno)
Checks whether the cause of the error is detected to be a timeout.
Definition: Database.php:1621
Wikimedia\Rdbms\Database\closeConnection
closeConnection()
Closes underlying database connection.
Wikimedia\Rdbms\Database\lastDoneWrites
lastDoneWrites()
Get the last time the connection may have been used for a write query.
Definition: Database.php:643
Wikimedia\Rdbms\Database\$SLOW_WRITE_SEC
static float $SLOW_WRITE_SEC
Consider a write slow if it took more than this many seconds.
Definition: Database.php:235
Wikimedia\Rdbms\Database\wasReadOnlyError
wasReadOnlyError()
Determines if the last failure was due to the database being read-only.
Definition: Database.php:3615
Wikimedia\Rdbms\Database\beginIfImplied
beginIfImplied( $sql, $fname)
Start an implicit transaction if DBO_TRX is enabled and no transaction is active.
Definition: Database.php:1422
Wikimedia\Rdbms\Database\getQueryException
getQueryException( $error, $errno, $sql, $fname)
Definition: Database.php:1673
Wikimedia\Rdbms\Database\getServerInfo
getServerInfo()
Get a human-readable string describing the current software version.
Definition: Database.php:533
Wikimedia\Rdbms\Database\commit
commit( $fname=__METHOD__, $flush=self::FLUSHING_ONE)
Commits a transaction previously started using begin()
Definition: Database.php:4338
Wikimedia\Rdbms\Database\buildLeast
buildLeast( $fields, $values)
Build a LEAST function statement comparing columns/values.
Definition: Database.php:2544
Wikimedia\Rdbms\Database\restoreFlags
restoreFlags( $state=self::RESTORE_PRIOR)
Restore the flags to their prior state before the last setFlag/clearFlag call.
Definition: Database.php:784
Wikimedia\Rdbms\Database\connectionErrorLogger
connectionErrorLogger( $errno, $errstr)
Error handler for logging errors during database connection This method should not be used outside of...
Definition: Database.php:866
Wikimedia\Rdbms\Database\setLBInfo
setLBInfo( $nameOrArray, $value=null)
Set the entire array or a particular key of the managing load balancer info array.
Definition: Database.php:611
Wikimedia\Rdbms\Database\getScopedLockAndFlush
getScopedLockAndFlush( $lockKey, $fname, $timeout)
Acquire a named lock, flush any transaction, and return an RAII style unlocker object.
Definition: Database.php:4971
Wikimedia\Rdbms\Database\fieldNamesWithAlias
fieldNamesWithAlias( $fields)
Gets an array of aliased field names.
Definition: Database.php:2885
Wikimedia\Rdbms\Database\getLBInfo
getLBInfo( $name=null)
Get properties passed down from the server info array of the load balancer.
Definition: Database.php:599
Wikimedia\Rdbms\DBReadOnlyError
Definition: DBReadOnlyError.php:27
Wikimedia\Rdbms\Database\$trxWriteAffectedRows
int $trxWriteAffectedRows
Number of rows affected by write queries for the current transaction.
Definition: Database.php:153
Wikimedia\Rdbms\Database\$trxWriteAdjDuration
float $trxWriteAdjDuration
Like trxWriteQueryCount but excludes lock-bound, easy to replicate, queries.
Definition: Database.php:155
Wikimedia\Rdbms\Database\insertSelect
insertSelect( $destTable, $srcTable, $varMap, $conds, $fname=__METHOD__, $insertOptions=[], $selectOptions=[], $selectJoinConds=[])
INSERT SELECT wrapper.
Definition: Database.php:3330
Wikimedia\Rdbms\Database\normalizeRowArray
normalizeRowArray(array $rowOrRows)
Definition: Database.php:2081
Wikimedia\Rdbms\Database\selectRow
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Wrapper to IDatabase::select() that only fetches one row (via LIMIT)
Definition: Database.php:1969
Wikimedia\Rdbms\DBQueryError
Definition: DBQueryError.php:27
Wikimedia\Rdbms\Database\useIndexClause
useIndexClause( $index)
USE INDEX clause.
Definition: Database.php:3098
Wikimedia\Rdbms\Database\makeUpdateOptions
makeUpdateOptions( $options)
Make UPDATE options for the Database::update function.
Definition: Database.php:2393
LIST_COMMA
const LIST_COMMA
Definition: Defines.php:47
Wikimedia\Rdbms\Database\getReadOnlyReason
getReadOnlyReason()
Definition: Database.php:5140
Wikimedia\Rdbms\Database\buildSuperlative
buildSuperlative( $sqlfunc, $fields, $values)
Build a superlative function statement comparing columns/values.
Definition: Database.php:2563
Wikimedia\Rdbms\DBQueryDisconnectedError
Definition: DBQueryDisconnectedError.php:28
Wikimedia\Rdbms\Database\$priorFlags
int[] $priorFlags
Prior flags member variable values.
Definition: Database.php:113
Wikimedia\Rdbms\Database\doGetLag
doGetLag()
Definition: Database.php:4730
Wikimedia\Rdbms\Database\installErrorHandler
installErrorHandler()
Set a custom error handler for logging errors during database connection.
Definition: Database.php:825
Wikimedia\Rdbms\Database\aggregateValue
aggregateValue( $valuedata, $valuename='value')
Return aggregated value alias.
Definition: Database.php:2512
Wikimedia\Rdbms\Database\restoreErrorHandler
restoreErrorHandler()
Restore the previous error handler and return the last PHP error for this DB.
Definition: Database.php:836
Wikimedia\Rdbms\Database\preCommitCallbacksPending
preCommitCallbacksPending()
Definition: Database.php:661
Wikimedia\Rdbms\Database\wasKnownStatementRollbackError
wasKnownStatementRollbackError()
Definition: Database.php:3643
Wikimedia\Rdbms\Database\pendingWriteAndCallbackCallers
pendingWriteAndCallbackCallers()
List the methods that have write queries or callbacks for the current transaction.
Definition: Database.php:725
Wikimedia\Rdbms\Database\$trxPreCommitCallbacks
array[] $trxPreCommitCallbacks
List of (callable, method name, atomic section id)
Definition: Database.php:161
Wikimedia\Rdbms\Database\$htmlErrors
string bool null $htmlErrors
Stashed value of html_errors INI setting.
Definition: Database.php:111
Wikimedia\Rdbms\Database\tableExists
tableExists( $table, $fname=__METHOD__)
Query whether a given table exists.
Wikimedia\Rdbms\Database\$SAVEPOINT_PREFIX
static string $SAVEPOINT_PREFIX
Prefix to the atomic section counter used to make savepoint IDs.
Definition: Database.php:213
Wikimedia\Rdbms\Database\pendingWriteCallers
pendingWriteCallers()
Get the list of method names that did write queries for this transaction.
Definition: Database.php:709
Wikimedia\Rdbms\Database\$DEADLOCK_DELAY_MAX
static int $DEADLOCK_DELAY_MAX
Maximum time to wait before retry.
Definition: Database.php:225
Wikimedia\Rdbms\Database\$trxEndCallbacks
array[] $trxEndCallbacks
List of (callable, method name, atomic section id) -var array<array{0:callable,1:string,...
Definition: Database.php:166
Wikimedia\Rdbms\Database\tableNames
tableNames(... $tables)
Fetch a number of table names into an array This is handy when you need to construct SQL for joins.
Definition: Database.php:2812
Wikimedia\Rdbms\Database\__toString
__toString()
Get a debugging string that mentions the database type, the ID of this instance, and the ID of any un...
Definition: Database.php:5195
Wikimedia\Rdbms\Database\$lastRoundTripEstimate
float $lastRoundTripEstimate
Query round trip time estimate.
Definition: Database.php:186
Wikimedia\Rdbms\Database\getLogContext
getLogContext(array $extras=[])
Create a log context to pass to PSR-3 logger functions.
Definition: Database.php:876
Wikimedia\Rdbms\Database\$connectionParams
array $connectionParams
Parameters used by initConnection() to establish a connection.
Definition: Database.php:91
DBO_DDLMODE
const DBO_DDLMODE
Definition: defines.php:16
Wikimedia\Rdbms\Database\$trxAutomatic
bool $trxAutomatic
Whether the current transaction was started implicitly due to DBO_TRX.
Definition: Database.php:139
Wikimedia\Rdbms\Database\flushSnapshot
flushSnapshot( $fname=__METHOD__, $flush=self::FLUSHING_ONE)
Commit any transaction but error out if writes or callbacks are pending.
Definition: Database.php:4483
Wikimedia\Rdbms\Database\pendingWriteRowsAffected
pendingWriteRowsAffected()
Get the number of affected rows from pending write queries.
Definition: Database.php:713
Wikimedia\Rdbms\Database\$trxWriteQueryCount
int $trxWriteQueryCount
Number of write queries for the current transaction.
Definition: Database.php:151
Wikimedia\Rdbms\AtomicSectionIdentifier
Class used for token representing identifiers for atomic sections from IDatabase instances.
Definition: AtomicSectionIdentifier.php:26
Wikimedia\Rdbms\Database\doReleaseSavepoint
doReleaseSavepoint( $identifier, $fname)
Release a savepoint.
Definition: Database.php:4071
Wikimedia\Rdbms\Database\executeQueryAttempt
executeQueryAttempt( $sql, $commentedSql, $isPermWrite, $fname, $flags)
Wrapper for doQuery() that handles DBO_TRX, profiling, logging, affected row count tracking,...
Definition: Database.php:1334
$line
$line
Definition: mcc.php:119
Wikimedia\Rdbms\Database\currentAtomicSectionId
currentAtomicSectionId()
Definition: Database.php:3757
Wikimedia\Rdbms\Database\modifyCallbacksForCancel
modifyCallbacksForCancel(array $sectionIds, AtomicSectionIdentifier $newSectionId=null)
Update callbacks that were owned by cancelled atomic sections.
Definition: Database.php:3817
Wikimedia\Rdbms\Database\setTrxEndCallbackSuppression
setTrxEndCallbackSuppression( $suppress)
Whether to disable running of post-COMMIT/ROLLBACK callbacks.
Definition: Database.php:3868
Wikimedia\Rdbms\Database\query
query( $sql, $fname=__METHOD__, $flags=self::QUERY_NORMAL)
Run an SQL query and return the result.
Definition: Database.php:1204
Wikimedia\Rdbms\IDatabase\fetchRow
fetchRow( $res)
Fetch the next row from the given result object, in associative array form.
Wikimedia\Rdbms\Database\getMasterPos
getMasterPos()
Get the position of this master.
Definition: Database.php:3692
Wikimedia\Rdbms\Database\pendingWriteQueryDuration
pendingWriteQueryDuration( $type=self::ESTIMATE_TOTAL)
Get the time spend running write queries for this transaction.
Definition: Database.php:679
Wikimedia\Rdbms\Database\$indexAliases
string[] $indexAliases
Current map of (index alias => index)
Definition: Database.php:106
Wikimedia\Rdbms\LikeMatch
Used by Database::buildLike() to represent characters that have special meaning in SQL LIKE clauses a...
Definition: LikeMatch.php:10
Wikimedia\Rdbms\Database\tableName
tableName( $name, $format='quoted')
Format a table name ready for use in constructing an SQL query.
Definition: Database.php:2703
Wikimedia\Rdbms\Database\wasConnectionError
wasConnectionError( $errno)
Do not use this method outside of Database/DBError classes.
Definition: Database.php:3633
Wikimedia\Rdbms\Database\doAtomicSection
doAtomicSection( $fname, callable $callback, $cancelable=self::ATOMIC_NOT_CANCELABLE)
Perform an atomic section of reversable SQL statements from a callback.
Definition: Database.php:4259
Wikimedia\Rdbms\Database\replace
replace( $table, $uniqueKeys, $rows, $fname=__METHOD__)
Insert row(s) into a table, deleting all conflicting rows beforehand.
Definition: Database.php:3116
Wikimedia\Rdbms\Database\getRecordedTransactionLagStatus
getRecordedTransactionLagStatus()
Get the replica DB lag when the current transaction started.
Definition: Database.php:4663
Wikimedia\Rdbms\Database\strencode
strencode( $s)
Wrapper for addslashes()
Wikimedia\Rdbms\Database\runOnAtomicSectionCancelCallbacks
runOnAtomicSectionCancelCallbacks( $trigger, array $sectionIds=null)
Actually run any "atomic section cancel" callbacks.
Definition: Database.php:3984
Wikimedia\Rdbms\Database\getServer
getServer()
Get the server hostname or IP address.
Definition: Database.php:2699
Wikimedia\Rdbms\DBUnexpectedError
Definition: DBUnexpectedError.php:27
Wikimedia\Rdbms\Database\doLockTables
doLockTables(array $read, array $write, $method)
Helper function for lockTables() that handles the actual table locking.
Definition: Database.php:5035
Wikimedia\Rdbms\Database\doUnlockTables
doUnlockTables( $method)
Helper function for unlockTables() that handles the actual table unlocking.
Definition: Database.php:5055
Wikimedia\Rdbms\Database\timestamp
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by ConvertibleTimestamp to the format used for ins...
Definition: Database.php:4533
Wikimedia\Rdbms\Database\selectField
selectField( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a single field from a single result row.
Definition: Database.php:1709
Wikimedia\Rdbms\Database\anyChar
anyChar()
Returns a token for buildLike() that denotes a '_' to be used in a LIKE query.
Definition: Database.php:3076
Wikimedia\Rdbms\Database\onTransactionIdle
onTransactionIdle(callable $callback, $fname=__METHOD__)
Alias for onTransactionCommitOrIdle() for backwards-compatibility.
Definition: Database.php:3721
Wikimedia\Rdbms\Database\unlock
unlock( $lockName, $method)
Release a lock.
Definition: Database.php:4965
Wikimedia\Rdbms\Database\$profiler
callable null $profiler
Definition: Database.php:63
Wikimedia\Rdbms\Database\$SMALL_WRITE_ROWS
static float $SMALL_WRITE_ROWS
Assume an insert of this many rows or less should be fast to replicate.
Definition: Database.php:237
Wikimedia\Rdbms\Database\addIdentifierQuotes
addIdentifierQuotes( $s)
Escape a SQL identifier (e.g.
Definition: Database.php:3021
Wikimedia\Rdbms\Database\getLazyMasterHandle
getLazyMasterHandle()
Get a handle to the master server of the cluster to which this server belongs.
Definition: Database.php:631
Wikimedia\Rdbms\Database\consumeTrxShortId
consumeTrxShortId()
Reset the transaction ID and return the old one.
Definition: Database.php:1604
Wikimedia\Rdbms\Database\estimateRowCount
estimateRowCount( $tables, $var=' *', $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Estimate the number of rows in dataset.
Definition: Database.php:1987
Wikimedia\Rdbms\Database\indexUnique
indexUnique( $table, $index)
Determines if a given index is unique.
Definition: Database.php:2263
Wikimedia\Rdbms\Database\truncate
truncate( $tables, $fname=__METHOD__)
Delete all data in a table(s) and reset any sequences owned by that table(s)
Definition: Database.php:5082
Wikimedia\Rdbms\Database\dbSchema
dbSchema( $schema=null)
Get/set the db schema.
Definition: Database.php:574
Wikimedia\Rdbms\Database\$trxRecurringCallbacks
callable[] $trxRecurringCallbacks
Map of (name => callable)
Definition: Database.php:170
Wikimedia\Rdbms\Database\getQueryVerb
getQueryVerb( $sql)
Definition: Database.php:1075
Wikimedia\Rdbms\Database\unionQueries
unionQueries( $sqls, $all)
Construct a UNION query.
Definition: Database.php:3510
Wikimedia\Rdbms\DBTransactionError
Definition: DBTransactionError.php:27
Wikimedia\Rdbms\Database\reassignCallbacksForSection
reassignCallbacksForSection(AtomicSectionIdentifier $old, AtomicSectionIdentifier $new)
Hoist callback ownership for callbacks in a section to a parent section.
Definition: Database.php:3773
Wikimedia\Rdbms\Database\insert
insert( $table, $rows, $fname=__METHOD__, $options=[])
Insert the given row(s) into a table.
Definition: Database.php:2273
Wikimedia\Rdbms\IDatabase\lastErrno
lastErrno()
Get the last error number.
Wikimedia\Rdbms\Database\relationSchemaQualifier
relationSchemaQualifier()
Definition: Database.php:595
Wikimedia\Rdbms\Database\$DEADLOCK_TRIES
static int $DEADLOCK_TRIES
Number of times to re-try an operation in case of deadlock.
Definition: Database.php:221
Wikimedia\Rdbms\Database\makeInsertNonConflictingVerbAndOptions
makeInsertNonConflictingVerbAndOptions()
Definition: Database.php:2326
Wikimedia\Rdbms\Database\newExceptionAfterConnectError
newExceptionAfterConnectError( $error)
Definition: Database.php:1687
Wikimedia\Rdbms\Database\lockTables
lockTables(array $read, array $write, $method)
Lock specific tables.
Definition: Database.php:5015
Wikimedia\Rdbms\Database\$trxAtomicCounter
int $trxAtomicCounter
Counter for atomic savepoint identifiers (reset with each transaction)
Definition: Database.php:141
Wikimedia\Rdbms\Database\encodeExpiry
encodeExpiry( $expiry)
Encode an expiry time into the DBMS dependent format.
Definition: Database.php:5115
Wikimedia\Rdbms\Database\$srvCache
BagOStuff $srvCache
APC cache.
Definition: Database.php:51
Wikimedia\Rdbms\Database\buildGreatest
buildGreatest( $fields, $values)
Build a GREATEST function statement comparing columns/values.
Definition: Database.php:2540
Wikimedia\Rdbms\Database\doSavepoint
doSavepoint( $identifier, $fname)
Create a savepoint.
Definition: Database.php:4056
Wikimedia\Rdbms\Database\setSchemaVars
setSchemaVars( $vars)
Set schema variables to be used when streaming commands from SQL files or stdin.
Definition: Database.php:4784
Wikimedia\Rdbms\Database\__construct
__construct(array $params)
Definition: Database.php:255
Wikimedia\Rdbms\Database\trxTimestamp
trxTimestamp()
Get the UNIX timestamp of the time that the transaction was established.
Definition: Database.php:549
Wikimedia\Rdbms\Database\$trxTimestamp
float null $trxTimestamp
UNIX timestamp at the time of BEGIN for the last transaction.
Definition: Database.php:131
Wikimedia\Rdbms\Database\makeGroupByWithHaving
makeGroupByWithHaving( $options)
Returns an optional GROUP BY with an optional HAVING.
Definition: Database.php:1836
Wikimedia\Rdbms\Database\tableNameWithAlias
tableNameWithAlias( $table, $alias=false)
Get an aliased table name.
Definition: Database.php:2843
Wikimedia\Rdbms\Database\getAttributes
static getAttributes()
Definition: Database.php:518
Wikimedia\Rdbms\Database\writesPending
writesPending()
Definition: Database.php:647
Wikimedia\Rdbms\Database\normalizeUpsertKeys
normalizeUpsertKeys( $uniqueKeys)
Definition: Database.php:2128
Wikimedia\Rdbms\Database\selectFieldsOrOptionsAggregate
selectFieldsOrOptionsAggregate( $fields, $options)
Definition: Database.php:2053
Wikimedia\Rdbms\Database\lockIsFree
lockIsFree( $lockName, $method)
Check to see if a named lock is not locked by any thread (non-blocking)
Definition: Database.php:4952
Wikimedia\Rdbms\Database\ping
ping(&$rtt=null)
Ping the server and try to reconnect if it there is no connection.
Definition: Database.php:4582
Wikimedia\Rdbms\Database\$nonNativeInsertSelectBatchSize
int $nonNativeInsertSelectBatchSize
Row batch size to use for emulated INSERT SELECT queries.
Definition: Database.php:95
Wikimedia\Rdbms\Database\runTransactionListenerCallbacks
runTransactionListenerCallbacks( $trigger)
Actually run any "transaction listener" callbacks.
Definition: Database.php:4024
Wikimedia\Rdbms\Database\onAtomicSectionCancel
onAtomicSectionCancel(callable $callback, $fname=__METHOD__)
Run a callback when the atomic section is cancelled.
Definition: Database.php:3747
Wikimedia\Rdbms\Database\getTopologyRootMaster
getTopologyRootMaster()
Get the host (or address) of the root master server for the replication topology.
Definition: Database.php:541
Wikimedia\Rdbms\DBConnectionError
Definition: DBConnectionError.php:26
Wikimedia\Rdbms\Database\setTableAliases
setTableAliases(array $aliases)
Make certain table names use their own database, schema, and table prefix when passed into SQL querie...
Definition: Database.php:5155
Wikimedia\Rdbms\Database\$MUTABLE_FLAGS
static string[] $MUTABLE_FLAGS
List of DBO_* flags that can be changed after connection.
Definition: Database.php:240
Wikimedia\Rdbms\Database\masterPosWait
masterPosWait(DBMasterPos $pos, $timeout)
Wait for the replica DB to catch up to a given master position.
Definition: Database.php:3682
Wikimedia\Rdbms\Database\onTransactionResolution
onTransactionResolution(callable $callback, $fname=__METHOD__)
Run a callback as soon as the current transaction commits or rolls back.
Definition: Database.php:3701
Wikimedia\Rdbms\IDatabase\getType
getType()
Get the type of the DBMS (e.g.
Wikimedia\Rdbms\Database\explicitTrxActive
explicitTrxActive()
Definition: Database.php:4515
Wikimedia\Rdbms\Database\$server
string $server
Server that this instance is currently connected to.
Definition: Database.php:77
Wikimedia\Rdbms\Database\freeResult
freeResult( $res)
Free a result object returned by query() or select()
Definition: Database.php:1702
Wikimedia\Rdbms\Database\isOpen
isOpen()
Definition: Database.php:750
Wikimedia\Rdbms\Database\doCommit
doCommit( $fname)
Issues the COMMIT command to the database server.
Definition: Database.php:4407
$source
$source
Definition: mwdoc-filter.php:34
Wikimedia\Rdbms\IMaintainableDatabase\fieldInfo
fieldInfo( $table, $field)
mysql_fetch_field() wrapper Returns false if the field doesn't exist
Wikimedia\Rdbms\Database\$topologyRootMaster
string null $topologyRootMaster
Host (or address) of the root master server for the replication topology.
Definition: Database.php:89
Wikimedia\Rdbms\Database\makeOrderBy
makeOrderBy( $options)
Returns an optional ORDER BY.
Definition: Database.php:1862
Wikimedia\Rdbms\Database\$flags
int $flags
Current bit field of class DBO_* constants.
Definition: Database.php:98
Wikimedia\Rdbms\Database\serverIsReadOnly
serverIsReadOnly()
Definition: Database.php:3697
Wikimedia\Rdbms\Database\makeSelectOptions
makeSelectOptions(array $options)
Returns an optional USE INDEX clause to go after the table, and a string to go at the end of the quer...
Definition: Database.php:1765
Wikimedia\Rdbms\Database\select
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
Definition: Database.php:1874
Wikimedia\Rdbms\Database\attributesFromType
static attributesFromType( $dbType, $driver=null)
Definition: Database.php:449
Wikimedia\Rdbms\Database\lastQuery
lastQuery()
Get the last query that sent on account of IDatabase::query()
Definition: Database.php:639
Wikimedia\Rdbms\Database\wasConnectionLoss
wasConnectionLoss()
Determines if the last query error was due to a dropped connection.
Definition: Database.php:3611
Wikimedia\Rdbms\Database\wasDeadlock
wasDeadlock()
Determines if the last failure was due to a deadlock.
Definition: Database.php:3603
$t
$t
Definition: testCompression.php:74
Wikimedia\Rdbms\Database\doSelectDomain
doSelectDomain(DatabaseDomain $domain)
Definition: Database.php:2691
Wikimedia\Rdbms\Database\$connLogger
LoggerInterface $connLogger
Definition: Database.php:53
Wikimedia\Rdbms\Database\addQuotes
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
Definition: Database.php:3006
LIST_NAMES
const LIST_NAMES
Definition: Defines.php:50
Wikimedia\Rdbms\Database\$replLogger
LoggerInterface $replLogger
Definition: Database.php:57
Wikimedia\Rdbms\Database\assertConditionIsNotEmpty
assertConditionIsNotEmpty( $conds, string $fname, bool $deprecate)
Check type and bounds conditions parameters for update.
Definition: Database.php:2637
Wikimedia\Rdbms\Database\getApproximateLagStatus
getApproximateLagStatus()
Get a replica DB lag estimate for this server at the start of a transaction.
Definition: Database.php:4677
Wikimedia\Rdbms\Database\qualifiedTableComponents
qualifiedTableComponents( $name)
Get the table components needed for a query given the currently selected database.
Definition: Database.php:2761
Wikimedia\Rdbms\Database\affectedRows
affectedRows()
Get the number of rows affected by the last write query.
Definition: Database.php:4547
Wikimedia\Rdbms\Database\$DEADLOCK_DELAY_MIN
static int $DEADLOCK_DELAY_MIN
Minimum time to wait before retry, in microseconds.
Definition: Database.php:223
Wikimedia\Rdbms\Database\fetchAffectedRowCount
fetchAffectedRowCount()
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/schema/prefix specifications for IDatabase.
Definition: DatabaseDomain.php:40
Wikimedia\Rdbms\Database\databasesAreIndependent
databasesAreIndependent()
Returns true if DBs are assumed to be on potentially different servers.
Definition: Database.php:2667
Wikimedia\Rdbms\TransactionProfiler
Detect high-contention DB queries via profiling calls.
Definition: TransactionProfiler.php:38
Wikimedia\Rdbms\Database\indexInfo
indexInfo( $table, $index, $fname=__METHOD__)
Get information about an index into an object.
Wikimedia\Rdbms\Database\strreplace
strreplace( $orig, $old, $new)
Returns a SQL expression for simple string replacement (e.g.
Definition: Database.php:3595
Wikimedia\Rdbms\Database\$ownerId
int null $ownerId
Integer ID of the managing LBFactory instance or null if none.
Definition: Database.php:189
Wikimedia\Rdbms\Database\fieldHasBit
fieldHasBit( $field, $flags)
Definition: Database.php:5169
Wikimedia\Rdbms\Database\sourceFile
sourceFile( $filename, callable $lineCallback=null, callable $resultCallback=null, $fname=false, callable $inputCallback=null)
Read and execute SQL commands from a file.
Definition: Database.php:4752
Wikimedia\Rdbms\Database\listViews
listViews( $prefix=null, $fname=__METHOD__)
Lists all the VIEWs in the database.
Definition: Database.php:4529
Wikimedia\Rdbms\Database\newSelectQueryBuilder
newSelectQueryBuilder()
Create an empty SelectQueryBuilder which can be used to run queries against this connection.
Definition: Database.php:1705
Wikimedia\Rdbms\Database\streamStatementEnd
streamStatementEnd(&$sql, &$newLine)
Called by sourceStream() to check if we've reached a statement end.
Definition: Database.php:4865
Wikimedia\Rdbms\Database\makeList
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
Definition: Database.php:2417
Wikimedia\Rdbms\IMaintainableDatabase
Advanced database interface for IDatabase handles that include maintenance methods.
Definition: IMaintainableDatabase.php:38
Wikimedia\Rdbms\Database\nextSequenceValue
nextSequenceValue( $seqName)
Deprecated method, calls should be removed.
Definition: Database.php:3084
Wikimedia\Rdbms\Subquery
Definition: Subquery.php:27
Wikimedia\Rdbms\Database\makeConditionCollidesUponKey
makeConditionCollidesUponKey(array $rows, array $uniqueKey)
Definition: Database.php:3168
Wikimedia\Rdbms\Database\selectFieldValues
selectFieldValues( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a list of single field values from result rows.
Definition: Database.php:1732
Wikimedia\Rdbms\Database\flatAtomicSectionList
flatAtomicSectionList()
Definition: Database.php:744
Wikimedia\Rdbms\Database\$deprecationLogger
callable $deprecationLogger
Deprecation logging callback.
Definition: Database.php:61
Wikimedia\Rdbms\Database\$errorLogger
callable $errorLogger
Error logging callback.
Definition: Database.php:59
Wikimedia\Rdbms\Database\$conn
object resource null $conn
Database connection.
Definition: Database.php:71
Wikimedia\Rdbms\Database\doRollback
doRollback( $fname)
Issues the ROLLBACK command to the database server.
Definition: Database.php:4476
Wikimedia\Rdbms\Database\$TINY_WRITE_SEC
static float $TINY_WRITE_SEC
Guess of how many seconds it takes to replicate a small insert.
Definition: Database.php:233
Wikimedia\Rdbms\Database\$lbInfo
array $lbInfo
Current LoadBalancer tracking information.
Definition: Database.php:100
Wikimedia\Rdbms\Database\getDBname
getDBname()
Get the current DB name.
Definition: Database.php:2695
Wikimedia\Rdbms\Blob
Definition: Blob.php:5
Wikimedia\Rdbms\Database\getInfinity
getInfinity()
Find out when 'infinity' is.
Definition: Database.php:5111
$type
$type
Definition: testCompression.php:52
Wikimedia\Rdbms\Database\$sessionNamedLocks
array $sessionNamedLocks
Map of (name => 1) for locks obtained via lock()
Definition: Database.php:116