85 foreach ( [
'KeyPath',
'CertPath',
'CAFile',
'CAPath',
'Ciphers' ] as $name ) {
87 if ( isset( $params[$var] ) ) {
88 $this->$var = $params[$var];
91 $this->utf8Mode = !empty( $params[
'utf8Mode'] );
92 parent::__construct( $params );
100 $params[
'topologyRole'],
103 $params[
'lagDetectionMethod'] ??
'Seconds_Behind_Master',
104 $params[
'lagDetectionOptions'] ?? [],
105 !empty( $params[
'useGTIDs' ] )
116 protected function open( $server, $user, $password, $db, $schema, $tablePrefix ) {
117 $this->
close( __METHOD__ );
119 if ( $schema !==
null ) {
125 $this->conn = $this->
mysqlConnect( $server, $user, $password, $db );
126 }
catch ( RuntimeException $e ) {
132 if ( !$this->conn ) {
138 $db && strlen( $db ) ? $db :
null,
142 $this->platform->setPrefix( $tablePrefix );
145 if ( !$this->flagsHolder->getFlag( self::DBO_GAUGE ) ) {
147 $set[] =
'group_concat_max_len = 262144';
150 foreach ( $this->connectionVariables as $var => $val ) {
152 if ( !is_int( $val ) && !is_float( $val ) ) {
155 $set[] = $this->platform->addIdentifierQuotes( $var ) .
' = ' . $val;
160 $sql =
'SET ' . implode(
', ', $set );
161 $flags = self::QUERY_NO_RETRY | self::QUERY_CHANGE_TRX;
162 $query =
new Query( $sql, $flags,
'SET' );
165 $qs = $this->
executeQuery( $query, __METHOD__, $flags );
166 if ( $qs->res ===
false ) {
170 }
catch ( RuntimeException $e ) {
179 __CLASS__ .
": domain '{$domain->getId()}' has a schema component"
185 if ( $database ===
null ) {
187 $this->currentDomain->getDatabase(),
196 if ( $database !== $this->
getDBname() ) {
198 $query =
new Query( $sql, self::QUERY_CHANGE_TRX,
'USE' );
199 $qs = $this->
executeQuery( $query, __METHOD__, self::QUERY_CHANGE_TRX );
200 if ( $qs->res ===
false ) {
207 $this->currentDomain = $domain;
223 abstract protected function mysqlConnect( $server, $user, $password, $db );
251 $row = $this->replicationReporter->getReplicationSafetyInfo( $this );
253 if ( $row->binlog_format ===
'ROW' ) {
257 if ( isset( $selectOptions[
'LIMIT'] ) ) {
268 in_array(
'NO_AUTO_COLUMNS', $insertOptions ) ||
269 (
int)$row->innodb_autoinc_lock_mode === 0
294 $conds = $this->platform->normalizeConditions( $conds, $fname );
295 $column = $this->platform->extractSingleFieldFromList( $var );
296 if ( is_string( $column ) && !in_array( $column, [
'*',
'1' ] ) ) {
297 $conds[] =
"$column IS NOT NULL";
300 $options[
'EXPLAIN'] =
true;
301 $res = $this->
select( $tables, $var, $conds, $fname, $options, $join_conds );
302 if (
$res ===
false ) {
305 if ( !
$res->numRows() ) {
310 foreach (
$res as $plan ) {
311 $rows *= $plan->rows > 0 ? $plan->rows : 1;
320 [ $database, , $prefix, $table ] = $this->platform->qualifiedTableComponents( $table );
321 $tableName =
"{$prefix}{$table}";
323 if ( isset( $this->sessionTempTables[$tableName] ) ) {
330 $encLike = $this->platform->escapeLikeInternal( $tableName,
'\\' );
333 if ( $database !==
'' ) {
334 $encDatabase = $this->platform->addIdentifierQuotes( $database );
335 $sql =
"SHOW TABLES FROM $encDatabase LIKE '$encLike'";
337 $sql =
"SHOW TABLES LIKE '$encLike'";
340 $query =
new Query( $sql, self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SHOW', $table );
343 return $res->numRows() > 0;
353 "SELECT * FROM " . $this->
tableName( $table ) .
" LIMIT 1",
354 self::QUERY_SILENCE_ERRORS | self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
358 $res = $this->
query( $query, __METHOD__ );
363 '@phan-var MysqliResultWrapper $res';
364 return $res->getInternalFieldInfo( $field );
376 public function indexInfo( $table, $index, $fname = __METHOD__ ) {
378 $index = $this->platform->indexName( $index );
380 'SHOW INDEX FROM ' . $this->
tableName( $table ),
381 self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
393 foreach (
$res as $row ) {
394 if ( $row->Key_name == $index ) {
399 return $result ?:
false;
420 $flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
421 $query =
new Query(
"SELECT @@GLOBAL.read_only AS Value", $flags,
'SELECT' );
422 $res = $this->
query( $query, __METHOD__ );
423 $row =
$res->fetchObject();
425 return $row && (bool)$row->Value;
433 if ( $variant ===
'MariaDB' ) {
434 return '[{{int:version-db-mariadb-url}} MariaDB]';
437 return '[{{int:version-db-mysql-url}} MySQL]';
450 $parts = explode(
'-', $version, 2 );
452 $suffix = $parts[1] ??
'';
453 if ( strpos( $suffix,
'MariaDB' ) !==
false || strpos( $suffix,
'-maria-' ) !==
false ) {
459 return [ $vendor, $number ];
470 $cache->makeGlobalKey(
'mysql-server-version', $this->getServerName() ),
472 function () use ( $fname ) {
476 return $this->
selectField(
'',
'VERSION()',
'', $fname );
485 $sqlAssignments = [];
487 if ( isset( $options[
'connTimeout'] ) ) {
488 $encTimeout = (int)$options[
'connTimeout'];
489 $sqlAssignments[] =
"net_read_timeout=$encTimeout";
490 $sqlAssignments[] =
"net_write_timeout=$encTimeout";
492 if ( isset( $options[
'groupConcatMaxLen'] ) ) {
493 $maxLength = (int)$options[
'groupConcatMaxLen'];
494 $sqlAssignments[] =
"group_concat_max_len=$maxLength";
497 if ( $sqlAssignments ) {
499 'SET ' . implode(
', ', $sqlAssignments ),
500 self::QUERY_CHANGE_TRX | self::QUERY_CHANGE_NONE,
503 $this->
query( $query, __METHOD__ );
513 if ( preg_match(
'/^DELIMITER\s+(\S+)/i', $newLine, $m ) ) {
514 $this->delimiter = $m[1];
518 return parent::streamStatementEnd( $sql, $newLine );
522 $query =
new Query( $this->platform->lockIsFreeSQLText( $lockName ), self::QUERY_CHANGE_LOCKS,
'SELECT' );
524 $row =
$res->fetchObject();
526 return ( $row->unlocked == 1 );
529 public function doLock(
string $lockName,
string $method,
int $timeout ) {
530 $query =
new Query( $this->platform->lockSQLText( $lockName, $timeout ), self::QUERY_CHANGE_LOCKS,
'SELECT' );
532 $row =
$res->fetchObject();
534 return ( $row->acquired !==
null ) ? (float)$row->acquired :
null;
537 public function doUnlock(
string $lockName,
string $method ) {
538 $query =
new Query( $this->platform->unlockSQLText( $lockName ), self::QUERY_CHANGE_LOCKS,
'SELECT' );
540 $row =
$res->fetchObject();
542 return ( $row->released == 1 );
552 $releaseLockFields = [];
553 foreach ( $this->sessionNamedLocks as $name => $info ) {
554 $encName = $this->
addQuotes( $this->platform->makeLockName( $name ) );
555 $releaseLockFields[] =
"RELEASE_LOCK($encName)";
557 if ( $releaseLockFields ) {
558 $sql =
'SELECT ' . implode(
',', $releaseLockFields );
559 $flags = self::QUERY_CHANGE_LOCKS | self::QUERY_NO_RETRY;
560 $query =
new Query( $sql, $flags,
'SELECT' );
561 $qs = $this->
executeQuery( $query, __METHOD__, $flags );
562 if ( $qs->res ===
false ) {
576 [ $sqlColumns, $sqlTuples ] = $this->platform->makeInsertLists( $rows );
583 "INSERT INTO $encTable " .
584 "($sqlColumns) VALUES $sqlTuples " .
585 "ON DUPLICATE KEY UPDATE $sqlColumnAssignments";
586 $query =
new Query( $sql, self::QUERY_CHANGE_ROWS,
'INSERT', $table );
587 $this->
query( $query, $fname );
590 protected function doReplace( $table, array $identityKey, array $rows, $fname ) {
592 [ $sqlColumns, $sqlTuples ] = $this->platform->makeInsertLists( $rows );
594 $sql =
"REPLACE INTO $encTable ($sqlColumns) VALUES $sqlTuples";
597 $query =
new Query( $sql, self::QUERY_CHANGE_ROWS,
'REPLACE', $table );
598 $this->
query( $query, $fname );
617 ( $this->
lastErrno() == 1290 && strpos( $this->
lastError(),
'--read-only' ) !== false );
624 return in_array( $errno, [ 2013, 2006, 2003, 1927, 1053 ],
true );
631 return in_array( $errno, [ 3024, 2062, 1969, 1028 ],
true );
639 [ 3024, 1969, 1022, 1062, 1216, 1217, 1137, 1146, 1051, 1054 ],
652 $oldName, $newName, $temporary =
false, $fname = __METHOD__
654 $tmp = $temporary ?
'TEMPORARY ' :
'';
659 "CREATE $tmp TABLE $newNameQuoted (LIKE $oldNameQuoted)",
660 self::QUERY_PSEUDO_PERMANENT | self::QUERY_CHANGE_SCHEMA,
662 [ $oldName, $newName ]
664 return $this->
query( $query, $fname );
674 public function listTables( $prefix =
null, $fname = __METHOD__ ) {
675 $query =
new Query(
"SHOW TABLES", self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SHOW' );
676 $result = $this->
query( $query, $fname );
680 foreach ( $result as $table ) {
681 $vars = get_object_vars( $table );
682 $table = array_pop( $vars );
684 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
685 $endArray[] = $table;
701 public function listViews( $prefix =
null, $fname = __METHOD__ ) {
703 $propertyName =
'Tables_in_' . $this->
getDBname();
705 'SHOW FULL TABLES WHERE TABLE_TYPE = "VIEW"',
706 self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
713 foreach (
$res as $row ) {
714 $allViews[] = $row->$propertyName;
717 if ( $prefix ===
null || $prefix ===
'' ) {
722 foreach ( $allViews as $viewName ) {
724 if ( strpos( $viewName, $prefix ) === 0 ) {
725 $filteredViews[] = $viewName;
729 return $filteredViews;
740 $sql = parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
743 $timeoutMsec = intval( $options[
'MAX_EXECUTION_TIME'] ?? 0 );
744 if ( $timeoutMsec > 0 ) {
746 if ( $vendor ===
'MariaDB' && version_compare( $number,
'10.1.2',
'>=' ) ) {
747 $timeoutSec = $timeoutMsec / 1000;
748 $sql =
"SET STATEMENT max_statement_time=$timeoutSec FOR $sql";
749 } elseif ( $vendor ===
'MySQL' && version_compare( $number,
'5.7.0',
'>=' ) ) {
752 "SELECT /*+ MAX_EXECUTION_TIME($timeoutMsec)*/",
765 class_alias( DatabaseMysqlBase::class,
'DatabaseMysqlBase' );
getWithSetCallback( $key, $exptime, $callback, $flags=0)
Get an item, regenerating and setting it if not found.
Class to handle database/schema/prefix specifications for IDatabase.