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( $db, $groups, $wiki, $flags ) = $this->params
;
50 $this->conn
= $this->lb
->getConnection( $db, $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 return $this->__call( __FUNCTION__
, func_get_args() );
217 public function fetchObject( $res ) {
218 return $this->__call( __FUNCTION__
, func_get_args() );
221 public function fetchRow( $res ) {
222 return $this->__call( __FUNCTION__
, func_get_args() );
225 public function numRows( $res ) {
226 return $this->__call( __FUNCTION__
, func_get_args() );
229 public function numFields( $res ) {
230 return $this->__call( __FUNCTION__
, func_get_args() );
233 public function fieldName( $res, $n ) {
234 return $this->__call( __FUNCTION__
, func_get_args() );
237 public function insertId() {
238 return $this->__call( __FUNCTION__
, func_get_args() );
241 public function dataSeek( $res, $row ) {
242 return $this->__call( __FUNCTION__
, func_get_args() );
245 public function lastErrno() {
246 return $this->__call( __FUNCTION__
, func_get_args() );
249 public function lastError() {
250 return $this->__call( __FUNCTION__
, func_get_args() );
253 public function affectedRows() {
254 return $this->__call( __FUNCTION__
, func_get_args() );
257 public function getSoftwareLink() {
258 return $this->__call( __FUNCTION__
, func_get_args() );
261 public function getServerVersion() {
262 return $this->__call( __FUNCTION__
, func_get_args() );
265 public function close() {
266 throw new DBUnexpectedError( $this->conn
, 'Cannot close shared connection.' );
269 public function query( $sql, $fname = __METHOD__
, $flags = 0 ) {
270 if ( $this->role
!== ILoadBalancer
::DB_MASTER
) {
271 $flags |
= IDatabase
::QUERY_REPLICA_ROLE
;
274 return $this->__call( __FUNCTION__
, [ $sql, $fname, $flags ] );
277 public function freeResult( $res ) {
278 return $this->__call( __FUNCTION__
, func_get_args() );
281 public function selectField(
282 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
284 return $this->__call( __FUNCTION__
, func_get_args() );
287 public function selectFieldValues(
288 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
290 return $this->__call( __FUNCTION__
, func_get_args() );
293 public function select(
294 $table, $vars, $conds = '', $fname = __METHOD__
,
295 $options = [], $join_conds = []
297 return $this->__call( __FUNCTION__
, func_get_args() );
300 public function selectSQLText(
301 $table, $vars, $conds = '', $fname = __METHOD__
,
302 $options = [], $join_conds = []
304 return $this->__call( __FUNCTION__
, func_get_args() );
307 public function selectRow(
308 $table, $vars, $conds, $fname = __METHOD__
,
309 $options = [], $join_conds = []
311 return $this->__call( __FUNCTION__
, func_get_args() );
314 public function estimateRowCount(
315 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
317 return $this->__call( __FUNCTION__
, func_get_args() );
320 public function selectRowCount(
321 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
323 return $this->__call( __FUNCTION__
, func_get_args() );
326 public function lockForUpdate(
327 $table, $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
329 $this->assertRoleAllowsWrites();
331 return $this->__call( __FUNCTION__
, func_get_args() );
334 public function fieldExists( $table, $field, $fname = __METHOD__
) {
335 return $this->__call( __FUNCTION__
, func_get_args() );
338 public function indexExists( $table, $index, $fname = __METHOD__
) {
339 return $this->__call( __FUNCTION__
, func_get_args() );
342 public function tableExists( $table, $fname = __METHOD__
) {
343 return $this->__call( __FUNCTION__
, func_get_args() );
346 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
347 $this->assertRoleAllowsWrites();
349 return $this->__call( __FUNCTION__
, func_get_args() );
352 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
353 $this->assertRoleAllowsWrites();
355 return $this->__call( __FUNCTION__
, func_get_args() );
358 public function makeList( $a, $mode = self
::LIST_COMMA
) {
359 return $this->__call( __FUNCTION__
, func_get_args() );
362 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
363 return $this->__call( __FUNCTION__
, func_get_args() );
366 public function aggregateValue( $valuedata, $valuename = 'value' ) {
367 return $this->__call( __FUNCTION__
, func_get_args() );
370 public function bitNot( $field ) {
371 return $this->__call( __FUNCTION__
, func_get_args() );
374 public function bitAnd( $fieldLeft, $fieldRight ) {
375 return $this->__call( __FUNCTION__
, func_get_args() );
378 public function bitOr( $fieldLeft, $fieldRight ) {
379 return $this->__call( __FUNCTION__
, func_get_args() );
382 public function buildConcat( $stringList ) {
383 return $this->__call( __FUNCTION__
, func_get_args() );
386 public function buildGroupConcatField(
387 $delim, $table, $field, $conds = '', $join_conds = []
389 return $this->__call( __FUNCTION__
, func_get_args() );
392 public function buildSubstring( $input, $startPosition, $length = null ) {
393 return $this->__call( __FUNCTION__
, func_get_args() );
396 public function buildStringCast( $field ) {
397 return $this->__call( __FUNCTION__
, func_get_args() );
400 public function buildIntegerCast( $field ) {
401 return $this->__call( __FUNCTION__
, func_get_args() );
404 public function buildSelectSubquery(
405 $table, $vars, $conds = '', $fname = __METHOD__
,
406 $options = [], $join_conds = []
408 return $this->__call( __FUNCTION__
, func_get_args() );
411 public function databasesAreIndependent() {
412 return $this->__call( __FUNCTION__
, func_get_args() );
415 public function selectDB( $db ) {
416 // Disallow things that might confuse the LoadBalancer tracking
417 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
420 public function selectDomain( $domain ) {
421 // Disallow things that might confuse the LoadBalancer tracking
422 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
425 public function getDBname() {
426 if ( $this->conn
=== null ) {
427 $domain = DatabaseDomain
::newFromId( $this->params
[self
::FLD_DOMAIN
] );
428 // Avoid triggering a database connection
429 return $domain->getDatabase();
432 return $this->__call( __FUNCTION__
, func_get_args() );
435 public function getServer() {
436 return $this->__call( __FUNCTION__
, func_get_args() );
439 public function addQuotes( $s ) {
440 return $this->__call( __FUNCTION__
, func_get_args() );
443 public function addIdentifierQuotes( $s ) {
444 return $this->__call( __FUNCTION__
, func_get_args() );
447 public function buildLike() {
448 return $this->__call( __FUNCTION__
, func_get_args() );
451 public function anyChar() {
452 return $this->__call( __FUNCTION__
, func_get_args() );
455 public function anyString() {
456 return $this->__call( __FUNCTION__
, func_get_args() );
459 public function nextSequenceValue( $seqName ) {
460 $this->assertRoleAllowsWrites();
462 return $this->__call( __FUNCTION__
, func_get_args() );
465 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
466 $this->assertRoleAllowsWrites();
468 return $this->__call( __FUNCTION__
, func_get_args() );
471 public function upsert(
472 $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__
474 $this->assertRoleAllowsWrites();
476 return $this->__call( __FUNCTION__
, func_get_args() );
479 public function deleteJoin(
480 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
482 $this->assertRoleAllowsWrites();
484 return $this->__call( __FUNCTION__
, func_get_args() );
487 public function delete( $table, $conds, $fname = __METHOD__
) {
488 $this->assertRoleAllowsWrites();
490 return $this->__call( __FUNCTION__
, func_get_args() );
493 public function insertSelect(
494 $destTable, $srcTable, $varMap, $conds,
495 $fname = __METHOD__
, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
497 $this->assertRoleAllowsWrites();
499 return $this->__call( __FUNCTION__
, func_get_args() );
502 public function unionSupportsOrderAndLimit() {
503 return $this->__call( __FUNCTION__
, func_get_args() );
506 public function unionQueries( $sqls, $all ) {
507 return $this->__call( __FUNCTION__
, func_get_args() );
510 public function unionConditionPermutations(
511 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__
,
512 $options = [], $join_conds = []
514 return $this->__call( __FUNCTION__
, func_get_args() );
517 public function conditional( $cond, $trueVal, $falseVal ) {
518 return $this->__call( __FUNCTION__
, func_get_args() );
521 public function strreplace( $orig, $old, $new ) {
522 return $this->__call( __FUNCTION__
, func_get_args() );
525 public function getServerUptime() {
526 return $this->__call( __FUNCTION__
, func_get_args() );
529 public function wasDeadlock() {
530 return $this->__call( __FUNCTION__
, func_get_args() );
533 public function wasLockTimeout() {
534 return $this->__call( __FUNCTION__
, func_get_args() );
537 public function wasConnectionLoss() {
538 return $this->__call( __FUNCTION__
, func_get_args() );
541 public function wasReadOnlyError() {
542 return $this->__call( __FUNCTION__
, func_get_args() );
545 public function wasErrorReissuable() {
546 return $this->__call( __FUNCTION__
, func_get_args() );
549 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
550 return $this->__call( __FUNCTION__
, func_get_args() );
553 public function getReplicaPos() {
554 return $this->__call( __FUNCTION__
, func_get_args() );
557 public function getMasterPos() {
558 return $this->__call( __FUNCTION__
, func_get_args() );
561 public function serverIsReadOnly() {
562 return $this->__call( __FUNCTION__
, func_get_args() );
565 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
566 // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
567 return $this->__call( __FUNCTION__
, func_get_args() );
570 public function onTransactionCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
571 // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
572 return $this->__call( __FUNCTION__
, func_get_args() );
575 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
576 return $this->onTransactionCommitOrIdle( $callback, $fname );
579 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
580 // DB_REPLICA role: caller might want to refresh cache after a cache mutex is released
581 return $this->__call( __FUNCTION__
, func_get_args() );
584 public function setTransactionListener( $name, callable
$callback = null ) {
585 return $this->__call( __FUNCTION__
, func_get_args() );
588 public function startAtomic(
589 $fname = __METHOD__
, $cancelable = IDatabase
::ATOMIC_NOT_CANCELABLE
591 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
592 return $this->__call( __FUNCTION__
, func_get_args() );
595 public function endAtomic( $fname = __METHOD__
) {
596 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
597 return $this->__call( __FUNCTION__
, func_get_args() );
600 public function cancelAtomic( $fname = __METHOD__
, AtomicSectionIdentifier
$sectionId = null ) {
601 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
602 return $this->__call( __FUNCTION__
, func_get_args() );
605 public function doAtomicSection(
606 $fname, callable
$callback, $cancelable = self
::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 begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
613 return $this->__call( __FUNCTION__
, func_get_args() );
616 public function commit( $fname = __METHOD__
, $flush = '' ) {
617 return $this->__call( __FUNCTION__
, func_get_args() );
620 public function rollback( $fname = __METHOD__
, $flush = '' ) {
621 return $this->__call( __FUNCTION__
, func_get_args() );
624 public function flushSnapshot( $fname = __METHOD__
) {
625 return $this->__call( __FUNCTION__
, func_get_args() );
628 public function timestamp( $ts = 0 ) {
629 return $this->__call( __FUNCTION__
, func_get_args() );
632 public function timestampOrNull( $ts = null ) {
633 return $this->__call( __FUNCTION__
, func_get_args() );
636 public function ping( &$rtt = null ) {
637 return func_num_args()
638 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
639 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
642 public function getLag() {
643 return $this->__call( __FUNCTION__
, func_get_args() );
646 public function getSessionLagStatus() {
647 return $this->__call( __FUNCTION__
, func_get_args() );
650 public function maxListLen() {
651 return $this->__call( __FUNCTION__
, func_get_args() );
654 public function encodeBlob( $b ) {
655 return $this->__call( __FUNCTION__
, func_get_args() );
658 public function decodeBlob( $b ) {
659 return $this->__call( __FUNCTION__
, func_get_args() );
662 public function setSessionOptions( array $options ) {
663 return $this->__call( __FUNCTION__
, func_get_args() );
666 public function setSchemaVars( $vars ) {
667 return $this->__call( __FUNCTION__
, func_get_args() );
670 public function lockIsFree( $lockName, $method ) {
671 $this->assertRoleAllowsWrites();
673 return $this->__call( __FUNCTION__
, func_get_args() );
676 public function lock( $lockName, $method, $timeout = 5 ) {
677 $this->assertRoleAllowsWrites();
679 return $this->__call( __FUNCTION__
, func_get_args() );
682 public function unlock( $lockName, $method ) {
683 $this->assertRoleAllowsWrites();
685 return $this->__call( __FUNCTION__
, func_get_args() );
688 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
689 $this->assertRoleAllowsWrites();
691 return $this->__call( __FUNCTION__
, func_get_args() );
694 public function namedLocksEnqueue() {
695 return $this->__call( __FUNCTION__
, func_get_args() );
698 public function getInfinity() {
699 return $this->__call( __FUNCTION__
, func_get_args() );
702 public function encodeExpiry( $expiry ) {
703 return $this->__call( __FUNCTION__
, func_get_args() );
706 public function decodeExpiry( $expiry, $format = TS_MW
) {
707 return $this->__call( __FUNCTION__
, func_get_args() );
710 public function setBigSelects( $value = true ) {
711 return $this->__call( __FUNCTION__
, func_get_args() );
714 public function isReadOnly() {
715 return $this->__call( __FUNCTION__
, func_get_args() );
718 public function setTableAliases( array $aliases ) {
719 return $this->__call( __FUNCTION__
, func_get_args() );
722 public function setIndexAliases( array $aliases ) {
723 return $this->__call( __FUNCTION__
, func_get_args() );
727 * Error out if the role is not DB_MASTER
729 * Note that the underlying connection may or may not itself be read-only.
730 * It could even be to a writable master (both server-side and to the application).
731 * This error is meant for the case when a DB_REPLICA handle was requested but a
732 * a write was attempted on that handle regardless.
734 * In configurations where the master DB has some generic read load or is the only server,
735 * DB_MASTER/DB_REPLICA will sometimes (or always) use the same connection to the master DB.
736 * This does not effect the role of DBConnRef instances.
737 * @throws DBReadOnlyRoleError
739 protected function assertRoleAllowsWrites() {
740 // DB_MASTER is "prima facie" writable
741 if ( $this->role
!== ILoadBalancer
::DB_MASTER
) {
742 throw new DBReadOnlyRoleError( $this->conn
, "Cannot write with role DB_REPLICA" );
747 * Clean up the connection when out of scope
749 function __destruct() {
751 $this->lb
->reuseConnection( $this->conn
);
758 * @deprecated since 1.29
760 class_alias( DBConnRef
::class, 'DBConnRef' );