MediaWiki  master
DatabaseMysqlBase.php
Go to the documentation of this file.
1 <?php
23 namespace Wikimedia\Rdbms;
24 
25 use DateTime;
26 use DateTimeZone;
27 use Exception;
28 use InvalidArgumentException;
29 use RuntimeException;
30 use stdClass;
31 use Wikimedia\AtEase\AtEase;
32 
41 abstract class DatabaseMysqlBase extends Database {
47  protected $lagDetectionOptions = [];
49  protected $useGTIDs = false;
51  protected $sslKeyPath;
53  protected $sslCertPath;
55  protected $sslCAFile;
57  protected $sslCAPath;
59  protected $sslCiphers;
61  protected $sqlMode;
63  protected $utf8Mode;
65  protected $defaultBigSelects = null;
66 
68  private $insertSelectIsSafe = null;
70  private $replicationInfoRow = null;
71 
72  // Cache getServerId() for 24 hours
73  const SERVER_ID_CACHE_TTL = 86400;
74 
76  const LAG_STALE_WARN_THRESHOLD = 0.100;
77 
97  public function __construct( array $params ) {
98  $this->lagDetectionMethod = $params['lagDetectionMethod'] ?? 'Seconds_Behind_Master';
99  $this->lagDetectionOptions = $params['lagDetectionOptions'] ?? [];
100  $this->useGTIDs = !empty( $params['useGTIDs' ] );
101  foreach ( [ 'KeyPath', 'CertPath', 'CAFile', 'CAPath', 'Ciphers' ] as $name ) {
102  $var = "ssl{$name}";
103  if ( isset( $params[$var] ) ) {
104  $this->$var = $params[$var];
105  }
106  }
107  $this->sqlMode = $params['sqlMode'] ?? null;
108  $this->utf8Mode = !empty( $params['utf8Mode'] );
109  $this->insertSelectIsSafe = isset( $params['insertSelectIsSafe'] )
110  ? (bool)$params['insertSelectIsSafe'] : null;
111 
112  parent::__construct( $params );
113  }
114 
118  public function getType() {
119  return 'mysql';
120  }
121 
122  protected function open( $server, $user, $password, $dbName, $schema, $tablePrefix ) {
123  $this->close();
124 
125  if ( $schema !== null ) {
126  throw $this->newExceptionAfterConnectError( "Got schema '$schema'; not supported." );
127  }
128 
129  $this->server = $server;
130  $this->user = $user;
131  $this->password = $password;
132 
133  $this->installErrorHandler();
134  try {
135  $this->conn = $this->mysqlConnect( $this->server, $dbName );
136  } catch ( Exception $e ) {
137  $this->restoreErrorHandler();
138  throw $this->newExceptionAfterConnectError( $e->getMessage() );
139  }
140  $error = $this->restoreErrorHandler();
141 
142  if ( !$this->conn ) {
143  throw $this->newExceptionAfterConnectError( $error ?: $this->lastError() );
144  }
145 
146  try {
147  $this->currentDomain = new DatabaseDomain(
148  strlen( $dbName ) ? $dbName : null,
149  null,
150  $tablePrefix
151  );
152  // Abstract over any insane MySQL defaults
153  $set = [ 'group_concat_max_len = 262144' ];
154  // Set SQL mode, default is turning them all off, can be overridden or skipped with null
155  if ( is_string( $this->sqlMode ) ) {
156  $set[] = 'sql_mode = ' . $this->addQuotes( $this->sqlMode );
157  }
158  // Set any custom settings defined by site config
159  // (e.g. https://dev.mysql.com/doc/refman/4.1/en/innodb-parameters.html)
160  foreach ( $this->connectionVariables as $var => $val ) {
161  // Escape strings but not numbers to avoid MySQL complaining
162  if ( !is_int( $val ) && !is_float( $val ) ) {
163  $val = $this->addQuotes( $val );
164  }
165  $set[] = $this->addIdentifierQuotes( $var ) . ' = ' . $val;
166  }
167 
168  if ( $set ) {
169  $this->query(
170  'SET ' . implode( ', ', $set ),
171  __METHOD__,
172  self::QUERY_IGNORE_DBO_TRX | self::QUERY_NO_RETRY
173  );
174  }
175  } catch ( Exception $e ) {
176  throw $this->newExceptionAfterConnectError( $e->getMessage() );
177  }
178  }
179 
180  protected function doSelectDomain( DatabaseDomain $domain ) {
181  if ( $domain->getSchema() !== null ) {
182  throw new DBExpectedError(
183  $this,
184  __CLASS__ . ": domain '{$domain->getId()}' has a schema component"
185  );
186  }
187 
188  $database = $domain->getDatabase();
189  // A null database means "don't care" so leave it as is and update the table prefix
190  if ( $database === null ) {
191  $this->currentDomain = new DatabaseDomain(
192  $this->currentDomain->getDatabase(),
193  null,
194  $domain->getTablePrefix()
195  );
196 
197  return true;
198  }
199 
200  if ( $database !== $this->getDBname() ) {
201  $sql = 'USE ' . $this->addIdentifierQuotes( $database );
202  list( $res, $err, $errno ) =
203  $this->executeQuery( $sql, __METHOD__, self::QUERY_IGNORE_DBO_TRX );
204 
205  if ( $res === false ) {
206  $this->reportQueryError( $err, $errno, $sql, __METHOD__ );
207  return false; // unreachable
208  }
209  }
210 
211  // Update that domain fields on success (no exception thrown)
212  $this->currentDomain = $domain;
213 
214  return true;
215  }
216 
225  abstract protected function mysqlConnect( $realServer, $dbName );
226 
231  public function freeResult( $res ) {
232  AtEase::suppressWarnings();
233  $ok = $this->mysqlFreeResult( ResultWrapper::unwrap( $res ) );
234  AtEase::restoreWarnings();
235  if ( !$ok ) {
236  throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
237  }
238  }
239 
246  abstract protected function mysqlFreeResult( $res );
247 
253  public function fetchObject( $res ) {
254  AtEase::suppressWarnings();
255  $row = $this->mysqlFetchObject( ResultWrapper::unwrap( $res ) );
256  AtEase::restoreWarnings();
257 
258  $errno = $this->lastErrno();
259  // Unfortunately, mysql_fetch_object does not reset the last errno.
260  // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
261  // these are the only errors mysql_fetch_object can cause.
262  // See https://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
263  if ( $errno == 2000 || $errno == 2013 ) {
264  throw new DBUnexpectedError(
265  $this,
266  'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() )
267  );
268  }
269 
270  return $row;
271  }
272 
279  abstract protected function mysqlFetchObject( $res );
280 
286  public function fetchRow( $res ) {
287  AtEase::suppressWarnings();
288  $row = $this->mysqlFetchArray( ResultWrapper::unwrap( $res ) );
289  AtEase::restoreWarnings();
290 
291  $errno = $this->lastErrno();
292  // Unfortunately, mysql_fetch_array does not reset the last errno.
293  // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
294  // these are the only errors mysql_fetch_array can cause.
295  // See https://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
296  if ( $errno == 2000 || $errno == 2013 ) {
297  throw new DBUnexpectedError(
298  $this,
299  'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() )
300  );
301  }
302 
303  return $row;
304  }
305 
312  abstract protected function mysqlFetchArray( $res );
313 
319  function numRows( $res ) {
320  if ( is_bool( $res ) ) {
321  $n = 0;
322  } else {
323  AtEase::suppressWarnings();
324  $n = $this->mysqlNumRows( ResultWrapper::unwrap( $res ) );
325  AtEase::restoreWarnings();
326  }
327 
328  // Unfortunately, mysql_num_rows does not reset the last errno.
329  // We are not checking for any errors here, since
330  // there are no errors mysql_num_rows can cause.
331  // See https://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
332  // See https://phabricator.wikimedia.org/T44430
333  return $n;
334  }
335 
342  abstract protected function mysqlNumRows( $res );
343 
348  public function numFields( $res ) {
349  return $this->mysqlNumFields( ResultWrapper::unwrap( $res ) );
350  }
351 
358  abstract protected function mysqlNumFields( $res );
359 
365  public function fieldName( $res, $n ) {
366  return $this->mysqlFieldName( ResultWrapper::unwrap( $res ), $n );
367  }
368 
376  abstract protected function mysqlFieldName( $res, $n );
377 
384  public function fieldType( $res, $n ) {
385  return $this->mysqlFieldType( ResultWrapper::unwrap( $res ), $n );
386  }
387 
395  abstract protected function mysqlFieldType( $res, $n );
396 
402  public function dataSeek( $res, $row ) {
403  return $this->mysqlDataSeek( ResultWrapper::unwrap( $res ), $row );
404  }
405 
413  abstract protected function mysqlDataSeek( $res, $row );
414 
418  public function lastError() {
419  if ( $this->conn ) {
420  # Even if it's non-zero, it can still be invalid
421  AtEase::suppressWarnings();
422  $error = $this->mysqlError( $this->conn );
423  if ( !$error ) {
424  $error = $this->mysqlError();
425  }
426  AtEase::restoreWarnings();
427  } else {
428  $error = $this->mysqlError();
429  }
430  if ( $error ) {
431  $error .= ' (' . $this->server . ')';
432  }
433 
434  return $error;
435  }
436 
443  abstract protected function mysqlError( $conn = null );
444 
445  protected function wasQueryTimeout( $error, $errno ) {
446  // https://dev.mysql.com/doc/refman/8.0/en/client-error-reference.html
447  // https://phabricator.wikimedia.org/T170638
448  return in_array( $errno, [ 2062, 3024 ] );
449  }
450 
451  public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
452  $this->nativeReplace( $table, $rows, $fname );
453  }
454 
455  protected function isInsertSelectSafe( array $insertOptions, array $selectOptions ) {
456  $row = $this->getReplicationSafetyInfo();
457  // For row-based-replication, the resulting changes will be relayed, not the query
458  if ( $row->binlog_format === 'ROW' ) {
459  return true;
460  }
461  // LIMIT requires ORDER BY on a unique key or it is non-deterministic
462  if ( isset( $selectOptions['LIMIT'] ) ) {
463  return false;
464  }
465  // In MySQL, an INSERT SELECT is only replication safe with row-based
466  // replication or if innodb_autoinc_lock_mode is 0. When those
467  // conditions aren't met, use non-native mode.
468  // While we could try to determine if the insert is safe anyway by
469  // checking if the target table has an auto-increment column that
470  // isn't set in $varMap, that seems unlikely to be worth the extra
471  // complexity.
472  return (
473  in_array( 'NO_AUTO_COLUMNS', $insertOptions ) ||
474  (int)$row->innodb_autoinc_lock_mode === 0
475  );
476  }
477 
481  protected function getReplicationSafetyInfo() {
482  if ( $this->replicationInfoRow === null ) {
483  $this->replicationInfoRow = $this->selectRow(
484  false,
485  [
486  'innodb_autoinc_lock_mode' => '@@innodb_autoinc_lock_mode',
487  'binlog_format' => '@@binlog_format',
488  ],
489  [],
490  __METHOD__
491  );
492  }
493 
495  }
496 
510  public function estimateRowCount( $table, $var = '*', $conds = '',
511  $fname = __METHOD__, $options = [], $join_conds = []
512  ) {
513  $conds = $this->normalizeConditions( $conds, $fname );
514  $column = $this->extractSingleFieldFromList( $var );
515  if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
516  $conds[] = "$column IS NOT NULL";
517  }
518 
519  $options['EXPLAIN'] = true;
520  $res = $this->select( $table, $var, $conds, $fname, $options, $join_conds );
521  if ( $res === false ) {
522  return false;
523  }
524  if ( !$this->numRows( $res ) ) {
525  return 0;
526  }
527 
528  $rows = 1;
529  foreach ( $res as $plan ) {
530  $rows *= $plan->rows > 0 ? $plan->rows : 1; // avoid resetting to zero
531  }
532 
533  return (int)$rows;
534  }
535 
536  public function tableExists( $table, $fname = __METHOD__ ) {
537  // Split database and table into proper variables as Database::tableName() returns
538  // shared tables prefixed with their database, which do not work in SHOW TABLES statements
539  list( $database, , $prefix, $table ) = $this->qualifiedTableComponents( $table );
540  $tableName = "{$prefix}{$table}";
541 
542  if ( isset( $this->sessionTempTables[$tableName] ) ) {
543  return true; // already known to exist and won't show in SHOW TABLES anyway
544  }
545 
546  // We can't use buildLike() here, because it specifies an escape character
547  // other than the backslash, which is the only one supported by SHOW TABLES
548  $encLike = $this->escapeLikeInternal( $tableName, '\\' );
549 
550  // If the database has been specified (such as for shared tables), use "FROM"
551  if ( $database !== '' ) {
552  $encDatabase = $this->addIdentifierQuotes( $database );
553  $query = "SHOW TABLES FROM $encDatabase LIKE '$encLike'";
554  } else {
555  $query = "SHOW TABLES LIKE '$encLike'";
556  }
557 
558  return $this->query( $query, $fname )->numRows() > 0;
559  }
560 
566  public function fieldInfo( $table, $field ) {
567  $table = $this->tableName( $table );
568  $flags = self::QUERY_SILENCE_ERRORS;
569  $res = $this->query( "SELECT * FROM $table LIMIT 1", __METHOD__, $flags );
570  if ( !$res ) {
571  return false;
572  }
573  $n = $this->mysqlNumFields( ResultWrapper::unwrap( $res ) );
574  for ( $i = 0; $i < $n; $i++ ) {
575  $meta = $this->mysqlFetchField( ResultWrapper::unwrap( $res ), $i );
576  if ( $field == $meta->name ) {
577  return new MySQLField( $meta );
578  }
579  }
580 
581  return false;
582  }
583 
591  abstract protected function mysqlFetchField( $res, $n );
592 
602  public function indexInfo( $table, $index, $fname = __METHOD__ ) {
603  # SHOW INDEX works in MySQL 3.23.58, but SHOW INDEXES does not.
604  # SHOW INDEX should work for 3.x and up:
605  # https://dev.mysql.com/doc/mysql/en/SHOW_INDEX.html
606  $table = $this->tableName( $table );
607  $index = $this->indexName( $index );
608 
609  $sql = 'SHOW INDEX FROM ' . $table;
610  $res = $this->query( $sql, $fname );
611 
612  if ( !$res ) {
613  return null;
614  }
615 
616  $result = [];
617 
618  foreach ( $res as $row ) {
619  if ( $row->Key_name == $index ) {
620  $result[] = $row;
621  }
622  }
623 
624  return $result ?: false;
625  }
626 
631  public function strencode( $s ) {
632  return $this->mysqlRealEscapeString( $s );
633  }
634 
639  abstract protected function mysqlRealEscapeString( $s );
640 
647  public function addIdentifierQuotes( $s ) {
648  // Characters in the range \u0001-\uFFFF are valid in a quoted identifier
649  // Remove NUL bytes and escape backticks by doubling
650  return '`' . str_replace( [ "\0", '`' ], [ '', '``' ], $s ) . '`';
651  }
652 
657  public function isQuotedIdentifier( $name ) {
658  return strlen( $name ) && $name[0] == '`' && substr( $name, -1, 1 ) == '`';
659  }
660 
661  protected function doGetLag() {
662  if ( $this->getLagDetectionMethod() === 'pt-heartbeat' ) {
663  return $this->getLagFromPtHeartbeat();
664  } else {
665  return $this->getLagFromSlaveStatus();
666  }
667  }
668 
672  protected function getLagDetectionMethod() {
674  }
675 
679  protected function getLagFromSlaveStatus() {
680  $flags = self::QUERY_SILENCE_ERRORS | self::QUERY_IGNORE_DBO_TRX;
681  $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__, $flags );
682  $row = $res ? $res->fetchObject() : false;
683  // If the server is not replicating, there will be no row
684  if ( $row && strval( $row->Seconds_Behind_Master ) !== '' ) {
685  return intval( $row->Seconds_Behind_Master );
686  }
687 
688  return false;
689  }
690 
694  protected function getLagFromPtHeartbeat() {
695  $options = $this->lagDetectionOptions;
696 
697  $currentTrxInfo = $this->getRecordedTransactionLagStatus();
698  if ( $currentTrxInfo ) {
699  // There is an active transaction and the initial lag was already queried
700  $staleness = microtime( true ) - $currentTrxInfo['since'];
701  if ( $staleness > self::LAG_STALE_WARN_THRESHOLD ) {
702  // Avoid returning higher and higher lag value due to snapshot age
703  // given that the isolation level will typically be REPEATABLE-READ
704  $this->queryLogger->warning(
705  "Using cached lag value for {db_server} due to active transaction",
706  $this->getLogContext( [
707  'method' => __METHOD__,
708  'age' => $staleness,
709  'exception' => new RuntimeException()
710  ] )
711  );
712  }
713 
714  return $currentTrxInfo['lag'];
715  }
716 
717  if ( isset( $options['conds'] ) ) {
718  // Best method for multi-DC setups: use logical channel names
719  $data = $this->getHeartbeatData( $options['conds'] );
720  } else {
721  // Standard method: use master server ID (works with stock pt-heartbeat)
722  $masterInfo = $this->getMasterServerInfo();
723  if ( !$masterInfo ) {
724  $this->queryLogger->error(
725  "Unable to query master of {db_server} for server ID",
726  $this->getLogContext( [
727  'method' => __METHOD__
728  ] )
729  );
730 
731  return false; // could not get master server ID
732  }
733 
734  $conds = [ 'server_id' => intval( $masterInfo['serverId'] ) ];
735  $data = $this->getHeartbeatData( $conds );
736  }
737 
738  list( $time, $nowUnix ) = $data;
739  if ( $time !== null ) {
740  // @time is in ISO format like "2015-09-25T16:48:10.000510"
741  $dateTime = new DateTime( $time, new DateTimeZone( 'UTC' ) );
742  $timeUnix = (int)$dateTime->format( 'U' ) + $dateTime->format( 'u' ) / 1e6;
743 
744  return max( $nowUnix - $timeUnix, 0.0 );
745  }
746 
747  $this->queryLogger->error(
748  "Unable to find pt-heartbeat row for {db_server}",
749  $this->getLogContext( [
750  'method' => __METHOD__
751  ] )
752  );
753 
754  return false;
755  }
756 
757  protected function getMasterServerInfo() {
759  $key = $cache->makeGlobalKey(
760  'mysql',
761  'master-info',
762  // Using one key for all cluster replica DBs is preferable
763  $this->topologyRootMaster ?? $this->getServer()
764  );
765  $fname = __METHOD__;
766 
767  return $cache->getWithSetCallback(
768  $key,
769  $cache::TTL_INDEFINITE,
770  function () use ( $cache, $key, $fname ) {
771  // Get and leave a lock key in place for a short period
772  if ( !$cache->lock( $key, 0, 10 ) ) {
773  return false; // avoid master connection spike slams
774  }
775 
776  $conn = $this->getLazyMasterHandle();
777  if ( !$conn ) {
778  return false; // something is misconfigured
779  }
780 
781  // Connect to and query the master; catch errors to avoid outages
782  try {
783  $flags = self::QUERY_SILENCE_ERRORS | self::QUERY_IGNORE_DBO_TRX;
784  $res = $conn->query( 'SELECT @@server_id AS id', $fname, $flags );
785  $row = $res ? $res->fetchObject() : false;
786  $id = $row ? (int)$row->id : 0;
787  } catch ( DBError $e ) {
788  $id = 0;
789  }
790 
791  // Cache the ID if it was retrieved
792  return $id ? [ 'serverId' => $id, 'asOf' => time() ] : false;
793  }
794  );
795  }
796 
802  protected function getHeartbeatData( array $conds ) {
803  // Query time and trip time are not counted
804  $nowUnix = microtime( true );
805  $whereSQL = $this->makeList( $conds, self::LIST_AND );
806  // Use ORDER BY for channel based queries since that field might not be UNIQUE.
807  // Note: this would use "TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6))" but the
808  // percision field is not supported in MySQL <= 5.5.
809  $res = $this->query(
810  "SELECT ts FROM heartbeat.heartbeat WHERE $whereSQL ORDER BY ts DESC LIMIT 1",
811  __METHOD__,
812  self::QUERY_SILENCE_ERRORS | self::QUERY_IGNORE_DBO_TRX
813  );
814  $row = $res ? $res->fetchObject() : false;
815 
816  return [ $row ? $row->ts : null, $nowUnix ];
817  }
818 
819  protected function getApproximateLagStatus() {
820  if ( $this->getLagDetectionMethod() === 'pt-heartbeat' ) {
821  // Disable caching since this is fast enough and we don't wan't
822  // to be *too* pessimistic by having both the cache TTL and the
823  // pt-heartbeat interval count as lag in getSessionLagStatus()
824  return parent::getApproximateLagStatus();
825  }
826 
827  $key = $this->srvCache->makeGlobalKey( 'mysql-lag', $this->getServer() );
828  $approxLag = $this->srvCache->get( $key );
829  if ( !$approxLag ) {
830  $approxLag = parent::getApproximateLagStatus();
831  $this->srvCache->set( $key, $approxLag, 1 );
832  }
833 
834  return $approxLag;
835  }
836 
837  public function masterPosWait( DBMasterPos $pos, $timeout ) {
838  if ( !( $pos instanceof MySQLMasterPos ) ) {
839  throw new InvalidArgumentException( "Position not an instance of MySQLMasterPos" );
840  }
841 
842  if ( $this->topologyRole === self::ROLE_STATIC_CLONE ) {
843  $this->queryLogger->debug(
844  "Bypassed replication wait; database has a static dataset",
845  $this->getLogContext( [ 'method' => __METHOD__, 'raw_pos' => $pos ] )
846  );
847 
848  return 0; // this is a copy of a read-only dataset with no master DB
849  } elseif ( $this->lastKnownReplicaPos && $this->lastKnownReplicaPos->hasReached( $pos ) ) {
850  $this->queryLogger->debug(
851  "Bypassed replication wait; replication known to have reached {raw_pos}",
852  $this->getLogContext( [ 'method' => __METHOD__, 'raw_pos' => $pos ] )
853  );
854 
855  return 0; // already reached this point for sure
856  }
857 
858  // Call doQuery() directly, to avoid opening a transaction if DBO_TRX is set
859  if ( $pos->getGTIDs() ) {
860  // Get the GTIDs from this replica server too see the domains (channels)
861  $refPos = $this->getReplicaPos();
862  if ( !$refPos ) {
863  $this->queryLogger->error(
864  "Could not get replication position on replica DB to compare to {raw_pos}",
865  $this->getLogContext( [ 'method' => __METHOD__, 'raw_pos' => $pos ] )
866  );
867 
868  return -1; // this is the master itself?
869  }
870  // GTIDs with domains (channels) that are active and are present on the replica
871  $gtidsWait = $pos::getRelevantActiveGTIDs( $pos, $refPos );
872  if ( !$gtidsWait ) {
873  $this->queryLogger->error(
874  "No active GTIDs in {raw_pos} share a domain with those in {current_pos}",
875  $this->getLogContext( [
876  'method' => __METHOD__,
877  'raw_pos' => $pos,
878  'current_pos' => $refPos
879  ] )
880  );
881 
882  return -1; // $pos is from the wrong cluster?
883  }
884  // Wait on the GTID set
885  $gtidArg = $this->addQuotes( implode( ',', $gtidsWait ) );
886  if ( strpos( $gtidArg, ':' ) !== false ) {
887  // MySQL GTIDs, e.g "source_id:transaction_id"
888  $sql = "SELECT WAIT_FOR_EXECUTED_GTID_SET($gtidArg, $timeout)";
889  } else {
890  // MariaDB GTIDs, e.g."domain:server:sequence"
891  $sql = "SELECT MASTER_GTID_WAIT($gtidArg, $timeout)";
892  }
893  $waitPos = implode( ',', $gtidsWait );
894  } else {
895  // Wait on the binlog coordinates
896  $encFile = $this->addQuotes( $pos->getLogFile() );
897  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
898  $encPos = intval( $pos->getLogPosition()[$pos::CORD_EVENT] );
899  $sql = "SELECT MASTER_POS_WAIT($encFile, $encPos, $timeout)";
900  $waitPos = $pos->__toString();
901  }
902 
903  $res = $this->query( $sql, __METHOD__, self::QUERY_IGNORE_DBO_TRX );
904  $row = $this->fetchRow( $res );
905 
906  // Result can be NULL (error), -1 (timeout), or 0+ per the MySQL manual
907  $status = ( $row[0] !== null ) ? intval( $row[0] ) : null;
908  if ( $status === null ) {
909  $this->queryLogger->error(
910  "An error occurred while waiting for replication to reach {raw_pos}",
911  $this->getLogContext( [
912  'method' => __METHOD__,
913  'raw_pos' => $pos,
914  'wait_pos' => $waitPos,
915  'sql' => $sql
916  ] )
917  );
918  } elseif ( $status < 0 ) {
919  $this->queryLogger->error(
920  "Timed out waiting for replication to reach {raw_pos}",
921  $this->getLogContext( [
922  'method' => __METHOD__,
923  'raw_pos' => $pos,
924  'wait_pos' => $waitPos,
925  'timeout' => $timeout,
926  'sql' => $sql,
927 
928  ] )
929  );
930  } elseif ( $status >= 0 ) {
931  $this->queryLogger->debug(
932  "Replication has reached {raw_pos}",
933  $this->getLogContext( [
934  'method' => __METHOD__,
935  'raw_pos' => $pos,
936  'wait_pos' => $waitPos
937  ] )
938  );
939  // Remember that this position was reached to save queries next time
940  $this->lastKnownReplicaPos = $pos;
941  }
942 
943  return $status;
944  }
945 
951  public function getReplicaPos() {
952  $now = microtime( true ); // as-of-time *before* fetching GTID variables
953 
954  if ( $this->useGTIDs() ) {
955  // Try to use GTIDs, fallbacking to binlog positions if not possible
956  $data = $this->getServerGTIDs( __METHOD__ );
957  // Use gtid_slave_pos for MariaDB and gtid_executed for MySQL
958  foreach ( [ 'gtid_slave_pos', 'gtid_executed' ] as $name ) {
959  if ( isset( $data[$name] ) && strlen( $data[$name] ) ) {
960  return new MySQLMasterPos( $data[$name], $now );
961  }
962  }
963  }
964 
965  $data = $this->getServerRoleStatus( 'SLAVE', __METHOD__ );
966  if ( $data && strlen( $data['Relay_Master_Log_File'] ) ) {
967  return new MySQLMasterPos(
968  "{$data['Relay_Master_Log_File']}/{$data['Exec_Master_Log_Pos']}",
969  $now
970  );
971  }
972 
973  return false;
974  }
975 
981  public function getMasterPos() {
982  $now = microtime( true ); // as-of-time *before* fetching GTID variables
983 
984  $pos = false;
985  if ( $this->useGTIDs() ) {
986  // Try to use GTIDs, fallbacking to binlog positions if not possible
987  $data = $this->getServerGTIDs( __METHOD__ );
988  // Use gtid_binlog_pos for MariaDB and gtid_executed for MySQL
989  foreach ( [ 'gtid_binlog_pos', 'gtid_executed' ] as $name ) {
990  if ( isset( $data[$name] ) && strlen( $data[$name] ) ) {
991  $pos = new MySQLMasterPos( $data[$name], $now );
992  break;
993  }
994  }
995  // Filter domains that are inactive or not relevant to the session
996  if ( $pos ) {
997  $pos->setActiveOriginServerId( $this->getServerId() );
998  $pos->setActiveOriginServerUUID( $this->getServerUUID() );
999  if ( isset( $data['gtid_domain_id'] ) ) {
1000  $pos->setActiveDomain( $data['gtid_domain_id'] );
1001  }
1002  }
1003  }
1004 
1005  if ( !$pos ) {
1006  $data = $this->getServerRoleStatus( 'MASTER', __METHOD__ );
1007  if ( $data && strlen( $data['File'] ) ) {
1008  $pos = new MySQLMasterPos( "{$data['File']}/{$data['Position']}", $now );
1009  }
1010  }
1011 
1012  return $pos;
1013  }
1014 
1019  protected function getServerId() {
1020  $fname = __METHOD__;
1021  return $this->srvCache->getWithSetCallback(
1022  $this->srvCache->makeGlobalKey( 'mysql-server-id', $this->getServer() ),
1023  self::SERVER_ID_CACHE_TTL,
1024  function () use ( $fname ) {
1025  $flags = self::QUERY_IGNORE_DBO_TRX;
1026  $res = $this->query( "SELECT @@server_id AS id", $fname, $flags );
1027 
1028  return intval( $this->fetchObject( $res )->id );
1029  }
1030  );
1031  }
1032 
1036  protected function getServerUUID() {
1037  $fname = __METHOD__;
1038  return $this->srvCache->getWithSetCallback(
1039  $this->srvCache->makeGlobalKey( 'mysql-server-uuid', $this->getServer() ),
1040  self::SERVER_ID_CACHE_TTL,
1041  function () use ( $fname ) {
1042  $flags = self::QUERY_IGNORE_DBO_TRX;
1043  $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'server_uuid'", $fname, $flags );
1044  $row = $this->fetchObject( $res );
1045 
1046  return $row ? $row->Value : null;
1047  }
1048  );
1049  }
1050 
1055  protected function getServerGTIDs( $fname = __METHOD__ ) {
1056  $map = [];
1057 
1058  $flags = self::QUERY_IGNORE_DBO_TRX;
1059  // Get global-only variables like gtid_executed
1060  $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'gtid_%'", $fname, $flags );
1061  foreach ( $res as $row ) {
1062  $map[$row->Variable_name] = $row->Value;
1063  }
1064  // Get session-specific (e.g. gtid_domain_id since that is were writes will log)
1065  $res = $this->query( "SHOW SESSION VARIABLES LIKE 'gtid_%'", $fname, $flags );
1066  foreach ( $res as $row ) {
1067  $map[$row->Variable_name] = $row->Value;
1068  }
1069 
1070  return $map;
1071  }
1072 
1078  protected function getServerRoleStatus( $role, $fname = __METHOD__ ) {
1079  $flags = self::QUERY_IGNORE_DBO_TRX;
1080 
1081  return $this->query( "SHOW $role STATUS", $fname, $flags )->fetchRow() ?: [];
1082  }
1083 
1084  public function serverIsReadOnly() {
1085  // Avoid SHOW to avoid internal temporary tables
1086  $flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_SILENCE_ERRORS;
1087  $res = $this->query( "SELECT @@GLOBAL.read_only AS Value", __METHOD__, $flags );
1088  $row = $this->fetchObject( $res );
1089 
1090  return $row ? (bool)$row->Value : false;
1091  }
1092 
1097  function useIndexClause( $index ) {
1098  return "FORCE INDEX (" . $this->indexName( $index ) . ")";
1099  }
1100 
1105  function ignoreIndexClause( $index ) {
1106  return "IGNORE INDEX (" . $this->indexName( $index ) . ")";
1107  }
1108 
1112  public function getSoftwareLink() {
1113  // MariaDB includes its name in its version string; this is how MariaDB's version of
1114  // the mysql command-line client identifies MariaDB servers (see mariadb_connection()
1115  // in libmysql/libmysql.c).
1116  $version = $this->getServerVersion();
1117  if ( strpos( $version, 'MariaDB' ) !== false || strpos( $version, '-maria-' ) !== false ) {
1118  return '[{{int:version-db-mariadb-url}} MariaDB]';
1119  }
1120 
1121  // Percona Server's version suffix is not very distinctive, and @@version_comment
1122  // doesn't give the necessary info for source builds, so assume the server is MySQL.
1123  // (Even Percona's version of mysql doesn't try to make the distinction.)
1124  return '[{{int:version-db-mysql-url}} MySQL]';
1125  }
1126 
1130  public function getServerVersion() {
1132  $fname = __METHOD__;
1133 
1134  return $cache->getWithSetCallback(
1135  $cache->makeGlobalKey( 'mysql-server-version', $this->getServer() ),
1136  $cache::TTL_HOUR,
1137  function () use ( $fname ) {
1138  // Not using mysql_get_server_info() or similar for consistency: in the handshake,
1139  // MariaDB 10 adds the prefix "5.5.5-", and only some newer client libraries strip
1140  // it off (see RPL_VERSION_HACK in include/mysql_com.h).
1141  return $this->selectField( '', 'VERSION()', '', $fname );
1142  }
1143  );
1144  }
1145 
1149  public function setSessionOptions( array $options ) {
1150  if ( isset( $options['connTimeout'] ) ) {
1151  $flags = self::QUERY_IGNORE_DBO_TRX;
1152  $timeout = (int)$options['connTimeout'];
1153  $this->query( "SET net_read_timeout=$timeout", __METHOD__, $flags );
1154  $this->query( "SET net_write_timeout=$timeout", __METHOD__, $flags );
1155  }
1156  }
1157 
1163  public function streamStatementEnd( &$sql, &$newLine ) {
1164  if ( preg_match( '/^DELIMITER\s+(\S+)/i', $newLine, $m ) ) {
1165  $this->delimiter = $m[1];
1166  $newLine = '';
1167  }
1168 
1169  return parent::streamStatementEnd( $sql, $newLine );
1170  }
1171 
1180  public function lockIsFree( $lockName, $method ) {
1181  if ( !parent::lockIsFree( $lockName, $method ) ) {
1182  return false; // already held
1183  }
1184 
1185  $encName = $this->addQuotes( $this->makeLockName( $lockName ) );
1186 
1187  $flags = self::QUERY_IGNORE_DBO_TRX;
1188  $res = $this->query( "SELECT IS_FREE_LOCK($encName) AS lockstatus", $method, $flags );
1189  $row = $this->fetchObject( $res );
1190 
1191  return ( $row->lockstatus == 1 );
1192  }
1193 
1200  public function lock( $lockName, $method, $timeout = 5 ) {
1201  $encName = $this->addQuotes( $this->makeLockName( $lockName ) );
1202 
1203  $flags = self::QUERY_IGNORE_DBO_TRX;
1204  $res = $this->query( "SELECT GET_LOCK($encName, $timeout) AS lockstatus", $method, $flags );
1205  $row = $this->fetchObject( $res );
1206 
1207  if ( $row->lockstatus == 1 ) {
1208  parent::lock( $lockName, $method, $timeout ); // record
1209  return true;
1210  }
1211 
1212  $this->queryLogger->info( __METHOD__ . " failed to acquire lock '{lockname}'",
1213  [ 'lockname' => $lockName ] );
1214 
1215  return false;
1216  }
1217 
1225  public function unlock( $lockName, $method ) {
1226  $encName = $this->addQuotes( $this->makeLockName( $lockName ) );
1227 
1228  $flags = self::QUERY_IGNORE_DBO_TRX;
1229  $res = $this->query( "SELECT RELEASE_LOCK($encName) as lockstatus", $method, $flags );
1230  $row = $this->fetchObject( $res );
1231 
1232  if ( $row->lockstatus == 1 ) {
1233  parent::unlock( $lockName, $method ); // record
1234  return true;
1235  }
1236 
1237  $this->queryLogger->warning( __METHOD__ . " failed to release lock '$lockName'\n" );
1238 
1239  return false;
1240  }
1241 
1242  private function makeLockName( $lockName ) {
1243  // https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
1244  // Newer version enforce a 64 char length limit.
1245  return ( strlen( $lockName ) > 64 ) ? sha1( $lockName ) : $lockName;
1246  }
1247 
1248  public function namedLocksEnqueue() {
1249  return true;
1250  }
1251 
1253  return false; // tied to TCP connection
1254  }
1255 
1256  protected function doLockTables( array $read, array $write, $method ) {
1257  $items = [];
1258  foreach ( $write as $table ) {
1259  $items[] = $this->tableName( $table ) . ' WRITE';
1260  }
1261  foreach ( $read as $table ) {
1262  $items[] = $this->tableName( $table ) . ' READ';
1263  }
1264 
1265  $sql = "LOCK TABLES " . implode( ',', $items );
1266  $this->query( $sql, $method, self::QUERY_IGNORE_DBO_TRX );
1267 
1268  return true;
1269  }
1270 
1271  protected function doUnlockTables( $method ) {
1272  $this->query( "UNLOCK TABLES", $method, self::QUERY_IGNORE_DBO_TRX );
1273 
1274  return true;
1275  }
1276 
1280  public function setBigSelects( $value = true ) {
1281  if ( $value === 'default' ) {
1282  if ( $this->defaultBigSelects === null ) {
1283  # Function hasn't been called before so it must already be set to the default
1284  return;
1285  } else {
1286  $value = $this->defaultBigSelects;
1287  }
1288  } elseif ( $this->defaultBigSelects === null ) {
1289  $this->defaultBigSelects =
1290  (bool)$this->selectField( false, '@@sql_big_selects', '', __METHOD__ );
1291  }
1292  $encValue = $value ? '1' : '0';
1293  $this->query( "SET sql_big_selects=$encValue", __METHOD__, self::QUERY_IGNORE_DBO_TRX );
1294  }
1295 
1306  public function deleteJoin(
1307  $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
1308  ) {
1309  if ( !$conds ) {
1310  throw new DBUnexpectedError( $this, __METHOD__ . ' called with empty $conds' );
1311  }
1312 
1313  $delTable = $this->tableName( $delTable );
1314  $joinTable = $this->tableName( $joinTable );
1315  $sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
1316 
1317  if ( $conds != '*' ) {
1318  $sql .= ' AND ' . $this->makeList( $conds, self::LIST_AND );
1319  }
1320 
1321  $this->query( $sql, $fname );
1322  }
1323 
1324  public function upsert(
1325  $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__
1326  ) {
1327  if ( $rows === [] ) {
1328  return true; // nothing to do
1329  }
1330 
1331  if ( !is_array( reset( $rows ) ) ) {
1332  $rows = [ $rows ];
1333  }
1334 
1335  $table = $this->tableName( $table );
1336  $columns = array_keys( $rows[0] );
1337 
1338  $sql = "INSERT INTO $table (" . implode( ',', $columns ) . ') VALUES ';
1339  $rowTuples = [];
1340  foreach ( $rows as $row ) {
1341  $rowTuples[] = '(' . $this->makeList( $row ) . ')';
1342  }
1343  $sql .= implode( ',', $rowTuples );
1344  $sql .= " ON DUPLICATE KEY UPDATE " . $this->makeList( $set, self::LIST_SET );
1345 
1346  $this->query( $sql, $fname );
1347 
1348  return true;
1349  }
1350 
1356  public function getServerUptime() {
1357  $vars = $this->getMysqlStatus( 'Uptime' );
1358 
1359  return (int)$vars['Uptime'];
1360  }
1361 
1367  public function wasDeadlock() {
1368  return $this->lastErrno() == 1213;
1369  }
1370 
1376  public function wasLockTimeout() {
1377  return $this->lastErrno() == 1205;
1378  }
1379 
1385  public function wasReadOnlyError() {
1386  return $this->lastErrno() == 1223 ||
1387  ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
1388  }
1389 
1390  public function wasConnectionError( $errno ) {
1391  return $errno == 2013 || $errno == 2006;
1392  }
1393 
1394  protected function wasKnownStatementRollbackError() {
1395  $errno = $this->lastErrno();
1396 
1397  if ( $errno === 1205 ) { // lock wait timeout
1398  // Note that this is uncached to avoid stale values of SET is used
1399  $row = $this->selectRow(
1400  false,
1401  [ 'innodb_rollback_on_timeout' => '@@innodb_rollback_on_timeout' ],
1402  [],
1403  __METHOD__
1404  );
1405  // https://dev.mysql.com/doc/refman/5.7/en/innodb-error-handling.html
1406  // https://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html
1407  return $row->innodb_rollback_on_timeout ? false : true;
1408  }
1409 
1410  // See https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
1411  return in_array( $errno, [ 1022, 1062, 1216, 1217, 1137, 1146, 1051, 1054 ], true );
1412  }
1413 
1421  public function duplicateTableStructure(
1422  $oldName, $newName, $temporary = false, $fname = __METHOD__
1423  ) {
1424  $tmp = $temporary ? 'TEMPORARY ' : '';
1425  $newName = $this->addIdentifierQuotes( $newName );
1426  $oldName = $this->addIdentifierQuotes( $oldName );
1427  $query = "CREATE $tmp TABLE $newName (LIKE $oldName)";
1428 
1429  return $this->query( $query, $fname, $this::QUERY_PSEUDO_PERMANENT );
1430  }
1431 
1439  public function listTables( $prefix = null, $fname = __METHOD__ ) {
1440  $result = $this->query( "SHOW TABLES", $fname );
1441 
1442  $endArray = [];
1443 
1444  foreach ( $result as $table ) {
1445  $vars = get_object_vars( $table );
1446  $table = array_pop( $vars );
1447 
1448  if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
1449  $endArray[] = $table;
1450  }
1451  }
1452 
1453  return $endArray;
1454  }
1455 
1461  public function dropTable( $tableName, $fName = __METHOD__ ) {
1462  if ( !$this->tableExists( $tableName, $fName ) ) {
1463  return false;
1464  }
1465 
1466  return $this->query( "DROP TABLE IF EXISTS " . $this->tableName( $tableName ), $fName );
1467  }
1468 
1475  private function getMysqlStatus( $which = "%" ) {
1476  $flags = self::QUERY_IGNORE_DBO_TRX;
1477  $res = $this->query( "SHOW STATUS LIKE '{$which}'", __METHOD__, $flags );
1478  $status = [];
1479 
1480  foreach ( $res as $row ) {
1481  $status[$row->Variable_name] = $row->Value;
1482  }
1483 
1484  return $status;
1485  }
1486 
1496  public function listViews( $prefix = null, $fname = __METHOD__ ) {
1497  // The name of the column containing the name of the VIEW
1498  $propertyName = 'Tables_in_' . $this->getDBname();
1499 
1500  // Query for the VIEWS
1501  $res = $this->query( 'SHOW FULL TABLES WHERE TABLE_TYPE = "VIEW"' );
1502  $allViews = [];
1503  foreach ( $res as $row ) {
1504  array_push( $allViews, $row->$propertyName );
1505  }
1506 
1507  if ( $prefix === null || $prefix === '' ) {
1508  return $allViews;
1509  }
1510 
1511  $filteredViews = [];
1512  foreach ( $allViews as $viewName ) {
1513  // Does the name of this VIEW start with the table-prefix?
1514  if ( strpos( $viewName, $prefix ) === 0 ) {
1515  array_push( $filteredViews, $viewName );
1516  }
1517  }
1518 
1519  return $filteredViews;
1520  }
1521 
1530  public function isView( $name, $prefix = null ) {
1531  return in_array( $name, $this->listViews( $prefix ) );
1532  }
1533 
1534  protected function isTransactableQuery( $sql ) {
1535  return parent::isTransactableQuery( $sql ) &&
1536  !preg_match( '/^SELECT\s+(GET|RELEASE|IS_FREE)_LOCK\(/', $sql );
1537  }
1538 
1539  public function buildStringCast( $field ) {
1540  return "CAST( $field AS BINARY )";
1541  }
1542 
1547  public function buildIntegerCast( $field ) {
1548  return 'CAST( ' . $field . ' AS SIGNED )';
1549  }
1550 
1551  /*
1552  * @return bool Whether GTID support is used (mockable for testing)
1553  */
1554  protected function useGTIDs() {
1555  return $this->useGTIDs;
1556  }
1557 }
1558 
1562 class_alias( DatabaseMysqlBase::class, 'DatabaseMysqlBase' );
Wikimedia\Rdbms\DatabaseMysqlBase\doLockTables
doLockTables(array $read, array $write, $method)
Helper function for lockTables() that handles the actual table locking.
Definition: DatabaseMysqlBase.php:1256
Wikimedia\Rdbms\DatabaseMysqlBase\estimateRowCount
estimateRowCount( $table, $var=' *', $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Estimate rows in dataset Returns estimated count, based on EXPLAIN output Takes same arguments as Dat...
Definition: DatabaseMysqlBase.php:510
Wikimedia\Rdbms\DatabaseMysqlBase\addIdentifierQuotes
addIdentifierQuotes( $s)
MySQL uses backticks for identifier quoting instead of the sql standard "double quotes".
Definition: DatabaseMysqlBase.php:647
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:49
Wikimedia\Rdbms\DatabaseMysqlBase\masterPosWait
masterPosWait(DBMasterPos $pos, $timeout)
Wait for the replica DB to catch up to a given master position.
Definition: DatabaseMysqlBase.php:837
Wikimedia\Rdbms\DatabaseMysqlBase\wasQueryTimeout
wasQueryTimeout( $error, $errno)
Checks whether the cause of the error is detected to be a timeout.
Definition: DatabaseMysqlBase.php:445
Wikimedia\Rdbms\DatabaseMysqlBase\$sslKeyPath
string null $sslKeyPath
Definition: DatabaseMysqlBase.php:51
Wikimedia\Rdbms\DatabaseMysqlBase\getLagDetectionMethod
getLagDetectionMethod()
Definition: DatabaseMysqlBase.php:672
Wikimedia\Rdbms\DatabaseMysqlBase\wasKnownStatementRollbackError
wasKnownStatementRollbackError()
Definition: DatabaseMysqlBase.php:1394
Wikimedia\Rdbms\DatabaseMysqlBase\getServerId
getServerId()
Definition: DatabaseMysqlBase.php:1019
Wikimedia\Rdbms\DatabaseMysqlBase\indexInfo
indexInfo( $table, $index, $fname=__METHOD__)
Get information about an index into an object Returns false if the index does not exist.
Definition: DatabaseMysqlBase.php:602
Wikimedia\Rdbms\DatabaseMysqlBase\setSessionOptions
setSessionOptions(array $options)
Definition: DatabaseMysqlBase.php:1149
Wikimedia\Rdbms\DatabaseMysqlBase\getServerUptime
getServerUptime()
Determines how long the server has been up.
Definition: DatabaseMysqlBase.php:1356
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlRealEscapeString
mysqlRealEscapeString( $s)
Wikimedia\Rdbms\DatabaseMysqlBase\$sslCAPath
string null $sslCAPath
Definition: DatabaseMysqlBase.php:57
Wikimedia\Rdbms\DatabaseMysqlBase\getApproximateLagStatus
getApproximateLagStatus()
Get a replica DB lag estimate for this server at the start of a transaction.
Definition: DatabaseMysqlBase.php:819
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlFieldName
mysqlFieldName( $res, $n)
Get the name of the specified field in a result.
Wikimedia\Rdbms\DatabaseMysqlBase\numRows
numRows( $res)
Definition: DatabaseMysqlBase.php:319
Wikimedia\Rdbms\Database\$password
string $password
Password used to establish the current connection.
Definition: Database.php:79
Wikimedia\Rdbms\Database\nativeReplace
nativeReplace( $table, $rows, $fname)
REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE statement.
Definition: Database.php:2926
Wikimedia\Rdbms\DatabaseMysqlBase\getServerUUID
getServerUUID()
Definition: DatabaseMysqlBase.php:1036
Wikimedia\Rdbms\DatabaseMysqlBase\wasLockTimeout
wasLockTimeout()
Determines if the last failure was due to a lock timeout.
Definition: DatabaseMysqlBase.php:1376
Wikimedia\Rdbms\Database\indexName
indexName( $index)
Allows for index remapping in queries where this is not consistent across DBMS.
Definition: Database.php:2751
Wikimedia\Rdbms\Database\executeQuery
executeQuery( $sql, $fname, $flags)
Execute a query, retrying it if there is a recoverable connection loss.
Definition: Database.php:1192
Wikimedia\Rdbms\DatabaseDomain\getTablePrefix
getTablePrefix()
Definition: DatabaseDomain.php:189
Wikimedia\Rdbms\DatabaseMysqlBase\$sslCertPath
string null $sslCertPath
Definition: DatabaseMysqlBase.php:53
Wikimedia\Rdbms\DatabaseMysqlBase\$useGTIDs
bool $useGTIDs
bool Whether to use GTID methods
Definition: DatabaseMysqlBase.php:49
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlNumRows
mysqlNumRows( $res)
Get number of rows in result.
Wikimedia\Rdbms\DatabaseMysqlBase\getHeartbeatData
getHeartbeatData(array $conds)
Definition: DatabaseMysqlBase.php:802
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\Database\normalizeConditions
normalizeConditions( $conds, $fname)
Definition: Database.php:2026
Wikimedia\Rdbms\DBMasterPos
An object representing a master or replica DB position in a replicated setup.
Definition: DBMasterPos.php:12
Wikimedia\Rdbms\DatabaseMysqlBase\doGetLag
doGetLag()
Definition: DatabaseMysqlBase.php:661
$s
$s
Definition: mergeMessageFileList.php:185
Wikimedia\Rdbms\Database\extractSingleFieldFromList
extractSingleFieldFromList( $var)
Definition: Database.php:2049
Wikimedia\Rdbms\DatabaseMysqlBase\ignoreIndexClause
ignoreIndexClause( $index)
Definition: DatabaseMysqlBase.php:1105
Wikimedia\Rdbms\DatabaseDomain\getDatabase
getDatabase()
Definition: DatabaseDomain.php:175
$res
$res
Definition: testCompression.php:54
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlFreeResult
mysqlFreeResult( $res)
Free result memory.
Wikimedia\Rdbms\DatabaseMysqlBase\fieldInfo
fieldInfo( $table, $field)
Definition: DatabaseMysqlBase.php:566
Wikimedia\Rdbms\DBError
Database error base class.
Definition: DBError.php:30
Wikimedia\Rdbms\DatabaseMysqlBase\setBigSelects
setBigSelects( $value=true)
Definition: DatabaseMysqlBase.php:1280
Wikimedia\Rdbms\DatabaseMysqlBase\__construct
__construct(array $params)
Additional $params include:
Definition: DatabaseMysqlBase.php:97
Wikimedia\Rdbms\DatabaseMysqlBase\buildIntegerCast
buildIntegerCast( $field)
Definition: DatabaseMysqlBase.php:1547
Wikimedia\Rdbms\DatabaseMysqlBase\wasDeadlock
wasDeadlock()
Determines if the last failure was due to a deadlock.
Definition: DatabaseMysqlBase.php:1367
LIST_AND
const LIST_AND
Definition: Defines.php:39
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlNumFields
mysqlNumFields( $res)
Get number of fields in result.
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlFetchObject
mysqlFetchObject( $res)
Fetch a result row as an object.
Wikimedia\Rdbms\Database\close
close( $fname=__METHOD__, $owner=null)
Close the database connection.
Definition: Database.php:892
Wikimedia\Rdbms\DatabaseMysqlBase\doUnlockTables
doUnlockTables( $method)
Helper function for unlockTables() that handles the actual table unlocking.
Definition: DatabaseMysqlBase.php:1271
Wikimedia\Rdbms\DatabaseMysqlBase\upsert
upsert( $table, array $rows, $uniqueIndexes, array $set, $fname=__METHOD__)
INSERT ON DUPLICATE KEY UPDATE wrapper, upserts an array into a table.
Definition: DatabaseMysqlBase.php:1324
Wikimedia\Rdbms\DatabaseMysqlBase\SERVER_ID_CACHE_TTL
const SERVER_ID_CACHE_TTL
Definition: DatabaseMysqlBase.php:73
Wikimedia\Rdbms\DatabaseMysqlBase\buildStringCast
buildStringCast( $field)
Definition: DatabaseMysqlBase.php:1539
Wikimedia\Rdbms\DatabaseMysqlBase\duplicateTableStructure
duplicateTableStructure( $oldName, $newName, $temporary=false, $fname=__METHOD__)
Definition: DatabaseMysqlBase.php:1421
Wikimedia\Rdbms\DatabaseMysqlBase\fieldName
fieldName( $res, $n)
Definition: DatabaseMysqlBase.php:365
Wikimedia\Rdbms\Database\escapeLikeInternal
escapeLikeInternal( $s, $escapeChar='`')
Definition: Database.php:2792
Wikimedia\Rdbms\DatabaseMysqlBase\getLagFromPtHeartbeat
getLagFromPtHeartbeat()
Definition: DatabaseMysqlBase.php:694
Wikimedia\Rdbms\Database\reportQueryError
reportQueryError( $error, $errno, $sql, $fname, $ignore=false)
Report a query error.
Definition: Database.php:1583
LIST_SET
const LIST_SET
Definition: Defines.php:40
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlConnect
mysqlConnect( $realServer, $dbName)
Open a connection to a MySQL server.
Wikimedia\Rdbms\DatabaseMysqlBase\getSoftwareLink
getSoftwareLink()
Definition: DatabaseMysqlBase.php:1112
Wikimedia\Rdbms\MySQLMasterPos
DBMasterPos class for MySQL/MariaDB.
Definition: MySQLMasterPos.php:19
Wikimedia\Rdbms\DatabaseMysqlBase\numFields
numFields( $res)
Definition: DatabaseMysqlBase.php:348
Wikimedia\Rdbms\DatabaseMysqlBase\listTables
listTables( $prefix=null, $fname=__METHOD__)
List all tables on the database.
Definition: DatabaseMysqlBase.php:1439
Wikimedia\Rdbms\DatabaseMysqlBase\lastError
lastError()
Definition: DatabaseMysqlBase.php:418
Wikimedia\Rdbms\Database\$user
string $user
User that this instance is currently connected under the name of.
Definition: Database.php:77
Wikimedia\Rdbms\DatabaseMysqlBase\getType
getType()
Definition: DatabaseMysqlBase.php:118
Wikimedia\Rdbms\DatabaseMysqlBase\fieldType
fieldType( $res, $n)
mysql_field_type() wrapper
Definition: DatabaseMysqlBase.php:384
Wikimedia\Rdbms\DatabaseMysqlBase\$sslCAFile
string null $sslCAFile
Definition: DatabaseMysqlBase.php:55
Wikimedia\Rdbms\DatabaseMysqlBase\fetchObject
fetchObject( $res)
Definition: DatabaseMysqlBase.php:253
Wikimedia\Rdbms\MySQLField
Definition: MySQLField.php:5
Wikimedia\Rdbms\DatabaseMysqlBase\getReplicationSafetyInfo
getReplicationSafetyInfo()
Definition: DatabaseMysqlBase.php:481
Wikimedia\Rdbms\DatabaseMysqlBase\streamStatementEnd
streamStatementEnd(&$sql, &$newLine)
Definition: DatabaseMysqlBase.php:1163
Wikimedia\Rdbms\DatabaseMysqlBase\open
open( $server, $user, $password, $dbName, $schema, $tablePrefix)
Open a new connection to the database (closing any existing one)
Definition: DatabaseMysqlBase.php:122
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:1914
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlFieldType
mysqlFieldType( $res, $n)
Get the type of the specified field in a result.
Wikimedia\Rdbms\DatabaseMysqlBase\dataSeek
dataSeek( $res, $row)
Definition: DatabaseMysqlBase.php:402
Wikimedia\Rdbms\Database\installErrorHandler
installErrorHandler()
Set a custom error handler for logging errors during database connection.
Definition: Database.php:830
Wikimedia\Rdbms\Database\restoreErrorHandler
restoreErrorHandler()
Restore the previous error handler and return the last PHP error for this DB.
Definition: Database.php:841
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlFetchField
mysqlFetchField( $res, $n)
Get column information from a result.
Wikimedia\Rdbms\DatabaseMysqlBase\unlock
unlock( $lockName, $method)
FROM MYSQL DOCS: https://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_releas...
Definition: DatabaseMysqlBase.php:1225
Wikimedia\Rdbms\DatabaseMysqlBase\wasReadOnlyError
wasReadOnlyError()
Determines if the last failure was due to the database being read-only.
Definition: DatabaseMysqlBase.php:1385
Wikimedia\Rdbms\DatabaseMysqlBase\$defaultBigSelects
bool null $defaultBigSelects
Definition: DatabaseMysqlBase.php:65
Wikimedia\Rdbms\Database\getLogContext
getLogContext(array $extras=[])
Create a log context to pass to PSR-3 logger functions.
Definition: Database.php:881
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlFetchArray
mysqlFetchArray( $res)
Fetch a result row as an associative and numeric array.
Wikimedia\Rdbms\DatabaseMysqlBase\freeResult
freeResult( $res)
Definition: DatabaseMysqlBase.php:231
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlDataSeek
mysqlDataSeek( $res, $row)
Move internal result pointer.
Wikimedia\Rdbms\DatabaseMysqlBase\getMasterPos
getMasterPos()
Get the position of the master from SHOW MASTER STATUS.
Definition: DatabaseMysqlBase.php:981
Wikimedia\Rdbms\Database\tableName
tableName( $name, $format='quoted')
Format a table name ready for use in constructing an SQL query.
Definition: Database.php:2434
Wikimedia\Rdbms\DatabaseMysqlBase\getReplicaPos
getReplicaPos()
Get the position of the master from SHOW SLAVE STATUS.
Definition: DatabaseMysqlBase.php:951
Wikimedia\Rdbms\DatabaseMysqlBase\getLagFromSlaveStatus
getLagFromSlaveStatus()
Definition: DatabaseMysqlBase.php:679
Wikimedia\Rdbms\DatabaseMysqlBase\$sslCiphers
string[] null $sslCiphers
Definition: DatabaseMysqlBase.php:59
Wikimedia\Rdbms\DatabaseMysqlBase\getServerGTIDs
getServerGTIDs( $fname=__METHOD__)
Definition: DatabaseMysqlBase.php:1055
Wikimedia\Rdbms\Database\getRecordedTransactionLagStatus
getRecordedTransactionLagStatus()
Get the replica DB lag when the current transaction started.
Definition: Database.php:4386
Wikimedia\Rdbms\DatabaseMysqlBase\$utf8Mode
bool $utf8Mode
Use experimental UTF-8 transmission encoding.
Definition: DatabaseMysqlBase.php:63
Wikimedia\Rdbms\Database\getServer
getServer()
Get the server hostname or IP address.
Definition: Database.php:2430
Wikimedia\Rdbms\DBUnexpectedError
Definition: DBUnexpectedError.php:27
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:1652
Wikimedia\Rdbms\DatabaseMysqlBase\$replicationInfoRow
stdClass null $replicationInfoRow
Definition: DatabaseMysqlBase.php:70
Wikimedia\Rdbms\DatabaseMysqlBase\serverIsReadOnly
serverIsReadOnly()
Definition: DatabaseMysqlBase.php:1084
Wikimedia\Rdbms\Database\getLazyMasterHandle
getLazyMasterHandle()
Get a handle to the master server of the cluster to which this server belongs.
Definition: Database.php:636
Wikimedia\Rdbms\DatabaseMysqlBase\$sqlMode
string $sqlMode
sql_mode value to send on connection
Definition: DatabaseMysqlBase.php:61
Wikimedia\Rdbms\DatabaseMysqlBase\isTransactableQuery
isTransactableQuery( $sql)
Determine whether a SQL statement is sensitive to isolation level.
Definition: DatabaseMysqlBase.php:1534
Wikimedia\Rdbms\IDatabase\lastErrno
lastErrno()
Get the last error number.
Wikimedia\Rdbms\DatabaseMysqlBase\makeLockName
makeLockName( $lockName)
Definition: DatabaseMysqlBase.php:1242
Wikimedia\Rdbms\Database\newExceptionAfterConnectError
newExceptionAfterConnectError( $error)
Definition: Database.php:1634
$cache
$cache
Definition: mcc.php:33
Wikimedia\Rdbms\Database\$srvCache
BagOStuff $srvCache
APC cache.
Definition: Database.php:51
Wikimedia\Rdbms\DatabaseMysqlBase\strencode
strencode( $s)
Definition: DatabaseMysqlBase.php:631
Wikimedia\Rdbms\DBExpectedError
Base class for the more common types of database errors.
Definition: DBExpectedError.php:32
Wikimedia\Rdbms\DatabaseMysqlBase\deleteJoin
deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname=__METHOD__)
DELETE where the condition is a join.
Definition: DatabaseMysqlBase.php:1306
Wikimedia\Rdbms\DatabaseMysqlBase\useIndexClause
useIndexClause( $index)
Definition: DatabaseMysqlBase.php:1097
Wikimedia\Rdbms\DBMasterPos\__toString
__toString()
Wikimedia\Rdbms\DatabaseMysqlBase\getServerRoleStatus
getServerRoleStatus( $role, $fname=__METHOD__)
Definition: DatabaseMysqlBase.php:1078
Wikimedia\Rdbms\DatabaseDomain\getSchema
getSchema()
Definition: DatabaseDomain.php:182
Wikimedia\Rdbms\DatabaseMysqlBase\tableLocksHaveTransactionScope
tableLocksHaveTransactionScope()
Checks if table locks acquired by lockTables() are transaction-bound in their scope.
Definition: DatabaseMysqlBase.php:1252
Wikimedia\Rdbms\Database\$server
string $server
Server that this instance is currently connected to.
Definition: Database.php:75
Wikimedia\Rdbms\DatabaseMysqlBase\listViews
listViews( $prefix=null, $fname=__METHOD__)
Lists VIEWs in the database.
Definition: DatabaseMysqlBase.php:1496
Wikimedia\Rdbms\Database\$flags
int $flags
Current bit field of class DBO_* constants.
Definition: Database.php:96
Wikimedia\Rdbms\DatabaseMysqlBase\replace
replace( $table, $uniqueIndexes, $rows, $fname=__METHOD__)
REPLACE query wrapper.
Definition: DatabaseMysqlBase.php:451
Wikimedia\Rdbms\DatabaseMysqlBase\$lastKnownReplicaPos
MysqlMasterPos $lastKnownReplicaPos
Definition: DatabaseMysqlBase.php:43
Wikimedia\Rdbms\DatabaseMysqlBase\isView
isView( $name, $prefix=null)
Differentiates between a TABLE and a VIEW.
Definition: DatabaseMysqlBase.php:1530
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:1823
Wikimedia\Rdbms\DatabaseMysqlBase\$lagDetectionOptions
array $lagDetectionOptions
Method to detect replica DB lag.
Definition: DatabaseMysqlBase.php:47
Wikimedia\Rdbms\Database\addQuotes
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
Definition: Database.php:2755
Wikimedia\Rdbms\DatabaseMysqlBase\$insertSelectIsSafe
bool null $insertSelectIsSafe
Definition: DatabaseMysqlBase.php:68
Wikimedia\Rdbms\Database\qualifiedTableComponents
qualifiedTableComponents( $name)
Get the table components needed for a query given the currently selected database.
Definition: Database.php:2492
Wikimedia\Rdbms\DatabaseMysqlBase\lock
lock( $lockName, $method, $timeout=5)
Definition: DatabaseMysqlBase.php:1200
Wikimedia\Rdbms\DatabaseMysqlBase\wasConnectionError
wasConnectionError( $errno)
Do not use this method outside of Database/DBError classes.
Definition: DatabaseMysqlBase.php:1390
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/schema/prefix specifications for IDatabase.
Definition: DatabaseDomain.php:40
Wikimedia\Rdbms\DatabaseMysqlBase\doSelectDomain
doSelectDomain(DatabaseDomain $domain)
Definition: DatabaseMysqlBase.php:180
Wikimedia\Rdbms\DatabaseMysqlBase\fetchRow
fetchRow( $res)
Definition: DatabaseMysqlBase.php:286
Wikimedia\Rdbms\DatabaseMysqlBase\getMysqlStatus
getMysqlStatus( $which="%")
Get status information from SHOW STATUS in an associative array.
Definition: DatabaseMysqlBase.php:1475
Wikimedia\Rdbms\DatabaseMysqlBase\getServerVersion
getServerVersion()
Definition: DatabaseMysqlBase.php:1130
Wikimedia\Rdbms\Database\makeList
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
Definition: Database.php:2222
Wikimedia\Rdbms\DatabaseMysqlBase\useGTIDs
useGTIDs()
Definition: DatabaseMysqlBase.php:1554
Wikimedia\Rdbms\ResultWrapper\unwrap
static & unwrap(&$res)
Get the underlying RDBMS driver-specific result resource.
Definition: ResultWrapper.php:59
Wikimedia\Rdbms\Database\query
query( $sql, $fname=__METHOD__, $flags=0)
Run an SQL query and return the result.
Definition: Database.php:1155
Wikimedia\Rdbms\DatabaseMysqlBase\mysqlError
mysqlError( $conn=null)
Returns the text of the error message from previous MySQL operation.
Wikimedia\Rdbms\DatabaseMysqlBase\getMasterServerInfo
getMasterServerInfo()
Definition: DatabaseMysqlBase.php:757
Wikimedia\Rdbms\Database\$conn
object resource null $conn
Database connection.
Definition: Database.php:69
Wikimedia\Rdbms\DatabaseMysqlBase
Database abstraction object for MySQL.
Definition: DatabaseMysqlBase.php:41
Wikimedia\Rdbms\DatabaseMysqlBase\isQuotedIdentifier
isQuotedIdentifier( $name)
Definition: DatabaseMysqlBase.php:657
Wikimedia\Rdbms\Database\getDBname
getDBname()
Get the current DB name.
Definition: Database.php:2426
Wikimedia\Rdbms\DatabaseMysqlBase\namedLocksEnqueue
namedLocksEnqueue()
Check to see if a named lock used by lock() use blocking queues.
Definition: DatabaseMysqlBase.php:1248
Wikimedia\Rdbms\DatabaseMysqlBase\tableExists
tableExists( $table, $fname=__METHOD__)
Query whether a given table exists.
Definition: DatabaseMysqlBase.php:536
Wikimedia\Rdbms\DatabaseMysqlBase\$lagDetectionMethod
string $lagDetectionMethod
Method to detect replica DB lag.
Definition: DatabaseMysqlBase.php:45
Wikimedia\Rdbms\DatabaseMysqlBase\lockIsFree
lockIsFree( $lockName, $method)
Check to see if a named lock is available.
Definition: DatabaseMysqlBase.php:1180
Wikimedia\Rdbms\DatabaseMysqlBase\dropTable
dropTable( $tableName, $fName=__METHOD__)
Definition: DatabaseMysqlBase.php:1461
Wikimedia\Rdbms\DatabaseMysqlBase\isInsertSelectSafe
isInsertSelectSafe(array $insertOptions, array $selectOptions)
Definition: DatabaseMysqlBase.php:455