| 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 InvalidArgumentException; |
| 23 |
use LogicException; |
| 24 |
use Psr\Log\LoggerAwareInterface; |
| 25 |
use Psr\Log\LoggerInterface; |
| 26 |
use Psr\Log\NullLogger; |
| 27 |
use RuntimeException; |
| 28 |
use Stringable; |
| 29 |
use Throwable; |
| 30 |
use Wikimedia\AtEase\AtEase; |
| 31 |
use Wikimedia\Rdbms\Database\DatabaseFlags; |
| 32 |
use Wikimedia\Rdbms\Platform\SQLPlatform; |
| 33 |
use Wikimedia\Rdbms\Replication\ReplicationReporter; |
| 34 |
use Wikimedia\RequestTimeout\CriticalSectionProvider; |
| 35 |
use Wikimedia\RequestTimeout\CriticalSectionScope; |
| 36 |
use Wikimedia\ScopedCallback; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
abstract class Database implements Stringable, IDatabaseForOwner, IMaintainableDatabase, LoggerAwareInterface { |
| 49 |
|
| 50 |
protected $csProvider; |
| 51 |
|
| 52 |
protected $logger; |
| 53 |
|
| 54 |
protected $errorLogger; |
| 55 |
|
| 56 |
protected $deprecationLogger; |
| 57 |
|
| 58 |
protected $profiler; |
| 59 |
|
| 60 |
private $transactionManager; |
| 61 |
|
| 62 |
|
| 63 |
protected $currentDomain; |
| 64 |
|
| 65 |
protected $flagsHolder; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
protected $conn; |
| 70 |
|
| 71 |
|
| 72 |
protected $serverName; |
| 73 |
|
| 74 |
protected $cliMode; |
| 75 |
|
| 76 |
protected $connectTimeout; |
| 77 |
|
| 78 |
protected $receiveTimeout; |
| 79 |
|
| 80 |
protected $agent; |
| 81 |
|
| 82 |
protected $connectionParams; |
| 83 |
|
| 84 |
protected $connectionVariables; |
| 85 |
|
| 86 |
protected $nonNativeInsertSelectBatchSize; |
| 87 |
|
| 88 |
|
| 89 |
protected $ssl; |
| 90 |
|
| 91 |
protected $strictWarnings; |
| 92 |
|
| 93 |
protected $lbInfo = []; |
| 94 |
|
| 95 |
protected $delimiter = ';'; |
| 96 |
|
| 97 |
|
| 98 |
private $htmlErrors; |
| 99 |
|
| 100 |
|
| 101 |
protected $sessionNamedLocks = []; |
| 102 |
|
| 103 |
protected $sessionTempTables = []; |
| 104 |
|
| 105 |
|
| 106 |
protected $lastQueryAffectedRows = 0; |
| 107 |
|
| 108 |
protected $lastQueryInsertId; |
| 109 |
|
| 110 |
|
| 111 |
protected $lastEmulatedAffectedRows; |
| 112 |
|
| 113 |
protected $lastEmulatedInsertId; |
| 114 |
|
| 115 |
|
| 116 |
protected $lastConnectError = ''; |
| 117 |
|
| 118 |
|
| 119 |
private $lastPing = 0.0; |
| 120 |
|
| 121 |
private $lastWriteTime = false; |
| 122 |
|
| 123 |
private $lastPhpError = false; |
| 124 |
|
| 125 |
|
| 126 |
private $csmId; |
| 127 |
|
| 128 |
private $csmFname; |
| 129 |
|
| 130 |
private $csmError; |
| 131 |
|
| 132 |
|
| 133 |
public const ATTR_DB_IS_FILE = 'db-is-file'; |
| 134 |
|
| 135 |
public const ATTR_DB_LEVEL_LOCKING = 'db-level-locking'; |
| 136 |
|
| 137 |
public const ATTR_SCHEMAS_AS_TABLE_GROUPS = 'supports-schemas'; |
| 138 |
|
| 139 |
|
| 140 |
public const NEW_UNCONNECTED = 0; |
| 141 |
|
| 142 |
public const NEW_CONNECTED = 1; |
| 143 |
|
| 144 |
|
| 145 |
protected const ERR_NONE = 0; |
| 146 |
|
| 147 |
protected const ERR_RETRY_QUERY = 1; |
| 148 |
|
| 149 |
protected const ERR_ABORT_QUERY = 2; |
| 150 |
|
| 151 |
protected const ERR_ABORT_TRX = 4; |
| 152 |
|
| 153 |
protected const ERR_ABORT_SESSION = 8; |
| 154 |
|
| 155 |
|
| 156 |
protected const DROPPED_CONN_BLAME_THRESHOLD_SEC = 3.0; |
| 157 |
|
| 158 |
|
| 159 |
private const NOT_APPLICABLE = 'n/a'; |
| 160 |
|
| 161 |
|
| 162 |
private const PING_TTL = 1.0; |
| 163 |
|
| 164 |
private const PING_QUERY = 'SELECT 1 AS ping'; |
| 165 |
|
| 166 |
|
| 167 |
protected const CONN_HOST = 'host'; |
| 168 |
|
| 169 |
protected const CONN_USER = 'user'; |
| 170 |
|
| 171 |
protected const CONN_PASSWORD = 'password'; |
| 172 |
|
| 173 |
protected const CONN_INITIAL_DB = 'dbname'; |
| 174 |
|
| 175 |
protected const CONN_INITIAL_SCHEMA = 'schema'; |
| 176 |
|
| 177 |
protected const CONN_INITIAL_TABLE_PREFIX = 'tablePrefix'; |
| 178 |
|
| 179 |
|
| 180 |
protected $platform; |
| 181 |
|
| 182 |
|
| 183 |
protected $replicationReporter; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function __construct( array $params ) { |
| 190 |
$this->logger = $params['logger'] ?? new NullLogger(); |
| 191 |
$this->transactionManager = new TransactionManager( |
| 192 |
$this->logger, |
| 193 |
$params['trxProfiler'] |
| 194 |
); |
| 195 |
$this->connectionParams = [ |
| 196 |
self::CONN_HOST => ( isset( $params['host'] ) && $params['host'] !== '' ) |
| 197 |
? $params['host'] |
| 198 |
: null, |
| 199 |
self::CONN_USER => ( isset( $params['user'] ) && $params['user'] !== '' ) |
| 200 |
? $params['user'] |
| 201 |
: null, |
| 202 |
self::CONN_INITIAL_DB => ( isset( $params['dbname'] ) && $params['dbname'] !== '' ) |
| 203 |
? $params['dbname'] |
| 204 |
: null, |
| 205 |
self::CONN_INITIAL_SCHEMA => ( isset( $params['schema'] ) && $params['schema'] !== '' ) |
| 206 |
? $params['schema'] |
| 207 |
: null, |
| 208 |
self::CONN_PASSWORD => is_string( $params['password'] ) ? $params['password'] : null, |
| 209 |
self::CONN_INITIAL_TABLE_PREFIX => (string)$params['tablePrefix'] |
| 210 |
]; |
| 211 |
|
| 212 |
$this->lbInfo = $params['lbInfo'] ?? []; |
| 213 |
$this->connectionVariables = $params['variables'] ?? []; |
| 214 |
|
| 215 |
if ( is_string( $params['sqlMode'] ?? null ) ) { |
| 216 |
$this->connectionVariables['sql_mode'] = $params['sqlMode']; |
| 217 |
} |
| 218 |
$flags = (int)$params['flags']; |
| 219 |
$this->flagsHolder = new DatabaseFlags( $flags ); |
| 220 |
$this->ssl = $params['ssl'] ?? (bool)( $flags & self::DBO_SSL ); |
| 221 |
$this->connectTimeout = $params['connectTimeout'] ?? null; |
| 222 |
$this->receiveTimeout = $params['receiveTimeout'] ?? null; |
| 223 |
$this->cliMode = (bool)$params['cliMode']; |
| 224 |
$this->agent = (string)$params['agent']; |
| 225 |
$this->serverName = $params['serverName']; |
| 226 |
$this->nonNativeInsertSelectBatchSize = $params['nonNativeInsertSelectBatchSize'] ?? 10000; |
| 227 |
$this->strictWarnings = !empty( $params['strictWarnings'] ); |
| 228 |
|
| 229 |
$this->profiler = is_callable( $params['profiler'] ) ? $params['profiler'] : null; |
| 230 |
$this->errorLogger = $params['errorLogger']; |
| 231 |
$this->deprecationLogger = $params['deprecationLogger']; |
| 232 |
|
| 233 |
$this->csProvider = $params['criticalSectionProvider'] ?? null; |
| 234 |
|
| 235 |
|
| 236 |
$this->currentDomain = new DatabaseDomain( |
| 237 |
$params['dbname'] != '' ? $params['dbname'] : null, |
| 238 |
$params['schema'] != '' ? $params['schema'] : null, |
| 239 |
$params['tablePrefix'] |
| 240 |
); |
| 241 |
$this->platform = new SQLPlatform( |
| 242 |
$this, |
| 243 |
$this->logger, |
| 244 |
$this->currentDomain, |
| 245 |
$this->errorLogger |
| 246 |
); |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
final public function initConnection() { |
| 259 |
if ( $this->isOpen() ) { |
| 260 |
throw new LogicException( __METHOD__ . ': already connected' ); |
| 261 |
} |
| 262 |
|
| 263 |
$this->open( |
| 264 |
$this->connectionParams[self::CONN_HOST], |
| 265 |
$this->connectionParams[self::CONN_USER], |
| 266 |
$this->connectionParams[self::CONN_PASSWORD], |
| 267 |
$this->connectionParams[self::CONN_INITIAL_DB], |
| 268 |
$this->connectionParams[self::CONN_INITIAL_SCHEMA], |
| 269 |
$this->connectionParams[self::CONN_INITIAL_TABLE_PREFIX] |
| 270 |
); |
| 271 |
$this->lastPing = microtime( true ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
abstract protected function open( $server, $user, $password, $db, $schema, $tablePrefix ); |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public static function getAttributes() { |
| 292 |
return []; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public function setLogger( LoggerInterface $logger ) { |
| 301 |
$this->logger = $logger; |
| 302 |
} |
| 303 |
|
| 304 |
public function getServerInfo() { |
| 305 |
return $this->getServerVersion(); |
| 306 |
} |
| 307 |
|
| 308 |
public function tablePrefix( $prefix = null ) { |
| 309 |
$old = $this->currentDomain->getTablePrefix(); |
| 310 |
|
| 311 |
if ( $prefix !== null ) { |
| 312 |
$this->currentDomain = new DatabaseDomain( |
| 313 |
$this->currentDomain->getDatabase(), |
| 314 |
$this->currentDomain->getSchema(), |
| 315 |
$prefix |
| 316 |
); |
| 317 |
$this->platform->setCurrentDomain( $this->currentDomain ); |
| 318 |
} |
| 319 |
|
| 320 |
return $old; |
| 321 |
} |
| 322 |
|
| 323 |
public function dbSchema( $schema = null ) { |
| 324 |
$old = $this->currentDomain->getSchema(); |
| 325 |
|
| 326 |
if ( $schema !== null ) { |
| 327 |
if ( $schema !== '' && $this->getDBname() === null ) { |
| 328 |
throw new DBUnexpectedError( |
| 329 |
$this, |
| 330 |
"Cannot set schema to '$schema'; no database set" |
| 331 |
); |
| 332 |
} |
| 333 |
|
| 334 |
$this->currentDomain = new DatabaseDomain( |
| 335 |
$this->currentDomain->getDatabase(), |
| 336 |
|
| 337 |
( $schema !== '' ) ? $schema : null, |
| 338 |
$this->currentDomain->getTablePrefix() |
| 339 |
); |
| 340 |
$this->platform->setCurrentDomain( $this->currentDomain ); |
| 341 |
} |
| 342 |
|
| 343 |
return (string)$old; |
| 344 |
} |
| 345 |
|
| 346 |
public function getLBInfo( $name = null ) { |
| 347 |
if ( $name === null ) { |
| 348 |
return $this->lbInfo; |
| 349 |
} |
| 350 |
|
| 351 |
if ( array_key_exists( $name, $this->lbInfo ) ) { |
| 352 |
return $this->lbInfo[$name]; |
| 353 |
} |
| 354 |
|
| 355 |
return null; |
| 356 |
} |
| 357 |
|
| 358 |
public function setLBInfo( $nameOrArray, $value = null ) { |
| 359 |
if ( is_array( $nameOrArray ) ) { |
| 360 |
$this->lbInfo = $nameOrArray; |
| 361 |
} elseif ( is_string( $nameOrArray ) ) { |
| 362 |
if ( $value !== null ) { |
| 363 |
$this->lbInfo[$nameOrArray] = $value; |
| 364 |
} else { |
| 365 |
unset( $this->lbInfo[$nameOrArray] ); |
| 366 |
} |
| 367 |
} else { |
| 368 |
throw new InvalidArgumentException( "Got non-string key" ); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
public function lastDoneWrites() { |
| 373 |
return $this->lastWriteTime ?: false; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function sessionLocksPending() { |
| 382 |
return (bool)$this->sessionNamedLocks; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
final protected function getTransactionRoundId() { |
| 389 |
if ( $this->flagsHolder->hasImplicitTrxFlag() ) { |
| 390 |
|
| 391 |
|
| 392 |
$id = $this->getLBInfo( self::LB_TRX_ROUND_ID ); |
| 393 |
|
| 394 |
return is_string( $id ) ? $id : null; |
| 395 |
} |
| 396 |
|
| 397 |
return null; |
| 398 |
} |
| 399 |
|
| 400 |
public function isOpen() { |
| 401 |
return (bool)$this->conn; |
| 402 |
} |
| 403 |
|
| 404 |
public function getDomainID() { |
| 405 |
return $this->currentDomain->getId(); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
abstract public function strencode( $s ); |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
protected function installErrorHandler() { |
| 420 |
$this->lastPhpError = false; |
| 421 |
$this->htmlErrors = ini_set( 'html_errors', '0' ); |
| 422 |
set_error_handler( [ $this, 'connectionErrorLogger' ] ); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
protected function restoreErrorHandler() { |
| 431 |
restore_error_handler(); |
| 432 |
if ( $this->htmlErrors !== false ) { |
| 433 |
ini_set( 'html_errors', $this->htmlErrors ); |
| 434 |
} |
| 435 |
|
| 436 |
return $this->getLastPHPError(); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
protected function getLastPHPError() { |
| 443 |
if ( $this->lastPhpError ) { |
| 444 |
$error = preg_replace( '!\[<a.*</a>\]!', '', $this->lastPhpError ); |
| 445 |
$error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error ); |
| 446 |
|
| 447 |
return $error; |
| 448 |
} |
| 449 |
|
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public function connectionErrorLogger( $errno, $errstr ) { |
| 462 |
$this->lastPhpError = $errstr; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
protected function getLogContext( array $extras = [] ) { |
| 472 |
return array_merge( |
| 473 |
[ |
| 474 |
'db_server' => $this->getServerName(), |
| 475 |
'db_name' => $this->getDBname(), |
| 476 |
'db_user' => $this->connectionParams[self::CONN_USER] ?? null, |
| 477 |
], |
| 478 |
$extras |
| 479 |
); |
| 480 |
} |
| 481 |
|
| 482 |
final public function close( $fname = __METHOD__ ) { |
| 483 |
$error = null; |
| 484 |
|
| 485 |
$wasOpen = (bool)$this->conn; |
| 486 |
|
| 487 |
if ( $this->conn ) { |
| 488 |
|
| 489 |
if ( $this->trxLevel() ) { |
| 490 |
$error = $this->transactionManager->trxCheckBeforeClose( $this, $fname ); |
| 491 |
|
| 492 |
$this->rollback( __METHOD__, self::FLUSHING_INTERNAL ); |
| 493 |
$this->runTransactionPostRollbackCallbacks(); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
$closed = $this->closeConnection(); |
| 498 |
} else { |
| 499 |
$closed = true; |
| 500 |
} |
| 501 |
|
| 502 |
$this->conn = null; |
| 503 |
|
| 504 |
|
| 505 |
if ( $error !== null ) { |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
$this->logger->error( $error, [ 'db_log_category' => 'query' ] ); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
if ( $wasOpen ) { |
| 517 |
|
| 518 |
$fnames = $this->pendingWriteAndCallbackCallers(); |
| 519 |
if ( $fnames ) { |
| 520 |
throw new RuntimeException( |
| 521 |
"Transaction callbacks are still pending: " . implode( ', ', $fnames ) |
| 522 |
); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
return $closed; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
final protected function assertHasConnectionHandle() { |
| 538 |
if ( !$this->isOpen() ) { |
| 539 |
throw new DBUnexpectedError( $this, "DB connection was already closed" ); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
abstract protected function closeConnection(); |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
abstract protected function doSingleStatementQuery( string $sql ): QueryStatus; |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
private function hasPermanentTable( Query $query ) { |
| 587 |
if ( $query->getVerb() === 'CREATE TEMPORARY' ) { |
| 588 |
|
| 589 |
return false; |
| 590 |
} |
| 591 |
$table = $query->getWriteTable(); |
| 592 |
if ( $table === null ) { |
| 593 |
|
| 594 |
return true; |
| 595 |
} |
| 596 |
[ $db, $pt ] = $this->platform->getDatabaseAndTableIdentifier( $table ); |
| 597 |
$tempInfo = $this->sessionTempTables[$db][$pt] ?? null; |
| 598 |
return !$tempInfo || $tempInfo->pseudoPermanent; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
protected function registerTempTables( Query $query ) { |
| 607 |
$table = $query->getWriteTable(); |
| 608 |
if ( $table === null ) { |
| 609 |
return; |
| 610 |
} |
| 611 |
switch ( $query->getVerb() ) { |
| 612 |
case 'CREATE TEMPORARY': |
| 613 |
[ $db, $pt ] = $this->platform->getDatabaseAndTableIdentifier( $table ); |
| 614 |
$this->sessionTempTables[$db][$pt] = new TempTableInfo( |
| 615 |
$this->transactionManager->getTrxId(), |
| 616 |
(bool)( $query->getFlags() & self::QUERY_PSEUDO_PERMANENT ) |
| 617 |
); |
| 618 |
break; |
| 619 |
|
| 620 |
case 'DROP': |
| 621 |
[ $db, $pt ] = $this->platform->getDatabaseAndTableIdentifier( $table ); |
| 622 |
unset( $this->sessionTempTables[$db][$pt] ); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
public function query( $sql, $fname = __METHOD__, $flags = 0 ) { |
| 627 |
if ( !( $sql instanceof Query ) ) { |
| 628 |
$flags = (int)$flags; |
| 629 |
$sql = QueryBuilderFromRawSql::buildQuery( $sql, $flags, $this->currentDomain->getTablePrefix() ); |
| 630 |
} else { |
| 631 |
$flags = $sql->getFlags(); |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
$this->assertQueryIsCurrentlyAllowed( $sql->getVerb(), $fname ); |
| 636 |
|
| 637 |
|
| 638 |
$status = $this->executeQuery( $sql, $fname, $flags ); |
| 639 |
if ( $status->res === false ) { |
| 640 |
|
| 641 |
|
| 642 |
$ignore = ( |
| 643 |
$this->flagsHolder::contains( $flags, self::QUERY_SILENCE_ERRORS ) && |
| 644 |
!$this->flagsHolder::contains( $status->flags, self::ERR_ABORT_SESSION ) && |
| 645 |
!$this->flagsHolder::contains( $status->flags, self::ERR_ABORT_TRX ) |
| 646 |
); |
| 647 |
$this->reportQueryError( $status->message, $status->code, $sql->getSQL(), $fname, $ignore ); |
| 648 |
} |
| 649 |
|
| 650 |
return $status->res; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
final protected function executeQuery( $sql, $fname, $flags ) { |
| 670 |
$this->assertHasConnectionHandle(); |
| 671 |
|
| 672 |
$isPermWrite = false; |
| 673 |
$isWrite = $sql->isWriteQuery(); |
| 674 |
if ( $isWrite ) { |
| 675 |
ChangedTablesTracker::recordQuery( $this->currentDomain, $sql ); |
| 676 |
|
| 677 |
|
| 678 |
if ( $this->hasPermanentTable( $sql ) ) { |
| 679 |
$isPermWrite = true; |
| 680 |
$info = $this->getReadOnlyReason(); |
| 681 |
if ( $info ) { |
| 682 |
[ $reason, $source ] = $info; |
| 683 |
if ( $source === 'role' ) { |
| 684 |
throw new DBReadOnlyRoleError( $this, "Database is read-only: $reason" ); |
| 685 |
} else { |
| 686 |
throw new DBReadOnlyError( $this, "Database is read-only: $reason" ); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
if ( $this->flagsHolder::contains( $sql->getFlags(), self::QUERY_REPLICA_ROLE ) ) { |
| 691 |
throw new DBReadOnlyRoleError( |
| 692 |
$this, |
| 693 |
"Cannot write; target role is DB_REPLICA" |
| 694 |
); |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
$retryLeft = !$this->flagsHolder::contains( $flags, self::QUERY_NO_RETRY ); |
| 701 |
|
| 702 |
$cs = $this->commenceCriticalSection( __METHOD__ ); |
| 703 |
|
| 704 |
do { |
| 705 |
|
| 706 |
if ( $this->beginIfImplied( $sql, $fname, $flags ) ) { |
| 707 |
|
| 708 |
$retryLeft = false; |
| 709 |
} |
| 710 |
|
| 711 |
$status = $this->attemptQuery( $sql, $fname, $isPermWrite ); |
| 712 |
} while ( |
| 713 |
|
| 714 |
$this->flagsHolder::contains( $status->flags, self::ERR_RETRY_QUERY ) && |
| 715 |
|
| 716 |
|
| 717 |
$retryLeft && !( $retryLeft = false ) |
| 718 |
); |
| 719 |
|
| 720 |
|
| 721 |
if ( $status->res ) { |
| 722 |
$this->registerTempTables( $sql ); |
| 723 |
} |
| 724 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 725 |
|
| 726 |
return $status; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
private function attemptQuery( |
| 751 |
$sql, |
| 752 |
string $fname, |
| 753 |
bool $isPermWrite |
| 754 |
) { |
| 755 |
|
| 756 |
$priorSessInfo = new CriticalSessionInfo( |
| 757 |
$this->transactionManager->getTrxId(), |
| 758 |
$this->transactionManager->explicitTrxActive(), |
| 759 |
$this->transactionManager->pendingWriteCallers(), |
| 760 |
$this->transactionManager->pendingPreCommitCallbackCallers(), |
| 761 |
$this->sessionNamedLocks, |
| 762 |
$this->sessionTempTables |
| 763 |
); |
| 764 |
|
| 765 |
$prefix = ( |
| 766 |
$this->replicationReporter->getTopologyRole() === self::ROLE_STREAMING_MASTER |
| 767 |
) ? 'role-primary: ' : ''; |
| 768 |
|
| 769 |
|
| 770 |
if ( $sql->getCleanedSql() ) { |
| 771 |
$generalizedSql = $sql; |
| 772 |
$ps = $this->profiler ? ( $this->profiler )( $sql->getCleanedSql() ) : null; |
| 773 |
} else { |
| 774 |
$generalizedSql = new GeneralizedSql( $sql->getSQL(), $prefix ); |
| 775 |
$ps = $this->profiler ? ( $this->profiler )( $generalizedSql->stringify() ) : null; |
| 776 |
} |
| 777 |
|
| 778 |
$cStatement = $this->makeCommentedSql( $sql->getSQL(), $fname ); |
| 779 |
$startTime = microtime( true ); |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
$this->lastEmulatedAffectedRows = null; |
| 785 |
$this->lastEmulatedInsertId = null; |
| 786 |
|
| 787 |
$status = $this->doSingleStatementQuery( $cStatement ); |
| 788 |
|
| 789 |
|
| 790 |
$endTime = microtime( true ); |
| 791 |
$queryRuntime = max( $endTime - $startTime, 0.0 ); |
| 792 |
unset( $ps ); |
| 793 |
|
| 794 |
if ( $status->res !== false ) { |
| 795 |
$this->lastPing = $endTime; |
| 796 |
} |
| 797 |
|
| 798 |
$affectedRowCount = $status->rowsAffected; |
| 799 |
$returnedRowCount = $status->rowsReturned; |
| 800 |
$this->lastQueryAffectedRows = $affectedRowCount; |
| 801 |
|
| 802 |
if ( $status->res !== false ) { |
| 803 |
if ( $isPermWrite ) { |
| 804 |
$this->lastWriteTime = $startTime; |
| 805 |
if ( $this->trxLevel() ) { |
| 806 |
$this->transactionManager->transactionWritingIn( |
| 807 |
$this->getServerName(), |
| 808 |
$this->getDomainID(), |
| 809 |
$startTime |
| 810 |
); |
| 811 |
$this->transactionManager->updateTrxWriteQueryReport( |
| 812 |
$sql->getSQL(), |
| 813 |
$queryRuntime, |
| 814 |
$affectedRowCount, |
| 815 |
$fname |
| 816 |
); |
| 817 |
} |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
$this->transactionManager->recordQueryCompletion( |
| 822 |
$generalizedSql, |
| 823 |
$startTime, |
| 824 |
$isPermWrite, |
| 825 |
$isPermWrite ? $affectedRowCount : $returnedRowCount, |
| 826 |
$this->getServerName() |
| 827 |
); |
| 828 |
|
| 829 |
|
| 830 |
$status->flags = $this->handleErroredQuery( $status, $sql, $fname, $queryRuntime, $priorSessInfo ); |
| 831 |
|
| 832 |
if ( $this->flagsHolder->getFlag( self::DBO_DEBUG ) ) { |
| 833 |
$this->logger->debug( |
| 834 |
"{method} [{runtime_ms}ms] {db_server}: {sql}", |
| 835 |
$this->getLogContext( [ |
| 836 |
'method' => $fname, |
| 837 |
'sql' => $sql->getSQL(), |
| 838 |
'domain' => $this->getDomainID(), |
| 839 |
'runtime_ms' => round( $queryRuntime * 1000, 3 ), |
| 840 |
'db_log_category' => 'query' |
| 841 |
] ) |
| 842 |
); |
| 843 |
} |
| 844 |
|
| 845 |
return $status; |
| 846 |
} |
| 847 |
|
| 848 |
private function handleErroredQuery( QueryStatus $status, $sql, $fname, $queryRuntime, $priorSessInfo ) { |
| 849 |
$errflags = self::ERR_NONE; |
| 850 |
$error = $status->message; |
| 851 |
$errno = $status->code; |
| 852 |
if ( $status->res !== false ) { |
| 853 |
|
| 854 |
return $errflags; |
| 855 |
} |
| 856 |
if ( $this->isConnectionError( $errno ) ) { |
| 857 |
|
| 858 |
|
| 859 |
$connLossFlag = $this->assessConnectionLoss( |
| 860 |
$sql->getVerb(), |
| 861 |
$queryRuntime, |
| 862 |
$priorSessInfo |
| 863 |
); |
| 864 |
|
| 865 |
$reconnected = $this->replaceLostConnection( $errno, __METHOD__ ); |
| 866 |
|
| 867 |
if ( $connLossFlag >= self::ERR_ABORT_SESSION ) { |
| 868 |
$ex = $this->getQueryException( $error, $errno, $sql->getSQL(), $fname ); |
| 869 |
$this->transactionManager->setSessionError( $ex ); |
| 870 |
} |
| 871 |
|
| 872 |
if ( $connLossFlag >= self::ERR_ABORT_TRX ) { |
| 873 |
$ex = $this->getQueryException( $error, $errno, $sql->getSQL(), $fname ); |
| 874 |
$this->transactionManager->setTransactionError( $ex ); |
| 875 |
} |
| 876 |
|
| 877 |
if ( $connLossFlag === self::ERR_RETRY_QUERY ) { |
| 878 |
$errflags |= ( $reconnected ? self::ERR_RETRY_QUERY : self::ERR_ABORT_QUERY ); |
| 879 |
} else { |
| 880 |
$errflags |= $connLossFlag; |
| 881 |
} |
| 882 |
} elseif ( $this->isKnownStatementRollbackError( $errno ) ) { |
| 883 |
|
| 884 |
$errflags |= self::ERR_ABORT_QUERY; |
| 885 |
if ( $this->trxLevel() ) { |
| 886 |
|
| 887 |
|
| 888 |
$cause = [ $error, $errno, $fname ]; |
| 889 |
$this->transactionManager->setTrxStatusIgnoredCause( $cause ); |
| 890 |
} |
| 891 |
} elseif ( $this->trxLevel() ) { |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
$ex = $this->getQueryException( $error, $errno, $sql->getSQL(), $fname ); |
| 900 |
$this->transactionManager->setTransactionError( $ex ); |
| 901 |
$errflags |= self::ERR_ABORT_TRX; |
| 902 |
} else { |
| 903 |
|
| 904 |
$errflags |= self::ERR_ABORT_QUERY; |
| 905 |
} |
| 906 |
|
| 907 |
return $errflags; |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
private function makeCommentedSql( $sql, $fname ): string { |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
$encName = preg_replace( '/[\x00-\x1F\/]/', '-', "$fname {$this->agent}" ); |
| 921 |
return preg_replace( '/\s|$/', " /* $encName */ ", $sql, 1 ); |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
private function beginIfImplied( $sql, $fname, $flags ) { |
| 934 |
if ( !$this->trxLevel() && $this->flagsHolder->hasApplicableImplicitTrxFlag( $flags ) ) { |
| 935 |
if ( $this->platform->isTransactableQuery( $sql ) ) { |
| 936 |
$this->begin( __METHOD__ . " ($fname)", self::TRANSACTION_INTERNAL ); |
| 937 |
$this->transactionManager->turnOnAutomatic(); |
| 938 |
|
| 939 |
return true; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
return false; |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
private function assertQueryIsCurrentlyAllowed( string $verb, string $fname ) { |
| 962 |
if ( $verb === 'USE' ) { |
| 963 |
throw new DBUnexpectedError( $this, "Got USE query; use selectDomain() instead" ); |
| 964 |
} |
| 965 |
|
| 966 |
if ( $verb === 'ROLLBACK' ) { |
| 967 |
|
| 968 |
|
| 969 |
return; |
| 970 |
} |
| 971 |
|
| 972 |
if ( $this->csmError ) { |
| 973 |
throw new DBTransactionStateError( |
| 974 |
$this, |
| 975 |
"Cannot execute query from $fname while session state is out of sync", |
| 976 |
[], |
| 977 |
$this->csmError |
| 978 |
); |
| 979 |
} |
| 980 |
|
| 981 |
$this->transactionManager->assertSessionStatus( $this, $fname ); |
| 982 |
|
| 983 |
if ( $verb !== 'ROLLBACK TO SAVEPOINT' ) { |
| 984 |
$this->transactionManager->assertTransactionStatus( |
| 985 |
$this, |
| 986 |
$this->deprecationLogger, |
| 987 |
$fname |
| 988 |
); |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
private function assessConnectionLoss( |
| 1012 |
string $verb, |
| 1013 |
float $walltime, |
| 1014 |
CriticalSessionInfo $priorSessInfo |
| 1015 |
) { |
| 1016 |
if ( $walltime < self::DROPPED_CONN_BLAME_THRESHOLD_SEC ) { |
| 1017 |
|
| 1018 |
$res = self::ERR_RETRY_QUERY; |
| 1019 |
} else { |
| 1020 |
|
| 1021 |
$res = self::ERR_ABORT_QUERY; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
$blockers = []; |
| 1026 |
|
| 1027 |
foreach ( $priorSessInfo->namedLocks as $lockName => $lockInfo ) { |
| 1028 |
if ( $lockInfo['trxId'] && $lockInfo['trxId'] === $priorSessInfo->trxId ) { |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
if ( $verb !== 'ROLLBACK' ) { |
| 1033 |
$res = max( $res, self::ERR_ABORT_TRX ); |
| 1034 |
$blockers[] = "named lock '$lockName'"; |
| 1035 |
} |
| 1036 |
} else { |
| 1037 |
|
| 1038 |
|
| 1039 |
$res = max( $res, self::ERR_ABORT_SESSION ); |
| 1040 |
$blockers[] = "named lock '$lockName'"; |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
foreach ( $priorSessInfo->tempTables as $domainTempTables ) { |
| 1045 |
foreach ( $domainTempTables as $tableName => $tableInfo ) { |
| 1046 |
if ( $tableInfo->trxId && $tableInfo->trxId === $priorSessInfo->trxId ) { |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
if ( $verb !== 'ROLLBACK' ) { |
| 1051 |
$res = max( $res, self::ERR_ABORT_TRX ); |
| 1052 |
$blockers[] = "temp table '$tableName'"; |
| 1053 |
} |
| 1054 |
} else { |
| 1055 |
|
| 1056 |
|
| 1057 |
$res = max( $res, self::ERR_ABORT_SESSION ); |
| 1058 |
$blockers[] = "temp table '$tableName'"; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
if ( $priorSessInfo->trxWriteCallers && $verb !== 'ROLLBACK' ) { |
| 1066 |
$res = max( $res, self::ERR_ABORT_TRX ); |
| 1067 |
$blockers[] = 'uncommitted writes'; |
| 1068 |
} |
| 1069 |
if ( $priorSessInfo->trxPreCommitCbCallers && $verb !== 'ROLLBACK' ) { |
| 1070 |
$res = max( $res, self::ERR_ABORT_TRX ); |
| 1071 |
$blockers[] = 'pre-commit callbacks'; |
| 1072 |
} |
| 1073 |
if ( $priorSessInfo->trxExplicit && $verb !== 'ROLLBACK' && $verb !== 'COMMIT' ) { |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
$res = max( $res, self::ERR_ABORT_TRX ); |
| 1081 |
$blockers[] = 'explicit transaction'; |
| 1082 |
} |
| 1083 |
|
| 1084 |
if ( $blockers ) { |
| 1085 |
$this->logger->warning( |
| 1086 |
"cannot reconnect to {db_server} silently: {error}", |
| 1087 |
$this->getLogContext( [ |
| 1088 |
'error' => 'session state loss (' . implode( ', ', $blockers ) . ')', |
| 1089 |
'exception' => new RuntimeException(), |
| 1090 |
'db_log_category' => 'connection' |
| 1091 |
] ) |
| 1092 |
); |
| 1093 |
} |
| 1094 |
|
| 1095 |
return $res; |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
private function handleSessionLossPreconnect() { |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
$this->sessionTempTables = []; |
| 1106 |
|
| 1107 |
|
| 1108 |
$this->sessionNamedLocks = []; |
| 1109 |
|
| 1110 |
$this->transactionManager->onSessionLoss( $this ); |
| 1111 |
|
| 1112 |
$this->doHandleSessionLossPreconnect(); |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
protected function doHandleSessionLossPreconnect() { |
| 1119 |
|
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
protected function isQueryTimeoutError( $errno ) { |
| 1133 |
return false; |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
public function reportQueryError( $error, $errno, $sql, $fname, $ignore = false ) { |
| 1150 |
if ( $ignore ) { |
| 1151 |
$this->logger->debug( |
| 1152 |
"SQL ERROR (ignored): $error", |
| 1153 |
[ 'db_log_category' => 'query' ] |
| 1154 |
); |
| 1155 |
} else { |
| 1156 |
throw $this->getQueryExceptionAndLog( $error, $errno, $sql, $fname ); |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
private function getQueryExceptionAndLog( $error, $errno, $sql, $fname ) { |
| 1168 |
|
| 1169 |
|
| 1170 |
$this->logger->error( |
| 1171 |
"Error $errno from $fname, {error} {sql1line} {db_server}", |
| 1172 |
$this->getLogContext( [ |
| 1173 |
'method' => __METHOD__, |
| 1174 |
'errno' => $errno, |
| 1175 |
'error' => $error, |
| 1176 |
'sql1line' => mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 ), |
| 1177 |
'fname' => $fname, |
| 1178 |
'db_log_category' => 'query', |
| 1179 |
'exception' => new RuntimeException() |
| 1180 |
] ) |
| 1181 |
); |
| 1182 |
return $this->getQueryException( $error, $errno, $sql, $fname ); |
| 1183 |
} |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
private function getQueryException( $error, $errno, $sql, $fname ) { |
| 1193 |
if ( $this->isQueryTimeoutError( $errno ) ) { |
| 1194 |
return new DBQueryTimeoutError( $this, $error, $errno, $sql, $fname ); |
| 1195 |
} elseif ( $this->isConnectionError( $errno ) ) { |
| 1196 |
return new DBQueryDisconnectedError( $this, $error, $errno, $sql, $fname ); |
| 1197 |
} else { |
| 1198 |
return new DBQueryError( $this, $error, $errno, $sql, $fname ); |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
final protected function newExceptionAfterConnectError( $error ) { |
| 1207 |
|
| 1208 |
|
| 1209 |
$this->lastConnectError = $error; |
| 1210 |
$this->conn = null; |
| 1211 |
|
| 1212 |
$this->logger->error( |
| 1213 |
"Error connecting to {db_server} as user {db_user}: {error}", |
| 1214 |
$this->getLogContext( [ |
| 1215 |
'error' => $error, |
| 1216 |
'exception' => new RuntimeException(), |
| 1217 |
'db_log_category' => 'connection', |
| 1218 |
] ) |
| 1219 |
); |
| 1220 |
|
| 1221 |
return new DBConnectionError( $this, $error ); |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
public function newSelectQueryBuilder(): SelectQueryBuilder { |
| 1231 |
return new SelectQueryBuilder( $this ); |
| 1232 |
} |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
public function newUnionQueryBuilder(): UnionQueryBuilder { |
| 1241 |
return new UnionQueryBuilder( $this ); |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
public function newUpdateQueryBuilder(): UpdateQueryBuilder { |
| 1251 |
return new UpdateQueryBuilder( $this ); |
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
public function newDeleteQueryBuilder(): DeleteQueryBuilder { |
| 1261 |
return new DeleteQueryBuilder( $this ); |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
public function newInsertQueryBuilder(): InsertQueryBuilder { |
| 1271 |
return new InsertQueryBuilder( $this ); |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
public function newReplaceQueryBuilder(): ReplaceQueryBuilder { |
| 1281 |
return new ReplaceQueryBuilder( $this ); |
| 1282 |
} |
| 1283 |
|
| 1284 |
public function selectField( |
| 1285 |
$tables, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = [] |
| 1286 |
) { |
| 1287 |
if ( $var === '*' ) { |
| 1288 |
throw new DBUnexpectedError( $this, "Cannot use a * field" ); |
| 1289 |
} elseif ( is_array( $var ) && count( $var ) !== 1 ) { |
| 1290 |
throw new DBUnexpectedError( $this, 'Cannot use more than one field' ); |
| 1291 |
} |
| 1292 |
|
| 1293 |
$options = $this->platform->normalizeOptions( $options ); |
| 1294 |
$options['LIMIT'] = 1; |
| 1295 |
|
| 1296 |
$res = $this->select( $tables, $var, $cond, $fname, $options, $join_conds ); |
| 1297 |
if ( $res === false ) { |
| 1298 |
throw new DBUnexpectedError( $this, "Got false from select()" ); |
| 1299 |
} |
| 1300 |
|
| 1301 |
$row = $res->fetchRow(); |
| 1302 |
if ( $row === false ) { |
| 1303 |
return false; |
| 1304 |
} |
| 1305 |
|
| 1306 |
return reset( $row ); |
| 1307 |
} |
| 1308 |
|
| 1309 |
public function selectFieldValues( |
| 1310 |
$tables, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = [] |
| 1311 |
): array { |
| 1312 |
if ( $var === '*' ) { |
| 1313 |
throw new DBUnexpectedError( $this, "Cannot use a * field" ); |
| 1314 |
} elseif ( !is_string( $var ) ) { |
| 1315 |
throw new DBUnexpectedError( $this, "Cannot use an array of fields" ); |
| 1316 |
} |
| 1317 |
|
| 1318 |
$options = $this->platform->normalizeOptions( $options ); |
| 1319 |
$res = $this->select( $tables, [ 'value' => $var ], $cond, $fname, $options, $join_conds ); |
| 1320 |
if ( $res === false ) { |
| 1321 |
throw new DBUnexpectedError( $this, "Got false from select()" ); |
| 1322 |
} |
| 1323 |
|
| 1324 |
$values = []; |
| 1325 |
foreach ( $res as $row ) { |
| 1326 |
$values[] = $row->value; |
| 1327 |
} |
| 1328 |
|
| 1329 |
return $values; |
| 1330 |
} |
| 1331 |
|
| 1332 |
public function select( |
| 1333 |
$tables, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = [] |
| 1334 |
) { |
| 1335 |
$options = (array)$options; |
| 1336 |
|
| 1337 |
$sql = $this->selectSQLText( $tables, $vars, $conds, $fname, $options, $join_conds ); |
| 1338 |
|
| 1339 |
|
| 1340 |
$flags = in_array( 'FOR UPDATE', $options, true ) |
| 1341 |
? self::QUERY_CHANGE_ROWS |
| 1342 |
: self::QUERY_CHANGE_NONE; |
| 1343 |
|
| 1344 |
$query = new Query( $sql, $flags, 'SELECT' ); |
| 1345 |
return $this->query( $query, $fname ); |
| 1346 |
} |
| 1347 |
|
| 1348 |
public function selectRow( $tables, $vars, $conds, $fname = __METHOD__, |
| 1349 |
$options = [], $join_conds = [] |
| 1350 |
) { |
| 1351 |
$options = (array)$options; |
| 1352 |
$options['LIMIT'] = 1; |
| 1353 |
|
| 1354 |
$res = $this->select( $tables, $vars, $conds, $fname, $options, $join_conds ); |
| 1355 |
if ( $res === false ) { |
| 1356 |
throw new DBUnexpectedError( $this, "Got false from select()" ); |
| 1357 |
} |
| 1358 |
|
| 1359 |
if ( !$res->numRows() ) { |
| 1360 |
return false; |
| 1361 |
} |
| 1362 |
|
| 1363 |
return $res->fetchObject(); |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
public function estimateRowCount( |
| 1370 |
$tables, $var = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = [] |
| 1371 |
): int { |
| 1372 |
$conds = $this->platform->normalizeConditions( $conds, $fname ); |
| 1373 |
$column = $this->platform->extractSingleFieldFromList( $var ); |
| 1374 |
if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) { |
| 1375 |
$conds[] = "$column IS NOT NULL"; |
| 1376 |
} |
| 1377 |
|
| 1378 |
$res = $this->select( |
| 1379 |
$tables, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options, $join_conds |
| 1380 |
); |
| 1381 |
$row = $res ? $res->fetchRow() : []; |
| 1382 |
|
| 1383 |
return isset( $row['rowcount'] ) ? (int)$row['rowcount'] : 0; |
| 1384 |
} |
| 1385 |
|
| 1386 |
public function selectRowCount( |
| 1387 |
$tables, $var = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = [] |
| 1388 |
): int { |
| 1389 |
$conds = $this->platform->normalizeConditions( $conds, $fname ); |
| 1390 |
$column = $this->platform->extractSingleFieldFromList( $var ); |
| 1391 |
if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) { |
| 1392 |
$conds[] = "$column IS NOT NULL"; |
| 1393 |
} |
| 1394 |
if ( in_array( 'DISTINCT', (array)$options ) ) { |
| 1395 |
if ( $column === null ) { |
| 1396 |
throw new DBUnexpectedError( $this, |
| 1397 |
'$var cannot be empty when the DISTINCT option is given' ); |
| 1398 |
} |
| 1399 |
$innerVar = $column; |
| 1400 |
} else { |
| 1401 |
$innerVar = '1'; |
| 1402 |
} |
| 1403 |
|
| 1404 |
$res = $this->select( |
| 1405 |
[ |
| 1406 |
'tmp_count' => $this->platform->buildSelectSubquery( |
| 1407 |
$tables, |
| 1408 |
$innerVar, |
| 1409 |
$conds, |
| 1410 |
$fname, |
| 1411 |
$options, |
| 1412 |
$join_conds |
| 1413 |
) |
| 1414 |
], |
| 1415 |
[ 'rowcount' => 'COUNT(*)' ], |
| 1416 |
[], |
| 1417 |
$fname |
| 1418 |
); |
| 1419 |
$row = $res ? $res->fetchRow() : []; |
| 1420 |
|
| 1421 |
return isset( $row['rowcount'] ) ? (int)$row['rowcount'] : 0; |
| 1422 |
} |
| 1423 |
|
| 1424 |
public function lockForUpdate( |
| 1425 |
$table, $conds = '', $fname = __METHOD__, $options = [], $join_conds = [] |
| 1426 |
) { |
| 1427 |
if ( !$this->trxLevel() && !$this->flagsHolder->hasImplicitTrxFlag() ) { |
| 1428 |
throw new DBUnexpectedError( |
| 1429 |
$this, |
| 1430 |
__METHOD__ . ': no transaction is active nor is DBO_TRX set' |
| 1431 |
); |
| 1432 |
} |
| 1433 |
|
| 1434 |
$options = (array)$options; |
| 1435 |
$options[] = 'FOR UPDATE'; |
| 1436 |
|
| 1437 |
return $this->selectRowCount( $table, '*', $conds, $fname, $options, $join_conds ); |
| 1438 |
} |
| 1439 |
|
| 1440 |
public function fieldExists( $table, $field, $fname = __METHOD__ ) { |
| 1441 |
$info = $this->fieldInfo( $table, $field ); |
| 1442 |
|
| 1443 |
return (bool)$info; |
| 1444 |
} |
| 1445 |
|
| 1446 |
abstract public function tableExists( $table, $fname = __METHOD__ ); |
| 1447 |
|
| 1448 |
public function indexExists( $table, $index, $fname = __METHOD__ ) { |
| 1449 |
$info = $this->indexInfo( $table, $index, $fname ); |
| 1450 |
|
| 1451 |
return (bool)$info; |
| 1452 |
} |
| 1453 |
|
| 1454 |
public function indexUnique( $table, $index, $fname = __METHOD__ ) { |
| 1455 |
$info = $this->indexInfo( $table, $index, $fname ); |
| 1456 |
|
| 1457 |
return $info ? $info['unique'] : null; |
| 1458 |
} |
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
abstract public function indexInfo( $table, $index, $fname = __METHOD__ ); |
| 1470 |
|
| 1471 |
public function insert( $table, $rows, $fname = __METHOD__, $options = [] ) { |
| 1472 |
$query = $this->platform->dispatchingInsertSqlText( $table, $rows, $options ); |
| 1473 |
if ( !$query ) { |
| 1474 |
return true; |
| 1475 |
} |
| 1476 |
$this->query( $query, $fname ); |
| 1477 |
if ( $this->strictWarnings ) { |
| 1478 |
$this->checkInsertWarnings( $query, $fname ); |
| 1479 |
} |
| 1480 |
return true; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
protected function checkInsertWarnings( Query $query, $fname ) { |
| 1492 |
} |
| 1493 |
|
| 1494 |
public function update( $table, $set, $conds, $fname = __METHOD__, $options = [] ) { |
| 1495 |
$query = $this->platform->updateSqlText( $table, $set, $conds, $options ); |
| 1496 |
$this->query( $query, $fname ); |
| 1497 |
|
| 1498 |
return true; |
| 1499 |
} |
| 1500 |
|
| 1501 |
public function databasesAreIndependent() { |
| 1502 |
return false; |
| 1503 |
} |
| 1504 |
|
| 1505 |
final public function selectDomain( $domain ) { |
| 1506 |
$cs = $this->commenceCriticalSection( __METHOD__ ); |
| 1507 |
|
| 1508 |
try { |
| 1509 |
$this->doSelectDomain( DatabaseDomain::newFromId( $domain ) ); |
| 1510 |
} catch ( DBError $e ) { |
| 1511 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 1512 |
throw $e; |
| 1513 |
} |
| 1514 |
|
| 1515 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
protected function doSelectDomain( DatabaseDomain $domain ) { |
| 1525 |
$this->currentDomain = $domain; |
| 1526 |
$this->platform->setCurrentDomain( $this->currentDomain ); |
| 1527 |
} |
| 1528 |
|
| 1529 |
public function getDBname() { |
| 1530 |
return $this->currentDomain->getDatabase(); |
| 1531 |
} |
| 1532 |
|
| 1533 |
public function getServer() { |
| 1534 |
return $this->connectionParams[self::CONN_HOST] ?? null; |
| 1535 |
} |
| 1536 |
|
| 1537 |
public function getServerName() { |
| 1538 |
return $this->serverName ?? $this->getServer() ?? 'unknown'; |
| 1539 |
} |
| 1540 |
|
| 1541 |
public function addQuotes( $s ) { |
| 1542 |
if ( $s instanceof RawSQLValue ) { |
| 1543 |
return $s->toSql(); |
| 1544 |
} |
| 1545 |
if ( $s instanceof Blob ) { |
| 1546 |
$s = $s->fetch(); |
| 1547 |
} |
| 1548 |
if ( $s === null ) { |
| 1549 |
return 'NULL'; |
| 1550 |
} elseif ( is_bool( $s ) ) { |
| 1551 |
return (string)(int)$s; |
| 1552 |
} elseif ( is_int( $s ) ) { |
| 1553 |
return (string)$s; |
| 1554 |
} else { |
| 1555 |
return "'" . $this->strencode( $s ) . "'"; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
public function expr( string $field, string $op, $value ): Expression { |
| 1560 |
return new Expression( $field, $op, $value ); |
| 1561 |
} |
| 1562 |
|
| 1563 |
public function andExpr( array $conds ): AndExpressionGroup { |
| 1564 |
return AndExpressionGroup::newFromArray( $conds ); |
| 1565 |
} |
| 1566 |
|
| 1567 |
public function orExpr( array $conds ): OrExpressionGroup { |
| 1568 |
return OrExpressionGroup::newFromArray( $conds ); |
| 1569 |
} |
| 1570 |
|
| 1571 |
public function replace( $table, $uniqueKeys, $rows, $fname = __METHOD__ ) { |
| 1572 |
$uniqueKey = $this->platform->normalizeUpsertParams( $uniqueKeys, $rows ); |
| 1573 |
if ( !$rows ) { |
| 1574 |
return; |
| 1575 |
} |
| 1576 |
$affectedRowCount = 0; |
| 1577 |
$insertId = null; |
| 1578 |
$this->startAtomic( $fname, self::ATOMIC_CANCELABLE ); |
| 1579 |
try { |
| 1580 |
foreach ( $rows as $row ) { |
| 1581 |
|
| 1582 |
$query = $this->platform->deleteSqlText( |
| 1583 |
$table, |
| 1584 |
[ $this->platform->makeKeyCollisionCondition( [ $row ], $uniqueKey ) ] |
| 1585 |
); |
| 1586 |
$this->query( $query, $fname ); |
| 1587 |
|
| 1588 |
$query = $this->platform->dispatchingInsertSqlText( $table, $row, [] ); |
| 1589 |
$this->query( $query, $fname ); |
| 1590 |
$affectedRowCount += $this->lastQueryAffectedRows; |
| 1591 |
$insertId = $insertId ?: $this->lastQueryInsertId; |
| 1592 |
} |
| 1593 |
$this->endAtomic( $fname ); |
| 1594 |
} catch ( DBError $e ) { |
| 1595 |
$this->cancelAtomic( $fname ); |
| 1596 |
throw $e; |
| 1597 |
} |
| 1598 |
$this->lastEmulatedAffectedRows = $affectedRowCount; |
| 1599 |
$this->lastEmulatedInsertId = $insertId; |
| 1600 |
} |
| 1601 |
|
| 1602 |
public function upsert( $table, array $rows, $uniqueKeys, array $set, $fname = __METHOD__ ) { |
| 1603 |
$uniqueKey = $this->platform->normalizeUpsertParams( $uniqueKeys, $rows ); |
| 1604 |
if ( !$rows ) { |
| 1605 |
return true; |
| 1606 |
} |
| 1607 |
$this->platform->assertValidUpsertSetArray( $set, $uniqueKey, $rows ); |
| 1608 |
|
| 1609 |
$encTable = $this->tableName( $table ); |
| 1610 |
$sqlColumnAssignments = $this->makeList( $set, self::LIST_SET ); |
| 1611 |
|
| 1612 |
$autoIncrementColumn = $this->getInsertIdColumnForUpsert( $table ); |
| 1613 |
|
| 1614 |
$useWith = (bool)array_filter( |
| 1615 |
$set, |
| 1616 |
static function ( $v, $k ) { |
| 1617 |
return $v instanceof RawSQLValue || is_int( $k ); |
| 1618 |
}, |
| 1619 |
ARRAY_FILTER_USE_BOTH |
| 1620 |
); |
| 1621 |
|
| 1622 |
|
| 1623 |
$typeByColumn = $useWith ? $this->getValueTypesForWithClause( $table ) : []; |
| 1624 |
|
| 1625 |
$first = true; |
| 1626 |
$affectedRowCount = 0; |
| 1627 |
$insertId = null; |
| 1628 |
$this->startAtomic( $fname, self::ATOMIC_CANCELABLE ); |
| 1629 |
try { |
| 1630 |
foreach ( $rows as $row ) { |
| 1631 |
|
| 1632 |
[ $sqlColumns, $sqlTuples, $sqlVals ] = $this->platform->makeInsertLists( |
| 1633 |
[ $row ], |
| 1634 |
'__', |
| 1635 |
$typeByColumn |
| 1636 |
); |
| 1637 |
$sqlConditions = $this->platform->makeKeyCollisionCondition( |
| 1638 |
[ $row ], |
| 1639 |
$uniqueKey |
| 1640 |
); |
| 1641 |
$query = new Query( |
| 1642 |
( $useWith ? "WITH __VALS ($sqlVals) AS (VALUES $sqlTuples) " : "" ) . |
| 1643 |
"UPDATE $encTable SET $sqlColumnAssignments " . |
| 1644 |
"WHERE ($sqlConditions)", |
| 1645 |
self::QUERY_CHANGE_ROWS, |
| 1646 |
'UPDATE', |
| 1647 |
$table |
| 1648 |
); |
| 1649 |
$this->query( $query, $fname ); |
| 1650 |
$rowsUpdated = $this->lastQueryAffectedRows; |
| 1651 |
$affectedRowCount += $rowsUpdated; |
| 1652 |
if ( $rowsUpdated > 0 ) { |
| 1653 |
|
| 1654 |
if ( $first && $autoIncrementColumn !== null ) { |
| 1655 |
|
| 1656 |
$query = new Query( |
| 1657 |
"SELECT $autoIncrementColumn AS id FROM $encTable " . |
| 1658 |
"WHERE ($sqlConditions)", |
| 1659 |
self::QUERY_CHANGE_NONE, |
| 1660 |
'SELECT' |
| 1661 |
); |
| 1662 |
$sRes = $this->query( $query, $fname, self::QUERY_CHANGE_ROWS ); |
| 1663 |
$insertId = (int)$sRes->fetchRow()['id']; |
| 1664 |
} |
| 1665 |
} else { |
| 1666 |
|
| 1667 |
$query = new Query( |
| 1668 |
"INSERT INTO $encTable ($sqlColumns) VALUES $sqlTuples", |
| 1669 |
self::QUERY_CHANGE_ROWS, |
| 1670 |
'INSERT', |
| 1671 |
$table |
| 1672 |
); |
| 1673 |
$this->query( $query, $fname ); |
| 1674 |
$affectedRowCount += $this->lastQueryAffectedRows; |
| 1675 |
} |
| 1676 |
$first = false; |
| 1677 |
} |
| 1678 |
$this->endAtomic( $fname ); |
| 1679 |
} catch ( DBError $e ) { |
| 1680 |
$this->cancelAtomic( $fname ); |
| 1681 |
throw $e; |
| 1682 |
} |
| 1683 |
$this->lastEmulatedAffectedRows = $affectedRowCount; |
| 1684 |
$this->lastEmulatedInsertId = $insertId; |
| 1685 |
return true; |
| 1686 |
} |
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
protected function getInsertIdColumnForUpsert( $table ) { |
| 1693 |
return null; |
| 1694 |
} |
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
protected function getValueTypesForWithClause( $table ) { |
| 1701 |
return []; |
| 1702 |
} |
| 1703 |
|
| 1704 |
public function deleteJoin( |
| 1705 |
$delTable, |
| 1706 |
$joinTable, |
| 1707 |
$delVar, |
| 1708 |
$joinVar, |
| 1709 |
$conds, |
| 1710 |
$fname = __METHOD__ |
| 1711 |
) { |
| 1712 |
$sql = $this->platform->deleteJoinSqlText( $delTable, $joinTable, $delVar, $joinVar, $conds ); |
| 1713 |
$query = new Query( $sql, self::QUERY_CHANGE_ROWS, 'DELETE', $delTable ); |
| 1714 |
$this->query( $query, $fname ); |
| 1715 |
} |
| 1716 |
|
| 1717 |
public function delete( $table, $conds, $fname = __METHOD__ ) { |
| 1718 |
$this->query( $this->platform->deleteSqlText( $table, $conds ), $fname ); |
| 1719 |
|
| 1720 |
return true; |
| 1721 |
} |
| 1722 |
|
| 1723 |
final public function insertSelect( |
| 1724 |
$destTable, |
| 1725 |
$srcTable, |
| 1726 |
$varMap, |
| 1727 |
$conds, |
| 1728 |
$fname = __METHOD__, |
| 1729 |
$insertOptions = [], |
| 1730 |
$selectOptions = [], |
| 1731 |
$selectJoinConds = [] |
| 1732 |
) { |
| 1733 |
static $hints = [ 'NO_AUTO_COLUMNS' ]; |
| 1734 |
|
| 1735 |
$insertOptions = $this->platform->normalizeOptions( $insertOptions ); |
| 1736 |
$selectOptions = $this->platform->normalizeOptions( $selectOptions ); |
| 1737 |
|
| 1738 |
if ( $this->cliMode && $this->isInsertSelectSafe( $insertOptions, $selectOptions, $fname ) ) { |
| 1739 |
|
| 1740 |
|
| 1741 |
$this->doInsertSelectNative( |
| 1742 |
$destTable, |
| 1743 |
$srcTable, |
| 1744 |
$varMap, |
| 1745 |
$conds, |
| 1746 |
$fname, |
| 1747 |
array_diff( $insertOptions, $hints ), |
| 1748 |
$selectOptions, |
| 1749 |
$selectJoinConds |
| 1750 |
); |
| 1751 |
} else { |
| 1752 |
$this->doInsertSelectGeneric( |
| 1753 |
$destTable, |
| 1754 |
$srcTable, |
| 1755 |
$varMap, |
| 1756 |
$conds, |
| 1757 |
$fname, |
| 1758 |
array_diff( $insertOptions, $hints ), |
| 1759 |
$selectOptions, |
| 1760 |
$selectJoinConds |
| 1761 |
); |
| 1762 |
} |
| 1763 |
|
| 1764 |
return true; |
| 1765 |
} |
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
protected function isInsertSelectSafe( array $insertOptions, array $selectOptions, $fname ) { |
| 1775 |
return true; |
| 1776 |
} |
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
private function doInsertSelectGeneric( |
| 1793 |
$destTable, |
| 1794 |
$srcTable, |
| 1795 |
array $varMap, |
| 1796 |
$conds, |
| 1797 |
$fname, |
| 1798 |
array $insertOptions, |
| 1799 |
array $selectOptions, |
| 1800 |
$selectJoinConds |
| 1801 |
) { |
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
$fields = []; |
| 1806 |
foreach ( $varMap as $dstColumn => $sourceColumnOrSql ) { |
| 1807 |
$fields[] = $this->platform->fieldNameWithAlias( $sourceColumnOrSql, $dstColumn ); |
| 1808 |
} |
| 1809 |
$res = $this->select( |
| 1810 |
$srcTable, |
| 1811 |
implode( ',', $fields ), |
| 1812 |
$conds, |
| 1813 |
$fname, |
| 1814 |
array_merge( $selectOptions, [ 'FOR UPDATE' ] ), |
| 1815 |
$selectJoinConds |
| 1816 |
); |
| 1817 |
|
| 1818 |
$affectedRowCount = 0; |
| 1819 |
$insertId = null; |
| 1820 |
if ( $res ) { |
| 1821 |
$this->startAtomic( $fname, self::ATOMIC_CANCELABLE ); |
| 1822 |
try { |
| 1823 |
$rows = []; |
| 1824 |
foreach ( $res as $row ) { |
| 1825 |
$rows[] = (array)$row; |
| 1826 |
} |
| 1827 |
|
| 1828 |
$rowBatches = array_chunk( $rows, $this->nonNativeInsertSelectBatchSize ); |
| 1829 |
foreach ( $rowBatches as $rows ) { |
| 1830 |
$query = $this->platform->dispatchingInsertSqlText( $destTable, $rows, $insertOptions ); |
| 1831 |
$this->query( $query, $fname ); |
| 1832 |
$affectedRowCount += $this->lastQueryAffectedRows; |
| 1833 |
$insertId = $insertId ?: $this->lastQueryInsertId; |
| 1834 |
} |
| 1835 |
$this->endAtomic( $fname ); |
| 1836 |
} catch ( DBError $e ) { |
| 1837 |
$this->cancelAtomic( $fname ); |
| 1838 |
throw $e; |
| 1839 |
} |
| 1840 |
} |
| 1841 |
$this->lastEmulatedAffectedRows = $affectedRowCount; |
| 1842 |
$this->lastEmulatedInsertId = $insertId; |
| 1843 |
} |
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
protected function doInsertSelectNative( |
| 1861 |
$destTable, |
| 1862 |
$srcTable, |
| 1863 |
array $varMap, |
| 1864 |
$conds, |
| 1865 |
$fname, |
| 1866 |
array $insertOptions, |
| 1867 |
array $selectOptions, |
| 1868 |
$selectJoinConds |
| 1869 |
) { |
| 1870 |
$sql = $this->platform->insertSelectNativeSqlText( |
| 1871 |
$destTable, |
| 1872 |
$srcTable, |
| 1873 |
$varMap, |
| 1874 |
$conds, |
| 1875 |
$fname, |
| 1876 |
$insertOptions, |
| 1877 |
$selectOptions, |
| 1878 |
$selectJoinConds |
| 1879 |
); |
| 1880 |
$query = new Query( |
| 1881 |
$sql, |
| 1882 |
self::QUERY_CHANGE_ROWS, |
| 1883 |
'INSERT', |
| 1884 |
$destTable |
| 1885 |
); |
| 1886 |
$this->query( $query, $fname ); |
| 1887 |
} |
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
protected function isConnectionError( $errno ) { |
| 1897 |
return false; |
| 1898 |
} |
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
protected function isKnownStatementRollbackError( $errno ) { |
| 1908 |
return false; |
| 1909 |
} |
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
public function serverIsReadOnly() { |
| 1915 |
return false; |
| 1916 |
} |
| 1917 |
|
| 1918 |
final public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) { |
| 1919 |
$this->transactionManager->onTransactionResolution( $this, $callback, $fname ); |
| 1920 |
} |
| 1921 |
|
| 1922 |
final public function onTransactionCommitOrIdle( callable $callback, $fname = __METHOD__ ) { |
| 1923 |
if ( !$this->trxLevel() && $this->getTransactionRoundId() ) { |
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
$this->begin( __METHOD__, self::TRANSACTION_INTERNAL ); |
| 1928 |
} |
| 1929 |
|
| 1930 |
$this->transactionManager->addPostCommitOrIdleCallback( $callback, $fname ); |
| 1931 |
if ( !$this->trxLevel() ) { |
| 1932 |
$dbErrors = []; |
| 1933 |
$this->runOnTransactionIdleCallbacks( self::TRIGGER_IDLE, $dbErrors ); |
| 1934 |
if ( $dbErrors ) { |
| 1935 |
throw $dbErrors[0]; |
| 1936 |
} |
| 1937 |
} |
| 1938 |
} |
| 1939 |
|
| 1940 |
final public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) { |
| 1941 |
if ( !$this->trxLevel() && $this->getTransactionRoundId() ) { |
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
$this->begin( __METHOD__, self::TRANSACTION_INTERNAL ); |
| 1946 |
} |
| 1947 |
|
| 1948 |
if ( $this->trxLevel() ) { |
| 1949 |
$this->transactionManager->addPreCommitOrIdleCallback( |
| 1950 |
$callback, |
| 1951 |
$fname |
| 1952 |
); |
| 1953 |
} else { |
| 1954 |
|
| 1955 |
$this->startAtomic( __METHOD__, self::ATOMIC_CANCELABLE ); |
| 1956 |
try { |
| 1957 |
$callback( $this ); |
| 1958 |
} catch ( Throwable $e ) { |
| 1959 |
|
| 1960 |
if ( !$this->csmError ) { |
| 1961 |
$this->cancelAtomic( __METHOD__ ); |
| 1962 |
} |
| 1963 |
throw $e; |
| 1964 |
} |
| 1965 |
$this->endAtomic( __METHOD__ ); |
| 1966 |
} |
| 1967 |
} |
| 1968 |
|
| 1969 |
final public function onAtomicSectionCancel( callable $callback, $fname = __METHOD__ ) { |
| 1970 |
$this->transactionManager->onAtomicSectionCancel( $this, $callback, $fname ); |
| 1971 |
} |
| 1972 |
|
| 1973 |
final public function setTransactionListener( $name, ?callable $callback = null ) { |
| 1974 |
$this->transactionManager->setTransactionListener( $name, $callback ); |
| 1975 |
} |
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
final public function setTrxEndCallbackSuppression( $suppress ) { |
| 1986 |
$this->transactionManager->setTrxEndCallbackSuppression( $suppress ); |
| 1987 |
} |
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
public function runOnTransactionIdleCallbacks( $trigger, array &$errors = [] ) { |
| 2002 |
if ( $this->trxLevel() ) { |
| 2003 |
throw new DBUnexpectedError( $this, __METHOD__ . ': a transaction is still open' ); |
| 2004 |
} |
| 2005 |
|
| 2006 |
if ( $this->transactionManager->isEndCallbacksSuppressed() ) { |
| 2007 |
|
| 2008 |
return 0; |
| 2009 |
} |
| 2010 |
|
| 2011 |
$cs = $this->commenceCriticalSection( __METHOD__ ); |
| 2012 |
|
| 2013 |
$count = 0; |
| 2014 |
$autoTrx = $this->flagsHolder->hasImplicitTrxFlag(); |
| 2015 |
|
| 2016 |
do { |
| 2017 |
$callbackEntries = $this->transactionManager->consumeEndCallbacks( $trigger ); |
| 2018 |
$count += count( $callbackEntries ); |
| 2019 |
foreach ( $callbackEntries as $entry ) { |
| 2020 |
$this->flagsHolder->clearFlag( self::DBO_TRX ); |
| 2021 |
try { |
| 2022 |
$entry[0]( $trigger, $this ); |
| 2023 |
} catch ( DBError $ex ) { |
| 2024 |
call_user_func( $this->errorLogger, $ex ); |
| 2025 |
$errors[] = $ex; |
| 2026 |
|
| 2027 |
|
| 2028 |
if ( $this->trxLevel() ) { |
| 2029 |
$this->rollback( __METHOD__, self::FLUSHING_INTERNAL ); |
| 2030 |
} |
| 2031 |
} finally { |
| 2032 |
if ( $autoTrx ) { |
| 2033 |
$this->flagsHolder->setFlag( self::DBO_TRX ); |
| 2034 |
} else { |
| 2035 |
$this->flagsHolder->clearFlag( self::DBO_TRX ); |
| 2036 |
} |
| 2037 |
} |
| 2038 |
} |
| 2039 |
} while ( $this->transactionManager->countPostCommitOrIdleCallbacks() ); |
| 2040 |
|
| 2041 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2042 |
|
| 2043 |
return $count; |
| 2044 |
} |
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
|
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
public function runTransactionListenerCallbacks( $trigger, array &$errors = [] ) { |
| 2057 |
if ( $this->transactionManager->isEndCallbacksSuppressed() ) { |
| 2058 |
|
| 2059 |
return; |
| 2060 |
} |
| 2061 |
|
| 2062 |
|
| 2063 |
foreach ( $this->transactionManager->getRecurringCallbacks() as $callback ) { |
| 2064 |
try { |
| 2065 |
$callback( $trigger, $this ); |
| 2066 |
} catch ( DBError $ex ) { |
| 2067 |
( $this->errorLogger )( $ex ); |
| 2068 |
$errors[] = $ex; |
| 2069 |
} |
| 2070 |
} |
| 2071 |
} |
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
private function runTransactionPostCommitCallbacks() { |
| 2080 |
$dbErrors = []; |
| 2081 |
$this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT, $dbErrors ); |
| 2082 |
$this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT, $dbErrors ); |
| 2083 |
$this->lastEmulatedAffectedRows = 0; |
| 2084 |
if ( $dbErrors ) { |
| 2085 |
throw $dbErrors[0]; |
| 2086 |
} |
| 2087 |
} |
| 2088 |
|
| 2089 |
|
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
private function runTransactionPostRollbackCallbacks() { |
| 2097 |
$this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK ); |
| 2098 |
$this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK ); |
| 2099 |
$this->lastEmulatedAffectedRows = 0; |
| 2100 |
} |
| 2101 |
|
| 2102 |
final public function startAtomic( |
| 2103 |
$fname = __METHOD__, |
| 2104 |
$cancelable = self::ATOMIC_NOT_CANCELABLE |
| 2105 |
) { |
| 2106 |
$cs = $this->commenceCriticalSection( __METHOD__ ); |
| 2107 |
|
| 2108 |
if ( $this->trxLevel() ) { |
| 2109 |
|
| 2110 |
$sectionOwnsTrx = false; |
| 2111 |
} else { |
| 2112 |
|
| 2113 |
try { |
| 2114 |
$this->begin( $fname, self::TRANSACTION_INTERNAL ); |
| 2115 |
} catch ( DBError $e ) { |
| 2116 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2117 |
throw $e; |
| 2118 |
} |
| 2119 |
if ( $this->flagsHolder->hasImplicitTrxFlag() ) { |
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
$sectionOwnsTrx = false; |
| 2125 |
} else { |
| 2126 |
|
| 2127 |
|
| 2128 |
$sectionOwnsTrx = true; |
| 2129 |
} |
| 2130 |
$this->transactionManager->setAutomaticAtomic( $sectionOwnsTrx ); |
| 2131 |
} |
| 2132 |
|
| 2133 |
if ( $cancelable === self::ATOMIC_CANCELABLE ) { |
| 2134 |
if ( $sectionOwnsTrx ) { |
| 2135 |
|
| 2136 |
|
| 2137 |
$savepointId = self::NOT_APPLICABLE; |
| 2138 |
} else { |
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
try { |
| 2143 |
$savepointId = $this->transactionManager->nextSavePointId( $this, $fname ); |
| 2144 |
$sql = $this->platform->savepointSqlText( $savepointId ); |
| 2145 |
$query = new Query( $sql, self::QUERY_CHANGE_TRX, 'SAVEPOINT' ); |
| 2146 |
$this->query( $query, $fname ); |
| 2147 |
} catch ( DBError $e ) { |
| 2148 |
$this->completeCriticalSection( __METHOD__, $cs, $e ); |
| 2149 |
throw $e; |
| 2150 |
} |
| 2151 |
} |
| 2152 |
} else { |
| 2153 |
$savepointId = null; |
| 2154 |
} |
| 2155 |
|
| 2156 |
$sectionId = new AtomicSectionIdentifier; |
| 2157 |
$this->transactionManager->addToAtomicLevels( $fname, $sectionId, $savepointId ); |
| 2158 |
|
| 2159 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2160 |
|
| 2161 |
return $sectionId; |
| 2162 |
} |
| 2163 |
|
| 2164 |
final public function endAtomic( $fname = __METHOD__ ) { |
| 2165 |
[ $savepointId, $sectionId ] = $this->transactionManager->onEndAtomic( $this, $fname ); |
| 2166 |
|
| 2167 |
$runPostCommitCallbacks = false; |
| 2168 |
|
| 2169 |
$cs = $this->commenceCriticalSection( __METHOD__ ); |
| 2170 |
|
| 2171 |
|
| 2172 |
$finalLevelOfImplicitTrxPopped = $this->transactionManager->popAtomicLevel(); |
| 2173 |
|
| 2174 |
try { |
| 2175 |
if ( $finalLevelOfImplicitTrxPopped ) { |
| 2176 |
$this->commit( $fname, self::FLUSHING_INTERNAL ); |
| 2177 |
$runPostCommitCallbacks = true; |
| 2178 |
} elseif ( $savepointId !== null && $savepointId !== self::NOT_APPLICABLE ) { |
| 2179 |
$sql = $this->platform->releaseSavepointSqlText( $savepointId ); |
| 2180 |
$query = new Query( $sql, self::QUERY_CHANGE_TRX, 'RELEASE SAVEPOINT' ); |
| 2181 |
$this->query( $query, $fname ); |
| 2182 |
} |
| 2183 |
} catch ( DBError $e ) { |
| 2184 |
$this->completeCriticalSection( __METHOD__, $cs, $e ); |
| 2185 |
throw $e; |
| 2186 |
} |
| 2187 |
|
| 2188 |
$this->transactionManager->onEndAtomicInCriticalSection( $sectionId ); |
| 2189 |
|
| 2190 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2191 |
|
| 2192 |
if ( $runPostCommitCallbacks ) { |
| 2193 |
$this->runTransactionPostCommitCallbacks(); |
| 2194 |
} |
| 2195 |
} |
| 2196 |
|
| 2197 |
final public function cancelAtomic( |
| 2198 |
$fname = __METHOD__, |
| 2199 |
?AtomicSectionIdentifier $sectionId = null |
| 2200 |
) { |
| 2201 |
$this->transactionManager->onCancelAtomicBeforeCriticalSection( $this, $fname ); |
| 2202 |
$pos = $this->transactionManager->getPositionFromSectionId( $sectionId ); |
| 2203 |
if ( $pos < 0 ) { |
| 2204 |
throw new DBUnexpectedError( $this, "Atomic section not found (for $fname)" ); |
| 2205 |
} |
| 2206 |
|
| 2207 |
$cs = $this->commenceCriticalSection( __METHOD__ ); |
| 2208 |
$runPostRollbackCallbacks = false; |
| 2209 |
[ $savedFname, $excisedSectionIds, $newTopSectionId, $savedSectionId, $savepointId ] = |
| 2210 |
$this->transactionManager->cancelAtomic( $pos ); |
| 2211 |
|
| 2212 |
try { |
| 2213 |
if ( $savedFname !== $fname ) { |
| 2214 |
$e = new DBUnexpectedError( |
| 2215 |
$this, |
| 2216 |
"Invalid atomic section ended (got $fname but expected $savedFname)" |
| 2217 |
); |
| 2218 |
$this->completeCriticalSection( __METHOD__, $cs, $e ); |
| 2219 |
throw $e; |
| 2220 |
} |
| 2221 |
|
| 2222 |
|
| 2223 |
$this->transactionManager->popAtomicLevel(); |
| 2224 |
$excisedSectionIds[] = $savedSectionId; |
| 2225 |
$newTopSectionId = $this->transactionManager->currentAtomicSectionId(); |
| 2226 |
|
| 2227 |
if ( $savepointId !== null ) { |
| 2228 |
|
| 2229 |
if ( $savepointId === self::NOT_APPLICABLE ) { |
| 2230 |
|
| 2231 |
|
| 2232 |
$this->rollback( $fname, self::FLUSHING_INTERNAL ); |
| 2233 |
$runPostRollbackCallbacks = true; |
| 2234 |
} else { |
| 2235 |
|
| 2236 |
|
| 2237 |
$sql = $this->platform->rollbackToSavepointSqlText( $savepointId ); |
| 2238 |
$query = new Query( $sql, self::QUERY_CHANGE_TRX, 'ROLLBACK TO SAVEPOINT' ); |
| 2239 |
$this->query( $query, $fname ); |
| 2240 |
$this->transactionManager->setTrxStatusToOk(); |
| 2241 |
$this->transactionManager->runOnAtomicSectionCancelCallbacks( |
| 2242 |
$this, |
| 2243 |
self::TRIGGER_CANCEL, |
| 2244 |
$excisedSectionIds |
| 2245 |
); |
| 2246 |
} |
| 2247 |
} else { |
| 2248 |
|
| 2249 |
$trxError = new DBUnexpectedError( |
| 2250 |
$this, |
| 2251 |
"Uncancelable atomic section canceled (got $fname)" |
| 2252 |
); |
| 2253 |
$this->transactionManager->setTransactionError( $trxError ); |
| 2254 |
} |
| 2255 |
} finally { |
| 2256 |
|
| 2257 |
|
| 2258 |
$this->transactionManager->modifyCallbacksForCancel( |
| 2259 |
$excisedSectionIds, |
| 2260 |
$newTopSectionId |
| 2261 |
); |
| 2262 |
} |
| 2263 |
|
| 2264 |
$this->lastEmulatedAffectedRows = 0; |
| 2265 |
|
| 2266 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2267 |
|
| 2268 |
if ( $runPostRollbackCallbacks ) { |
| 2269 |
$this->runTransactionPostRollbackCallbacks(); |
| 2270 |
} |
| 2271 |
} |
| 2272 |
|
| 2273 |
final public function doAtomicSection( |
| 2274 |
$fname, |
| 2275 |
callable $callback, |
| 2276 |
$cancelable = self::ATOMIC_NOT_CANCELABLE |
| 2277 |
) { |
| 2278 |
$sectionId = $this->startAtomic( $fname, $cancelable ); |
| 2279 |
try { |
| 2280 |
$res = $callback( $this, $fname ); |
| 2281 |
} catch ( Throwable $e ) { |
| 2282 |
|
| 2283 |
if ( !$this->csmError ) { |
| 2284 |
$this->cancelAtomic( $fname, $sectionId ); |
| 2285 |
} |
| 2286 |
|
| 2287 |
throw $e; |
| 2288 |
} |
| 2289 |
$this->endAtomic( $fname ); |
| 2290 |
|
| 2291 |
return $res; |
| 2292 |
} |
| 2293 |
|
| 2294 |
final public function begin( $fname = __METHOD__, $mode = self::TRANSACTION_EXPLICIT ) { |
| 2295 |
static $modes = [ self::TRANSACTION_EXPLICIT, self::TRANSACTION_INTERNAL ]; |
| 2296 |
if ( !in_array( $mode, $modes, true ) ) { |
| 2297 |
throw new DBUnexpectedError( $this, "$fname: invalid mode parameter '$mode'" ); |
| 2298 |
} |
| 2299 |
|
| 2300 |
$this->transactionManager->onBegin( $this, $fname ); |
| 2301 |
|
| 2302 |
if ( $this->flagsHolder->hasImplicitTrxFlag() && $mode !== self::TRANSACTION_INTERNAL ) { |
| 2303 |
$msg = "$fname: implicit transaction expected (DBO_TRX set)"; |
| 2304 |
throw new DBUnexpectedError( $this, $msg ); |
| 2305 |
} |
| 2306 |
|
| 2307 |
$this->assertHasConnectionHandle(); |
| 2308 |
|
| 2309 |
$cs = $this->commenceCriticalSection( __METHOD__ ); |
| 2310 |
$timeStart = microtime( true ); |
| 2311 |
try { |
| 2312 |
$this->doBegin( $fname ); |
| 2313 |
} catch ( DBError $e ) { |
| 2314 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2315 |
throw $e; |
| 2316 |
} |
| 2317 |
$timeEnd = microtime( true ); |
| 2318 |
|
| 2319 |
$rtt = max( $timeEnd - $timeStart, 0.0 ); |
| 2320 |
$this->transactionManager->onBeginInCriticalSection( $mode, $fname, $rtt ); |
| 2321 |
$this->replicationReporter->resetReplicationLagStatus( $this ); |
| 2322 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2323 |
} |
| 2324 |
|
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
protected function doBegin( $fname ) { |
| 2333 |
$query = new Query( 'BEGIN', self::QUERY_CHANGE_TRX, 'BEGIN' ); |
| 2334 |
$this->query( $query, $fname ); |
| 2335 |
} |
| 2336 |
|
| 2337 |
final public function commit( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) { |
| 2338 |
static $modes = [ self::FLUSHING_ONE, self::FLUSHING_ALL_PEERS, self::FLUSHING_INTERNAL ]; |
| 2339 |
if ( !in_array( $flush, $modes, true ) ) { |
| 2340 |
throw new DBUnexpectedError( $this, "$fname: invalid flush parameter '$flush'" ); |
| 2341 |
} |
| 2342 |
|
| 2343 |
if ( !$this->transactionManager->onCommit( $this, $fname, $flush ) ) { |
| 2344 |
return; |
| 2345 |
} |
| 2346 |
|
| 2347 |
$this->assertHasConnectionHandle(); |
| 2348 |
|
| 2349 |
$this->runOnTransactionPreCommitCallbacks(); |
| 2350 |
|
| 2351 |
$cs = $this->commenceCriticalSection( __METHOD__ ); |
| 2352 |
try { |
| 2353 |
if ( $this->trxLevel() ) { |
| 2354 |
$query = new Query( 'COMMIT', self::QUERY_CHANGE_TRX, 'COMMIT' ); |
| 2355 |
$this->query( $query, $fname ); |
| 2356 |
} |
| 2357 |
} catch ( DBError $e ) { |
| 2358 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2359 |
throw $e; |
| 2360 |
} |
| 2361 |
$lastWriteTime = $this->transactionManager->onCommitInCriticalSection( $this ); |
| 2362 |
if ( $lastWriteTime ) { |
| 2363 |
$this->lastWriteTime = $lastWriteTime; |
| 2364 |
} |
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
if ( $flush === self::FLUSHING_ONE ) { |
| 2369 |
$this->runTransactionPostCommitCallbacks(); |
| 2370 |
} |
| 2371 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2372 |
} |
| 2373 |
|
| 2374 |
final public function rollback( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) { |
| 2375 |
if ( |
| 2376 |
$flush !== self::FLUSHING_INTERNAL && |
| 2377 |
$flush !== self::FLUSHING_ALL_PEERS && |
| 2378 |
$this->flagsHolder->hasImplicitTrxFlag() |
| 2379 |
) { |
| 2380 |
throw new DBUnexpectedError( |
| 2381 |
$this, |
| 2382 |
"$fname: Expected mass rollback of all peer transactions (DBO_TRX set)" |
| 2383 |
); |
| 2384 |
} |
| 2385 |
|
| 2386 |
if ( !$this->trxLevel() ) { |
| 2387 |
$this->transactionManager->setTrxStatusToNone(); |
| 2388 |
$this->transactionManager->clearPreEndCallbacks(); |
| 2389 |
if ( $this->transactionManager->trxLevel() === TransactionManager::STATUS_TRX_ERROR ) { |
| 2390 |
$this->logger->info( |
| 2391 |
"$fname: acknowledged server-side transaction loss on {db_server}", |
| 2392 |
$this->getLogContext() |
| 2393 |
); |
| 2394 |
} |
| 2395 |
|
| 2396 |
return; |
| 2397 |
} |
| 2398 |
|
| 2399 |
$this->assertHasConnectionHandle(); |
| 2400 |
|
| 2401 |
if ( $this->csmError ) { |
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
$this->logger->info( |
| 2407 |
"$fname: acknowledged client-side transaction loss on {db_server}", |
| 2408 |
$this->getLogContext() |
| 2409 |
); |
| 2410 |
|
| 2411 |
return; |
| 2412 |
} |
| 2413 |
|
| 2414 |
$cs = $this->commenceCriticalSection( __METHOD__ ); |
| 2415 |
if ( $this->trxLevel() ) { |
| 2416 |
|
| 2417 |
$query = new Query( |
| 2418 |
$this->platform->rollbackSqlText(), |
| 2419 |
self::QUERY_SILENCE_ERRORS | self::QUERY_CHANGE_TRX, |
| 2420 |
'ROLLBACK' |
| 2421 |
); |
| 2422 |
$this->query( $query, $fname ); |
| 2423 |
} |
| 2424 |
$this->transactionManager->onRollbackInCriticalSection( $this ); |
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
if ( $flush === self::FLUSHING_ONE ) { |
| 2429 |
$this->runTransactionPostRollbackCallbacks(); |
| 2430 |
} |
| 2431 |
$this->completeCriticalSection( __METHOD__, $cs ); |
| 2432 |
} |
| 2433 |
|
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
public function setTransactionManager( TransactionManager $transactionManager ) { |
| 2439 |
$this->transactionManager = $transactionManager; |
| 2440 |
} |
| 2441 |
|
| 2442 |
public function flushSession( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) { |
| 2443 |
if ( |
| 2444 |
$flush !== self::FLUSHING_INTERNAL && |
| 2445 |
$flush !== self::FLUSHING_ALL_PEERS && |
| 2446 |
$this->flagsHolder->hasImplicitTrxFlag() |
| 2447 |
) { |
| 2448 |
throw new DBUnexpectedError( |
| 2449 |
$this, |
| 2450 |
"$fname: Expected mass flush of all peer connections (DBO_TRX set)" |
| 2451 |
); |
| 2452 |
} |
| 2453 |
|
| 2454 |
if ( $this->csmError ) { |
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
$this->logger->info( |
| 2460 |
"$fname: acknowledged client-side session loss on {db_server}", |
| 2461 |
$this->getLogContext() |
| 2462 |
); |
| 2463 |
$this->csmError = null; |
| 2464 |
$this->csmFname = null; |
| 2465 |
$this->replaceLostConnection( 2048, __METHOD__ ); |
| 2466 |
|
| 2467 |
return; |
| 2468 |
} |
| 2469 |
|
| 2470 |
if ( $this->trxLevel() ) { |
| 2471 |
|
| 2472 |
throw new DBUnexpectedError( |
| 2473 |
$this, |
| 2474 |
"$fname: transaction still in progress (not yet rolled back)" |
| 2475 |
); |
| 2476 |
} |
| 2477 |
|
| 2478 |
if ( $this->transactionManager->sessionStatus() === TransactionManager::STATUS_SESS_ERROR ) { |
| 2479 |
|
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
|
| 2484 |
$this->logger->info( |
| 2485 |
"$fname: acknowledged server-side session loss on {db_server}", |
| 2486 |
$this->getLogContext() |
| 2487 |
); |
| 2488 |
} elseif ( $this->isOpen() ) { |
| 2489 |
|
| 2490 |
$this->doFlushSession( $fname ); |
| 2491 |
$this->sessionNamedLocks = []; |
| 2492 |
} |
| 2493 |
|
| 2494 |
$this->transactionManager->clearSessionError(); |
| 2495 |
} |
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
|
| 2505 |
protected function doFlushSession( $fname ) { |
| 2506 |
|
| 2507 |
} |
| 2508 |
|
| 2509 |
public function flushSnapshot( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) { |
| 2510 |
$this->transactionManager->onFlushSnapshot( $this, $fname, $flush, $this->getTransactionRoundId() ); |
| 2511 |
if ( |
| 2512 |
$this->transactionManager->sessionStatus() === TransactionManager::STATUS_SESS_ERROR || |
| 2513 |
$this->transactionManager->trxStatus() === TransactionManager::STATUS_TRX_ERROR |
| 2514 |
) { |
| 2515 |
$this->rollback( $fname, self::FLUSHING_INTERNAL ); |
| 2516 |
} else { |
| 2517 |
$this->commit( $fname, self::FLUSHING_INTERNAL ); |
| 2518 |
} |
| 2519 |
} |
| 2520 |
|
| 2521 |
public function duplicateTableStructure( |
| 2522 |
$oldName, |
| 2523 |
$newName, |
| 2524 |
$temporary = false, |
| 2525 |
$fname = __METHOD__ |
| 2526 |
) { |
| 2527 |
throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' ); |
| 2528 |
} |
| 2529 |
|
| 2530 |
public function listTables( $prefix = null, $fname = __METHOD__ ) { |
| 2531 |
throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' ); |
| 2532 |
} |
| 2533 |
|
| 2534 |
public function affectedRows() { |
| 2535 |
$this->lastEmulatedAffectedRows ??= $this->lastQueryAffectedRows; |
| 2536 |
|
| 2537 |
return $this->lastEmulatedAffectedRows; |
| 2538 |
} |
| 2539 |
|
| 2540 |
public function insertId() { |
| 2541 |
if ( $this->lastEmulatedInsertId === null ) { |
| 2542 |
|
| 2543 |
|
| 2544 |
if ( $this->affectedRows() ) { |
| 2545 |
$this->lastEmulatedInsertId = $this->lastInsertId(); |
| 2546 |
} else { |
| 2547 |
$this->lastEmulatedInsertId = 0; |
| 2548 |
} |
| 2549 |
} |
| 2550 |
|
| 2551 |
return $this->lastEmulatedInsertId; |
| 2552 |
} |
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
|
| 2562 |
|
| 2563 |
|
| 2564 |
|
| 2565 |
|
| 2566 |
|
| 2567 |
abstract protected function lastInsertId(); |
| 2568 |
|
| 2569 |
public function ping() { |
| 2570 |
if ( $this->isOpen() ) { |
| 2571 |
|
| 2572 |
if ( ( microtime( true ) - $this->lastPing ) < self::PING_TTL ) { |
| 2573 |
return true; |
| 2574 |
} |
| 2575 |
|
| 2576 |
|
| 2577 |
$query = new Query( |
| 2578 |
self::PING_QUERY, |
| 2579 |
self::QUERY_IGNORE_DBO_TRX | self::QUERY_SILENCE_ERRORS | self::QUERY_CHANGE_NONE, |
| 2580 |
'SELECT' |
| 2581 |
); |
| 2582 |
$res = $this->query( $query, __METHOD__ ); |
| 2583 |
$ok = ( $res !== false ); |
| 2584 |
} else { |
| 2585 |
|
| 2586 |
$ok = $this->replaceLostConnection( null, __METHOD__ ); |
| 2587 |
} |
| 2588 |
|
| 2589 |
return $ok; |
| 2590 |
} |
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
protected function replaceLostConnection( $lastErrno, $fname ) { |
| 2600 |
if ( $this->conn ) { |
| 2601 |
$this->closeConnection(); |
| 2602 |
$this->conn = null; |
| 2603 |
$this->handleSessionLossPreconnect(); |
| 2604 |
} |
| 2605 |
|
| 2606 |
try { |
| 2607 |
$this->open( |
| 2608 |
$this->connectionParams[self::CONN_HOST], |
| 2609 |
$this->connectionParams[self::CONN_USER], |
| 2610 |
$this->connectionParams[self::CONN_PASSWORD], |
| 2611 |
$this->currentDomain->getDatabase(), |
| 2612 |
$this->currentDomain->getSchema(), |
| 2613 |
$this->tablePrefix() |
| 2614 |
); |
| 2615 |
$this->lastPing = microtime( true ); |
| 2616 |
$ok = true; |
| 2617 |
|
| 2618 |
$this->logger->warning( |
| 2619 |
$fname . ': lost connection to {db_server} with error {errno}; reconnected', |
| 2620 |
$this->getLogContext( [ |
| 2621 |
'exception' => new RuntimeException(), |
| 2622 |
'db_log_category' => 'connection', |
| 2623 |
'errno' => $lastErrno |
| 2624 |
] ) |
| 2625 |
); |
| 2626 |
} catch ( DBConnectionError $e ) { |
| 2627 |
$ok = false; |
| 2628 |
|
| 2629 |
$this->logger->error( |
| 2630 |
$fname . ': lost connection to {db_server} with error {errno}; reconnection failed: {connect_msg}', |
| 2631 |
$this->getLogContext( [ |
| 2632 |
'exception' => new RuntimeException(), |
| 2633 |
'db_log_category' => 'connection', |
| 2634 |
'errno' => $lastErrno, |
| 2635 |
'connect_msg' => $e->getMessage() |
| 2636 |
] ) |
| 2637 |
); |
| 2638 |
} |
| 2639 |
|
| 2640 |
|
| 2641 |
|
| 2642 |
$this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK ); |
| 2643 |
|
| 2644 |
|
| 2645 |
$this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK ); |
| 2646 |
|
| 2647 |
return $ok; |
| 2648 |
} |
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
|
| 2658 |
|
| 2659 |
|
| 2660 |
|
| 2661 |
|
| 2662 |
|
| 2663 |
|
| 2664 |
|
| 2665 |
|
| 2666 |
|
| 2667 |
|
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
|
| 2672 |
public static function getCacheSetOptions( ?IReadableDatabase ...$dbs ) { |
| 2673 |
$res = [ 'lag' => 0, 'since' => INF, 'pending' => false ]; |
| 2674 |
|
| 2675 |
foreach ( func_get_args() as $db ) { |
| 2676 |
if ( $db instanceof IReadableDatabase ) { |
| 2677 |
$status = $db->getSessionLagStatus(); |
| 2678 |
|
| 2679 |
if ( $status['lag'] === false ) { |
| 2680 |
$res['lag'] = false; |
| 2681 |
} elseif ( $res['lag'] !== false ) { |
| 2682 |
$res['lag'] = max( $res['lag'], $status['lag'] ); |
| 2683 |
} |
| 2684 |
$res['since'] = min( $res['since'], $status['since'] ); |
| 2685 |
} |
| 2686 |
|
| 2687 |
if ( $db instanceof IDatabaseForOwner ) { |
| 2688 |
$res['pending'] = $res['pending'] ?: $db->writesPending(); |
| 2689 |
} |
| 2690 |
} |
| 2691 |
|
| 2692 |
return $res; |
| 2693 |
} |
| 2694 |
|
| 2695 |
public function encodeBlob( $b ) { |
| 2696 |
return $b; |
| 2697 |
} |
| 2698 |
|
| 2699 |
public function decodeBlob( $b ) { |
| 2700 |
if ( $b instanceof Blob ) { |
| 2701 |
$b = $b->fetch(); |
| 2702 |
} |
| 2703 |
return $b; |
| 2704 |
} |
| 2705 |
|
| 2706 |
public function setSessionOptions( array $options ) { |
| 2707 |
} |
| 2708 |
|
| 2709 |
public function sourceFile( |
| 2710 |
$filename, |
| 2711 |
?callable $lineCallback = null, |
| 2712 |
?callable $resultCallback = null, |
| 2713 |
$fname = false, |
| 2714 |
?callable $inputCallback = null |
| 2715 |
) { |
| 2716 |
AtEase::suppressWarnings(); |
| 2717 |
$fp = fopen( $filename, 'r' ); |
| 2718 |
AtEase::restoreWarnings(); |
| 2719 |
|
| 2720 |
if ( $fp === false ) { |
| 2721 |
throw new RuntimeException( "Could not open \"{$filename}\"" ); |
| 2722 |
} |
| 2723 |
|
| 2724 |
if ( !$fname ) { |
| 2725 |
$fname = __METHOD__ . "( $filename )"; |
| 2726 |
} |
| 2727 |
|
| 2728 |
try { |
| 2729 |
return $this->sourceStream( |
| 2730 |
$fp, |
| 2731 |
$lineCallback, |
| 2732 |
$resultCallback, |
| 2733 |
$fname, |
| 2734 |
$inputCallback |
| 2735 |
); |
| 2736 |
} finally { |
| 2737 |
fclose( $fp ); |
| 2738 |
} |
| 2739 |
} |
| 2740 |
|
| 2741 |
public function sourceStream( |
| 2742 |
$fp, |
| 2743 |
?callable $lineCallback = null, |
| 2744 |
?callable $resultCallback = null, |
| 2745 |
$fname = __METHOD__, |
| 2746 |
?callable $inputCallback = null |
| 2747 |
) { |
| 2748 |
$delimiterReset = new ScopedCallback( |
| 2749 |
function ( $delimiter ) { |
| 2750 |
$this->delimiter = $delimiter; |
| 2751 |
}, |
| 2752 |
[ $this->delimiter ] |
| 2753 |
); |
| 2754 |
$cmd = ''; |
| 2755 |
|
| 2756 |
while ( !feof( $fp ) ) { |
| 2757 |
if ( $lineCallback ) { |
| 2758 |
call_user_func( $lineCallback ); |
| 2759 |
} |
| 2760 |
|
| 2761 |
$line = trim( fgets( $fp ) ); |
| 2762 |
|
| 2763 |
if ( $line == '' ) { |
| 2764 |
continue; |
| 2765 |
} |
| 2766 |
|
| 2767 |
if ( $line[0] == '-' && $line[1] == '-' ) { |
| 2768 |
continue; |
| 2769 |
} |
| 2770 |
|
| 2771 |
if ( $cmd != '' ) { |
| 2772 |
$cmd .= ' '; |
| 2773 |
} |
| 2774 |
|
| 2775 |
$done = $this->streamStatementEnd( $cmd, $line ); |
| 2776 |
|
| 2777 |
$cmd .= "$line\n"; |
| 2778 |
|
| 2779 |
if ( $done || feof( $fp ) ) { |
| 2780 |
$cmd = $this->platform->replaceVars( $cmd ); |
| 2781 |
|
| 2782 |
if ( $inputCallback ) { |
| 2783 |
$callbackResult = $inputCallback( $cmd ); |
| 2784 |
|
| 2785 |
if ( is_string( $callbackResult ) || !$callbackResult ) { |
| 2786 |
$cmd = $callbackResult; |
| 2787 |
} |
| 2788 |
} |
| 2789 |
|
| 2790 |
if ( $cmd ) { |
| 2791 |
$res = $this->query( $cmd, $fname ); |
| 2792 |
|
| 2793 |
if ( $resultCallback ) { |
| 2794 |
$resultCallback( $res, $this ); |
| 2795 |
} |
| 2796 |
|
| 2797 |
if ( $res === false ) { |
| 2798 |
$err = $this->lastError(); |
| 2799 |
|
| 2800 |
return "Query \"{$cmd}\" failed with error code \"$err\".\n"; |
| 2801 |
} |
| 2802 |
} |
| 2803 |
$cmd = ''; |
| 2804 |
} |
| 2805 |
} |
| 2806 |
|
| 2807 |
ScopedCallback::consume( $delimiterReset ); |
| 2808 |
return true; |
| 2809 |
} |
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
|
| 2815 |
|
| 2816 |
|
| 2817 |
|
| 2818 |
public function streamStatementEnd( &$sql, &$newLine ) { |
| 2819 |
if ( $this->delimiter ) { |
| 2820 |
$prev = $newLine; |
| 2821 |
$newLine = preg_replace( |
| 2822 |
'/' . preg_quote( $this->delimiter, '/' ) . '$/', |
| 2823 |
'', |
| 2824 |
$newLine |
| 2825 |
); |
| 2826 |
if ( $newLine != $prev ) { |
| 2827 |
return true; |
| 2828 |
} |
| 2829 |
} |
| 2830 |
|
| 2831 |
return false; |
| 2832 |
} |
| 2833 |
|
| 2834 |
|
| 2835 |
|
| 2836 |
|
| 2837 |
public function lockIsFree( $lockName, $method ) { |
| 2838 |
|
| 2839 |
|
| 2840 |
|
| 2841 |
if ( isset( $this->sessionNamedLocks[$lockName] ) ) { |
| 2842 |
$lockIsFree = false; |
| 2843 |
} else { |
| 2844 |
$lockIsFree = $this->doLockIsFree( $lockName, $method ); |
| 2845 |
} |
| 2846 |
|
| 2847 |
return $lockIsFree; |
| 2848 |
} |
| 2849 |
|
| 2850 |
|
| 2851 |
|
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
|
| 2856 |
|
| 2857 |
|
| 2858 |
protected function doLockIsFree( string $lockName, string $method ) { |
| 2859 |
return true; |
| 2860 |
} |
| 2861 |
|
| 2862 |
|
| 2863 |
|
| 2864 |
|
| 2865 |
public function lock( $lockName, $method, $timeout = 5, $flags = 0 ) { |
| 2866 |
$lockTsUnix = $this->doLock( $lockName, $method, $timeout ); |
| 2867 |
if ( $lockTsUnix !== null ) { |
| 2868 |
$locked = true; |
| 2869 |
$this->sessionNamedLocks[$lockName] = [ |
| 2870 |
'ts' => $lockTsUnix, |
| 2871 |
'trxId' => $this->transactionManager->getTrxId() |
| 2872 |
]; |
| 2873 |
} else { |
| 2874 |
$locked = false; |
| 2875 |
$this->logger->info( |
| 2876 |
__METHOD__ . ": failed to acquire lock '{lockname}'", |
| 2877 |
[ |
| 2878 |
'lockname' => $lockName, |
| 2879 |
'db_log_category' => 'locking' |
| 2880 |
] |
| 2881 |
); |
| 2882 |
} |
| 2883 |
|
| 2884 |
return $this->flagsHolder::contains( $flags, self::LOCK_TIMESTAMP ) ? $lockTsUnix : $locked; |
| 2885 |
} |
| 2886 |
|
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
|
| 2891 |
|
| 2892 |
|
| 2893 |
|
| 2894 |
|
| 2895 |
|
| 2896 |
protected function doLock( string $lockName, string $method, int $timeout ) { |
| 2897 |
return microtime( true ); |
| 2898 |
} |
| 2899 |
|
| 2900 |
|
| 2901 |
|
| 2902 |
|
| 2903 |
public function unlock( $lockName, $method ) { |
| 2904 |
if ( !isset( $this->sessionNamedLocks[$lockName] ) ) { |
| 2905 |
$released = false; |
| 2906 |
$this->logger->warning( |
| 2907 |
__METHOD__ . ": trying to release unheld lock '$lockName'\n", |
| 2908 |
[ 'db_log_category' => 'locking' ] |
| 2909 |
); |
| 2910 |
} else { |
| 2911 |
$released = $this->doUnlock( $lockName, $method ); |
| 2912 |
if ( $released ) { |
| 2913 |
unset( $this->sessionNamedLocks[$lockName] ); |
| 2914 |
} else { |
| 2915 |
$this->logger->warning( |
| 2916 |
__METHOD__ . ": failed to release lock '$lockName'\n", |
| 2917 |
[ 'db_log_category' => 'locking' ] |
| 2918 |
); |
| 2919 |
} |
| 2920 |
} |
| 2921 |
|
| 2922 |
return $released; |
| 2923 |
} |
| 2924 |
|
| 2925 |
|
| 2926 |
|
| 2927 |
|
| 2928 |
|
| 2929 |
|
| 2930 |
|
| 2931 |
|
| 2932 |
|
| 2933 |
protected function doUnlock( string $lockName, string $method ) { |
| 2934 |
return true; |
| 2935 |
} |
| 2936 |
|
| 2937 |
public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) { |
| 2938 |
$this->transactionManager->onGetScopedLockAndFlush( $this, $fname ); |
| 2939 |
|
| 2940 |
if ( !$this->lock( $lockKey, $fname, $timeout ) ) { |
| 2941 |
return null; |
| 2942 |
} |
| 2943 |
|
| 2944 |
$unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) { |
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
|
| 2949 |
if ( |
| 2950 |
$this->transactionManager->sessionStatus() === TransactionManager::STATUS_SESS_ERROR || |
| 2951 |
$this->transactionManager->trxStatus() === TransactionManager::STATUS_TRX_ERROR |
| 2952 |
) { |
| 2953 |
return; |
| 2954 |
} |
| 2955 |
if ( $this->trxLevel() ) { |
| 2956 |
$this->onTransactionResolution( |
| 2957 |
function () use ( $lockKey, $fname ) { |
| 2958 |
$this->unlock( $lockKey, $fname ); |
| 2959 |
}, |
| 2960 |
$fname |
| 2961 |
); |
| 2962 |
} else { |
| 2963 |
$this->unlock( $lockKey, $fname ); |
| 2964 |
} |
| 2965 |
} ); |
| 2966 |
|
| 2967 |
$this->commit( $fname, self::FLUSHING_INTERNAL ); |
| 2968 |
|
| 2969 |
return $unlocker; |
| 2970 |
} |
| 2971 |
|
| 2972 |
public function dropTable( $table, $fname = __METHOD__ ) { |
| 2973 |
if ( !$this->tableExists( $table, $fname ) ) { |
| 2974 |
return false; |
| 2975 |
} |
| 2976 |
|
| 2977 |
$query = new Query( |
| 2978 |
$this->platform->dropTableSqlText( $table ), |
| 2979 |
self::QUERY_CHANGE_SCHEMA, |
| 2980 |
'DROP', |
| 2981 |
$table |
| 2982 |
); |
| 2983 |
$this->query( $query, $fname ); |
| 2984 |
|
| 2985 |
return true; |
| 2986 |
} |
| 2987 |
|
| 2988 |
public function truncateTable( $table, $fname = __METHOD__ ) { |
| 2989 |
$sql = "TRUNCATE TABLE " . $this->tableName( $table ); |
| 2990 |
$query = new Query( $sql, self::QUERY_CHANGE_SCHEMA, 'TRUNCATE', $table ); |
| 2991 |
$this->query( $query, $fname ); |
| 2992 |
} |
| 2993 |
|
| 2994 |
public function isReadOnly() { |
| 2995 |
return ( $this->getReadOnlyReason() !== null ); |
| 2996 |
} |
| 2997 |
|
| 2998 |
|
| 2999 |
|
| 3000 |
|
| 3001 |
protected function getReadOnlyReason() { |
| 3002 |
$reason = $this->replicationReporter->getTopologyBasedReadOnlyReason(); |
| 3003 |
if ( $reason ) { |
| 3004 |
return $reason; |
| 3005 |
} |
| 3006 |
|
| 3007 |
$reason = $this->getLBInfo( self::LB_READ_ONLY_REASON ); |
| 3008 |
if ( is_string( $reason ) ) { |
| 3009 |
return [ $reason, 'lb' ]; |
| 3010 |
} |
| 3011 |
|
| 3012 |
return null; |
| 3013 |
} |
| 3014 |
|
| 3015 |
|
| 3016 |
|
| 3017 |
|
| 3018 |
|
| 3019 |
|
| 3020 |
|
| 3021 |
|
| 3022 |
|
| 3023 |
|
| 3024 |
|
| 3025 |
|
| 3026 |
protected function getBindingHandle() { |
| 3027 |
if ( !$this->conn ) { |
| 3028 |
throw new DBUnexpectedError( |
| 3029 |
$this, |
| 3030 |
'DB connection was already closed or the connection dropped' |
| 3031 |
); |
| 3032 |
} |
| 3033 |
|
| 3034 |
return $this->conn; |
| 3035 |
} |
| 3036 |
|
| 3037 |
|
| 3038 |
|
| 3039 |
|
| 3040 |
|
| 3041 |
|
| 3042 |
|
| 3043 |
|
| 3044 |
|
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 3048 |
|
| 3049 |
|
| 3050 |
|
| 3051 |
|
| 3052 |
|
| 3053 |
|
| 3054 |
|
| 3055 |
|
| 3056 |
|
| 3057 |
|
| 3058 |
|
| 3059 |
|
| 3060 |
|
| 3061 |
|
| 3062 |
|
| 3063 |
|
| 3064 |
|
| 3065 |
|
| 3066 |
|
| 3067 |
|
| 3068 |
|
| 3069 |
|
| 3070 |
|
| 3071 |
|
| 3072 |
|
| 3073 |
protected function commenceCriticalSection( string $fname ) { |
| 3074 |
if ( $this->csmError ) { |
| 3075 |
throw new DBUnexpectedError( |
| 3076 |
$this, |
| 3077 |
"Cannot execute $fname critical section while session state is out of sync.\n\n" . |
| 3078 |
$this->csmError->getMessage() . "\n" . |
| 3079 |
$this->csmError->getTraceAsString() |
| 3080 |
); |
| 3081 |
} |
| 3082 |
|
| 3083 |
if ( $this->csmId ) { |
| 3084 |
$csm = null; |
| 3085 |
} elseif ( $this->csProvider ) { |
| 3086 |
$csm = $this->csProvider->scopedEnter( |
| 3087 |
$fname, |
| 3088 |
null, |
| 3089 |
null, |
| 3090 |
function () use ( $fname ) { |
| 3091 |
|
| 3092 |
$e = new RuntimeException( "A critical section from {$fname} has failed" ); |
| 3093 |
$this->csmError = $e; |
| 3094 |
$this->csmId = null; |
| 3095 |
} |
| 3096 |
); |
| 3097 |
$this->csmId = $csm->getId(); |
| 3098 |
$this->csmFname = $fname; |
| 3099 |
} else { |
| 3100 |
$csm = null; |
| 3101 |
} |
| 3102 |
|
| 3103 |
return $csm; |
| 3104 |
} |
| 3105 |
|
| 3106 |
|
| 3107 |
|
| 3108 |
|
| 3109 |
|
| 3110 |
|
| 3111 |
|
| 3112 |
|
| 3113 |
|
| 3114 |
|
| 3115 |
|
| 3116 |
protected function completeCriticalSection( |
| 3117 |
string $fname, |
| 3118 |
?CriticalSectionScope $csm, |
| 3119 |
?Throwable $trxError = null |
| 3120 |
) { |
| 3121 |
if ( $csm !== null ) { |
| 3122 |
if ( $this->csmId === null ) { |
| 3123 |
throw new LogicException( "$fname critical section is not active" ); |
| 3124 |
} elseif ( $csm->getId() !== $this->csmId ) { |
| 3125 |
throw new LogicException( |
| 3126 |
"$fname critical section is not the active ({$this->csmFname}) one" |
| 3127 |
); |
| 3128 |
} |
| 3129 |
|
| 3130 |
$csm->exit(); |
| 3131 |
$this->csmId = null; |
| 3132 |
} |
| 3133 |
|
| 3134 |
if ( $trxError ) { |
| 3135 |
$this->transactionManager->setTransactionError( $trxError ); |
| 3136 |
} |
| 3137 |
} |
| 3138 |
|
| 3139 |
public function __toString() { |
| 3140 |
$id = spl_object_id( $this ); |
| 3141 |
|
| 3142 |
$description = $this->getType() . ' object #' . $id; |
| 3143 |
|
| 3144 |
if ( is_resource( $this->conn ) ) { |
| 3145 |
$description .= ' (' . (string)$this->conn . ')'; |
| 3146 |
} elseif ( is_object( $this->conn ) ) { |
| 3147 |
$handleId = spl_object_id( $this->conn ); |
| 3148 |
$description .= " (handle id #$handleId)"; |
| 3149 |
} |
| 3150 |
|
| 3151 |
return $description; |
| 3152 |
} |
| 3153 |
|
| 3154 |
|
| 3155 |
|
| 3156 |
|
| 3157 |
|
| 3158 |
public function __clone() { |
| 3159 |
$this->logger->warning( |
| 3160 |
"Cloning " . static::class . " is not recommended; forking connection", |
| 3161 |
[ |
| 3162 |
'exception' => new RuntimeException(), |
| 3163 |
'db_log_category' => 'connection' |
| 3164 |
] |
| 3165 |
); |
| 3166 |
|
| 3167 |
if ( $this->isOpen() ) { |
| 3168 |
|
| 3169 |
$this->conn = null; |
| 3170 |
$this->transactionManager->clearEndCallbacks(); |
| 3171 |
$this->handleSessionLossPreconnect(); |
| 3172 |
$this->open( |
| 3173 |
$this->connectionParams[self::CONN_HOST], |
| 3174 |
$this->connectionParams[self::CONN_USER], |
| 3175 |
$this->connectionParams[self::CONN_PASSWORD], |
| 3176 |
$this->currentDomain->getDatabase(), |
| 3177 |
$this->currentDomain->getSchema(), |
| 3178 |
$this->tablePrefix() |
| 3179 |
); |
| 3180 |
$this->lastPing = microtime( true ); |
| 3181 |
} |
| 3182 |
} |
| 3183 |
|
| 3184 |
|
| 3185 |
|
| 3186 |
|
| 3187 |
|
| 3188 |
|
| 3189 |
|
| 3190 |
public function __sleep() { |
| 3191 |
throw new RuntimeException( 'Database serialization may cause problems, since ' . |
| 3192 |
'the connection is not restored on wakeup' ); |
| 3193 |
} |
| 3194 |
|
| 3195 |
|
| 3196 |
|
| 3197 |
|
| 3198 |
public function __destruct() { |
| 3199 |
if ( $this->transactionManager ) { |
| 3200 |
|
| 3201 |
$this->transactionManager->onDestruct(); |
| 3202 |
} |
| 3203 |
|
| 3204 |
$danglingWriters = $this->pendingWriteAndCallbackCallers(); |
| 3205 |
if ( $danglingWriters ) { |
| 3206 |
$fnames = implode( ', ', $danglingWriters ); |
| 3207 |
trigger_error( "DB transaction writes or callbacks still pending ($fnames)" ); |
| 3208 |
} |
| 3209 |
|
| 3210 |
if ( $this->conn ) { |
| 3211 |
|
| 3212 |
|
| 3213 |
AtEase::suppressWarnings(); |
| 3214 |
$this->closeConnection(); |
| 3215 |
AtEase::restoreWarnings(); |
| 3216 |
$this->conn = null; |
| 3217 |
} |
| 3218 |
} |
| 3219 |
|
| 3220 |
|
| 3221 |
|
| 3222 |
public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) { |
| 3223 |
$this->flagsHolder->setFlag( $flag, $remember ); |
| 3224 |
} |
| 3225 |
|
| 3226 |
public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) { |
| 3227 |
$this->flagsHolder->clearFlag( $flag, $remember ); |
| 3228 |
} |
| 3229 |
|
| 3230 |
public function restoreFlags( $state = self::RESTORE_PRIOR ) { |
| 3231 |
$this->flagsHolder->restoreFlags( $state ); |
| 3232 |
} |
| 3233 |
|
| 3234 |
public function getFlag( $flag ) { |
| 3235 |
return $this->flagsHolder->getFlag( $flag ); |
| 3236 |
} |
| 3237 |
|
| 3238 |
|
| 3239 |
|
| 3240 |
|
| 3241 |
|
| 3242 |
final public function trxLevel() { |
| 3243 |
|
| 3244 |
|
| 3245 |
|
| 3246 |
if ( !$this->transactionManager ) { |
| 3247 |
$this->transactionManager = new TransactionManager( new NullLogger() ); |
| 3248 |
} |
| 3249 |
return $this->transactionManager->trxLevel(); |
| 3250 |
} |
| 3251 |
|
| 3252 |
public function trxTimestamp() { |
| 3253 |
return $this->transactionManager->trxTimestamp(); |
| 3254 |
} |
| 3255 |
|
| 3256 |
public function trxStatus() { |
| 3257 |
return $this->transactionManager->trxStatus(); |
| 3258 |
} |
| 3259 |
|
| 3260 |
public function writesPending() { |
| 3261 |
return $this->transactionManager->writesPending(); |
| 3262 |
} |
| 3263 |
|
| 3264 |
public function writesOrCallbacksPending() { |
| 3265 |
return $this->transactionManager->writesOrCallbacksPending(); |
| 3266 |
} |
| 3267 |
|
| 3268 |
public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) { |
| 3269 |
return $this->transactionManager->pendingWriteQueryDuration( $type ); |
| 3270 |
} |
| 3271 |
|
| 3272 |
public function pendingWriteCallers() { |
| 3273 |
if ( !$this->transactionManager ) { |
| 3274 |
return []; |
| 3275 |
} |
| 3276 |
return $this->transactionManager->pendingWriteCallers(); |
| 3277 |
} |
| 3278 |
|
| 3279 |
public function pendingWriteAndCallbackCallers() { |
| 3280 |
if ( !$this->transactionManager ) { |
| 3281 |
return []; |
| 3282 |
} |
| 3283 |
return $this->transactionManager->pendingWriteAndCallbackCallers(); |
| 3284 |
} |
| 3285 |
|
| 3286 |
public function runOnTransactionPreCommitCallbacks() { |
| 3287 |
return $this->transactionManager->runOnTransactionPreCommitCallbacks( $this ); |
| 3288 |
} |
| 3289 |
|
| 3290 |
public function explicitTrxActive() { |
| 3291 |
return $this->transactionManager->explicitTrxActive(); |
| 3292 |
} |
| 3293 |
|
| 3294 |
|
| 3295 |
|
| 3296 |
|
| 3297 |
|
| 3298 |
public function implicitOrderby() { |
| 3299 |
return $this->platform->implicitOrderby(); |
| 3300 |
} |
| 3301 |
|
| 3302 |
public function selectSQLText( |
| 3303 |
$tables, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = [] |
| 3304 |
) { |
| 3305 |
return $this->platform->selectSQLText( $tables, $vars, $conds, $fname, $options, $join_conds ); |
| 3306 |
} |
| 3307 |
|
| 3308 |
public function buildComparison( string $op, array $conds ): string { |
| 3309 |
return $this->platform->buildComparison( $op, $conds ); |
| 3310 |
} |
| 3311 |
|
| 3312 |
public function makeList( array $a, $mode = self::LIST_COMMA ) { |
| 3313 |
return $this->platform->makeList( $a, $mode ); |
| 3314 |
} |
| 3315 |
|
| 3316 |
public function makeWhereFrom2d( $data, $baseKey, $subKey ) { |
| 3317 |
return $this->platform->makeWhereFrom2d( $data, $baseKey, $subKey ); |
| 3318 |
} |
| 3319 |
|
| 3320 |
public function factorConds( $condsArray ) { |
| 3321 |
return $this->platform->factorConds( $condsArray ); |
| 3322 |
} |
| 3323 |
|
| 3324 |
public function bitNot( $field ) { |
| 3325 |
return $this->platform->bitNot( $field ); |
| 3326 |
} |
| 3327 |
|
| 3328 |
public function bitAnd( $fieldLeft, $fieldRight ) { |
| 3329 |
return $this->platform->bitAnd( $fieldLeft, $fieldRight ); |
| 3330 |
} |
| 3331 |
|
| 3332 |
public function bitOr( $fieldLeft, $fieldRight ) { |
| 3333 |
return $this->platform->bitOr( $fieldLeft, $fieldRight ); |
| 3334 |
} |
| 3335 |
|
| 3336 |
public function buildConcat( $stringList ) { |
| 3337 |
return $this->platform->buildConcat( $stringList ); |
| 3338 |
} |
| 3339 |
|
| 3340 |
public function buildGreatest( $fields, $values ) { |
| 3341 |
return $this->platform->buildGreatest( $fields, $values ); |
| 3342 |
} |
| 3343 |
|
| 3344 |
public function buildLeast( $fields, $values ) { |
| 3345 |
return $this->platform->buildLeast( $fields, $values ); |
| 3346 |
} |
| 3347 |
|
| 3348 |
public function buildSubstring( $input, $startPosition, $length = null ) { |
| 3349 |
return $this->platform->buildSubstring( $input, $startPosition, $length ); |
| 3350 |
} |
| 3351 |
|
| 3352 |
public function buildStringCast( $field ) { |
| 3353 |
return $this->platform->buildStringCast( $field ); |
| 3354 |
} |
| 3355 |
|
| 3356 |
public function buildIntegerCast( $field ) { |
| 3357 |
return $this->platform->buildIntegerCast( $field ); |
| 3358 |
} |
| 3359 |
|
| 3360 |
public function tableName( string $name, $format = 'quoted' ) { |
| 3361 |
return $this->platform->tableName( $name, $format ); |
| 3362 |
} |
| 3363 |
|
| 3364 |
public function tableNames( ...$tables ) { |
| 3365 |
return $this->platform->tableNames( ...$tables ); |
| 3366 |
} |
| 3367 |
|
| 3368 |
public function tableNamesN( ...$tables ) { |
| 3369 |
return $this->platform->tableNamesN( ...$tables ); |
| 3370 |
} |
| 3371 |
|
| 3372 |
public function addIdentifierQuotes( $s ) { |
| 3373 |
return $this->platform->addIdentifierQuotes( $s ); |
| 3374 |
} |
| 3375 |
|
| 3376 |
public function isQuotedIdentifier( $name ) { |
| 3377 |
return $this->platform->isQuotedIdentifier( $name ); |
| 3378 |
} |
| 3379 |
|
| 3380 |
public function buildLike( $param, ...$params ) { |
| 3381 |
return $this->platform->buildLike( $param, ...$params ); |
| 3382 |
} |
| 3383 |
|
| 3384 |
public function anyChar() { |
| 3385 |
return $this->platform->anyChar(); |
| 3386 |
} |
| 3387 |
|
| 3388 |
public function anyString() { |
| 3389 |
return $this->platform->anyString(); |
| 3390 |
} |
| 3391 |
|
| 3392 |
public function limitResult( $sql, $limit, $offset = false ) { |
| 3393 |
return $this->platform->limitResult( $sql, $limit, $offset ); |
| 3394 |
} |
| 3395 |
|
| 3396 |
public function unionSupportsOrderAndLimit() { |
| 3397 |
return $this->platform->unionSupportsOrderAndLimit(); |
| 3398 |
} |
| 3399 |
|
| 3400 |
public function unionQueries( $sqls, $all, $options = [] ) { |
| 3401 |
return $this->platform->unionQueries( $sqls, $all, $options ); |
| 3402 |
} |
| 3403 |
|
| 3404 |
public function conditional( $cond, $caseTrueExpression, $caseFalseExpression ) { |
| 3405 |
return $this->platform->conditional( $cond, $caseTrueExpression, $caseFalseExpression ); |
| 3406 |
} |
| 3407 |
|
| 3408 |
public function strreplace( $orig, $old, $new ) { |
| 3409 |
return $this->platform->strreplace( $orig, $old, $new ); |
| 3410 |
} |
| 3411 |
|
| 3412 |
public function timestamp( $ts = 0 ) { |
| 3413 |
return $this->platform->timestamp( $ts ); |
| 3414 |
} |
| 3415 |
|
| 3416 |
public function timestampOrNull( $ts = null ) { |
| 3417 |
return $this->platform->timestampOrNull( $ts ); |
| 3418 |
} |
| 3419 |
|
| 3420 |
public function getInfinity() { |
| 3421 |
return $this->platform->getInfinity(); |
| 3422 |
} |
| 3423 |
|
| 3424 |
public function encodeExpiry( $expiry ) { |
| 3425 |
return $this->platform->encodeExpiry( $expiry ); |
| 3426 |
} |
| 3427 |
|
| 3428 |
public function decodeExpiry( $expiry, $format = TS_MW ) { |
| 3429 |
return $this->platform->decodeExpiry( $expiry, $format ); |
| 3430 |
} |
| 3431 |
|
| 3432 |
public function setTableAliases( array $aliases ) { |
| 3433 |
$this->platform->setTableAliases( $aliases ); |
| 3434 |
} |
| 3435 |
|
| 3436 |
public function getTableAliases() { |
| 3437 |
return $this->platform->getTableAliases(); |
| 3438 |
} |
| 3439 |
|
| 3440 |
public function setIndexAliases( array $aliases ) { |
| 3441 |
$this->platform->setIndexAliases( $aliases ); |
| 3442 |
} |
| 3443 |
|
| 3444 |
public function buildGroupConcatField( |
| 3445 |
$delim, $tables, $field, $conds = '', $join_conds = [] |
| 3446 |
) { |
| 3447 |
return $this->platform->buildGroupConcatField( $delim, $tables, $field, $conds, $join_conds ); |
| 3448 |
} |
| 3449 |
|
| 3450 |
public function buildSelectSubquery( |
| 3451 |
$tables, $vars, $conds = '', $fname = __METHOD__, |
| 3452 |
$options = [], $join_conds = [] |
| 3453 |
) { |
| 3454 |
return $this->platform->buildSelectSubquery( $tables, $vars, $conds, $fname, $options, $join_conds ); |
| 3455 |
} |
| 3456 |
|
| 3457 |
public function buildExcludedValue( $column ) { |
| 3458 |
return $this->platform->buildExcludedValue( $column ); |
| 3459 |
} |
| 3460 |
|
| 3461 |
public function setSchemaVars( $vars ) { |
| 3462 |
$this->platform->setSchemaVars( $vars ); |
| 3463 |
} |
| 3464 |
|
| 3465 |
|
| 3466 |
|
| 3467 |
|
| 3468 |
public function primaryPosWait( DBPrimaryPos $pos, $timeout ) { |
| 3469 |
return $this->replicationReporter->primaryPosWait( $this, $pos, $timeout ); |
| 3470 |
} |
| 3471 |
|
| 3472 |
public function getPrimaryPos() { |
| 3473 |
return $this->replicationReporter->getPrimaryPos( $this ); |
| 3474 |
} |
| 3475 |
|
| 3476 |
public function getLag() { |
| 3477 |
return $this->replicationReporter->getLag( $this ); |
| 3478 |
} |
| 3479 |
|
| 3480 |
public function getSessionLagStatus() { |
| 3481 |
return $this->replicationReporter->getSessionLagStatus( $this ); |
| 3482 |
} |
| 3483 |
|
| 3484 |
|
| 3485 |
} |