MediaWiki  master
DBConnRef.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
6 
29 class DBConnRef implements IDatabase {
31  private $lb;
33  private $conn;
35  private $params;
37  private $role;
38 
39  const FLD_INDEX = 0;
40  const FLD_GROUP = 1;
41  const FLD_DOMAIN = 2;
42  const FLD_FLAGS = 3;
43 
50  public function __construct( ILoadBalancer $lb, $conn, $role ) {
51  $this->lb = $lb;
52  $this->role = $role;
53  if ( $conn instanceof IDatabase && !( $conn instanceof DBConnRef ) ) {
54  $this->conn = $conn; // live handle
55  } elseif ( is_array( $conn ) && count( $conn ) >= 4 && $conn[self::FLD_DOMAIN] !== false ) {
56  $this->params = $conn;
57  } else {
58  throw new InvalidArgumentException( "Missing lazy connection arguments." );
59  }
60  }
61 
62  function __call( $name, array $arguments ) {
63  if ( $this->conn === null ) {
64  list( $index, $groups, $wiki, $flags ) = $this->params;
65  $this->conn = $this->lb->getConnection( $index, $groups, $wiki, $flags );
66  }
67 
68  return $this->conn->$name( ...$arguments );
69  }
70 
75  public function getReferenceRole() {
76  return $this->role;
77  }
78 
79  public function getServerInfo() {
80  return $this->__call( __FUNCTION__, func_get_args() );
81  }
82 
83  public function getTopologyRole() {
84  return $this->__call( __FUNCTION__, func_get_args() );
85  }
86 
87  public function getTopologyRootMaster() {
88  return $this->__call( __FUNCTION__, func_get_args() );
89  }
90 
96  public function bufferResults( $buffer = null ) {
97  return $this->__call( __FUNCTION__, func_get_args() );
98  }
99 
100  public function trxLevel() {
101  return $this->__call( __FUNCTION__, func_get_args() );
102  }
103 
104  public function trxTimestamp() {
105  return $this->__call( __FUNCTION__, func_get_args() );
106  }
107 
108  public function explicitTrxActive() {
109  return $this->__call( __FUNCTION__, func_get_args() );
110  }
111 
112  public function assertNoOpenTransactions() {
113  return $this->__call( __FUNCTION__, func_get_args() );
114  }
115 
116  public function tablePrefix( $prefix = null ) {
117  if ( $this->conn === null && $prefix === null ) {
118  $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
119  // Avoid triggering a database connection
120  return $domain->getTablePrefix();
121  } elseif ( $this->conn !== null && $prefix === null ) {
122  // This will just return the prefix
123  return $this->__call( __FUNCTION__, func_get_args() );
124  }
125  // Disallow things that might confuse the LoadBalancer tracking
126  throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
127  }
128 
129  public function dbSchema( $schema = null ) {
130  if ( $this->conn === null && $schema === null ) {
131  $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
132  // Avoid triggering a database connection
133  return $domain->getSchema();
134  } elseif ( $this->conn !== null && $schema === null ) {
135  // This will just return the schema
136  return $this->__call( __FUNCTION__, func_get_args() );
137  }
138  // Disallow things that might confuse the LoadBalancer tracking
139  throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
140  }
141 
142  public function getLBInfo( $name = null ) {
143  return $this->__call( __FUNCTION__, func_get_args() );
144  }
145 
146  public function setLBInfo( $nameOrArray, $value = null ) {
147  // Disallow things that might confuse the LoadBalancer tracking
148  throw new DBUnexpectedError( $this, "Changing LB info is disallowed to enable reuse." );
149  }
150 
151  public function implicitOrderby() {
152  return $this->__call( __FUNCTION__, func_get_args() );
153  }
154 
155  public function lastQuery() {
156  return $this->__call( __FUNCTION__, func_get_args() );
157  }
158 
159  public function lastDoneWrites() {
160  return $this->__call( __FUNCTION__, func_get_args() );
161  }
162 
163  public function writesPending() {
164  return $this->__call( __FUNCTION__, func_get_args() );
165  }
166 
167  public function preCommitCallbacksPending() {
168  return $this->__call( __FUNCTION__, func_get_args() );
169  }
170 
171  public function writesOrCallbacksPending() {
172  return $this->__call( __FUNCTION__, func_get_args() );
173  }
174 
175  public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
176  return $this->__call( __FUNCTION__, func_get_args() );
177  }
178 
179  public function pendingWriteCallers() {
180  return $this->__call( __FUNCTION__, func_get_args() );
181  }
182 
183  public function pendingWriteRowsAffected() {
184  return $this->__call( __FUNCTION__, func_get_args() );
185  }
186 
187  public function isOpen() {
188  return $this->__call( __FUNCTION__, func_get_args() );
189  }
190 
191  public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
192  return $this->__call( __FUNCTION__, func_get_args() );
193  }
194 
195  public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
196  return $this->__call( __FUNCTION__, func_get_args() );
197  }
198 
199  public function restoreFlags( $state = self::RESTORE_PRIOR ) {
200  return $this->__call( __FUNCTION__, func_get_args() );
201  }
202 
203  public function getFlag( $flag ) {
204  return $this->__call( __FUNCTION__, func_get_args() );
205  }
206 
207  public function getProperty( $name ) {
208  return $this->__call( __FUNCTION__, func_get_args() );
209  }
210 
211  public function getDomainID() {
212  if ( $this->conn === null ) {
213  $domain = $this->params[self::FLD_DOMAIN];
214  // Avoid triggering a database connection
215  return $domain instanceof DatabaseDomain ? $domain->getId() : $domain;
216  }
217 
218  return $this->__call( __FUNCTION__, func_get_args() );
219  }
220 
221  public function getType() {
222  if ( $this->conn === null ) {
223  // Avoid triggering a database connection
224  if ( $this->params[self::FLD_INDEX] === ILoadBalancer::DB_MASTER ) {
225  $index = $this->lb->getWriterIndex();
226  } else {
227  $index = $this->params[self::FLD_INDEX];
228  }
229  if ( $index >= 0 ) {
230  // In theory, if $index is DB_REPLICA, the type could vary
231  return $this->lb->getServerType( $index );
232  }
233  }
234 
235  return $this->__call( __FUNCTION__, func_get_args() );
236  }
237 
238  public function fetchObject( $res ) {
239  return $this->__call( __FUNCTION__, func_get_args() );
240  }
241 
242  public function fetchRow( $res ) {
243  return $this->__call( __FUNCTION__, func_get_args() );
244  }
245 
246  public function numRows( $res ) {
247  return $this->__call( __FUNCTION__, func_get_args() );
248  }
249 
250  public function numFields( $res ) {
251  return $this->__call( __FUNCTION__, func_get_args() );
252  }
253 
254  public function fieldName( $res, $n ) {
255  return $this->__call( __FUNCTION__, func_get_args() );
256  }
257 
258  public function insertId() {
259  return $this->__call( __FUNCTION__, func_get_args() );
260  }
261 
262  public function dataSeek( $res, $row ) {
263  return $this->__call( __FUNCTION__, func_get_args() );
264  }
265 
266  public function lastErrno() {
267  return $this->__call( __FUNCTION__, func_get_args() );
268  }
269 
270  public function lastError() {
271  return $this->__call( __FUNCTION__, func_get_args() );
272  }
273 
274  public function affectedRows() {
275  return $this->__call( __FUNCTION__, func_get_args() );
276  }
277 
278  public function getSoftwareLink() {
279  return $this->__call( __FUNCTION__, func_get_args() );
280  }
281 
282  public function getServerVersion() {
283  return $this->__call( __FUNCTION__, func_get_args() );
284  }
285 
286  public function close( $fname = __METHOD__, $owner = null ) {
287  throw new DBUnexpectedError( $this->conn, 'Cannot close shared connection.' );
288  }
289 
290  public function query( $sql, $fname = __METHOD__, $flags = 0 ) {
291  if ( $this->role !== ILoadBalancer::DB_MASTER ) {
292  $flags |= IDatabase::QUERY_REPLICA_ROLE;
293  }
294 
295  return $this->__call( __FUNCTION__, [ $sql, $fname, $flags ] );
296  }
297 
298  public function freeResult( $res ) {
299  return $this->__call( __FUNCTION__, func_get_args() );
300  }
301 
302  public function selectField(
303  $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
304  ) {
305  return $this->__call( __FUNCTION__, func_get_args() );
306  }
307 
308  public function selectFieldValues(
309  $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
310  ) {
311  return $this->__call( __FUNCTION__, func_get_args() );
312  }
313 
314  public function select(
315  $table, $vars, $conds = '', $fname = __METHOD__,
316  $options = [], $join_conds = []
317  ) {
318  return $this->__call( __FUNCTION__, func_get_args() );
319  }
320 
321  public function selectSQLText(
322  $table, $vars, $conds = '', $fname = __METHOD__,
323  $options = [], $join_conds = []
324  ) {
325  return $this->__call( __FUNCTION__, func_get_args() );
326  }
327 
328  public function limitResult( $sql, $limit, $offset = false ) {
329  return $this->__call( __FUNCTION__, func_get_args() );
330  }
331 
332  public function selectRow(
333  $table, $vars, $conds, $fname = __METHOD__,
334  $options = [], $join_conds = []
335  ) {
336  return $this->__call( __FUNCTION__, func_get_args() );
337  }
338 
339  public function estimateRowCount(
340  $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
341  ) {
342  return $this->__call( __FUNCTION__, func_get_args() );
343  }
344 
345  public function selectRowCount(
346  $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
347  ) {
348  return $this->__call( __FUNCTION__, func_get_args() );
349  }
350 
351  public function lockForUpdate(
352  $table, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
353  ) {
354  $this->assertRoleAllowsWrites();
355 
356  return $this->__call( __FUNCTION__, func_get_args() );
357  }
358 
359  public function fieldExists( $table, $field, $fname = __METHOD__ ) {
360  return $this->__call( __FUNCTION__, func_get_args() );
361  }
362 
363  public function indexExists( $table, $index, $fname = __METHOD__ ) {
364  return $this->__call( __FUNCTION__, func_get_args() );
365  }
366 
367  public function tableExists( $table, $fname = __METHOD__ ) {
368  return $this->__call( __FUNCTION__, func_get_args() );
369  }
370 
371  public function insert( $table, $rows, $fname = __METHOD__, $options = [] ) {
372  $this->assertRoleAllowsWrites();
373 
374  return $this->__call( __FUNCTION__, func_get_args() );
375  }
376 
377  public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
378  $this->assertRoleAllowsWrites();
379 
380  return $this->__call( __FUNCTION__, func_get_args() );
381  }
382 
383  public function makeList( array $a, $mode = self::LIST_COMMA ) {
384  return $this->__call( __FUNCTION__, func_get_args() );
385  }
386 
387  public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
388  return $this->__call( __FUNCTION__, func_get_args() );
389  }
390 
391  public function aggregateValue( $valuedata, $valuename = 'value' ) {
392  return $this->__call( __FUNCTION__, func_get_args() );
393  }
394 
395  public function bitNot( $field ) {
396  return $this->__call( __FUNCTION__, func_get_args() );
397  }
398 
399  public function bitAnd( $fieldLeft, $fieldRight ) {
400  return $this->__call( __FUNCTION__, func_get_args() );
401  }
402 
403  public function bitOr( $fieldLeft, $fieldRight ) {
404  return $this->__call( __FUNCTION__, func_get_args() );
405  }
406 
407  public function buildConcat( $stringList ) {
408  return $this->__call( __FUNCTION__, func_get_args() );
409  }
410 
411  public function buildGroupConcatField(
412  $delim, $table, $field, $conds = '', $join_conds = []
413  ) {
414  return $this->__call( __FUNCTION__, func_get_args() );
415  }
416 
417  public function buildSubstring( $input, $startPosition, $length = null ) {
418  return $this->__call( __FUNCTION__, func_get_args() );
419  }
420 
421  public function buildStringCast( $field ) {
422  return $this->__call( __FUNCTION__, func_get_args() );
423  }
424 
425  public function buildIntegerCast( $field ) {
426  return $this->__call( __FUNCTION__, func_get_args() );
427  }
428 
429  public function buildSelectSubquery(
430  $table, $vars, $conds = '', $fname = __METHOD__,
431  $options = [], $join_conds = []
432  ) {
433  return $this->__call( __FUNCTION__, func_get_args() );
434  }
435 
436  public function databasesAreIndependent() {
437  return $this->__call( __FUNCTION__, func_get_args() );
438  }
439 
440  public function selectDB( $db ) {
441  // Disallow things that might confuse the LoadBalancer tracking
442  throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
443  }
444 
445  public function selectDomain( $domain ) {
446  // Disallow things that might confuse the LoadBalancer tracking
447  throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
448  }
449 
450  public function getDBname() {
451  if ( $this->conn === null ) {
452  $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
453  // Avoid triggering a database connection
454  return $domain->getDatabase();
455  }
456 
457  return $this->__call( __FUNCTION__, func_get_args() );
458  }
459 
460  public function getServer() {
461  return $this->__call( __FUNCTION__, func_get_args() );
462  }
463 
464  public function addQuotes( $s ) {
465  return $this->__call( __FUNCTION__, func_get_args() );
466  }
467 
468  public function addIdentifierQuotes( $s ) {
469  return $this->__call( __FUNCTION__, func_get_args() );
470  }
471 
472  public function buildLike( $param, ...$params ) {
473  return $this->__call( __FUNCTION__, func_get_args() );
474  }
475 
476  public function anyChar() {
477  return $this->__call( __FUNCTION__, func_get_args() );
478  }
479 
480  public function anyString() {
481  return $this->__call( __FUNCTION__, func_get_args() );
482  }
483 
484  public function nextSequenceValue( $seqName ) {
485  $this->assertRoleAllowsWrites();
486 
487  return $this->__call( __FUNCTION__, func_get_args() );
488  }
489 
490  public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
491  $this->assertRoleAllowsWrites();
492 
493  return $this->__call( __FUNCTION__, func_get_args() );
494  }
495 
496  public function upsert(
497  $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__
498  ) {
499  $this->assertRoleAllowsWrites();
500 
501  return $this->__call( __FUNCTION__, func_get_args() );
502  }
503 
504  public function deleteJoin(
505  $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
506  ) {
507  $this->assertRoleAllowsWrites();
508 
509  return $this->__call( __FUNCTION__, func_get_args() );
510  }
511 
512  public function delete( $table, $conds, $fname = __METHOD__ ) {
513  $this->assertRoleAllowsWrites();
514 
515  return $this->__call( __FUNCTION__, func_get_args() );
516  }
517 
518  public function insertSelect(
519  $destTable, $srcTable, $varMap, $conds,
520  $fname = __METHOD__, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
521  ) {
522  $this->assertRoleAllowsWrites();
523 
524  return $this->__call( __FUNCTION__, func_get_args() );
525  }
526 
527  public function unionSupportsOrderAndLimit() {
528  return $this->__call( __FUNCTION__, func_get_args() );
529  }
530 
531  public function unionQueries( $sqls, $all ) {
532  return $this->__call( __FUNCTION__, func_get_args() );
533  }
534 
535  public function unionConditionPermutations(
536  $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__,
537  $options = [], $join_conds = []
538  ) {
539  return $this->__call( __FUNCTION__, func_get_args() );
540  }
541 
542  public function conditional( $cond, $trueVal, $falseVal ) {
543  return $this->__call( __FUNCTION__, func_get_args() );
544  }
545 
546  public function strreplace( $orig, $old, $new ) {
547  return $this->__call( __FUNCTION__, func_get_args() );
548  }
549 
550  public function getServerUptime() {
551  return $this->__call( __FUNCTION__, func_get_args() );
552  }
553 
554  public function wasDeadlock() {
555  return $this->__call( __FUNCTION__, func_get_args() );
556  }
557 
558  public function wasLockTimeout() {
559  return $this->__call( __FUNCTION__, func_get_args() );
560  }
561 
562  public function wasConnectionLoss() {
563  return $this->__call( __FUNCTION__, func_get_args() );
564  }
565 
566  public function wasReadOnlyError() {
567  return $this->__call( __FUNCTION__, func_get_args() );
568  }
569 
570  public function wasErrorReissuable() {
571  return $this->__call( __FUNCTION__, func_get_args() );
572  }
573 
574  public function masterPosWait( DBMasterPos $pos, $timeout ) {
575  return $this->__call( __FUNCTION__, func_get_args() );
576  }
577 
578  public function getReplicaPos() {
579  return $this->__call( __FUNCTION__, func_get_args() );
580  }
581 
582  public function getMasterPos() {
583  return $this->__call( __FUNCTION__, func_get_args() );
584  }
585 
586  public function serverIsReadOnly() {
587  return $this->__call( __FUNCTION__, func_get_args() );
588  }
589 
590  public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
591  // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
592  return $this->__call( __FUNCTION__, func_get_args() );
593  }
594 
595  public function onTransactionCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
596  // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
597  return $this->__call( __FUNCTION__, func_get_args() );
598  }
599 
600  public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
601  return $this->onTransactionCommitOrIdle( $callback, $fname );
602  }
603 
604  public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
605  // DB_REPLICA role: caller might want to refresh cache after a cache mutex is released
606  return $this->__call( __FUNCTION__, func_get_args() );
607  }
608 
609  public function onAtomicSectionCancel( callable $callback, $fname = __METHOD__ ) {
610  return $this->__call( __FUNCTION__, func_get_args() );
611  }
612 
613  public function setTransactionListener( $name, callable $callback = null ) {
614  return $this->__call( __FUNCTION__, func_get_args() );
615  }
616 
617  public function startAtomic(
618  $fname = __METHOD__, $cancelable = IDatabase::ATOMIC_NOT_CANCELABLE
619  ) {
620  // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
621  return $this->__call( __FUNCTION__, func_get_args() );
622  }
623 
624  public function endAtomic( $fname = __METHOD__ ) {
625  // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
626  return $this->__call( __FUNCTION__, func_get_args() );
627  }
628 
629  public function cancelAtomic( $fname = __METHOD__, AtomicSectionIdentifier $sectionId = null ) {
630  // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
631  return $this->__call( __FUNCTION__, func_get_args() );
632  }
633 
634  public function doAtomicSection(
635  $fname, callable $callback, $cancelable = self::ATOMIC_NOT_CANCELABLE
636  ) {
637  // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
638  return $this->__call( __FUNCTION__, func_get_args() );
639  }
640 
641  public function begin( $fname = __METHOD__, $mode = IDatabase::TRANSACTION_EXPLICIT ) {
642  return $this->__call( __FUNCTION__, func_get_args() );
643  }
644 
645  public function commit( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
646  return $this->__call( __FUNCTION__, func_get_args() );
647  }
648 
649  public function rollback( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
650  return $this->__call( __FUNCTION__, func_get_args() );
651  }
652 
653  public function flushSnapshot( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
654  return $this->__call( __FUNCTION__, func_get_args() );
655  }
656 
657  public function timestamp( $ts = 0 ) {
658  return $this->__call( __FUNCTION__, func_get_args() );
659  }
660 
661  public function timestampOrNull( $ts = null ) {
662  return $this->__call( __FUNCTION__, func_get_args() );
663  }
664 
665  public function ping( &$rtt = null ) {
666  return func_num_args()
667  ? $this->__call( __FUNCTION__, [ &$rtt ] )
668  : $this->__call( __FUNCTION__, [] ); // method cares about null vs missing
669  }
670 
671  public function getLag() {
672  return $this->__call( __FUNCTION__, func_get_args() );
673  }
674 
675  public function getSessionLagStatus() {
676  return $this->__call( __FUNCTION__, func_get_args() );
677  }
678 
679  public function maxListLen() {
680  return $this->__call( __FUNCTION__, func_get_args() );
681  }
682 
683  public function encodeBlob( $b ) {
684  return $this->__call( __FUNCTION__, func_get_args() );
685  }
686 
687  public function decodeBlob( $b ) {
688  return $this->__call( __FUNCTION__, func_get_args() );
689  }
690 
691  public function setSessionOptions( array $options ) {
692  return $this->__call( __FUNCTION__, func_get_args() );
693  }
694 
695  public function setSchemaVars( $vars ) {
696  return $this->__call( __FUNCTION__, func_get_args() );
697  }
698 
699  public function lockIsFree( $lockName, $method ) {
700  $this->assertRoleAllowsWrites();
701 
702  return $this->__call( __FUNCTION__, func_get_args() );
703  }
704 
705  public function lock( $lockName, $method, $timeout = 5 ) {
706  $this->assertRoleAllowsWrites();
707 
708  return $this->__call( __FUNCTION__, func_get_args() );
709  }
710 
711  public function unlock( $lockName, $method ) {
712  $this->assertRoleAllowsWrites();
713 
714  return $this->__call( __FUNCTION__, func_get_args() );
715  }
716 
717  public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
718  $this->assertRoleAllowsWrites();
719 
720  return $this->__call( __FUNCTION__, func_get_args() );
721  }
722 
723  public function namedLocksEnqueue() {
724  return $this->__call( __FUNCTION__, func_get_args() );
725  }
726 
727  public function getInfinity() {
728  return $this->__call( __FUNCTION__, func_get_args() );
729  }
730 
731  public function encodeExpiry( $expiry ) {
732  return $this->__call( __FUNCTION__, func_get_args() );
733  }
734 
735  public function decodeExpiry( $expiry, $format = TS_MW ) {
736  return $this->__call( __FUNCTION__, func_get_args() );
737  }
738 
739  public function setBigSelects( $value = true ) {
740  return $this->__call( __FUNCTION__, func_get_args() );
741  }
742 
743  public function isReadOnly() {
744  return $this->__call( __FUNCTION__, func_get_args() );
745  }
746 
747  public function setTableAliases( array $aliases ) {
748  return $this->__call( __FUNCTION__, func_get_args() );
749  }
750 
751  public function setIndexAliases( array $aliases ) {
752  return $this->__call( __FUNCTION__, func_get_args() );
753  }
754 
755  public function __toString() {
756  if ( $this->conn === null ) {
757  // spl_object_id is PHP >= 7.2
758  $id = function_exists( 'spl_object_id' )
759  ? spl_object_id( $this )
760  : spl_object_hash( $this );
761 
762  return $this->getType() . ' object #' . $id;
763  }
764 
765  return $this->__call( __FUNCTION__, func_get_args() );
766  }
767 
781  protected function assertRoleAllowsWrites() {
782  // DB_MASTER is "prima facie" writable
783  if ( $this->role !== ILoadBalancer::DB_MASTER ) {
784  throw new DBReadOnlyRoleError( $this->conn, "Cannot write with role DB_REPLICA" );
785  }
786  }
787 
791  function __destruct() {
792  if ( $this->conn ) {
793  $this->lb->reuseConnection( $this->conn );
794  }
795  }
796 }
797 
802 class_alias( DBConnRef::class, 'DBConnRef' );
insertId()
Get the inserted value of an auto-increment row.
Definition: DBConnRef.php:258
affectedRows()
Get the number of rows affected by the last write query.
Definition: DBConnRef.php:274
bufferResults( $buffer=null)
Definition: DBConnRef.php:96
buildSelectSubquery( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Equivalent to IDatabase::selectSQLText() except wraps the result in Subqyery.
Definition: DBConnRef.php:429
bitAnd( $fieldLeft, $fieldRight)
Definition: DBConnRef.php:399
int $role
One of DB_MASTER/DB_REPLICA.
Definition: DBConnRef.php:37
endAtomic( $fname=__METHOD__)
Ends an atomic section of SQL statements.
Definition: DBConnRef.php:624
anyString()
Returns a token for buildLike() that denotes a &#39;&#39; to be used in a LIKE query.
Definition: DBConnRef.php:480
close( $fname=__METHOD__, $owner=null)
Close the database connection.
Definition: DBConnRef.php:286
getScopedLockAndFlush( $lockKey, $fname, $timeout)
Acquire a named lock, flush any transaction, and return an RAII style unlocker object.
Definition: DBConnRef.php:717
getReplicaPos()
Get the replication position of this replica DB.
Definition: DBConnRef.php:578
lastDoneWrites()
Get the last time the connection may have been used for a write query.
Definition: DBConnRef.php:159
ping(&$rtt=null)
Ping the server and try to reconnect if it there is no connection.
Definition: DBConnRef.php:665
getFlag( $flag)
Returns a boolean whether the flag $flag is set for this connection.
Definition: DBConnRef.php:203
selectField( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a single field from a single result row.
Definition: DBConnRef.php:302
addIdentifierQuotes( $s)
Escape a SQL identifier (e.g.
Definition: DBConnRef.php:468
buildLike( $param,... $params)
LIKE statement wrapper.
Definition: DBConnRef.php:472
rollback( $fname=__METHOD__, $flush=self::FLUSHING_ONE)
Rollback a transaction previously started using begin()
Definition: DBConnRef.php:649
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
Definition: DBConnRef.php:464
pendingWriteCallers()
Get the list of method names that did write queries for this transaction.
Definition: DBConnRef.php:179
numFields( $res)
Get the number of fields in a result object.
Definition: DBConnRef.php:250
getSessionLagStatus()
Get the replica DB lag when the current transaction started or a general lag estimate if not transact...
Definition: DBConnRef.php:675
setTransactionListener( $name, callable $callback=null)
Run a callback after each time any transaction commits or rolls back.
Definition: DBConnRef.php:613
implicitOrderby()
Returns true if this database does an implicit order by when the column has an index For example: SEL...
Definition: DBConnRef.php:151
getServer()
Get the server hostname or IP address.
Definition: DBConnRef.php:460
pendingWriteRowsAffected()
Get the number of affected rows from pending write queries.
Definition: DBConnRef.php:183
__construct(ILoadBalancer $lb, $conn, $role)
Definition: DBConnRef.php:50
anyChar()
Returns a token for buildLike() that denotes a &#39;_&#39; to be used in a LIKE query.
Definition: DBConnRef.php:476
Helper class used for automatically marking an IDatabase connection as reusable (once it no longer ma...
Definition: DBConnRef.php:29
fieldName( $res, $n)
Get a field name in a result object.
Definition: DBConnRef.php:254
selectDomain( $domain)
Set the current domain (database, schema, and table prefix)
Definition: DBConnRef.php:445
setBigSelects( $value=true)
Allow or deny "big selects" for this session only.
Definition: DBConnRef.php:739
insertSelect( $destTable, $srcTable, $varMap, $conds, $fname=__METHOD__, $insertOptions=[], $selectOptions=[], $selectJoinConds=[])
INSERT SELECT wrapper.
Definition: DBConnRef.php:518
selectSQLText( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Take the same arguments as IDatabase::select() and return the SQL it would use.
Definition: DBConnRef.php:321
array $params
N-tuple of (server index, group, DatabaseDomain|string)
Definition: DBConnRef.php:35
fetchObject( $res)
Fetch the next row from the given result object, in object form.
Definition: DBConnRef.php:238
aggregateValue( $valuedata, $valuename='value')
Return aggregated value alias.
Definition: DBConnRef.php:391
maxListLen()
Return the maximum number of items allowed in a list, or 0 for unlimited.
Definition: DBConnRef.php:679
const DB_MASTER
Definition: defines.php:26
getServerInfo()
Get a human-readable string describing the current software version.
Definition: DBConnRef.php:79
makeWhereFrom2d( $data, $baseKey, $subKey)
Build a partial where clause from a 2-d array such as used for LinkBatch.
Definition: DBConnRef.php:387
selectRowCount( $tables, $vars=' *', $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Get the number of rows in dataset.
Definition: DBConnRef.php:345
nextSequenceValue( $seqName)
Deprecated method, calls should be removed.
Definition: DBConnRef.php:484
startAtomic( $fname=__METHOD__, $cancelable=IDatabase::ATOMIC_NOT_CANCELABLE)
Begin an atomic section of SQL statements.
Definition: DBConnRef.php:617
deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname=__METHOD__)
DELETE where the condition is a join.
Definition: DBConnRef.php:504
__call( $name, array $arguments)
Definition: DBConnRef.php:62
buildGroupConcatField( $delim, $table, $field, $conds='', $join_conds=[])
Build a GROUP_CONCAT or equivalent statement for a query.
Definition: DBConnRef.php:411
freeResult( $res)
Free a result object returned by query() or select()
Definition: DBConnRef.php:298
update( $table, $values, $conds, $fname=__METHOD__, $options=[])
UPDATE wrapper.
Definition: DBConnRef.php:377
commit( $fname=__METHOD__, $flush=self::FLUSHING_ONE)
Commits a transaction previously started using begin()
Definition: DBConnRef.php:645
writesOrCallbacksPending()
Whether there is a transaction open with either possible write queries or unresolved pre-commit/commi...
Definition: DBConnRef.php:171
conditional( $cond, $trueVal, $falseVal)
Returns an SQL expression for a simple conditional.
Definition: DBConnRef.php:542
tablePrefix( $prefix=null)
Get/set the table prefix.
Definition: DBConnRef.php:116
getLBInfo( $name=null)
Get properties passed down from the server info array of the load balancer.
Definition: DBConnRef.php:142
cancelAtomic( $fname=__METHOD__, AtomicSectionIdentifier $sectionId=null)
Cancel an atomic section of SQL statements.
Definition: DBConnRef.php:629
An object representing a master or replica DB position in a replicated setup.
Definition: DBMasterPos.php:12
Database null $conn
Live connection handle.
Definition: DBConnRef.php:33
begin( $fname=__METHOD__, $mode=IDatabase::TRANSACTION_EXPLICIT)
Begin a transaction.
Definition: DBConnRef.php:641
unionSupportsOrderAndLimit()
Determine if the RDBMS supports ORDER BY and LIMIT for separate subqueries within UNION...
Definition: DBConnRef.php:527
const LIST_COMMA
Definition: Defines.php:38
restoreFlags( $state=self::RESTORE_PRIOR)
Restore the flags to their prior state before the last setFlag/clearFlag call.
Definition: DBConnRef.php:199
encodeBlob( $b)
Some DBMSs have a special format for inserting into blob fields, they don&#39;t allow simple quoted strin...
Definition: DBConnRef.php:683
unlock( $lockName, $method)
Release a lock.
Definition: DBConnRef.php:711
wasReadOnlyError()
Determines if the last failure was due to the database being read-only.
Definition: DBConnRef.php:566
setSessionOptions(array $options)
Override database&#39;s default behavior.
Definition: DBConnRef.php:691
setSchemaVars( $vars)
Set schema variables to be used when streaming commands from SQL files or stdin.
Definition: DBConnRef.php:695
fetchRow( $res)
Fetch the next row from the given result object, in associative array form.
Definition: DBConnRef.php:242
databasesAreIndependent()
Returns true if DBs are assumed to be on potentially different servers.
Definition: DBConnRef.php:436
tableExists( $table, $fname=__METHOD__)
Query whether a given table exists.
Definition: DBConnRef.php:367
__destruct()
Clean up the connection when out of scope.
Definition: DBConnRef.php:791
bitOr( $fieldLeft, $fieldRight)
Definition: DBConnRef.php:403
lockIsFree( $lockName, $method)
Check to see if a named lock is not locked by any thread (non-blocking)
Definition: DBConnRef.php:699
limitResult( $sql, $limit, $offset=false)
Construct a LIMIT query with optional offset.
Definition: DBConnRef.php:328
getServerVersion()
A string describing the current software version, like from mysql_get_server_info() ...
Definition: DBConnRef.php:282
numRows( $res)
Get the number of rows in a query result.
Definition: DBConnRef.php:246
trxLevel()
Gets the current transaction level.
Definition: DBConnRef.php:100
unionConditionPermutations( $table, $vars, array $permute_conds, $extra_conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Construct a UNION query for permutations of conditions.
Definition: DBConnRef.php:535
lastErrno()
Get the last error number.
Definition: DBConnRef.php:266
indexExists( $table, $index, $fname=__METHOD__)
Determines whether an index exists.
Definition: DBConnRef.php:363
dataSeek( $res, $row)
Change the position of the cursor in a result object.
Definition: DBConnRef.php:262
setFlag( $flag, $remember=self::REMEMBER_NOTHING)
Set a flag for this connection.
Definition: DBConnRef.php:191
doAtomicSection( $fname, callable $callback, $cancelable=self::ATOMIC_NOT_CANCELABLE)
Perform an atomic section of reversable SQL statements from a callback.
Definition: DBConnRef.php:634
query( $sql, $fname=__METHOD__, $flags=0)
Run an SQL query and return the result.
Definition: DBConnRef.php:290
getDBname()
Get the current DB name.
Definition: DBConnRef.php:450
lockForUpdate( $table, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Lock all rows meeting the given conditions/options FOR UPDATE.
Definition: DBConnRef.php:351
onTransactionCommitOrIdle(callable $callback, $fname=__METHOD__)
Run a callback as soon as there is no transaction pending.
Definition: DBConnRef.php:595
decodeBlob( $b)
Some DBMSs return a special placeholder object representing blob fields in result objects...
Definition: DBConnRef.php:687
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
Definition: DBConnRef.php:314
getMasterPos()
Get the position of this master.
Definition: DBConnRef.php:582
decodeExpiry( $expiry, $format=TS_MW)
Decode an expiry time into a DBMS independent format.
Definition: DBConnRef.php:735
wasDeadlock()
Determines if the last failure was due to a deadlock.
Definition: DBConnRef.php:554
buildConcat( $stringList)
Build a concatenation list to feed into a SQL query.
Definition: DBConnRef.php:407
upsert( $table, array $rows, $uniqueIndexes, array $set, $fname=__METHOD__)
INSERT ON DUPLICATE KEY UPDATE wrapper, upserts an array into a table.
Definition: DBConnRef.php:496
trxTimestamp()
Get the UNIX timestamp of the time that the transaction was established.
Definition: DBConnRef.php:104
getLag()
Get the amount of replication lag for this database server.
Definition: DBConnRef.php:671
getServerUptime()
Determines how long the server has been up.
Definition: DBConnRef.php:550
getTopologyRole()
Get the replication topology role of this server.
Definition: DBConnRef.php:83
Database cluster connection, tracking, load balancing, and transaction manager interface.
wasConnectionLoss()
Determines if the last query error was due to a dropped connection.
Definition: DBConnRef.php:562
pendingWriteQueryDuration( $type=self::ESTIMATE_TOTAL)
Get the time spend running write queries for this transaction.
Definition: DBConnRef.php:175
strreplace( $orig, $old, $new)
Returns a SQL expression for simple string replacement (e.g.
Definition: DBConnRef.php:546
selectDB( $db)
Change the current database.
Definition: DBConnRef.php:440
Class to handle database/schema/prefix specifications for IDatabase.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
unionQueries( $sqls, $all)
Construct a UNION query.
Definition: DBConnRef.php:531
selectFieldValues( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a list of single field values from result rows.
Definition: DBConnRef.php:308
getType()
Get the type of the DBMS (e.g.
Definition: DBConnRef.php:221
assertRoleAllowsWrites()
Error out if the role is not DB_MASTER.
Definition: DBConnRef.php:781
onTransactionPreCommitOrIdle(callable $callback, $fname=__METHOD__)
Run a callback before the current transaction commits or now if there is none.
Definition: DBConnRef.php:604
masterPosWait(DBMasterPos $pos, $timeout)
Wait for the replica DB to catch up to a given master position.
Definition: DBConnRef.php:574
clearFlag( $flag, $remember=self::REMEMBER_NOTHING)
Clear a flag for this connection.
Definition: DBConnRef.php:195
onTransactionIdle(callable $callback, $fname=__METHOD__)
Alias for onTransactionCommitOrIdle() for backwards-compatibility.
Definition: DBConnRef.php:600
onAtomicSectionCancel(callable $callback, $fname=__METHOD__)
Run a callback when the atomic section is cancelled.
Definition: DBConnRef.php:609
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Wrapper to IDatabase::select() that only fetches one row (via LIMIT)
Definition: DBConnRef.php:332
lastError()
Get a description of the last error.
Definition: DBConnRef.php:270
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
Definition: DBConnRef.php:383
fieldExists( $table, $field, $fname=__METHOD__)
Determines whether a field exists in a table.
Definition: DBConnRef.php:359
encodeExpiry( $expiry)
Encode an expiry time into the DBMS dependent format.
Definition: DBConnRef.php:731
replace( $table, $uniqueIndexes, $rows, $fname=__METHOD__)
REPLACE query wrapper.
Definition: DBConnRef.php:490
lastQuery()
Get the last query that sent on account of IDatabase::query()
Definition: DBConnRef.php:155
onTransactionResolution(callable $callback, $fname=__METHOD__)
Run a callback as soon as the current transaction commits or rolls back.
Definition: DBConnRef.php:590
Exception class for attempted DB write access to a DBConnRef with the DB_REPLICA role.
assertNoOpenTransactions()
Assert that all explicit transactions or atomic sections have been closed.
Definition: DBConnRef.php:112
__toString()
Get a debugging string that mentions the database type, the ID of this instance, and the ID of any un...
Definition: DBConnRef.php:755
getTopologyRootMaster()
Get the host (or address) of the root master server for the replication topology. ...
Definition: DBConnRef.php:87
timestampOrNull( $ts=null)
Convert a timestamp in one of the formats accepted by ConvertibleTimestamp to the format used for ins...
Definition: DBConnRef.php:661
setIndexAliases(array $aliases)
Convert certain index names to alternative names before querying the DB.
Definition: DBConnRef.php:751
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by ConvertibleTimestamp to the format used for ins...
Definition: DBConnRef.php:657
buildSubstring( $input, $startPosition, $length=null)
Definition: DBConnRef.php:417
Class used for token representing identifiers for atomic sections from IDatabase instances.
getDomainID()
Return the currently selected domain ID.
Definition: DBConnRef.php:211
wasLockTimeout()
Determines if the last failure was due to a lock timeout.
Definition: DBConnRef.php:558
dbSchema( $schema=null)
Get/set the db schema.
Definition: DBConnRef.php:129
getSoftwareLink()
Returns a wikitext style link to the DB&#39;s website (e.g.
Definition: DBConnRef.php:278
setTableAliases(array $aliases)
Make certain table names use their own database, schema, and table prefix when passed into SQL querie...
Definition: DBConnRef.php:747
flushSnapshot( $fname=__METHOD__, $flush=self::FLUSHING_ONE)
Commit any transaction but error out if writes or callbacks are pending.
Definition: DBConnRef.php:653
estimateRowCount( $tables, $vars=' *', $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Estimate the number of rows in dataset.
Definition: DBConnRef.php:339
namedLocksEnqueue()
Check to see if a named lock used by lock() use blocking queues.
Definition: DBConnRef.php:723
wasErrorReissuable()
Determines if the last query error was due to something outside of the query itself.
Definition: DBConnRef.php:570
insert( $table, $rows, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
Definition: DBConnRef.php:371
getInfinity()
Find out when &#39;infinity&#39; is.
Definition: DBConnRef.php:727
lock( $lockName, $method, $timeout=5)
Acquire a named lock.
Definition: DBConnRef.php:705
setLBInfo( $nameOrArray, $value=null)
Set the entire array or a particular key of the managing load balancer info array.
Definition: DBConnRef.php:146