3 namespace Wikimedia\Rdbms
;
5 use InvalidArgumentException
;
8 * Helper class to handle automatically marking connections as reusable (via RAII pattern)
9 * as well handling deferring the actual network connection until the handle is used
14 class DBConnRef
implements IDatabase
{
15 /** @var ILoadBalancer */
17 /** @var Database|null Live connection handle */
19 /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */
21 /** @var int One of DB_MASTER/DB_REPLICA */
30 * @param ILoadBalancer $lb Connection manager for $conn
31 * @param Database|array $conn Database or (server index, query groups, domain, flags)
32 * @param int $role The type of connection asked for; one of DB_MASTER/DB_REPLICA
33 * @internal This method should not be called outside of LoadBalancer
35 public function __construct( ILoadBalancer
$lb, $conn, $role ) {
38 if ( $conn instanceof Database
) {
39 $this->conn
= $conn; // live handle
40 } elseif ( is_array( $conn ) && count( $conn ) >= 4 && $conn[self
::FLD_DOMAIN
] !== false ) {
41 $this->params
= $conn;
43 throw new InvalidArgumentException( "Missing lazy connection arguments." );
47 function __call( $name, array $arguments ) {
48 if ( $this->conn
=== null ) {
49 list( $index, $groups, $wiki, $flags ) = $this->params
;
50 $this->conn
= $this->lb
->getConnection( $index, $groups, $wiki, $flags );
53 return $this->conn
->$name( ...$arguments );
57 * @return int DB_MASTER when this *requires* the master DB, otherwise DB_REPLICA
60 public function getReferenceRole() {
64 public function getServerInfo() {
65 return $this->__call( __FUNCTION__
, func_get_args() );
68 public function bufferResults( $buffer = null ) {
69 return $this->__call( __FUNCTION__
, func_get_args() );
72 public function trxLevel() {
73 return $this->__call( __FUNCTION__
, func_get_args() );
76 public function trxTimestamp() {
77 return $this->__call( __FUNCTION__
, func_get_args() );
80 public function explicitTrxActive() {
81 return $this->__call( __FUNCTION__
, func_get_args() );
84 public function assertNoOpenTransactions() {
85 return $this->__call( __FUNCTION__
, func_get_args() );
88 public function tablePrefix( $prefix = null ) {
89 if ( $this->conn
=== null && $prefix === null ) {
90 $domain = DatabaseDomain
::newFromId( $this->params
[self
::FLD_DOMAIN
] );
91 // Avoid triggering a database connection
92 return $domain->getTablePrefix();
93 } elseif ( $this->conn
!== null && $prefix === null ) {
94 // This will just return the prefix
95 return $this->__call( __FUNCTION__
, func_get_args() );
97 // Disallow things that might confuse the LoadBalancer tracking
98 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
101 public function dbSchema( $schema = null ) {
102 if ( $this->conn
=== null && $schema === null ) {
103 $domain = DatabaseDomain
::newFromId( $this->params
[self
::FLD_DOMAIN
] );
104 // Avoid triggering a database connection
105 return $domain->getSchema();
106 } elseif ( $this->conn
!== null && $schema === null ) {
107 // This will just return the schema
108 return $this->__call( __FUNCTION__
, func_get_args() );
110 // Disallow things that might confuse the LoadBalancer tracking
111 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
114 public function getLBInfo( $name = null ) {
115 return $this->__call( __FUNCTION__
, func_get_args() );
118 public function setLBInfo( $name, $value = null ) {
119 // Disallow things that might confuse the LoadBalancer tracking
120 throw new DBUnexpectedError( $this, "Changing LB info is disallowed to enable reuse." );
123 public function setLazyMasterHandle( IDatabase
$conn ) {
124 // Disallow things that might confuse the LoadBalancer tracking
125 throw new DBUnexpectedError( $this, "Database injection is disallowed to enable reuse." );
128 public function implicitGroupby() {
129 return $this->__call( __FUNCTION__
, func_get_args() );
132 public function implicitOrderby() {
133 return $this->__call( __FUNCTION__
, func_get_args() );
136 public function lastQuery() {
137 return $this->__call( __FUNCTION__
, func_get_args() );
140 public function doneWrites() {
141 return $this->__call( __FUNCTION__
, func_get_args() );
144 public function lastDoneWrites() {
145 return $this->__call( __FUNCTION__
, func_get_args() );
148 public function writesPending() {
149 return $this->__call( __FUNCTION__
, func_get_args() );
152 public function preCommitCallbacksPending() {
153 return $this->__call( __FUNCTION__
, func_get_args() );
156 public function writesOrCallbacksPending() {
157 return $this->__call( __FUNCTION__
, func_get_args() );
160 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
161 return $this->__call( __FUNCTION__
, func_get_args() );
164 public function pendingWriteCallers() {
165 return $this->__call( __FUNCTION__
, func_get_args() );
168 public function pendingWriteRowsAffected() {
169 return $this->__call( __FUNCTION__
, func_get_args() );
172 public function isOpen() {
173 return $this->__call( __FUNCTION__
, func_get_args() );
176 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
177 return $this->__call( __FUNCTION__
, func_get_args() );
180 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
181 return $this->__call( __FUNCTION__
, func_get_args() );
184 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
185 return $this->__call( __FUNCTION__
, func_get_args() );
188 public function getFlag( $flag ) {
189 return $this->__call( __FUNCTION__
, func_get_args() );
192 public function getProperty( $name ) {
193 return $this->__call( __FUNCTION__
, func_get_args() );
196 public function getDomainID() {
197 if ( $this->conn
=== null ) {
198 $domain = $this->params
[self
::FLD_DOMAIN
];
199 // Avoid triggering a database connection
200 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
203 return $this->__call( __FUNCTION__
, func_get_args() );
207 * @codeCoverageIgnore
209 public function getWikiID() {
210 return $this->getDomainID();
213 public function getType() {
214 if ( $this->conn
=== null ) {
215 // Avoid triggering a database connection
216 if ( $this->params
[self
::FLD_INDEX
] === ILoadBalancer
::DB_MASTER
) {
217 $index = $this->lb
->getWriterIndex();
219 $index = $this->params
[self
::FLD_INDEX
];
222 // In theory, if $index is DB_REPLICA, the type could vary
223 return $this->lb
->getServerType( $index );
227 return $this->__call( __FUNCTION__
, func_get_args() );
230 public function fetchObject( $res ) {
231 return $this->__call( __FUNCTION__
, func_get_args() );
234 public function fetchRow( $res ) {
235 return $this->__call( __FUNCTION__
, func_get_args() );
238 public function numRows( $res ) {
239 return $this->__call( __FUNCTION__
, func_get_args() );
242 public function numFields( $res ) {
243 return $this->__call( __FUNCTION__
, func_get_args() );
246 public function fieldName( $res, $n ) {
247 return $this->__call( __FUNCTION__
, func_get_args() );
250 public function insertId() {
251 return $this->__call( __FUNCTION__
, func_get_args() );
254 public function dataSeek( $res, $row ) {
255 return $this->__call( __FUNCTION__
, func_get_args() );
258 public function lastErrno() {
259 return $this->__call( __FUNCTION__
, func_get_args() );
262 public function lastError() {
263 return $this->__call( __FUNCTION__
, func_get_args() );
266 public function affectedRows() {
267 return $this->__call( __FUNCTION__
, func_get_args() );
270 public function getSoftwareLink() {
271 return $this->__call( __FUNCTION__
, func_get_args() );
274 public function getServerVersion() {
275 return $this->__call( __FUNCTION__
, func_get_args() );
278 public function close() {
279 throw new DBUnexpectedError( $this->conn
, 'Cannot close shared connection.' );
282 public function query( $sql, $fname = __METHOD__
, $flags = 0 ) {
283 if ( $this->role
!== ILoadBalancer
::DB_MASTER
) {
284 $flags |
= IDatabase
::QUERY_REPLICA_ROLE
;
287 return $this->__call( __FUNCTION__
, [ $sql, $fname, $flags ] );
290 public function freeResult( $res ) {
291 return $this->__call( __FUNCTION__
, func_get_args() );
294 public function selectField(
295 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
297 return $this->__call( __FUNCTION__
, func_get_args() );
300 public function selectFieldValues(
301 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
303 return $this->__call( __FUNCTION__
, func_get_args() );
306 public function select(
307 $table, $vars, $conds = '', $fname = __METHOD__
,
308 $options = [], $join_conds = []
310 return $this->__call( __FUNCTION__
, func_get_args() );
313 public function selectSQLText(
314 $table, $vars, $conds = '', $fname = __METHOD__
,
315 $options = [], $join_conds = []
317 return $this->__call( __FUNCTION__
, func_get_args() );
320 public function limitResult( $sql, $limit, $offset = false ) {
321 return $this->__call( __FUNCTION__
, func_get_args() );
324 public function selectRow(
325 $table, $vars, $conds, $fname = __METHOD__
,
326 $options = [], $join_conds = []
328 return $this->__call( __FUNCTION__
, func_get_args() );
331 public function estimateRowCount(
332 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
334 return $this->__call( __FUNCTION__
, func_get_args() );
337 public function selectRowCount(
338 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
340 return $this->__call( __FUNCTION__
, func_get_args() );
343 public function lockForUpdate(
344 $table, $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
346 $this->assertRoleAllowsWrites();
348 return $this->__call( __FUNCTION__
, func_get_args() );
351 public function fieldExists( $table, $field, $fname = __METHOD__
) {
352 return $this->__call( __FUNCTION__
, func_get_args() );
355 public function indexExists( $table, $index, $fname = __METHOD__
) {
356 return $this->__call( __FUNCTION__
, func_get_args() );
359 public function tableExists( $table, $fname = __METHOD__
) {
360 return $this->__call( __FUNCTION__
, func_get_args() );
363 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
364 $this->assertRoleAllowsWrites();
366 return $this->__call( __FUNCTION__
, func_get_args() );
369 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
370 $this->assertRoleAllowsWrites();
372 return $this->__call( __FUNCTION__
, func_get_args() );
375 public function makeList( $a, $mode = self
::LIST_COMMA
) {
376 return $this->__call( __FUNCTION__
, func_get_args() );
379 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
380 return $this->__call( __FUNCTION__
, func_get_args() );
383 public function aggregateValue( $valuedata, $valuename = 'value' ) {
384 return $this->__call( __FUNCTION__
, func_get_args() );
387 public function bitNot( $field ) {
388 return $this->__call( __FUNCTION__
, func_get_args() );
391 public function bitAnd( $fieldLeft, $fieldRight ) {
392 return $this->__call( __FUNCTION__
, func_get_args() );
395 public function bitOr( $fieldLeft, $fieldRight ) {
396 return $this->__call( __FUNCTION__
, func_get_args() );
399 public function buildConcat( $stringList ) {
400 return $this->__call( __FUNCTION__
, func_get_args() );
403 public function buildGroupConcatField(
404 $delim, $table, $field, $conds = '', $join_conds = []
406 return $this->__call( __FUNCTION__
, func_get_args() );
409 public function buildSubstring( $input, $startPosition, $length = null ) {
410 return $this->__call( __FUNCTION__
, func_get_args() );
413 public function buildStringCast( $field ) {
414 return $this->__call( __FUNCTION__
, func_get_args() );
417 public function buildIntegerCast( $field ) {
418 return $this->__call( __FUNCTION__
, func_get_args() );
421 public function buildSelectSubquery(
422 $table, $vars, $conds = '', $fname = __METHOD__
,
423 $options = [], $join_conds = []
425 return $this->__call( __FUNCTION__
, func_get_args() );
428 public function databasesAreIndependent() {
429 return $this->__call( __FUNCTION__
, func_get_args() );
432 public function selectDB( $db ) {
433 // Disallow things that might confuse the LoadBalancer tracking
434 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
437 public function selectDomain( $domain ) {
438 // Disallow things that might confuse the LoadBalancer tracking
439 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
442 public function getDBname() {
443 if ( $this->conn
=== null ) {
444 $domain = DatabaseDomain
::newFromId( $this->params
[self
::FLD_DOMAIN
] );
445 // Avoid triggering a database connection
446 return $domain->getDatabase();
449 return $this->__call( __FUNCTION__
, func_get_args() );
452 public function getServer() {
453 return $this->__call( __FUNCTION__
, func_get_args() );
456 public function addQuotes( $s ) {
457 return $this->__call( __FUNCTION__
, func_get_args() );
460 public function addIdentifierQuotes( $s ) {
461 return $this->__call( __FUNCTION__
, func_get_args() );
464 public function buildLike() {
465 return $this->__call( __FUNCTION__
, func_get_args() );
468 public function anyChar() {
469 return $this->__call( __FUNCTION__
, func_get_args() );
472 public function anyString() {
473 return $this->__call( __FUNCTION__
, func_get_args() );
476 public function nextSequenceValue( $seqName ) {
477 $this->assertRoleAllowsWrites();
479 return $this->__call( __FUNCTION__
, func_get_args() );
482 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
483 $this->assertRoleAllowsWrites();
485 return $this->__call( __FUNCTION__
, func_get_args() );
488 public function upsert(
489 $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__
491 $this->assertRoleAllowsWrites();
493 return $this->__call( __FUNCTION__
, func_get_args() );
496 public function deleteJoin(
497 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
499 $this->assertRoleAllowsWrites();
501 return $this->__call( __FUNCTION__
, func_get_args() );
504 public function delete( $table, $conds, $fname = __METHOD__
) {
505 $this->assertRoleAllowsWrites();
507 return $this->__call( __FUNCTION__
, func_get_args() );
510 public function insertSelect(
511 $destTable, $srcTable, $varMap, $conds,
512 $fname = __METHOD__
, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
514 $this->assertRoleAllowsWrites();
516 return $this->__call( __FUNCTION__
, func_get_args() );
519 public function unionSupportsOrderAndLimit() {
520 return $this->__call( __FUNCTION__
, func_get_args() );
523 public function unionQueries( $sqls, $all ) {
524 return $this->__call( __FUNCTION__
, func_get_args() );
527 public function unionConditionPermutations(
528 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__
,
529 $options = [], $join_conds = []
531 return $this->__call( __FUNCTION__
, func_get_args() );
534 public function conditional( $cond, $trueVal, $falseVal ) {
535 return $this->__call( __FUNCTION__
, func_get_args() );
538 public function strreplace( $orig, $old, $new ) {
539 return $this->__call( __FUNCTION__
, func_get_args() );
542 public function getServerUptime() {
543 return $this->__call( __FUNCTION__
, func_get_args() );
546 public function wasDeadlock() {
547 return $this->__call( __FUNCTION__
, func_get_args() );
550 public function wasLockTimeout() {
551 return $this->__call( __FUNCTION__
, func_get_args() );
554 public function wasConnectionLoss() {
555 return $this->__call( __FUNCTION__
, func_get_args() );
558 public function wasReadOnlyError() {
559 return $this->__call( __FUNCTION__
, func_get_args() );
562 public function wasErrorReissuable() {
563 return $this->__call( __FUNCTION__
, func_get_args() );
566 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
567 return $this->__call( __FUNCTION__
, func_get_args() );
570 public function getReplicaPos() {
571 return $this->__call( __FUNCTION__
, func_get_args() );
574 public function getMasterPos() {
575 return $this->__call( __FUNCTION__
, func_get_args() );
578 public function serverIsReadOnly() {
579 return $this->__call( __FUNCTION__
, func_get_args() );
582 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
583 // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
584 return $this->__call( __FUNCTION__
, func_get_args() );
587 public function onTransactionCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
588 // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
589 return $this->__call( __FUNCTION__
, func_get_args() );
592 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
593 return $this->onTransactionCommitOrIdle( $callback, $fname );
596 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
597 // DB_REPLICA role: caller might want to refresh cache after a cache mutex is released
598 return $this->__call( __FUNCTION__
, func_get_args() );
601 public function setTransactionListener( $name, callable
$callback = null ) {
602 return $this->__call( __FUNCTION__
, func_get_args() );
605 public function startAtomic(
606 $fname = __METHOD__
, $cancelable = IDatabase
::ATOMIC_NOT_CANCELABLE
608 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
609 return $this->__call( __FUNCTION__
, func_get_args() );
612 public function endAtomic( $fname = __METHOD__
) {
613 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
614 return $this->__call( __FUNCTION__
, func_get_args() );
617 public function cancelAtomic( $fname = __METHOD__
, AtomicSectionIdentifier
$sectionId = null ) {
618 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
619 return $this->__call( __FUNCTION__
, func_get_args() );
622 public function doAtomicSection(
623 $fname, callable
$callback, $cancelable = self
::ATOMIC_NOT_CANCELABLE
625 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
626 return $this->__call( __FUNCTION__
, func_get_args() );
629 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
630 return $this->__call( __FUNCTION__
, func_get_args() );
633 public function commit( $fname = __METHOD__
, $flush = '' ) {
634 return $this->__call( __FUNCTION__
, func_get_args() );
637 public function rollback( $fname = __METHOD__
, $flush = '' ) {
638 return $this->__call( __FUNCTION__
, func_get_args() );
641 public function flushSnapshot( $fname = __METHOD__
) {
642 return $this->__call( __FUNCTION__
, func_get_args() );
645 public function timestamp( $ts = 0 ) {
646 return $this->__call( __FUNCTION__
, func_get_args() );
649 public function timestampOrNull( $ts = null ) {
650 return $this->__call( __FUNCTION__
, func_get_args() );
653 public function ping( &$rtt = null ) {
654 return func_num_args()
655 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
656 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
659 public function getLag() {
660 return $this->__call( __FUNCTION__
, func_get_args() );
663 public function getSessionLagStatus() {
664 return $this->__call( __FUNCTION__
, func_get_args() );
667 public function maxListLen() {
668 return $this->__call( __FUNCTION__
, func_get_args() );
671 public function encodeBlob( $b ) {
672 return $this->__call( __FUNCTION__
, func_get_args() );
675 public function decodeBlob( $b ) {
676 return $this->__call( __FUNCTION__
, func_get_args() );
679 public function setSessionOptions( array $options ) {
680 return $this->__call( __FUNCTION__
, func_get_args() );
683 public function setSchemaVars( $vars ) {
684 return $this->__call( __FUNCTION__
, func_get_args() );
687 public function lockIsFree( $lockName, $method ) {
688 $this->assertRoleAllowsWrites();
690 return $this->__call( __FUNCTION__
, func_get_args() );
693 public function lock( $lockName, $method, $timeout = 5 ) {
694 $this->assertRoleAllowsWrites();
696 return $this->__call( __FUNCTION__
, func_get_args() );
699 public function unlock( $lockName, $method ) {
700 $this->assertRoleAllowsWrites();
702 return $this->__call( __FUNCTION__
, func_get_args() );
705 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
706 $this->assertRoleAllowsWrites();
708 return $this->__call( __FUNCTION__
, func_get_args() );
711 public function namedLocksEnqueue() {
712 return $this->__call( __FUNCTION__
, func_get_args() );
715 public function getInfinity() {
716 return $this->__call( __FUNCTION__
, func_get_args() );
719 public function encodeExpiry( $expiry ) {
720 return $this->__call( __FUNCTION__
, func_get_args() );
723 public function decodeExpiry( $expiry, $format = TS_MW
) {
724 return $this->__call( __FUNCTION__
, func_get_args() );
727 public function setBigSelects( $value = true ) {
728 return $this->__call( __FUNCTION__
, func_get_args() );
731 public function isReadOnly() {
732 return $this->__call( __FUNCTION__
, func_get_args() );
735 public function setTableAliases( array $aliases ) {
736 return $this->__call( __FUNCTION__
, func_get_args() );
739 public function setIndexAliases( array $aliases ) {
740 return $this->__call( __FUNCTION__
, func_get_args() );
744 * Error out if the role is not DB_MASTER
746 * Note that the underlying connection may or may not itself be read-only.
747 * It could even be to a writable master (both server-side and to the application).
748 * This error is meant for the case when a DB_REPLICA handle was requested but a
749 * a write was attempted on that handle regardless.
751 * In configurations where the master DB has some generic read load or is the only server,
752 * DB_MASTER/DB_REPLICA will sometimes (or always) use the same connection to the master DB.
753 * This does not effect the role of DBConnRef instances.
754 * @throws DBReadOnlyRoleError
756 protected function assertRoleAllowsWrites() {
757 // DB_MASTER is "prima facie" writable
758 if ( $this->role
!== ILoadBalancer
::DB_MASTER
) {
759 throw new DBReadOnlyRoleError( $this->conn
, "Cannot write with role DB_REPLICA" );
764 * Clean up the connection when out of scope
766 function __destruct() {
768 $this->lb
->reuseConnection( $this->conn
);
775 * @deprecated since 1.29
777 class_alias( DBConnRef
::class, 'DBConnRef' );