| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\Rdbms; |
| 21 |
|
| 22 |
use mysqli; |
| 23 |
use mysqli_result; |
| 24 |
use RuntimeException; |
| 25 |
use Wikimedia\AtEase\AtEase; |
| 26 |
use Wikimedia\IPUtils; |
| 27 |
use Wikimedia\Rdbms\Platform\MySQLPlatform; |
| 28 |
use Wikimedia\Rdbms\Platform\SQLPlatform; |
| 29 |
use Wikimedia\Rdbms\Replication\MysqlReplicationReporter; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class DatabaseMySQL extends Database { |
| 43 |
|
| 44 |
private $sslKeyPath; |
| 45 |
|
| 46 |
private $sslCertPath; |
| 47 |
|
| 48 |
private $sslCAFile; |
| 49 |
|
| 50 |
private $sslCAPath; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private $sslCiphers; |
| 57 |
|
| 58 |
private $utf8Mode; |
| 59 |
|
| 60 |
|
| 61 |
protected $platform; |
| 62 |
|
| 63 |
|
| 64 |
protected $replicationReporter; |
| 65 |
|
| 66 |
private $sessionLastAutoRowId; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function __construct( array $params ) { |
| 88 |
foreach ( [ 'KeyPath', 'CertPath', 'CAFile', 'CAPath', 'Ciphers' ] as $name ) { |
| 89 |
$var = "ssl{$name}"; |
| 90 |
if ( isset( $params[$var] ) ) { |
| 91 |
$this->$var = $params[$var]; |
| 92 |
} |
| 93 |
} |
| 94 |
$this->utf8Mode = !empty( $params['utf8Mode'] ); |
| 95 |
parent::__construct( $params ); |
| 96 |
$this->platform = new MySQLPlatform( |
| 97 |
$this, |
| 98 |
$this->logger, |
| 99 |
$this->currentDomain, |
| 100 |
$this->errorLogger |
| 101 |
); |
| 102 |
$this->replicationReporter = new MysqlReplicationReporter( |
| 103 |
$params['topologyRole'], |
| 104 |
$this->logger, |
| 105 |
$params['srvCache'], |
| 106 |
$params['lagDetectionMethod'] ?? 'Seconds_Behind_Master', |
| 107 |
$params['lagDetectionOptions'] ?? [], |
| 108 |
!empty( $params['useGTIDs' ] ) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function getType() { |
| 116 |
return 'mysql'; |
| 117 |
} |
| 118 |
|
| 119 |
protected function open( $server, $user, $password, $db, $schema, $tablePrefix ) { |
| 120 |
$this->close( __METHOD__ ); |
| 121 |
|
| 122 |
if ( $schema !== null ) { |
| 123 |
throw $this->newExceptionAfterConnectError( "Got schema '$schema'; not supported." ); |
| 124 |
} |
| 125 |
|
| 126 |
$this->installErrorHandler(); |
| 127 |
try { |
| 128 |
$this->conn = $this->mysqlConnect( $server, $user, $password, $db ); |
| 129 |
} catch ( RuntimeException $e ) { |
| 130 |
$this->restoreErrorHandler(); |
| 131 |
throw $this->newExceptionAfterConnectError( $e->getMessage() ); |
| 132 |
} |
| 133 |
$error = $this->restoreErrorHandler(); |
| 134 |
|
| 135 |
if ( !$this->conn ) { |
| 136 |
throw $this->newExceptionAfterConnectError( $error ?: $this->lastError() ); |
| 137 |
} |
| 138 |
|
| 139 |
try { |
| 140 |
$this->currentDomain = new DatabaseDomain( |
| 141 |
( $db !== '' ) ? $db : null, |
| 142 |
null, |
| 143 |
$tablePrefix |
| 144 |
); |
| 145 |
$this->platform->setCurrentDomain( $this->currentDomain ); |
| 146 |
|
| 147 |
$set = []; |
| 148 |
if ( !$this->flagsHolder->getFlag( self::DBO_GAUGE ) ) { |
| 149 |
|
| 150 |
$set[] = 'group_concat_max_len = 262144'; |
| 151 |
|
| 152 |
|
| 153 |
foreach ( $this->connectionVariables as $var => $val ) { |
| 154 |
|
| 155 |
if ( !is_int( $val ) && !is_float( $val ) ) { |
| 156 |
$val = $this->addQuotes( $val ); |
| 157 |
} |
| 158 |
$set[] = $this->platform->addIdentifierQuotes( $var ) . ' = ' . $val; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if ( $set ) { |
| 163 |
$sql = 'SET ' . implode( ', ', $set ); |
| 164 |
$flags = self::QUERY_NO_RETRY | self::QUERY_CHANGE_TRX; |
| 165 |
$query = new Query( $sql, $flags, 'SET' ); |
| 166 |
|
| 167 |
|
| 168 |
$qs = $this->executeQuery( $query, __METHOD__, $flags ); |
| 169 |
if ( $qs->res === false ) { |
| 170 |
$this->reportQueryError( $qs->message, $qs->code, $sql, __METHOD__ ); |
| 171 |
} |
| 172 |
} |
| 173 |
} catch ( RuntimeException $e ) { |
| 174 |
throw $this->newExceptionAfterConnectError( $e->getMessage() ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
protected function doSelectDomain( DatabaseDomain $domain ) { |
| 179 |
if ( $domain->getSchema() !== null ) { |
| 180 |
throw new DBExpectedError( |
| 181 |
$this, |
| 182 |
__CLASS__ . ": domain '{$domain->getId()}' has a schema component" |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
$database = $domain->getDatabase(); |
| 187 |
|
| 188 |
if ( $database === null ) { |
| 189 |
$this->currentDomain = new DatabaseDomain( |
| 190 |
$this->currentDomain->getDatabase(), |
| 191 |
null, |
| 192 |
$domain->getTablePrefix() |
| 193 |
); |
| 194 |
$this->platform->setCurrentDomain( $this->currentDomain ); |
| 195 |
|
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
if ( $database !== $this->getDBname() ) { |
| 200 |
$sql = 'USE ' . $this->addIdentifierQuotes( $database ); |
| 201 |
$query = new Query( $sql, self::QUERY_CHANGE_TRX, 'USE' ); |
| 202 |
$qs = $this->executeQuery( $query, __METHOD__, self::QUERY_CHANGE_TRX ); |
| 203 |
if ( $qs->res === false ) { |
| 204 |
$this->reportQueryError( $qs->message, $qs->code, $sql, __METHOD__ ); |
| 205 |
return false; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
$this->currentDomain = $domain; |
| 211 |
$this->platform->setCurrentDomain( $domain ); |
| 212 |
|
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function lastError() { |
| 220 |
if ( $this->conn ) { |
| 221 |
|
| 222 |
$error = $this->mysqlError( $this->conn ); |
| 223 |
if ( !$error ) { |
| 224 |
$error = $this->mysqlError(); |
| 225 |
} |
| 226 |
} else { |
| 227 |
$error = $this->mysqlError() ?: $this->lastConnectError; |
| 228 |
} |
| 229 |
|
| 230 |
return $error; |
| 231 |
} |
| 232 |
|
| 233 |
protected function isInsertSelectSafe( array $insertOptions, array $selectOptions, $fname ) { |
| 234 |
$row = $this->replicationReporter->getReplicationSafetyInfo( $this, $fname ); |
| 235 |
|
| 236 |
if ( $row->binlog_format === 'ROW' ) { |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
if ( isset( $selectOptions['LIMIT'] ) ) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
return ( |
| 251 |
in_array( 'NO_AUTO_COLUMNS', $insertOptions ) || |
| 252 |
(int)$row->innodb_autoinc_lock_mode === 0 |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
protected function checkInsertWarnings( Query $query, $fname ) { |
| 257 |
if ( $this->conn && $this->conn->warning_count ) { |
| 258 |
|
| 259 |
$warnings = $this->conn->get_warnings(); |
| 260 |
$done = $warnings === false; |
| 261 |
while ( !$done ) { |
| 262 |
if ( in_array( $warnings->errno, [ |
| 263 |
|
| 264 |
1048, |
| 265 |
1526, |
| 266 |
1748, |
| 267 |
1242, |
| 268 |
1369, |
| 269 |
|
| 270 |
1264, |
| 271 |
1265, |
| 272 |
] ) ) { |
| 273 |
$this->reportQueryError( |
| 274 |
'Insert returned unacceptable warning: ' . $warnings->message, |
| 275 |
$warnings->errno, |
| 276 |
$query->getSQL(), |
| 277 |
$fname |
| 278 |
); |
| 279 |
} |
| 280 |
$done = !$warnings->next(); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
public function estimateRowCount( |
| 286 |
$tables, |
| 287 |
$var = '*', |
| 288 |
$conds = '', |
| 289 |
$fname = __METHOD__, |
| 290 |
$options = [], |
| 291 |
$join_conds = [] |
| 292 |
): int { |
| 293 |
$conds = $this->platform->normalizeConditions( $conds, $fname ); |
| 294 |
$column = $this->platform->extractSingleFieldFromList( $var ); |
| 295 |
if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) { |
| 296 |
$conds[] = "$column IS NOT NULL"; |
| 297 |
} |
| 298 |
|
| 299 |
$options['EXPLAIN'] = true; |
| 300 |
$res = $this->select( $tables, $var, $conds, $fname, $options, $join_conds ); |
| 301 |
if ( $res === false ) { |
| 302 |
return -1; |
| 303 |
} |
| 304 |
if ( !$res->numRows() ) { |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
$rows = 1; |
| 309 |
foreach ( $res as $plan ) { |
| 310 |
$rows *= $plan->rows > 0 ? $plan->rows : 1; |
| 311 |
} |
| 312 |
|
| 313 |
return (int)$rows; |
| 314 |
} |
| 315 |
|
| 316 |
public function tableExists( $table, $fname = __METHOD__ ) { |
| 317 |
[ $db, $pt ] = $this->platform->getDatabaseAndTableIdentifier( $table ); |
| 318 |
if ( isset( $this->sessionTempTables[$db][$pt] ) ) { |
| 319 |
return true; |
| 320 |
} |
| 321 |
|
| 322 |
return (bool)$this->newSelectQueryBuilder() |
| 323 |
->select( '1' ) |
| 324 |
->from( 'information_schema.tables' ) |
| 325 |
->where( [ |
| 326 |
'table_schema' => $db, |
| 327 |
'table_name' => $pt, |
| 328 |
] ) |
| 329 |
->caller( $fname ) |
| 330 |
->fetchField(); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public function fieldInfo( $table, $field ) { |
| 339 |
$query = new Query( |
| 340 |
"SELECT * FROM " . $this->tableName( $table ) . " LIMIT 1", |
| 341 |
self::QUERY_SILENCE_ERRORS | self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE, |
| 342 |
'SELECT' |
| 343 |
); |
| 344 |
$res = $this->query( $query, __METHOD__ ); |
| 345 |
if ( !$res ) { |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
'@phan-var MysqliResultWrapper $res'; |
| 350 |
return $res->getInternalFieldInfo( $field ); |
| 351 |
} |
| 352 |
|
| 353 |
public function indexInfo( $table, $index, $fname = __METHOD__ ) { |
| 354 |
|
| 355 |
$index = $this->platform->indexName( $index ); |
| 356 |
$query = new Query( |
| 357 |
'SHOW INDEX FROM ' . $this->tableName( $table ), |
| 358 |
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE, |
| 359 |
'SHOW' |
| 360 |
); |
| 361 |
$res = $this->query( $query, $fname ); |
| 362 |
|
| 363 |
foreach ( $res as $row ) { |
| 364 |
if ( $row->Key_name === $index ) { |
| 365 |
return [ 'unique' => !$row->Non_unique ]; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public function strencode( $s ) { |
| 377 |
return $this->mysqlRealEscapeString( $s ); |
| 378 |
} |
| 379 |
|
| 380 |
public function serverIsReadOnly() { |
| 381 |
|
| 382 |
$flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE; |
| 383 |
$query = new Query( "SELECT @@GLOBAL.read_only AS Value", $flags, 'SELECT' ); |
| 384 |
$res = $this->query( $query, __METHOD__ ); |
| 385 |
$row = $res->fetchObject(); |
| 386 |
|
| 387 |
return $row && $row->Value && $row->Value !== 'OFF'; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public function getSoftwareLink() { |
| 394 |
[ $variant ] = $this->getMySqlServerVariant(); |
| 395 |
if ( $variant === 'MariaDB' ) { |
| 396 |
return '[{{int:version-db-mariadb-url}} MariaDB]'; |
| 397 |
} |
| 398 |
|
| 399 |
return '[{{int:version-db-mysql-url}} MySQL]'; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
private function getMySqlServerVariant() { |
| 406 |
$version = $this->getServerVersion(); |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
$parts = explode( '-', $version, 2 ); |
| 413 |
$number = $parts[0]; |
| 414 |
$suffix = $parts[1] ?? ''; |
| 415 |
if ( strpos( $suffix, 'MariaDB' ) !== false || strpos( $suffix, '-maria-' ) !== false ) { |
| 416 |
$vendor = 'MariaDB'; |
| 417 |
} else { |
| 418 |
$vendor = 'MySQL'; |
| 419 |
} |
| 420 |
|
| 421 |
return [ $vendor, $number ]; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public function getServerVersion() { |
| 428 |
|
| 429 |
|
| 430 |
$version = $this->conn->server_info; |
| 431 |
if ( |
| 432 |
str_starts_with( $version, '5.5.5-' ) && |
| 433 |
( str_contains( $version, 'MariaDB' ) || str_contains( $version, '-maria-' ) ) |
| 434 |
) { |
| 435 |
$version = substr( $version, strlen( '5.5.5-' ) ); |
| 436 |
} |
| 437 |
return $version; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public function setSessionOptions( array $options ) { |
| 444 |
$sqlAssignments = []; |
| 445 |
|
| 446 |
if ( isset( $options['connTimeout'] ) ) { |
| 447 |
$encTimeout = (int)$options['connTimeout']; |
| 448 |
$sqlAssignments[] = "net_read_timeout=$encTimeout"; |
| 449 |
$sqlAssignments[] = "net_write_timeout=$encTimeout"; |
| 450 |
} |
| 451 |
if ( isset( $options['groupConcatMaxLen'] ) ) { |
| 452 |
$maxLength = (int)$options['groupConcatMaxLen']; |
| 453 |
$sqlAssignments[] = "group_concat_max_len=$maxLength"; |
| 454 |
} |
| 455 |
|
| 456 |
if ( $sqlAssignments ) { |
| 457 |
$query = new Query( |
| 458 |
'SET ' . implode( ', ', $sqlAssignments ), |
| 459 |
self::QUERY_CHANGE_TRX | self::QUERY_CHANGE_NONE, |
| 460 |
'SET' |
| 461 |
); |
| 462 |
$this->query( $query, __METHOD__ ); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
public function streamStatementEnd( &$sql, &$newLine ) { |
| 472 |
if ( preg_match( '/^DELIMITER\s+(\S+)/i', $newLine, $m ) ) { |
| 473 |
$this->delimiter = $m[1]; |
| 474 |
$newLine = ''; |
| 475 |
} |
| 476 |
|
| 477 |
return parent::streamStatementEnd( $sql, $newLine ); |
| 478 |
} |
| 479 |
|
| 480 |
public function doLockIsFree( string $lockName, string $method ) { |
| 481 |
$query = new Query( $this->platform->lockIsFreeSQLText( $lockName ), self::QUERY_CHANGE_LOCKS, 'SELECT' ); |
| 482 |
$res = $this->query( $query, $method ); |
| 483 |
$row = $res->fetchObject(); |
| 484 |
|
| 485 |
return ( $row->unlocked == 1 ); |
| 486 |
} |
| 487 |
|
| 488 |
public function doLock( string $lockName, string $method, int $timeout ) { |
| 489 |
$query = new Query( $this->platform->lockSQLText( $lockName, $timeout ), self::QUERY_CHANGE_LOCKS, 'SELECT' ); |
| 490 |
$res = $this->query( $query, $method ); |
| 491 |
$row = $res->fetchObject(); |
| 492 |
|
| 493 |
return ( $row->acquired !== null ) ? (float)$row->acquired : null; |
| 494 |
} |
| 495 |
|
| 496 |
public function doUnlock( string $lockName, string $method ) { |
| 497 |
$query = new Query( $this->platform->unlockSQLText( $lockName ), self::QUERY_CHANGE_LOCKS, 'SELECT' ); |
| 498 |
$res = $this->query( $query, $method ); |
| 499 |
$row = $res->fetchObject(); |
| 500 |
|
| 501 |
return ( $row->released == 1 ); |
| 502 |
} |
| 503 |
|
| 504 |
protected function doFlushSession( $fname ) { |
| 505 |
|
| 506 |
|
| 507 |
$releaseLockFields = []; |
| 508 |
foreach ( $this->sessionNamedLocks as $name => $info ) { |
| 509 |
$encName = $this->addQuotes( $this->platform->makeLockName( $name ) ); |
| 510 |
$releaseLockFields[] = "RELEASE_LOCK($encName)"; |
| 511 |
} |
| 512 |
if ( $releaseLockFields ) { |
| 513 |
$sql = 'SELECT ' . implode( ',', $releaseLockFields ); |
| 514 |
$flags = self::QUERY_CHANGE_LOCKS | self::QUERY_NO_RETRY; |
| 515 |
$query = new Query( $sql, $flags, 'SELECT' ); |
| 516 |
$qs = $this->executeQuery( $query, __METHOD__, $flags ); |
| 517 |
if ( $qs->res === false ) { |
| 518 |
$this->reportQueryError( $qs->message, $qs->code, $sql, $fname, true ); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
public function upsert( $table, array $rows, $uniqueKeys, array $set, $fname = __METHOD__ ) { |
| 524 |
$identityKey = $this->platform->normalizeUpsertParams( $uniqueKeys, $rows ); |
| 525 |
if ( !$rows ) { |
| 526 |
return; |
| 527 |
} |
| 528 |
$this->platform->assertValidUpsertSetArray( $set, $identityKey, $rows ); |
| 529 |
|
| 530 |
$encTable = $this->tableName( $table ); |
| 531 |
[ $sqlColumns, $sqlTuples ] = $this->platform->makeInsertLists( $rows ); |
| 532 |
$sqlColumnAssignments = $this->makeList( $set, self::LIST_SET ); |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
$sql = |
| 538 |
"INSERT INTO $encTable " . |
| 539 |
"($sqlColumns) VALUES $sqlTuples " . |
| 540 |
"ON DUPLICATE KEY UPDATE $sqlColumnAssignments"; |
| 541 |
$query = new Query( $sql, self::QUERY_CHANGE_ROWS, 'INSERT', $table ); |
| 542 |
$this->query( $query, $fname ); |
| 543 |
|
| 544 |
$this->lastQueryAffectedRows = min( $this->lastQueryAffectedRows, count( $rows ) ); |
| 545 |
} |
| 546 |
|
| 547 |
public function replace( $table, $uniqueKeys, $rows, $fname = __METHOD__ ) { |
| 548 |
$this->platform->normalizeUpsertParams( $uniqueKeys, $rows ); |
| 549 |
if ( !$rows ) { |
| 550 |
return; |
| 551 |
} |
| 552 |
$encTable = $this->tableName( $table ); |
| 553 |
[ $sqlColumns, $sqlTuples ] = $this->platform->makeInsertLists( $rows ); |
| 554 |
|
| 555 |
$sql = "REPLACE INTO $encTable ($sqlColumns) VALUES $sqlTuples"; |
| 556 |
|
| 557 |
|
| 558 |
$query = new Query( $sql, self::QUERY_CHANGE_ROWS, 'REPLACE', $table ); |
| 559 |
$this->query( $query, $fname ); |
| 560 |
|
| 561 |
$this->lastQueryAffectedRows = min( $this->lastQueryAffectedRows, count( $rows ) ); |
| 562 |
} |
| 563 |
|
| 564 |
protected function isConnectionError( $errno ) { |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
return in_array( $errno, [ 2013, 2006, 2003, 1927, 1053 ], true ); |
| 569 |
} |
| 570 |
|
| 571 |
protected function isQueryTimeoutError( $errno ) { |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
return in_array( $errno, [ 3024, 2062, 1969, 1028 ], true ); |
| 576 |
} |
| 577 |
|
| 578 |
protected function isKnownStatementRollbackError( $errno ) { |
| 579 |
|
| 580 |
|
| 581 |
return in_array( |
| 582 |
$errno, |
| 583 |
[ 3024, 1969, 1022, 1062, 1216, 1217, 1137, 1146, 1051, 1054 ], |
| 584 |
true |
| 585 |
); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public function duplicateTableStructure( |
| 596 |
$oldName, $newName, $temporary = false, $fname = __METHOD__ |
| 597 |
) { |
| 598 |
$tmp = $temporary ? 'TEMPORARY ' : ''; |
| 599 |
$newNameQuoted = $this->addIdentifierQuotes( $newName ); |
| 600 |
$oldNameQuoted = $this->addIdentifierQuotes( $oldName ); |
| 601 |
|
| 602 |
$query = new Query( |
| 603 |
"CREATE $tmp TABLE $newNameQuoted (LIKE $oldNameQuoted)", |
| 604 |
self::QUERY_PSEUDO_PERMANENT | self::QUERY_CHANGE_SCHEMA, |
| 605 |
$temporary ? 'CREATE TEMPORARY' : 'CREATE', |
| 606 |
|
| 607 |
'.' . $newName |
| 608 |
); |
| 609 |
return $this->query( $query, $fname ); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
public function listTables( $prefix = null, $fname = __METHOD__ ) { |
| 620 |
$qb = $this->newSelectQueryBuilder() |
| 621 |
->select( 'table_name' ) |
| 622 |
->from( 'information_schema.tables' ) |
| 623 |
->where( [ |
| 624 |
'table_schema' => $this->currentDomain->getDatabase(), |
| 625 |
'table_type' => 'BASE TABLE' |
| 626 |
] ) |
| 627 |
->caller( $fname ); |
| 628 |
if ( $prefix !== null && $prefix !== '' ) { |
| 629 |
$qb->andWhere( $this->expr( |
| 630 |
'table_name', IExpression::LIKE, new LikeValue( $prefix, $this->anyString() ) |
| 631 |
) ); |
| 632 |
} |
| 633 |
return $qb->fetchFieldValues(); |
| 634 |
} |
| 635 |
|
| 636 |
public function selectSQLText( |
| 637 |
$tables, |
| 638 |
$vars, |
| 639 |
$conds = '', |
| 640 |
$fname = __METHOD__, |
| 641 |
$options = [], |
| 642 |
$join_conds = [] |
| 643 |
) { |
| 644 |
$sql = parent::selectSQLText( $tables, $vars, $conds, $fname, $options, $join_conds ); |
| 645 |
|
| 646 |
|
| 647 |
$timeoutMsec = intval( $options['MAX_EXECUTION_TIME'] ?? 0 ); |
| 648 |
if ( $timeoutMsec > 0 ) { |
| 649 |
[ $vendor, $number ] = $this->getMySqlServerVariant(); |
| 650 |
if ( $vendor === 'MariaDB' && version_compare( $number, '10.1.2', '>=' ) ) { |
| 651 |
$timeoutSec = $timeoutMsec / 1000; |
| 652 |
$sql = "SET STATEMENT max_statement_time=$timeoutSec FOR $sql"; |
| 653 |
} elseif ( $vendor === 'MySQL' && version_compare( $number, '5.7.0', '>=' ) ) { |
| 654 |
$sql = preg_replace( |
| 655 |
'/^SELECT(?=\s)/', |
| 656 |
"SELECT /*+ MAX_EXECUTION_TIME($timeoutMsec)*/", |
| 657 |
$sql |
| 658 |
); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
return $sql; |
| 663 |
} |
| 664 |
|
| 665 |
protected function doSingleStatementQuery( string $sql ): QueryStatus { |
| 666 |
$conn = $this->getBindingHandle(); |
| 667 |
|
| 668 |
|
| 669 |
AtEase::suppressWarnings(); |
| 670 |
$res = $conn->query( $sql ); |
| 671 |
AtEase::restoreWarnings(); |
| 672 |
|
| 673 |
$insertId = (int)$conn->insert_id; |
| 674 |
$this->lastQueryInsertId = $insertId; |
| 675 |
$this->sessionLastAutoRowId = $insertId ?: $this->sessionLastAutoRowId; |
| 676 |
|
| 677 |
return new QueryStatus( |
| 678 |
$res instanceof mysqli_result ? new MysqliResultWrapper( $this, $res ) : $res, |
| 679 |
$conn->affected_rows, |
| 680 |
$conn->error, |
| 681 |
$conn->errno |
| 682 |
); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
private function mysqlConnect( $server, $user, $password, $db ) { |
| 694 |
if ( !function_exists( 'mysqli_init' ) ) { |
| 695 |
throw $this->newExceptionAfterConnectError( |
| 696 |
"MySQLi functions missing, have you compiled PHP with the --with-mysqli option?" |
| 697 |
); |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
mysqli_report( MYSQLI_REPORT_OFF ); |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
$port = null; |
| 712 |
$socket = null; |
| 713 |
$hostAndPort = IPUtils::splitHostAndPort( $server ); |
| 714 |
if ( $hostAndPort ) { |
| 715 |
$realServer = $hostAndPort[0]; |
| 716 |
if ( $hostAndPort[1] ) { |
| 717 |
$port = $hostAndPort[1]; |
| 718 |
} |
| 719 |
} elseif ( substr_count( $server, ':/' ) == 1 ) { |
| 720 |
|
| 721 |
|
| 722 |
[ $realServer, $socket ] = explode( ':', $server, 2 ); |
| 723 |
} else { |
| 724 |
$realServer = $server; |
| 725 |
} |
| 726 |
|
| 727 |
$mysqli = mysqli_init(); |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
$flags = MYSQLI_CLIENT_FOUND_ROWS; |
| 732 |
if ( $this->ssl ) { |
| 733 |
$flags |= MYSQLI_CLIENT_SSL; |
| 734 |
$mysqli->ssl_set( |
| 735 |
$this->sslKeyPath, |
| 736 |
$this->sslCertPath, |
| 737 |
$this->sslCAFile, |
| 738 |
$this->sslCAPath, |
| 739 |
$this->sslCiphers |
| 740 |
); |
| 741 |
} |
| 742 |
if ( $this->getFlag( self::DBO_COMPRESS ) ) { |
| 743 |
$flags |= MYSQLI_CLIENT_COMPRESS; |
| 744 |
} |
| 745 |
if ( $this->getFlag( self::DBO_PERSISTENT ) ) { |
| 746 |
$realServer = 'p:' . $realServer; |
| 747 |
} |
| 748 |
|
| 749 |
if ( $this->utf8Mode ) { |
| 750 |
|
| 751 |
|
| 752 |
$mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' ); |
| 753 |
} else { |
| 754 |
$mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' ); |
| 755 |
} |
| 756 |
|
| 757 |
$mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, $this->connectTimeout ?: 3 ); |
| 758 |
if ( $this->receiveTimeout ) { |
| 759 |
$mysqli->options( MYSQLI_OPT_READ_TIMEOUT, $this->receiveTimeout ); |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
$ok = $mysqli->real_connect( $realServer, $user, $password, $db, $port, $socket, $flags ); |
| 764 |
|
| 765 |
return $ok ? $mysqli : null; |
| 766 |
} |
| 767 |
|
| 768 |
protected function closeConnection() { |
| 769 |
return ( $this->conn instanceof mysqli ) ? mysqli_close( $this->conn ) : true; |
| 770 |
} |
| 771 |
|
| 772 |
protected function lastInsertId() { |
| 773 |
return $this->sessionLastAutoRowId; |
| 774 |
} |
| 775 |
|
| 776 |
protected function doHandleSessionLossPreconnect() { |
| 777 |
|
| 778 |
$this->sessionLastAutoRowId = 0; |
| 779 |
} |
| 780 |
|
| 781 |
public function insertId() { |
| 782 |
if ( $this->lastEmulatedInsertId === null ) { |
| 783 |
$conn = $this->getBindingHandle(); |
| 784 |
|
| 785 |
$this->lastEmulatedInsertId = (int)$conn->insert_id; |
| 786 |
} |
| 787 |
|
| 788 |
return $this->lastEmulatedInsertId; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
public function lastErrno() { |
| 795 |
if ( $this->conn instanceof mysqli ) { |
| 796 |
return $this->conn->errno; |
| 797 |
} else { |
| 798 |
return mysqli_connect_errno(); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
private function mysqlError( $conn = null ) { |
| 807 |
if ( $conn === null ) { |
| 808 |
return (string)mysqli_connect_error(); |
| 809 |
} else { |
| 810 |
return $conn->error; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
private function mysqlRealEscapeString( $s ) { |
| 815 |
$conn = $this->getBindingHandle(); |
| 816 |
|
| 817 |
return $conn->real_escape_string( (string)$s ); |
| 818 |
} |
| 819 |
} |