DatabaseMysqlBase visibility cleanups
[mediawiki.git] / includes / libs / rdbms / database / DatabaseMysqlBase.php
blob76208c4c469393e732019bb1427bfb138b5b668c
1 <?php
2 /**
3 * This is the MySQL database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Database
24 /**
25 * Database abstraction object for MySQL.
26 * Defines methods independent on used MySQL extension.
28 * @ingroup Database
29 * @since 1.22
30 * @see Database
32 abstract class DatabaseMysqlBase extends Database {
33 /** @var MysqlMasterPos */
34 protected $lastKnownReplicaPos;
35 /** @var string Method to detect replica DB lag */
36 protected $lagDetectionMethod;
37 /** @var array Method to detect replica DB lag */
38 protected $lagDetectionOptions = [];
39 /** @var bool bool Whether to use GTID methods */
40 protected $useGTIDs = false;
41 /** @var string|null */
42 protected $sslKeyPath;
43 /** @var string|null */
44 protected $sslCertPath;
45 /** @var string|null */
46 protected $sslCAPath;
47 /** @var string[]|null */
48 protected $sslCiphers;
49 /** @var string sql_mode value to send on connection */
50 protected $sqlMode;
51 /** @var bool Use experimental UTF-8 transmission encoding */
52 protected $utf8Mode;
54 /** @var string|null */
55 private $serverVersion = null;
57 /**
58 * Additional $params include:
59 * - lagDetectionMethod : set to one of (Seconds_Behind_Master,pt-heartbeat).
60 * pt-heartbeat assumes the table is at heartbeat.heartbeat
61 * and uses UTC timestamps in the heartbeat.ts column.
62 * (https://www.percona.com/doc/percona-toolkit/2.2/pt-heartbeat.html)
63 * - lagDetectionOptions : if using pt-heartbeat, this can be set to an array map to change
64 * the default behavior. Normally, the heartbeat row with the server
65 * ID of this server's master will be used. Set the "conds" field to
66 * override the query conditions, e.g. ['shard' => 's1'].
67 * - useGTIDs : use GTID methods like MASTER_GTID_WAIT() when possible.
68 * - sslKeyPath : path to key file [default: null]
69 * - sslCertPath : path to certificate file [default: null]
70 * - sslCAPath : parth to certificate authority PEM files [default: null]
71 * - sslCiphers : array list of allowable ciphers [default: null]
72 * @param array $params
74 function __construct( array $params ) {
75 $this->lagDetectionMethod = isset( $params['lagDetectionMethod'] )
76 ? $params['lagDetectionMethod']
77 : 'Seconds_Behind_Master';
78 $this->lagDetectionOptions = isset( $params['lagDetectionOptions'] )
79 ? $params['lagDetectionOptions']
80 : [];
81 $this->useGTIDs = !empty( $params['useGTIDs' ] );
82 foreach ( [ 'KeyPath', 'CertPath', 'CAPath', 'Ciphers' ] as $name ) {
83 $var = "ssl{$name}";
84 if ( isset( $params[$var] ) ) {
85 $this->$var = $params[$var];
88 $this->sqlMode = isset( $params['sqlMode'] ) ? $params['sqlMode'] : '';
89 $this->utf8Mode = !empty( $params['utf8Mode'] );
91 parent::__construct( $params );
94 /**
95 * @return string
97 public function getType() {
98 return 'mysql';
102 * @param string $server
103 * @param string $user
104 * @param string $password
105 * @param string $dbName
106 * @throws Exception|DBConnectionError
107 * @return bool
109 public function open( $server, $user, $password, $dbName ) {
110 # Close/unset connection handle
111 $this->close();
113 $this->mServer = $server;
114 $this->mUser = $user;
115 $this->mPassword = $password;
116 $this->mDBname = $dbName;
118 $this->installErrorHandler();
119 try {
120 $this->mConn = $this->mysqlConnect( $this->mServer );
121 } catch ( Exception $ex ) {
122 $this->restoreErrorHandler();
123 throw $ex;
125 $error = $this->restoreErrorHandler();
127 # Always log connection errors
128 if ( !$this->mConn ) {
129 if ( !$error ) {
130 $error = $this->lastError();
132 $this->connLogger->error(
133 "Error connecting to {db_server}: {error}",
134 $this->getLogContext( [
135 'method' => __METHOD__,
136 'error' => $error,
139 $this->connLogger->debug( "DB connection error\n" .
140 "Server: $server, User: $user, Password: " .
141 substr( $password, 0, 3 ) . "..., error: " . $error . "\n" );
143 $this->reportConnectionError( $error );
146 if ( $dbName != '' ) {
147 MediaWiki\suppressWarnings();
148 $success = $this->selectDB( $dbName );
149 MediaWiki\restoreWarnings();
150 if ( !$success ) {
151 $this->queryLogger->error(
152 "Error selecting database {db_name} on server {db_server}",
153 $this->getLogContext( [
154 'method' => __METHOD__,
157 $this->queryLogger->debug(
158 "Error selecting database $dbName on server {$this->mServer}" );
160 $this->reportConnectionError( "Error selecting database $dbName" );
164 // Tell the server what we're communicating with
165 if ( !$this->connectInitCharset() ) {
166 $this->reportConnectionError( "Error setting character set" );
169 // Abstract over any insane MySQL defaults
170 $set = [ 'group_concat_max_len = 262144' ];
171 // Set SQL mode, default is turning them all off, can be overridden or skipped with null
172 if ( is_string( $this->sqlMode ) ) {
173 $set[] = 'sql_mode = ' . $this->addQuotes( $this->sqlMode );
175 // Set any custom settings defined by site config
176 // (e.g. https://dev.mysql.com/doc/refman/4.1/en/innodb-parameters.html)
177 foreach ( $this->mSessionVars as $var => $val ) {
178 // Escape strings but not numbers to avoid MySQL complaining
179 if ( !is_int( $val ) && !is_float( $val ) ) {
180 $val = $this->addQuotes( $val );
182 $set[] = $this->addIdentifierQuotes( $var ) . ' = ' . $val;
185 if ( $set ) {
186 // Use doQuery() to avoid opening implicit transactions (DBO_TRX)
187 $success = $this->doQuery( 'SET ' . implode( ', ', $set ) );
188 if ( !$success ) {
189 $this->queryLogger->error(
190 'Error setting MySQL variables on server {db_server} (check $wgSQLMode)',
191 $this->getLogContext( [
192 'method' => __METHOD__,
195 $this->reportConnectionError(
196 'Error setting MySQL variables on server {db_server} (check $wgSQLMode)' );
200 $this->mOpened = true;
202 return true;
206 * Set the character set information right after connection
207 * @return bool
209 protected function connectInitCharset() {
210 if ( $this->utf8Mode ) {
211 // Tell the server we're communicating with it in UTF-8.
212 // This may engage various charset conversions.
213 return $this->mysqlSetCharset( 'utf8' );
214 } else {
215 return $this->mysqlSetCharset( 'binary' );
220 * Open a connection to a MySQL server
222 * @param string $realServer
223 * @return mixed Raw connection
224 * @throws DBConnectionError
226 abstract protected function mysqlConnect( $realServer );
229 * Set the character set of the MySQL link
231 * @param string $charset
232 * @return bool
234 abstract protected function mysqlSetCharset( $charset );
237 * @param ResultWrapper|resource $res
238 * @throws DBUnexpectedError
240 public function freeResult( $res ) {
241 if ( $res instanceof ResultWrapper ) {
242 $res = $res->result;
244 MediaWiki\suppressWarnings();
245 $ok = $this->mysqlFreeResult( $res );
246 MediaWiki\restoreWarnings();
247 if ( !$ok ) {
248 throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
253 * Free result memory
255 * @param resource $res Raw result
256 * @return bool
258 abstract protected function mysqlFreeResult( $res );
261 * @param ResultWrapper|resource $res
262 * @return stdClass|bool
263 * @throws DBUnexpectedError
265 public function fetchObject( $res ) {
266 if ( $res instanceof ResultWrapper ) {
267 $res = $res->result;
269 MediaWiki\suppressWarnings();
270 $row = $this->mysqlFetchObject( $res );
271 MediaWiki\restoreWarnings();
273 $errno = $this->lastErrno();
274 // Unfortunately, mysql_fetch_object does not reset the last errno.
275 // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
276 // these are the only errors mysql_fetch_object can cause.
277 // See http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
278 if ( $errno == 2000 || $errno == 2013 ) {
279 throw new DBUnexpectedError(
280 $this,
281 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() )
285 return $row;
289 * Fetch a result row as an object
291 * @param resource $res Raw result
292 * @return stdClass
294 abstract protected function mysqlFetchObject( $res );
297 * @param ResultWrapper|resource $res
298 * @return array|bool
299 * @throws DBUnexpectedError
301 public function fetchRow( $res ) {
302 if ( $res instanceof ResultWrapper ) {
303 $res = $res->result;
305 MediaWiki\suppressWarnings();
306 $row = $this->mysqlFetchArray( $res );
307 MediaWiki\restoreWarnings();
309 $errno = $this->lastErrno();
310 // Unfortunately, mysql_fetch_array does not reset the last errno.
311 // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
312 // these are the only errors mysql_fetch_array can cause.
313 // See http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
314 if ( $errno == 2000 || $errno == 2013 ) {
315 throw new DBUnexpectedError(
316 $this,
317 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() )
321 return $row;
325 * Fetch a result row as an associative and numeric array
327 * @param resource $res Raw result
328 * @return array
330 abstract protected function mysqlFetchArray( $res );
333 * @throws DBUnexpectedError
334 * @param ResultWrapper|resource $res
335 * @return int
337 function numRows( $res ) {
338 if ( $res instanceof ResultWrapper ) {
339 $res = $res->result;
341 MediaWiki\suppressWarnings();
342 $n = $this->mysqlNumRows( $res );
343 MediaWiki\restoreWarnings();
345 // Unfortunately, mysql_num_rows does not reset the last errno.
346 // We are not checking for any errors here, since
347 // these are no errors mysql_num_rows can cause.
348 // See http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
349 // See https://phabricator.wikimedia.org/T44430
350 return $n;
354 * Get number of rows in result
356 * @param resource $res Raw result
357 * @return int
359 abstract protected function mysqlNumRows( $res );
362 * @param ResultWrapper|resource $res
363 * @return int
365 public function numFields( $res ) {
366 if ( $res instanceof ResultWrapper ) {
367 $res = $res->result;
370 return $this->mysqlNumFields( $res );
374 * Get number of fields in result
376 * @param resource $res Raw result
377 * @return int
379 abstract protected function mysqlNumFields( $res );
382 * @param ResultWrapper|resource $res
383 * @param int $n
384 * @return string
386 public function fieldName( $res, $n ) {
387 if ( $res instanceof ResultWrapper ) {
388 $res = $res->result;
391 return $this->mysqlFieldName( $res, $n );
395 * Get the name of the specified field in a result
397 * @param ResultWrapper|resource $res
398 * @param int $n
399 * @return string
401 abstract protected function mysqlFieldName( $res, $n );
404 * mysql_field_type() wrapper
405 * @param ResultWrapper|resource $res
406 * @param int $n
407 * @return string
409 public function fieldType( $res, $n ) {
410 if ( $res instanceof ResultWrapper ) {
411 $res = $res->result;
414 return $this->mysqlFieldType( $res, $n );
418 * Get the type of the specified field in a result
420 * @param ResultWrapper|resource $res
421 * @param int $n
422 * @return string
424 abstract protected function mysqlFieldType( $res, $n );
427 * @param ResultWrapper|resource $res
428 * @param int $row
429 * @return bool
431 public function dataSeek( $res, $row ) {
432 if ( $res instanceof ResultWrapper ) {
433 $res = $res->result;
436 return $this->mysqlDataSeek( $res, $row );
440 * Move internal result pointer
442 * @param ResultWrapper|resource $res
443 * @param int $row
444 * @return bool
446 abstract protected function mysqlDataSeek( $res, $row );
449 * @return string
451 public function lastError() {
452 if ( $this->mConn ) {
453 # Even if it's non-zero, it can still be invalid
454 MediaWiki\suppressWarnings();
455 $error = $this->mysqlError( $this->mConn );
456 if ( !$error ) {
457 $error = $this->mysqlError();
459 MediaWiki\restoreWarnings();
460 } else {
461 $error = $this->mysqlError();
463 if ( $error ) {
464 $error .= ' (' . $this->mServer . ')';
467 return $error;
471 * Returns the text of the error message from previous MySQL operation
473 * @param resource $conn Raw connection
474 * @return string
476 abstract protected function mysqlError( $conn = null );
479 * @param string $table
480 * @param array $uniqueIndexes
481 * @param array $rows
482 * @param string $fname
483 * @return ResultWrapper
485 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
486 return $this->nativeReplace( $table, $rows, $fname );
490 * Estimate rows in dataset
491 * Returns estimated count, based on EXPLAIN output
492 * Takes same arguments as Database::select()
494 * @param string|array $table
495 * @param string|array $vars
496 * @param string|array $conds
497 * @param string $fname
498 * @param string|array $options
499 * @return bool|int
501 public function estimateRowCount( $table, $vars = '*', $conds = '',
502 $fname = __METHOD__, $options = []
504 $options['EXPLAIN'] = true;
505 $res = $this->select( $table, $vars, $conds, $fname, $options );
506 if ( $res === false ) {
507 return false;
509 if ( !$this->numRows( $res ) ) {
510 return 0;
513 $rows = 1;
514 foreach ( $res as $plan ) {
515 $rows *= $plan->rows > 0 ? $plan->rows : 1; // avoid resetting to zero
518 return (int)$rows;
521 public function tableExists( $table, $fname = __METHOD__ ) {
522 $table = $this->tableName( $table, 'raw' );
523 if ( isset( $this->mSessionTempTables[$table] ) ) {
524 return true; // already known to exist and won't show in SHOW TABLES anyway
527 $encLike = $this->buildLike( $table );
529 return $this->query( "SHOW TABLES $encLike", $fname )->numRows() > 0;
533 * @param string $table
534 * @param string $field
535 * @return bool|MySQLField
537 public function fieldInfo( $table, $field ) {
538 $table = $this->tableName( $table );
539 $res = $this->query( "SELECT * FROM $table LIMIT 1", __METHOD__, true );
540 if ( !$res ) {
541 return false;
543 $n = $this->mysqlNumFields( $res->result );
544 for ( $i = 0; $i < $n; $i++ ) {
545 $meta = $this->mysqlFetchField( $res->result, $i );
546 if ( $field == $meta->name ) {
547 return new MySQLField( $meta );
551 return false;
555 * Get column information from a result
557 * @param resource $res Raw result
558 * @param int $n
559 * @return stdClass
561 abstract protected function mysqlFetchField( $res, $n );
564 * Get information about an index into an object
565 * Returns false if the index does not exist
567 * @param string $table
568 * @param string $index
569 * @param string $fname
570 * @return bool|array|null False or null on failure
572 public function indexInfo( $table, $index, $fname = __METHOD__ ) {
573 # SHOW INDEX works in MySQL 3.23.58, but SHOW INDEXES does not.
574 # SHOW INDEX should work for 3.x and up:
575 # http://dev.mysql.com/doc/mysql/en/SHOW_INDEX.html
576 $table = $this->tableName( $table );
577 $index = $this->indexName( $index );
579 $sql = 'SHOW INDEX FROM ' . $table;
580 $res = $this->query( $sql, $fname );
582 if ( !$res ) {
583 return null;
586 $result = [];
588 foreach ( $res as $row ) {
589 if ( $row->Key_name == $index ) {
590 $result[] = $row;
594 return empty( $result ) ? false : $result;
598 * @param string $s
599 * @return string
601 public function strencode( $s ) {
602 return $this->mysqlRealEscapeString( $s );
606 * @param string $s
607 * @return mixed
609 abstract protected function mysqlRealEscapeString( $s );
611 public function addQuotes( $s ) {
612 if ( is_bool( $s ) ) {
613 // Parent would transform to int, which does not play nice with MySQL type juggling.
614 // When searching for an int in a string column, the strings are cast to int, which
615 // means false would match any string not starting with a number.
616 $s = (string)(int)$s;
618 return parent::addQuotes( $s );
622 * MySQL uses `backticks` for identifier quoting instead of the sql standard "double quotes".
624 * @param string $s
625 * @return string
627 public function addIdentifierQuotes( $s ) {
628 // Characters in the range \u0001-\uFFFF are valid in a quoted identifier
629 // Remove NUL bytes and escape backticks by doubling
630 return '`' . str_replace( [ "\0", '`' ], [ '', '``' ], $s ) . '`';
634 * @param string $name
635 * @return bool
637 public function isQuotedIdentifier( $name ) {
638 return strlen( $name ) && $name[0] == '`' && substr( $name, -1, 1 ) == '`';
641 public function getLag() {
642 if ( $this->getLagDetectionMethod() === 'pt-heartbeat' ) {
643 return $this->getLagFromPtHeartbeat();
644 } else {
645 return $this->getLagFromSlaveStatus();
650 * @return string
652 protected function getLagDetectionMethod() {
653 return $this->lagDetectionMethod;
657 * @return bool|int
659 protected function getLagFromSlaveStatus() {
660 $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
661 $row = $res ? $res->fetchObject() : false;
662 if ( $row && strval( $row->Seconds_Behind_Master ) !== '' ) {
663 return intval( $row->Seconds_Behind_Master );
666 return false;
670 * @return bool|float
672 protected function getLagFromPtHeartbeat() {
673 $options = $this->lagDetectionOptions;
675 if ( isset( $options['conds'] ) ) {
676 // Best method for multi-DC setups: use logical channel names
677 $data = $this->getHeartbeatData( $options['conds'] );
678 } else {
679 // Standard method: use master server ID (works with stock pt-heartbeat)
680 $masterInfo = $this->getMasterServerInfo();
681 if ( !$masterInfo ) {
682 $this->queryLogger->error(
683 "Unable to query master of {db_server} for server ID",
684 $this->getLogContext( [
685 'method' => __METHOD__
689 return false; // could not get master server ID
692 $conds = [ 'server_id' => intval( $masterInfo['serverId'] ) ];
693 $data = $this->getHeartbeatData( $conds );
696 list( $time, $nowUnix ) = $data;
697 if ( $time !== null ) {
698 // @time is in ISO format like "2015-09-25T16:48:10.000510"
699 $dateTime = new DateTime( $time, new DateTimeZone( 'UTC' ) );
700 $timeUnix = (int)$dateTime->format( 'U' ) + $dateTime->format( 'u' ) / 1e6;
702 return max( $nowUnix - $timeUnix, 0.0 );
705 $this->queryLogger->error(
706 "Unable to find pt-heartbeat row for {db_server}",
707 $this->getLogContext( [
708 'method' => __METHOD__
712 return false;
715 protected function getMasterServerInfo() {
716 $cache = $this->srvCache;
717 $key = $cache->makeGlobalKey(
718 'mysql',
719 'master-info',
720 // Using one key for all cluster replica DBs is preferable
721 $this->getLBInfo( 'clusterMasterHost' ) ?: $this->getServer()
724 return $cache->getWithSetCallback(
725 $key,
726 $cache::TTL_INDEFINITE,
727 function () use ( $cache, $key ) {
728 // Get and leave a lock key in place for a short period
729 if ( !$cache->lock( $key, 0, 10 ) ) {
730 return false; // avoid master connection spike slams
733 $conn = $this->getLazyMasterHandle();
734 if ( !$conn ) {
735 return false; // something is misconfigured
738 // Connect to and query the master; catch errors to avoid outages
739 try {
740 $res = $conn->query( 'SELECT @@server_id AS id', __METHOD__ );
741 $row = $res ? $res->fetchObject() : false;
742 $id = $row ? (int)$row->id : 0;
743 } catch ( DBError $e ) {
744 $id = 0;
747 // Cache the ID if it was retrieved
748 return $id ? [ 'serverId' => $id, 'asOf' => time() ] : false;
754 * @param array $conds WHERE clause conditions to find a row
755 * @return array (heartbeat `ts` column value or null, UNIX timestamp) for the newest beat
756 * @see https://www.percona.com/doc/percona-toolkit/2.1/pt-heartbeat.html
758 protected function getHeartbeatData( array $conds ) {
759 $whereSQL = $this->makeList( $conds, self::LIST_AND );
760 // Use ORDER BY for channel based queries since that field might not be UNIQUE.
761 // Note: this would use "TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6))" but the
762 // percision field is not supported in MySQL <= 5.5.
763 $res = $this->query(
764 "SELECT ts FROM heartbeat.heartbeat WHERE $whereSQL ORDER BY ts DESC LIMIT 1"
766 $row = $res ? $res->fetchObject() : false;
768 return [ $row ? $row->ts : null, microtime( true ) ];
771 protected function getApproximateLagStatus() {
772 if ( $this->getLagDetectionMethod() === 'pt-heartbeat' ) {
773 // Disable caching since this is fast enough and we don't wan't
774 // to be *too* pessimistic by having both the cache TTL and the
775 // pt-heartbeat interval count as lag in getSessionLagStatus()
776 return parent::getApproximateLagStatus();
779 $key = $this->srvCache->makeGlobalKey( 'mysql-lag', $this->getServer() );
780 $approxLag = $this->srvCache->get( $key );
781 if ( !$approxLag ) {
782 $approxLag = parent::getApproximateLagStatus();
783 $this->srvCache->set( $key, $approxLag, 1 );
786 return $approxLag;
789 public function masterPosWait( DBMasterPos $pos, $timeout ) {
790 if ( !( $pos instanceof MySQLMasterPos ) ) {
791 throw new InvalidArgumentException( "Position not an instance of MySQLMasterPos" );
794 if ( $this->getLBInfo( 'is static' ) === true ) {
795 return 0; // this is a copy of a read-only dataset with no master DB
796 } elseif ( $this->lastKnownReplicaPos && $this->lastKnownReplicaPos->hasReached( $pos ) ) {
797 return 0; // already reached this point for sure
800 // Call doQuery() directly, to avoid opening a transaction if DBO_TRX is set
801 if ( $this->useGTIDs && $pos->gtids ) {
802 // Wait on the GTID set (MariaDB only)
803 $gtidArg = $this->addQuotes( implode( ',', $pos->gtids ) );
804 $res = $this->doQuery( "SELECT MASTER_GTID_WAIT($gtidArg, $timeout)" );
805 } else {
806 // Wait on the binlog coordinates
807 $encFile = $this->addQuotes( $pos->file );
808 $encPos = intval( $pos->pos );
809 $res = $this->doQuery( "SELECT MASTER_POS_WAIT($encFile, $encPos, $timeout)" );
812 $row = $res ? $this->fetchRow( $res ) : false;
813 if ( !$row ) {
814 throw new DBExpectedError( $this, "Failed to query MASTER_POS_WAIT()" );
817 // Result can be NULL (error), -1 (timeout), or 0+ per the MySQL manual
818 $status = ( $row[0] !== null ) ? intval( $row[0] ) : null;
819 if ( $status === null ) {
820 // T126436: jobs programmed to wait on master positions might be referencing binlogs
821 // with an old master hostname. Such calls make MASTER_POS_WAIT() return null. Try
822 // to detect this and treat the replica DB as having reached the position; a proper master
823 // switchover already requires that the new master be caught up before the switch.
824 $replicationPos = $this->getReplicaPos();
825 if ( $replicationPos && !$replicationPos->channelsMatch( $pos ) ) {
826 $this->lastKnownReplicaPos = $replicationPos;
827 $status = 0;
829 } elseif ( $status >= 0 ) {
830 // Remember that this position was reached to save queries next time
831 $this->lastKnownReplicaPos = $pos;
834 return $status;
838 * Get the position of the master from SHOW SLAVE STATUS
840 * @return MySQLMasterPos|bool
842 public function getReplicaPos() {
843 $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
844 $row = $this->fetchObject( $res );
846 if ( $row ) {
847 $pos = isset( $row->Exec_master_log_pos )
848 ? $row->Exec_master_log_pos
849 : $row->Exec_Master_Log_Pos;
850 // Also fetch the last-applied GTID set (MariaDB)
851 if ( $this->useGTIDs ) {
852 $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'gtid_slave_pos'", __METHOD__ );
853 $gtidRow = $this->fetchObject( $res );
854 $gtidSet = $gtidRow ? $gtidRow->Value : '';
855 } else {
856 $gtidSet = '';
859 return new MySQLMasterPos( $row->Relay_Master_Log_File, $pos, $gtidSet );
860 } else {
861 return false;
866 * Get the position of the master from SHOW MASTER STATUS
868 * @return MySQLMasterPos|bool
870 public function getMasterPos() {
871 $res = $this->query( 'SHOW MASTER STATUS', __METHOD__ );
872 $row = $this->fetchObject( $res );
874 if ( $row ) {
875 // Also fetch the last-written GTID set (MariaDB)
876 if ( $this->useGTIDs ) {
877 $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'gtid_binlog_pos'", __METHOD__ );
878 $gtidRow = $this->fetchObject( $res );
879 $gtidSet = $gtidRow ? $gtidRow->Value : '';
880 } else {
881 $gtidSet = '';
884 return new MySQLMasterPos( $row->File, $row->Position, $gtidSet );
885 } else {
886 return false;
890 public function serverIsReadOnly() {
891 $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'read_only'", __METHOD__ );
892 $row = $this->fetchObject( $res );
894 return $row ? ( strtolower( $row->Value ) === 'on' ) : false;
898 * @param string $index
899 * @return string
901 function useIndexClause( $index ) {
902 return "FORCE INDEX (" . $this->indexName( $index ) . ")";
906 * @param string $index
907 * @return string
909 function ignoreIndexClause( $index ) {
910 return "IGNORE INDEX (" . $this->indexName( $index ) . ")";
914 * @return string
916 function lowPriorityOption() {
917 return 'LOW_PRIORITY';
921 * @return string
923 public function getSoftwareLink() {
924 // MariaDB includes its name in its version string; this is how MariaDB's version of
925 // the mysql command-line client identifies MariaDB servers (see mariadb_connection()
926 // in libmysql/libmysql.c).
927 $version = $this->getServerVersion();
928 if ( strpos( $version, 'MariaDB' ) !== false || strpos( $version, '-maria-' ) !== false ) {
929 return '[{{int:version-db-mariadb-url}} MariaDB]';
932 // Percona Server's version suffix is not very distinctive, and @@version_comment
933 // doesn't give the necessary info for source builds, so assume the server is MySQL.
934 // (Even Percona's version of mysql doesn't try to make the distinction.)
935 return '[{{int:version-db-mysql-url}} MySQL]';
939 * @return string
941 public function getServerVersion() {
942 // Not using mysql_get_server_info() or similar for consistency: in the handshake,
943 // MariaDB 10 adds the prefix "5.5.5-", and only some newer client libraries strip
944 // it off (see RPL_VERSION_HACK in include/mysql_com.h).
945 if ( $this->serverVersion === null ) {
946 $this->serverVersion = $this->selectField( '', 'VERSION()', '', __METHOD__ );
948 return $this->serverVersion;
952 * @param array $options
954 public function setSessionOptions( array $options ) {
955 if ( isset( $options['connTimeout'] ) ) {
956 $timeout = (int)$options['connTimeout'];
957 $this->query( "SET net_read_timeout=$timeout" );
958 $this->query( "SET net_write_timeout=$timeout" );
963 * @param string $sql
964 * @param string $newLine
965 * @return bool
967 public function streamStatementEnd( &$sql, &$newLine ) {
968 if ( strtoupper( substr( $newLine, 0, 9 ) ) == 'DELIMITER' ) {
969 preg_match( '/^DELIMITER\s+(\S+)/', $newLine, $m );
970 $this->delimiter = $m[1];
971 $newLine = '';
974 return parent::streamStatementEnd( $sql, $newLine );
978 * Check to see if a named lock is available. This is non-blocking.
980 * @param string $lockName Name of lock to poll
981 * @param string $method Name of method calling us
982 * @return bool
983 * @since 1.20
985 public function lockIsFree( $lockName, $method ) {
986 $encName = $this->addQuotes( $this->makeLockName( $lockName ) );
987 $result = $this->query( "SELECT IS_FREE_LOCK($encName) AS lockstatus", $method );
988 $row = $this->fetchObject( $result );
990 return ( $row->lockstatus == 1 );
994 * @param string $lockName
995 * @param string $method
996 * @param int $timeout
997 * @return bool
999 public function lock( $lockName, $method, $timeout = 5 ) {
1000 $encName = $this->addQuotes( $this->makeLockName( $lockName ) );
1001 $result = $this->query( "SELECT GET_LOCK($encName, $timeout) AS lockstatus", $method );
1002 $row = $this->fetchObject( $result );
1004 if ( $row->lockstatus == 1 ) {
1005 parent::lock( $lockName, $method, $timeout ); // record
1006 return true;
1009 $this->queryLogger->warning( __METHOD__ . " failed to acquire lock '$lockName'\n" );
1011 return false;
1015 * FROM MYSQL DOCS:
1016 * http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
1017 * @param string $lockName
1018 * @param string $method
1019 * @return bool
1021 public function unlock( $lockName, $method ) {
1022 $encName = $this->addQuotes( $this->makeLockName( $lockName ) );
1023 $result = $this->query( "SELECT RELEASE_LOCK($encName) as lockstatus", $method );
1024 $row = $this->fetchObject( $result );
1026 if ( $row->lockstatus == 1 ) {
1027 parent::unlock( $lockName, $method ); // record
1028 return true;
1031 $this->queryLogger->warning( __METHOD__ . " failed to release lock '$lockName'\n" );
1033 return false;
1036 private function makeLockName( $lockName ) {
1037 // http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
1038 // Newer version enforce a 64 char length limit.
1039 return ( strlen( $lockName ) > 64 ) ? sha1( $lockName ) : $lockName;
1042 public function namedLocksEnqueue() {
1043 return true;
1047 * @param array $read
1048 * @param array $write
1049 * @param string $method
1050 * @param bool $lowPriority
1051 * @return bool
1053 public function lockTables( $read, $write, $method, $lowPriority = true ) {
1054 $items = [];
1056 foreach ( $write as $table ) {
1057 $tbl = $this->tableName( $table ) .
1058 ( $lowPriority ? ' LOW_PRIORITY' : '' ) .
1059 ' WRITE';
1060 $items[] = $tbl;
1062 foreach ( $read as $table ) {
1063 $items[] = $this->tableName( $table ) . ' READ';
1065 $sql = "LOCK TABLES " . implode( ',', $items );
1066 $this->query( $sql, $method );
1068 return true;
1072 * @param string $method
1073 * @return bool
1075 public function unlockTables( $method ) {
1076 $this->query( "UNLOCK TABLES", $method );
1078 return true;
1082 * @param bool $value
1084 public function setBigSelects( $value = true ) {
1085 if ( $value === 'default' ) {
1086 if ( $this->mDefaultBigSelects === null ) {
1087 # Function hasn't been called before so it must already be set to the default
1088 return;
1089 } else {
1090 $value = $this->mDefaultBigSelects;
1092 } elseif ( $this->mDefaultBigSelects === null ) {
1093 $this->mDefaultBigSelects =
1094 (bool)$this->selectField( false, '@@sql_big_selects', '', __METHOD__ );
1096 $encValue = $value ? '1' : '0';
1097 $this->query( "SET sql_big_selects=$encValue", __METHOD__ );
1101 * DELETE where the condition is a join. MySql uses multi-table deletes.
1102 * @param string $delTable
1103 * @param string $joinTable
1104 * @param string $delVar
1105 * @param string $joinVar
1106 * @param array|string $conds
1107 * @param bool|string $fname
1108 * @throws DBUnexpectedError
1109 * @return bool|ResultWrapper
1111 public function deleteJoin(
1112 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
1114 if ( !$conds ) {
1115 throw new DBUnexpectedError( $this, __METHOD__ . ' called with empty $conds' );
1118 $delTable = $this->tableName( $delTable );
1119 $joinTable = $this->tableName( $joinTable );
1120 $sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
1122 if ( $conds != '*' ) {
1123 $sql .= ' AND ' . $this->makeList( $conds, self::LIST_AND );
1126 return $this->query( $sql, $fname );
1130 * @param string $table
1131 * @param array $rows
1132 * @param array $uniqueIndexes
1133 * @param array $set
1134 * @param string $fname
1135 * @return bool
1137 public function upsert( $table, array $rows, array $uniqueIndexes,
1138 array $set, $fname = __METHOD__
1140 if ( !count( $rows ) ) {
1141 return true; // nothing to do
1144 if ( !is_array( reset( $rows ) ) ) {
1145 $rows = [ $rows ];
1148 $table = $this->tableName( $table );
1149 $columns = array_keys( $rows[0] );
1151 $sql = "INSERT INTO $table (" . implode( ',', $columns ) . ') VALUES ';
1152 $rowTuples = [];
1153 foreach ( $rows as $row ) {
1154 $rowTuples[] = '(' . $this->makeList( $row ) . ')';
1156 $sql .= implode( ',', $rowTuples );
1157 $sql .= " ON DUPLICATE KEY UPDATE " . $this->makeList( $set, self::LIST_SET );
1159 return (bool)$this->query( $sql, $fname );
1163 * Determines how long the server has been up
1165 * @return int
1167 public function getServerUptime() {
1168 $vars = $this->getMysqlStatus( 'Uptime' );
1170 return (int)$vars['Uptime'];
1174 * Determines if the last failure was due to a deadlock
1176 * @return bool
1178 public function wasDeadlock() {
1179 return $this->lastErrno() == 1213;
1183 * Determines if the last failure was due to a lock timeout
1185 * @return bool
1187 public function wasLockTimeout() {
1188 return $this->lastErrno() == 1205;
1191 public function wasErrorReissuable() {
1192 return $this->lastErrno() == 2013 || $this->lastErrno() == 2006;
1196 * Determines if the last failure was due to the database being read-only.
1198 * @return bool
1200 public function wasReadOnlyError() {
1201 return $this->lastErrno() == 1223 ||
1202 ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
1205 public function wasConnectionError( $errno ) {
1206 return $errno == 2013 || $errno == 2006;
1210 * @param string $oldName
1211 * @param string $newName
1212 * @param bool $temporary
1213 * @param string $fname
1214 * @return bool
1216 public function duplicateTableStructure(
1217 $oldName, $newName, $temporary = false, $fname = __METHOD__
1219 $tmp = $temporary ? 'TEMPORARY ' : '';
1220 $newName = $this->addIdentifierQuotes( $newName );
1221 $oldName = $this->addIdentifierQuotes( $oldName );
1222 $query = "CREATE $tmp TABLE $newName (LIKE $oldName)";
1224 return $this->query( $query, $fname );
1228 * List all tables on the database
1230 * @param string $prefix Only show tables with this prefix, e.g. mw_
1231 * @param string $fname Calling function name
1232 * @return array
1234 public function listTables( $prefix = null, $fname = __METHOD__ ) {
1235 $result = $this->query( "SHOW TABLES", $fname );
1237 $endArray = [];
1239 foreach ( $result as $table ) {
1240 $vars = get_object_vars( $table );
1241 $table = array_pop( $vars );
1243 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
1244 $endArray[] = $table;
1248 return $endArray;
1252 * @param string $tableName
1253 * @param string $fName
1254 * @return bool|ResultWrapper
1256 public function dropTable( $tableName, $fName = __METHOD__ ) {
1257 if ( !$this->tableExists( $tableName, $fName ) ) {
1258 return false;
1261 return $this->query( "DROP TABLE IF EXISTS " . $this->tableName( $tableName ), $fName );
1265 * Get status information from SHOW STATUS in an associative array
1267 * @param string $which
1268 * @return array
1270 private function getMysqlStatus( $which = "%" ) {
1271 $res = $this->query( "SHOW STATUS LIKE '{$which}'" );
1272 $status = [];
1274 foreach ( $res as $row ) {
1275 $status[$row->Variable_name] = $row->Value;
1278 return $status;
1282 * Lists VIEWs in the database
1284 * @param string $prefix Only show VIEWs with this prefix, eg.
1285 * unit_test_, or $wgDBprefix. Default: null, would return all views.
1286 * @param string $fname Name of calling function
1287 * @return array
1288 * @since 1.22
1290 public function listViews( $prefix = null, $fname = __METHOD__ ) {
1291 // The name of the column containing the name of the VIEW
1292 $propertyName = 'Tables_in_' . $this->mDBname;
1294 // Query for the VIEWS
1295 $res = $this->query( 'SHOW FULL TABLES WHERE TABLE_TYPE = "VIEW"' );
1296 $allViews = [];
1297 foreach ( $res as $row ) {
1298 array_push( $allViews, $row->$propertyName );
1301 if ( is_null( $prefix ) || $prefix === '' ) {
1302 return $allViews;
1305 $filteredViews = [];
1306 foreach ( $allViews as $viewName ) {
1307 // Does the name of this VIEW start with the table-prefix?
1308 if ( strpos( $viewName, $prefix ) === 0 ) {
1309 array_push( $filteredViews, $viewName );
1313 return $filteredViews;
1317 * Differentiates between a TABLE and a VIEW.
1319 * @param string $name Name of the TABLE/VIEW to test
1320 * @param string $prefix
1321 * @return bool
1322 * @since 1.22
1324 public function isView( $name, $prefix = null ) {
1325 return in_array( $name, $this->listViews( $prefix ) );