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