87 foreach ( [
'KeyPath',
'CertPath',
'CAFile',
'CAPath',
'Ciphers' ] as $name ) {
89 if ( isset( $params[$var] ) ) {
90 $this->$var = $params[$var];
93 $this->utf8Mode = !empty( $params[
'utf8Mode'] );
94 parent::__construct( $params );
102 $params[
'topologyRole'],
105 $params[
'lagDetectionMethod'] ??
'Seconds_Behind_Master',
106 $params[
'lagDetectionOptions'] ?? [],
107 !empty( $params[
'useGTIDs' ] )
118 protected function open( $server, $user, $password, $db, $schema, $tablePrefix ) {
119 $this->
close( __METHOD__ );
121 if ( $schema !==
null ) {
127 $this->conn = $this->
mysqlConnect( $server, $user, $password, $db );
128 }
catch ( RuntimeException $e ) {
134 if ( !$this->conn ) {
140 $db && strlen( $db ) ? $db :
null,
144 $this->platform->setPrefix( $tablePrefix );
146 $set = [
'group_concat_max_len = 262144' ];
149 foreach ( $this->connectionVariables as $var => $val ) {
151 if ( !is_int( $val ) && !is_float( $val ) ) {
154 $set[] = $this->platform->addIdentifierQuotes( $var ) .
' = ' . $val;
160 $sql =
'SET ' . implode(
', ', $set );
161 $flags = self::QUERY_NO_RETRY | self::QUERY_CHANGE_TRX;
164 $qs = $this->
executeQuery( $sql, __METHOD__, $flags, $sql );
165 if ( $qs->res ===
false ) {
169 }
catch ( RuntimeException $e ) {
178 __CLASS__ .
": domain '{$domain->getId()}' has a schema component"
184 if ( $database ===
null ) {
186 $this->currentDomain->getDatabase(),
195 if ( $database !== $this->
getDBname() ) {
197 $qs = $this->
executeQuery( $sql, __METHOD__, self::QUERY_IGNORE_DBO_TRX, $sql );
198 if ( $qs->res ===
false ) {
205 $this->currentDomain = $domain;
221 abstract protected function mysqlConnect( $server, $user, $password, $db );
249 $row = $this->replicationReporter->getReplicationSafetyInfo( $this );
251 if ( $row->binlog_format ===
'ROW' ) {
255 if ( isset( $selectOptions[
'LIMIT'] ) ) {
266 in_array(
'NO_AUTO_COLUMNS', $insertOptions ) ||
267 (
int)$row->innodb_autoinc_lock_mode === 0
292 $conds = $this->platform->normalizeConditions( $conds, $fname );
293 $column = $this->platform->extractSingleFieldFromList( $var );
294 if ( is_string( $column ) && !in_array( $column, [
'*',
'1' ] ) ) {
295 $conds[] =
"$column IS NOT NULL";
298 $options[
'EXPLAIN'] =
true;
299 $res = $this->
select( $tables, $var, $conds, $fname, $options, $join_conds );
300 if (
$res ===
false ) {
303 if ( !
$res->numRows() ) {
308 foreach (
$res as $plan ) {
309 $rows *= $plan->rows > 0 ? $plan->rows : 1;
318 [ $database, , $prefix, $table ] = $this->platform->qualifiedTableComponents( $table );
319 $tableName =
"{$prefix}{$table}";
321 if ( isset( $this->sessionTempTables[$tableName] ) ) {
328 $encLike = $this->platform->escapeLikeInternal( $tableName,
'\\' );
331 if ( $database !==
'' ) {
332 $encDatabase = $this->platform->addIdentifierQuotes( $database );
333 $sql =
"SHOW TABLES FROM $encDatabase LIKE '$encLike'";
335 $sql =
"SHOW TABLES LIKE '$encLike'";
341 self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
344 return $res->numRows() > 0;
354 "SELECT * FROM " . $this->
tableName( $table ) .
" LIMIT 1",
356 self::QUERY_SILENCE_ERRORS | self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
362 '@phan-var MysqliResultWrapper $res';
363 return $res->getInternalFieldInfo( $field );
375 public function indexInfo( $table, $index, $fname = __METHOD__ ) {
380 'SHOW INDEX FROM ' . $this->
tableName( $table ),
382 self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
391 foreach (
$res as $row ) {
392 if ( $row->Key_name == $index ) {
397 return $result ?:
false;
418 $flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
419 $res = $this->
query(
"SELECT @@GLOBAL.read_only AS Value", __METHOD__, $flags );
420 $row =
$res->fetchObject();
422 return $row ? (bool)$row->Value :
false;
430 if ( $variant ===
'MariaDB' ) {
431 return '[{{int:version-db-mariadb-url}} MariaDB]';
434 return '[{{int:version-db-mysql-url}} MySQL]';
447 $parts = explode(
'-', $version, 2 );
449 $suffix = $parts[1] ??
'';
450 if ( strpos( $suffix,
'MariaDB' ) !==
false || strpos( $suffix,
'-maria-' ) !==
false ) {
456 return [ $vendor, $number ];
467 $cache->makeGlobalKey(
'mysql-server-version', $this->getServerName() ),
469 function () use ( $fname ) {
473 return $this->
selectField(
'',
'VERSION()',
'', $fname );
482 $sqlAssignments = [];
484 if ( isset( $options[
'connTimeout'] ) ) {
485 $encTimeout = (int)$options[
'connTimeout'];
486 $sqlAssignments[] =
"net_read_timeout=$encTimeout";
487 $sqlAssignments[] =
"net_write_timeout=$encTimeout";
490 if ( $sqlAssignments ) {
492 'SET ' . implode(
', ', $sqlAssignments ),
494 self::QUERY_CHANGE_TRX | self::QUERY_CHANGE_NONE
505 if ( preg_match(
'/^DELIMITER\s+(\S+)/i', $newLine, $m ) ) {
506 $this->delimiter = $m[1];
510 return parent::streamStatementEnd( $sql, $newLine );
515 $this->platform->lockIsFreeSQLText( $lockName ),
517 self::QUERY_CHANGE_LOCKS
519 $row =
$res->fetchObject();
521 return ( $row->unlocked == 1 );
524 public function doLock(
string $lockName,
string $method,
int $timeout ) {
526 $this->platform->lockSQLText( $lockName, $timeout ),
528 self::QUERY_CHANGE_LOCKS
530 $row =
$res->fetchObject();
532 return ( $row->acquired !==
null ) ? (float)$row->acquired :
null;
535 public function doUnlock(
string $lockName,
string $method ) {
537 $this->platform->unlockSQLText( $lockName ),
539 self::QUERY_CHANGE_LOCKS
541 $row =
$res->fetchObject();
543 return ( $row->released == 1 );
551 $flags = self::QUERY_CHANGE_LOCKS | self::QUERY_NO_RETRY;
554 $releaseLockFields = [];
555 foreach ( $this->sessionNamedLocks as $name => $info ) {
556 $encName = $this->
addQuotes( $this->platform->makeLockName( $name ) );
557 $releaseLockFields[] =
"RELEASE_LOCK($encName)";
559 if ( $releaseLockFields ) {
560 $sql =
'SELECT ' . implode(
',', $releaseLockFields );
561 $qs = $this->
executeQuery( $sql, __METHOD__, $flags, $sql );
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";
587 $this->
query( $sql, $fname, self::QUERY_CHANGE_ROWS );
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";
596 $this->
query( $sql, $fname, self::QUERY_CHANGE_ROWS );
624 ( $this->
lastErrno() == 1290 && strpos( $this->
lastError(),
'--read-only' ) !== false );
631 return in_array( $errno, [ 2013, 2006, 2003, 1927, 1053 ],
true );
638 return in_array( $errno, [ 3024, 2062, 1969, 1028 ],
true );
646 [ 3024, 1969, 1022, 1062, 1216, 1217, 1137, 1146, 1051, 1054 ],
659 $oldName, $newName, $temporary =
false, $fname = __METHOD__
661 $tmp = $temporary ?
'TEMPORARY ' :
'';
666 "CREATE $tmp TABLE $newName (LIKE $oldName)",
668 self::QUERY_PSEUDO_PERMANENT | self::QUERY_CHANGE_SCHEMA
679 public function listTables( $prefix =
null, $fname = __METHOD__ ) {
680 $result = $this->
query(
683 self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
688 foreach ( $result as $table ) {
689 $vars = get_object_vars( $table );
690 $table = array_pop( $vars );
692 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
693 $endArray[] = $table;
709 public function listViews( $prefix =
null, $fname = __METHOD__ ) {
711 $propertyName =
'Tables_in_' . $this->
getDBname();
715 'SHOW FULL TABLES WHERE TABLE_TYPE = "VIEW"',
717 self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
721 foreach (
$res as $row ) {
722 array_push( $allViews, $row->$propertyName );
725 if ( $prefix ===
null || $prefix ===
'' ) {
730 foreach ( $allViews as $viewName ) {
732 if ( strpos( $viewName, $prefix ) === 0 ) {
733 array_push( $filteredViews, $viewName );
737 return $filteredViews;
748 public function isView( $name, $prefix =
null ) {
749 return in_array( $name, $this->
listViews( $prefix, __METHOD__ ) );
760 $sql = parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
763 $timeoutMsec = intval( $options[
'MAX_EXECUTION_TIME'] ?? 0 );
764 if ( $timeoutMsec > 0 ) {
766 if ( $vendor ===
'MariaDB' && version_compare( $number,
'10.1.2',
'>=' ) ) {
767 $timeoutSec = $timeoutMsec / 1000;
768 $sql =
"SET STATEMENT max_statement_time=$timeoutSec FOR $sql";
769 } elseif ( $vendor ===
'MySQL' && version_compare( $number,
'5.7.0',
'>=' ) ) {
772 "SELECT /*+ MAX_EXECUTION_TIME($timeoutMsec)*/",
785 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.
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s