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
11 * @note: proxy methods are defined explicitly to avoid interface errors
15 class DBConnRef
implements IDatabase
{
16 /** @var ILoadBalancer */
18 /** @var Database|null Live connection handle */
20 /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */
29 * @param ILoadBalancer $lb Connection manager for $conn
30 * @param Database|array $conn Database handle or (server index, query groups, domain, flags)
32 public function __construct( ILoadBalancer
$lb, $conn ) {
34 if ( $conn instanceof Database
) {
35 $this->conn
= $conn; // live handle
36 } elseif ( is_array( $conn ) && count( $conn ) >= 4 && $conn[self
::FLD_DOMAIN
] !== false ) {
37 $this->params
= $conn;
39 throw new InvalidArgumentException( "Missing lazy connection arguments." );
43 function __call( $name, array $arguments ) {
44 if ( $this->conn
=== null ) {
45 list( $db, $groups, $wiki, $flags ) = $this->params
;
46 $this->conn
= $this->lb
->getConnection( $db, $groups, $wiki, $flags );
49 return $this->conn
->$name( ...$arguments );
52 public function getServerInfo() {
53 return $this->__call( __FUNCTION__
, func_get_args() );
56 public function bufferResults( $buffer = null ) {
57 return $this->__call( __FUNCTION__
, func_get_args() );
60 public function trxLevel() {
61 return $this->__call( __FUNCTION__
, func_get_args() );
64 public function trxTimestamp() {
65 return $this->__call( __FUNCTION__
, func_get_args() );
68 public function explicitTrxActive() {
69 return $this->__call( __FUNCTION__
, func_get_args() );
72 public function assertNoOpenTransactions() {
73 return $this->__call( __FUNCTION__
, func_get_args() );
76 public function tablePrefix( $prefix = null ) {
77 return $this->__call( __FUNCTION__
, func_get_args() );
80 public function dbSchema( $schema = null ) {
81 return $this->__call( __FUNCTION__
, func_get_args() );
84 public function getLBInfo( $name = null ) {
85 return $this->__call( __FUNCTION__
, func_get_args() );
88 public function setLBInfo( $name, $value = null ) {
89 return $this->__call( __FUNCTION__
, func_get_args() );
92 public function setLazyMasterHandle( IDatabase
$conn ) {
93 return $this->__call( __FUNCTION__
, func_get_args() );
96 public function implicitGroupby() {
97 return $this->__call( __FUNCTION__
, func_get_args() );
100 public function implicitOrderby() {
101 return $this->__call( __FUNCTION__
, func_get_args() );
104 public function lastQuery() {
105 return $this->__call( __FUNCTION__
, func_get_args() );
108 public function doneWrites() {
109 return $this->__call( __FUNCTION__
, func_get_args() );
112 public function lastDoneWrites() {
113 return $this->__call( __FUNCTION__
, func_get_args() );
116 public function writesPending() {
117 return $this->__call( __FUNCTION__
, func_get_args() );
120 public function preCommitCallbacksPending() {
121 return $this->__call( __FUNCTION__
, func_get_args() );
124 public function writesOrCallbacksPending() {
125 return $this->__call( __FUNCTION__
, func_get_args() );
128 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
129 return $this->__call( __FUNCTION__
, func_get_args() );
132 public function pendingWriteCallers() {
133 return $this->__call( __FUNCTION__
, func_get_args() );
136 public function pendingWriteRowsAffected() {
137 return $this->__call( __FUNCTION__
, func_get_args() );
140 public function isOpen() {
141 return $this->__call( __FUNCTION__
, func_get_args() );
144 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
145 return $this->__call( __FUNCTION__
, func_get_args() );
148 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
149 return $this->__call( __FUNCTION__
, func_get_args() );
152 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
153 return $this->__call( __FUNCTION__
, func_get_args() );
156 public function getFlag( $flag ) {
157 return $this->__call( __FUNCTION__
, func_get_args() );
160 public function getProperty( $name ) {
161 return $this->__call( __FUNCTION__
, func_get_args() );
164 public function getDomainID() {
165 if ( $this->conn
=== null ) {
166 $domain = $this->params
[self
::FLD_DOMAIN
];
167 // Avoid triggering a database connection
168 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
171 return $this->__call( __FUNCTION__
, func_get_args() );
175 * @codeCoverageIgnore
177 public function getWikiID() {
178 return $this->getDomainID();
181 public function getType() {
182 return $this->__call( __FUNCTION__
, func_get_args() );
185 public function fetchObject( $res ) {
186 return $this->__call( __FUNCTION__
, func_get_args() );
189 public function fetchRow( $res ) {
190 return $this->__call( __FUNCTION__
, func_get_args() );
193 public function numRows( $res ) {
194 return $this->__call( __FUNCTION__
, func_get_args() );
197 public function numFields( $res ) {
198 return $this->__call( __FUNCTION__
, func_get_args() );
201 public function fieldName( $res, $n ) {
202 return $this->__call( __FUNCTION__
, func_get_args() );
205 public function insertId() {
206 return $this->__call( __FUNCTION__
, func_get_args() );
209 public function dataSeek( $res, $row ) {
210 return $this->__call( __FUNCTION__
, func_get_args() );
213 public function lastErrno() {
214 return $this->__call( __FUNCTION__
, func_get_args() );
217 public function lastError() {
218 return $this->__call( __FUNCTION__
, func_get_args() );
221 public function affectedRows() {
222 return $this->__call( __FUNCTION__
, func_get_args() );
225 public function getSoftwareLink() {
226 return $this->__call( __FUNCTION__
, func_get_args() );
229 public function getServerVersion() {
230 return $this->__call( __FUNCTION__
, func_get_args() );
233 public function close() {
234 return $this->__call( __FUNCTION__
, func_get_args() );
237 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
238 return $this->__call( __FUNCTION__
, func_get_args() );
241 public function freeResult( $res ) {
242 return $this->__call( __FUNCTION__
, func_get_args() );
245 public function selectField(
246 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
248 return $this->__call( __FUNCTION__
, func_get_args() );
251 public function selectFieldValues(
252 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
254 return $this->__call( __FUNCTION__
, func_get_args() );
257 public function select(
258 $table, $vars, $conds = '', $fname = __METHOD__
,
259 $options = [], $join_conds = []
261 return $this->__call( __FUNCTION__
, func_get_args() );
264 public function selectSQLText(
265 $table, $vars, $conds = '', $fname = __METHOD__
,
266 $options = [], $join_conds = []
268 return $this->__call( __FUNCTION__
, func_get_args() );
271 public function selectRow(
272 $table, $vars, $conds, $fname = __METHOD__
,
273 $options = [], $join_conds = []
275 return $this->__call( __FUNCTION__
, func_get_args() );
278 public function estimateRowCount(
279 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
281 return $this->__call( __FUNCTION__
, func_get_args() );
284 public function selectRowCount(
285 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
287 return $this->__call( __FUNCTION__
, func_get_args() );
290 public function lockForUpdate(
291 $table, $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
293 return $this->__call( __FUNCTION__
, func_get_args() );
296 public function fieldExists( $table, $field, $fname = __METHOD__
) {
297 return $this->__call( __FUNCTION__
, func_get_args() );
300 public function indexExists( $table, $index, $fname = __METHOD__
) {
301 return $this->__call( __FUNCTION__
, func_get_args() );
304 public function tableExists( $table, $fname = __METHOD__
) {
305 return $this->__call( __FUNCTION__
, func_get_args() );
308 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
309 return $this->__call( __FUNCTION__
, func_get_args() );
312 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
313 return $this->__call( __FUNCTION__
, func_get_args() );
316 public function makeList( $a, $mode = self
::LIST_COMMA
) {
317 return $this->__call( __FUNCTION__
, func_get_args() );
320 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
321 return $this->__call( __FUNCTION__
, func_get_args() );
324 public function aggregateValue( $valuedata, $valuename = 'value' ) {
325 return $this->__call( __FUNCTION__
, func_get_args() );
328 public function bitNot( $field ) {
329 return $this->__call( __FUNCTION__
, func_get_args() );
332 public function bitAnd( $fieldLeft, $fieldRight ) {
333 return $this->__call( __FUNCTION__
, func_get_args() );
336 public function bitOr( $fieldLeft, $fieldRight ) {
337 return $this->__call( __FUNCTION__
, func_get_args() );
340 public function buildConcat( $stringList ) {
341 return $this->__call( __FUNCTION__
, func_get_args() );
344 public function buildGroupConcatField(
345 $delim, $table, $field, $conds = '', $join_conds = []
347 return $this->__call( __FUNCTION__
, func_get_args() );
350 public function buildSubstring( $input, $startPosition, $length = null ) {
351 return $this->__call( __FUNCTION__
, func_get_args() );
354 public function buildStringCast( $field ) {
355 return $this->__call( __FUNCTION__
, func_get_args() );
358 public function buildIntegerCast( $field ) {
359 return $this->__call( __FUNCTION__
, func_get_args() );
362 public function buildSelectSubquery(
363 $table, $vars, $conds = '', $fname = __METHOD__
,
364 $options = [], $join_conds = []
366 return $this->__call( __FUNCTION__
, func_get_args() );
369 public function databasesAreIndependent() {
370 return $this->__call( __FUNCTION__
, func_get_args() );
373 public function selectDB( $db ) {
374 // Disallow things that might confuse the LoadBalancer tracking
375 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
378 public function getDBname() {
379 return $this->__call( __FUNCTION__
, func_get_args() );
382 public function getServer() {
383 return $this->__call( __FUNCTION__
, func_get_args() );
386 public function addQuotes( $s ) {
387 return $this->__call( __FUNCTION__
, func_get_args() );
390 public function buildLike() {
391 return $this->__call( __FUNCTION__
, func_get_args() );
394 public function anyChar() {
395 return $this->__call( __FUNCTION__
, func_get_args() );
398 public function anyString() {
399 return $this->__call( __FUNCTION__
, func_get_args() );
402 public function nextSequenceValue( $seqName ) {
403 return $this->__call( __FUNCTION__
, func_get_args() );
406 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
407 return $this->__call( __FUNCTION__
, func_get_args() );
410 public function upsert(
411 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
413 return $this->__call( __FUNCTION__
, func_get_args() );
416 public function deleteJoin(
417 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
419 return $this->__call( __FUNCTION__
, func_get_args() );
422 public function delete( $table, $conds, $fname = __METHOD__
) {
423 return $this->__call( __FUNCTION__
, func_get_args() );
426 public function insertSelect(
427 $destTable, $srcTable, $varMap, $conds,
428 $fname = __METHOD__
, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
430 return $this->__call( __FUNCTION__
, func_get_args() );
433 public function unionSupportsOrderAndLimit() {
434 return $this->__call( __FUNCTION__
, func_get_args() );
437 public function unionQueries( $sqls, $all ) {
438 return $this->__call( __FUNCTION__
, func_get_args() );
441 public function unionConditionPermutations(
442 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__
,
443 $options = [], $join_conds = []
445 return $this->__call( __FUNCTION__
, func_get_args() );
448 public function conditional( $cond, $trueVal, $falseVal ) {
449 return $this->__call( __FUNCTION__
, func_get_args() );
452 public function strreplace( $orig, $old, $new ) {
453 return $this->__call( __FUNCTION__
, func_get_args() );
456 public function getServerUptime() {
457 return $this->__call( __FUNCTION__
, func_get_args() );
460 public function wasDeadlock() {
461 return $this->__call( __FUNCTION__
, func_get_args() );
464 public function wasLockTimeout() {
465 return $this->__call( __FUNCTION__
, func_get_args() );
468 public function wasConnectionLoss() {
469 return $this->__call( __FUNCTION__
, func_get_args() );
472 public function wasReadOnlyError() {
473 return $this->__call( __FUNCTION__
, func_get_args() );
476 public function wasErrorReissuable() {
477 return $this->__call( __FUNCTION__
, func_get_args() );
480 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
481 return $this->__call( __FUNCTION__
, func_get_args() );
484 public function getReplicaPos() {
485 return $this->__call( __FUNCTION__
, func_get_args() );
488 public function getMasterPos() {
489 return $this->__call( __FUNCTION__
, func_get_args() );
492 public function serverIsReadOnly() {
493 return $this->__call( __FUNCTION__
, func_get_args() );
496 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
497 return $this->__call( __FUNCTION__
, func_get_args() );
500 public function onTransactionCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
501 return $this->__call( __FUNCTION__
, func_get_args() );
504 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
505 return $this->__call( __FUNCTION__
, func_get_args() );
508 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
509 return $this->__call( __FUNCTION__
, func_get_args() );
512 public function setTransactionListener( $name, callable
$callback = null ) {
513 return $this->__call( __FUNCTION__
, func_get_args() );
516 public function startAtomic(
517 $fname = __METHOD__
, $cancelable = IDatabase
::ATOMIC_NOT_CANCELABLE
519 return $this->__call( __FUNCTION__
, func_get_args() );
522 public function endAtomic( $fname = __METHOD__
) {
523 return $this->__call( __FUNCTION__
, func_get_args() );
526 public function cancelAtomic( $fname = __METHOD__
, AtomicSectionIdentifier
$sectionId = null ) {
527 return $this->__call( __FUNCTION__
, func_get_args() );
530 public function doAtomicSection(
531 $fname, callable
$callback, $cancelable = self
::ATOMIC_NOT_CANCELABLE
533 return $this->__call( __FUNCTION__
, func_get_args() );
536 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
537 return $this->__call( __FUNCTION__
, func_get_args() );
540 public function commit( $fname = __METHOD__
, $flush = '' ) {
541 return $this->__call( __FUNCTION__
, func_get_args() );
544 public function rollback( $fname = __METHOD__
, $flush = '' ) {
545 return $this->__call( __FUNCTION__
, func_get_args() );
548 public function flushSnapshot( $fname = __METHOD__
) {
549 return $this->__call( __FUNCTION__
, func_get_args() );
552 public function timestamp( $ts = 0 ) {
553 return $this->__call( __FUNCTION__
, func_get_args() );
556 public function timestampOrNull( $ts = null ) {
557 return $this->__call( __FUNCTION__
, func_get_args() );
560 public function ping( &$rtt = null ) {
561 return func_num_args()
562 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
563 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
566 public function getLag() {
567 return $this->__call( __FUNCTION__
, func_get_args() );
570 public function getSessionLagStatus() {
571 return $this->__call( __FUNCTION__
, func_get_args() );
574 public function maxListLen() {
575 return $this->__call( __FUNCTION__
, func_get_args() );
578 public function encodeBlob( $b ) {
579 return $this->__call( __FUNCTION__
, func_get_args() );
582 public function decodeBlob( $b ) {
583 return $this->__call( __FUNCTION__
, func_get_args() );
586 public function setSessionOptions( array $options ) {
587 return $this->__call( __FUNCTION__
, func_get_args() );
590 public function setSchemaVars( $vars ) {
591 return $this->__call( __FUNCTION__
, func_get_args() );
594 public function lockIsFree( $lockName, $method ) {
595 return $this->__call( __FUNCTION__
, func_get_args() );
598 public function lock( $lockName, $method, $timeout = 5 ) {
599 return $this->__call( __FUNCTION__
, func_get_args() );
602 public function unlock( $lockName, $method ) {
603 return $this->__call( __FUNCTION__
, func_get_args() );
606 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
607 return $this->__call( __FUNCTION__
, func_get_args() );
610 public function namedLocksEnqueue() {
611 return $this->__call( __FUNCTION__
, func_get_args() );
614 public function getInfinity() {
615 return $this->__call( __FUNCTION__
, func_get_args() );
618 public function encodeExpiry( $expiry ) {
619 return $this->__call( __FUNCTION__
, func_get_args() );
622 public function decodeExpiry( $expiry, $format = TS_MW
) {
623 return $this->__call( __FUNCTION__
, func_get_args() );
626 public function setBigSelects( $value = true ) {
627 return $this->__call( __FUNCTION__
, func_get_args() );
630 public function isReadOnly() {
631 return $this->__call( __FUNCTION__
, func_get_args() );
634 public function setTableAliases( array $aliases ) {
635 return $this->__call( __FUNCTION__
, func_get_args() );
638 public function setIndexAliases( array $aliases ) {
639 return $this->__call( __FUNCTION__
, func_get_args() );
643 * Clean up the connection when out of scope
645 function __destruct() {
647 $this->lb
->reuseConnection( $this->conn
);
654 * @deprecated since 1.29
656 class_alias( DBConnRef
::class, 'DBConnRef' );