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 tablePrefix( $prefix = null ) {
73 return $this->__call( __FUNCTION__
, func_get_args() );
76 public function dbSchema( $schema = null ) {
77 return $this->__call( __FUNCTION__
, func_get_args() );
80 public function getLBInfo( $name = null ) {
81 return $this->__call( __FUNCTION__
, func_get_args() );
84 public function setLBInfo( $name, $value = null ) {
85 return $this->__call( __FUNCTION__
, func_get_args() );
88 public function setLazyMasterHandle( IDatabase
$conn ) {
89 return $this->__call( __FUNCTION__
, func_get_args() );
92 public function implicitGroupby() {
93 return $this->__call( __FUNCTION__
, func_get_args() );
96 public function implicitOrderby() {
97 return $this->__call( __FUNCTION__
, func_get_args() );
100 public function lastQuery() {
101 return $this->__call( __FUNCTION__
, func_get_args() );
104 public function doneWrites() {
105 return $this->__call( __FUNCTION__
, func_get_args() );
108 public function lastDoneWrites() {
109 return $this->__call( __FUNCTION__
, func_get_args() );
112 public function writesPending() {
113 return $this->__call( __FUNCTION__
, func_get_args() );
116 public function preCommitCallbacksPending() {
117 return $this->__call( __FUNCTION__
, func_get_args() );
120 public function writesOrCallbacksPending() {
121 return $this->__call( __FUNCTION__
, func_get_args() );
124 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
125 return $this->__call( __FUNCTION__
, func_get_args() );
128 public function pendingWriteCallers() {
129 return $this->__call( __FUNCTION__
, func_get_args() );
132 public function pendingWriteRowsAffected() {
133 return $this->__call( __FUNCTION__
, func_get_args() );
136 public function isOpen() {
137 return $this->__call( __FUNCTION__
, func_get_args() );
140 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
141 return $this->__call( __FUNCTION__
, func_get_args() );
144 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
145 return $this->__call( __FUNCTION__
, func_get_args() );
148 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
149 return $this->__call( __FUNCTION__
, func_get_args() );
152 public function getFlag( $flag ) {
153 return $this->__call( __FUNCTION__
, func_get_args() );
156 public function getProperty( $name ) {
157 return $this->__call( __FUNCTION__
, func_get_args() );
160 public function getDomainID() {
161 if ( $this->conn
=== null ) {
162 $domain = $this->params
[self
::FLD_DOMAIN
];
163 // Avoid triggering a database connection
164 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
167 return $this->__call( __FUNCTION__
, func_get_args() );
170 public function getWikiID() {
171 return $this->getDomainID();
174 public function getType() {
175 return $this->__call( __FUNCTION__
, func_get_args() );
178 public function open( $server, $user, $password, $dbName ) {
179 return $this->__call( __FUNCTION__
, func_get_args() );
182 public function fetchObject( $res ) {
183 return $this->__call( __FUNCTION__
, func_get_args() );
186 public function fetchRow( $res ) {
187 return $this->__call( __FUNCTION__
, func_get_args() );
190 public function numRows( $res ) {
191 return $this->__call( __FUNCTION__
, func_get_args() );
194 public function numFields( $res ) {
195 return $this->__call( __FUNCTION__
, func_get_args() );
198 public function fieldName( $res, $n ) {
199 return $this->__call( __FUNCTION__
, func_get_args() );
202 public function insertId() {
203 return $this->__call( __FUNCTION__
, func_get_args() );
206 public function dataSeek( $res, $row ) {
207 return $this->__call( __FUNCTION__
, func_get_args() );
210 public function lastErrno() {
211 return $this->__call( __FUNCTION__
, func_get_args() );
214 public function lastError() {
215 return $this->__call( __FUNCTION__
, func_get_args() );
218 public function affectedRows() {
219 return $this->__call( __FUNCTION__
, func_get_args() );
222 public function getSoftwareLink() {
223 return $this->__call( __FUNCTION__
, func_get_args() );
226 public function getServerVersion() {
227 return $this->__call( __FUNCTION__
, func_get_args() );
230 public function close() {
231 return $this->__call( __FUNCTION__
, func_get_args() );
234 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
235 return $this->__call( __FUNCTION__
, func_get_args() );
238 public function freeResult( $res ) {
239 return $this->__call( __FUNCTION__
, func_get_args() );
242 public function selectField(
243 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
245 return $this->__call( __FUNCTION__
, func_get_args() );
248 public function selectFieldValues(
249 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
251 return $this->__call( __FUNCTION__
, func_get_args() );
254 public function select(
255 $table, $vars, $conds = '', $fname = __METHOD__
,
256 $options = [], $join_conds = []
258 return $this->__call( __FUNCTION__
, func_get_args() );
261 public function selectSQLText(
262 $table, $vars, $conds = '', $fname = __METHOD__
,
263 $options = [], $join_conds = []
265 return $this->__call( __FUNCTION__
, func_get_args() );
268 public function selectRow(
269 $table, $vars, $conds, $fname = __METHOD__
,
270 $options = [], $join_conds = []
272 return $this->__call( __FUNCTION__
, func_get_args() );
275 public function estimateRowCount(
276 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
278 return $this->__call( __FUNCTION__
, func_get_args() );
281 public function selectRowCount(
282 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
284 return $this->__call( __FUNCTION__
, func_get_args() );
287 public function lockForUpdate(
288 $table, $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
290 return $this->__call( __FUNCTION__
, func_get_args() );
293 public function fieldExists( $table, $field, $fname = __METHOD__
) {
294 return $this->__call( __FUNCTION__
, func_get_args() );
297 public function indexExists( $table, $index, $fname = __METHOD__
) {
298 return $this->__call( __FUNCTION__
, func_get_args() );
301 public function tableExists( $table, $fname = __METHOD__
) {
302 return $this->__call( __FUNCTION__
, func_get_args() );
305 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
306 return $this->__call( __FUNCTION__
, func_get_args() );
309 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
310 return $this->__call( __FUNCTION__
, func_get_args() );
313 public function makeList( $a, $mode = self
::LIST_COMMA
) {
314 return $this->__call( __FUNCTION__
, func_get_args() );
317 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
318 return $this->__call( __FUNCTION__
, func_get_args() );
321 public function aggregateValue( $valuedata, $valuename = 'value' ) {
322 return $this->__call( __FUNCTION__
, func_get_args() );
325 public function bitNot( $field ) {
326 return $this->__call( __FUNCTION__
, func_get_args() );
329 public function bitAnd( $fieldLeft, $fieldRight ) {
330 return $this->__call( __FUNCTION__
, func_get_args() );
333 public function bitOr( $fieldLeft, $fieldRight ) {
334 return $this->__call( __FUNCTION__
, func_get_args() );
337 public function buildConcat( $stringList ) {
338 return $this->__call( __FUNCTION__
, func_get_args() );
341 public function buildGroupConcatField(
342 $delim, $table, $field, $conds = '', $join_conds = []
344 return $this->__call( __FUNCTION__
, func_get_args() );
347 public function buildSubstring( $input, $startPosition, $length = null ) {
348 return $this->__call( __FUNCTION__
, func_get_args() );
351 public function buildStringCast( $field ) {
352 return $this->__call( __FUNCTION__
, func_get_args() );
355 public function buildIntegerCast( $field ) {
356 return $this->__call( __FUNCTION__
, func_get_args() );
359 public function buildSelectSubquery(
360 $table, $vars, $conds = '', $fname = __METHOD__
,
361 $options = [], $join_conds = []
363 return $this->__call( __FUNCTION__
, func_get_args() );
366 public function databasesAreIndependent() {
367 return $this->__call( __FUNCTION__
, func_get_args() );
370 public function selectDB( $db ) {
371 return $this->__call( __FUNCTION__
, func_get_args() );
374 public function getDBname() {
375 return $this->__call( __FUNCTION__
, func_get_args() );
378 public function getServer() {
379 return $this->__call( __FUNCTION__
, func_get_args() );
382 public function addQuotes( $s ) {
383 return $this->__call( __FUNCTION__
, func_get_args() );
386 public function buildLike() {
387 return $this->__call( __FUNCTION__
, func_get_args() );
390 public function anyChar() {
391 return $this->__call( __FUNCTION__
, func_get_args() );
394 public function anyString() {
395 return $this->__call( __FUNCTION__
, func_get_args() );
398 public function nextSequenceValue( $seqName ) {
399 return $this->__call( __FUNCTION__
, func_get_args() );
402 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
403 return $this->__call( __FUNCTION__
, func_get_args() );
406 public function upsert(
407 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
409 return $this->__call( __FUNCTION__
, func_get_args() );
412 public function deleteJoin(
413 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
415 return $this->__call( __FUNCTION__
, func_get_args() );
418 public function delete( $table, $conds, $fname = __METHOD__
) {
419 return $this->__call( __FUNCTION__
, func_get_args() );
422 public function insertSelect(
423 $destTable, $srcTable, $varMap, $conds,
424 $fname = __METHOD__
, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
426 return $this->__call( __FUNCTION__
, func_get_args() );
429 public function unionSupportsOrderAndLimit() {
430 return $this->__call( __FUNCTION__
, func_get_args() );
433 public function unionQueries( $sqls, $all ) {
434 return $this->__call( __FUNCTION__
, func_get_args() );
437 public function unionConditionPermutations(
438 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__
,
439 $options = [], $join_conds = []
441 return $this->__call( __FUNCTION__
, func_get_args() );
444 public function conditional( $cond, $trueVal, $falseVal ) {
445 return $this->__call( __FUNCTION__
, func_get_args() );
448 public function strreplace( $orig, $old, $new ) {
449 return $this->__call( __FUNCTION__
, func_get_args() );
452 public function getServerUptime() {
453 return $this->__call( __FUNCTION__
, func_get_args() );
456 public function wasDeadlock() {
457 return $this->__call( __FUNCTION__
, func_get_args() );
460 public function wasLockTimeout() {
461 return $this->__call( __FUNCTION__
, func_get_args() );
464 public function wasConnectionLoss() {
465 return $this->__call( __FUNCTION__
, func_get_args() );
468 public function wasReadOnlyError() {
469 return $this->__call( __FUNCTION__
, func_get_args() );
472 public function wasErrorReissuable() {
473 return $this->__call( __FUNCTION__
, func_get_args() );
476 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
477 return $this->__call( __FUNCTION__
, func_get_args() );
480 public function getReplicaPos() {
481 return $this->__call( __FUNCTION__
, func_get_args() );
484 public function getMasterPos() {
485 return $this->__call( __FUNCTION__
, func_get_args() );
488 public function serverIsReadOnly() {
489 return $this->__call( __FUNCTION__
, func_get_args() );
492 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
493 return $this->__call( __FUNCTION__
, func_get_args() );
496 public function onTransactionCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
497 return $this->__call( __FUNCTION__
, func_get_args() );
500 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
501 return $this->__call( __FUNCTION__
, func_get_args() );
504 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
505 return $this->__call( __FUNCTION__
, func_get_args() );
508 public function setTransactionListener( $name, callable
$callback = null ) {
509 return $this->__call( __FUNCTION__
, func_get_args() );
512 public function startAtomic(
513 $fname = __METHOD__
, $cancelable = IDatabase
::ATOMIC_NOT_CANCELABLE
515 return $this->__call( __FUNCTION__
, func_get_args() );
518 public function endAtomic( $fname = __METHOD__
) {
519 return $this->__call( __FUNCTION__
, func_get_args() );
522 public function cancelAtomic( $fname = __METHOD__
, AtomicSectionIdentifier
$sectionId = null ) {
523 return $this->__call( __FUNCTION__
, func_get_args() );
526 public function doAtomicSection(
527 $fname, callable
$callback, $cancelable = self
::ATOMIC_NOT_CANCELABLE
529 return $this->__call( __FUNCTION__
, func_get_args() );
532 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
533 return $this->__call( __FUNCTION__
, func_get_args() );
536 public function commit( $fname = __METHOD__
, $flush = '' ) {
537 return $this->__call( __FUNCTION__
, func_get_args() );
540 public function rollback( $fname = __METHOD__
, $flush = '' ) {
541 return $this->__call( __FUNCTION__
, func_get_args() );
544 public function flushSnapshot( $fname = __METHOD__
) {
545 return $this->__call( __FUNCTION__
, func_get_args() );
548 public function timestamp( $ts = 0 ) {
549 return $this->__call( __FUNCTION__
, func_get_args() );
552 public function timestampOrNull( $ts = null ) {
553 return $this->__call( __FUNCTION__
, func_get_args() );
556 public function ping( &$rtt = null ) {
557 return func_num_args()
558 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
559 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
562 public function getLag() {
563 return $this->__call( __FUNCTION__
, func_get_args() );
566 public function getSessionLagStatus() {
567 return $this->__call( __FUNCTION__
, func_get_args() );
570 public function maxListLen() {
571 return $this->__call( __FUNCTION__
, func_get_args() );
574 public function encodeBlob( $b ) {
575 return $this->__call( __FUNCTION__
, func_get_args() );
578 public function decodeBlob( $b ) {
579 return $this->__call( __FUNCTION__
, func_get_args() );
582 public function setSessionOptions( array $options ) {
583 return $this->__call( __FUNCTION__
, func_get_args() );
586 public function setSchemaVars( $vars ) {
587 return $this->__call( __FUNCTION__
, func_get_args() );
590 public function lockIsFree( $lockName, $method ) {
591 return $this->__call( __FUNCTION__
, func_get_args() );
594 public function lock( $lockName, $method, $timeout = 5 ) {
595 return $this->__call( __FUNCTION__
, func_get_args() );
598 public function unlock( $lockName, $method ) {
599 return $this->__call( __FUNCTION__
, func_get_args() );
602 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
603 return $this->__call( __FUNCTION__
, func_get_args() );
606 public function namedLocksEnqueue() {
607 return $this->__call( __FUNCTION__
, func_get_args() );
610 public function getInfinity() {
611 return $this->__call( __FUNCTION__
, func_get_args() );
614 public function encodeExpiry( $expiry ) {
615 return $this->__call( __FUNCTION__
, func_get_args() );
618 public function decodeExpiry( $expiry, $format = TS_MW
) {
619 return $this->__call( __FUNCTION__
, func_get_args() );
622 public function setBigSelects( $value = true ) {
623 return $this->__call( __FUNCTION__
, func_get_args() );
626 public function isReadOnly() {
627 return $this->__call( __FUNCTION__
, func_get_args() );
630 public function setTableAliases( array $aliases ) {
631 return $this->__call( __FUNCTION__
, func_get_args() );
634 public function setIndexAliases( array $aliases ) {
635 return $this->__call( __FUNCTION__
, func_get_args() );
639 * Clean up the connection when out of scope
641 function __destruct() {
643 $this->lb
->reuseConnection( $this->conn
);
650 * @deprecated since 1.29
652 class_alias( DBConnRef
::class, 'DBConnRef' );