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 ( 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 call_user_func_array( [ $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 writesOrCallbacksPending() {
117 return $this->__call( __FUNCTION__
, func_get_args() );
120 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
121 return $this->__call( __FUNCTION__
, func_get_args() );
124 public function pendingWriteCallers() {
125 return $this->__call( __FUNCTION__
, func_get_args() );
128 public function pendingWriteRowsAffected() {
129 return $this->__call( __FUNCTION__
, func_get_args() );
132 public function isOpen() {
133 return $this->__call( __FUNCTION__
, func_get_args() );
136 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
137 return $this->__call( __FUNCTION__
, func_get_args() );
140 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
141 return $this->__call( __FUNCTION__
, func_get_args() );
144 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
145 return $this->__call( __FUNCTION__
, func_get_args() );
148 public function getFlag( $flag ) {
149 return $this->__call( __FUNCTION__
, func_get_args() );
152 public function getProperty( $name ) {
153 return $this->__call( __FUNCTION__
, func_get_args() );
156 public function getDomainID() {
157 if ( $this->conn
=== null ) {
158 $domain = $this->params
[self
::FLD_DOMAIN
];
159 // Avoid triggering a database connection
160 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
163 return $this->__call( __FUNCTION__
, func_get_args() );
166 public function getWikiID() {
167 return $this->getDomainID();
170 public function getType() {
171 return $this->__call( __FUNCTION__
, func_get_args() );
174 public function open( $server, $user, $password, $dbName ) {
175 return $this->__call( __FUNCTION__
, func_get_args() );
178 public function fetchObject( $res ) {
179 return $this->__call( __FUNCTION__
, func_get_args() );
182 public function fetchRow( $res ) {
183 return $this->__call( __FUNCTION__
, func_get_args() );
186 public function numRows( $res ) {
187 return $this->__call( __FUNCTION__
, func_get_args() );
190 public function numFields( $res ) {
191 return $this->__call( __FUNCTION__
, func_get_args() );
194 public function fieldName( $res, $n ) {
195 return $this->__call( __FUNCTION__
, func_get_args() );
198 public function insertId() {
199 return $this->__call( __FUNCTION__
, func_get_args() );
202 public function dataSeek( $res, $row ) {
203 return $this->__call( __FUNCTION__
, func_get_args() );
206 public function lastErrno() {
207 return $this->__call( __FUNCTION__
, func_get_args() );
210 public function lastError() {
211 return $this->__call( __FUNCTION__
, func_get_args() );
214 public function fieldInfo( $table, $field ) {
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 reportConnectionError( $error = 'Unknown error' ) {
235 return $this->__call( __FUNCTION__
, func_get_args() );
238 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
239 return $this->__call( __FUNCTION__
, func_get_args() );
242 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
243 return $this->__call( __FUNCTION__
, func_get_args() );
246 public function freeResult( $res ) {
247 return $this->__call( __FUNCTION__
, func_get_args() );
250 public function selectField(
251 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
253 return $this->__call( __FUNCTION__
, func_get_args() );
256 public function selectFieldValues(
257 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
259 return $this->__call( __FUNCTION__
, func_get_args() );
262 public function select(
263 $table, $vars, $conds = '', $fname = __METHOD__
,
264 $options = [], $join_conds = []
266 return $this->__call( __FUNCTION__
, func_get_args() );
269 public function selectSQLText(
270 $table, $vars, $conds = '', $fname = __METHOD__
,
271 $options = [], $join_conds = []
273 return $this->__call( __FUNCTION__
, func_get_args() );
276 public function selectRow(
277 $table, $vars, $conds, $fname = __METHOD__
,
278 $options = [], $join_conds = []
280 return $this->__call( __FUNCTION__
, func_get_args() );
283 public function estimateRowCount(
284 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
286 return $this->__call( __FUNCTION__
, func_get_args() );
289 public function selectRowCount(
290 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
292 return $this->__call( __FUNCTION__
, func_get_args() );
295 public function fieldExists( $table, $field, $fname = __METHOD__
) {
296 return $this->__call( __FUNCTION__
, func_get_args() );
299 public function indexExists( $table, $index, $fname = __METHOD__
) {
300 return $this->__call( __FUNCTION__
, func_get_args() );
303 public function tableExists( $table, $fname = __METHOD__
) {
304 return $this->__call( __FUNCTION__
, func_get_args() );
307 public function indexUnique( $table, $index ) {
308 return $this->__call( __FUNCTION__
, func_get_args() );
311 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
312 return $this->__call( __FUNCTION__
, func_get_args() );
315 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
316 return $this->__call( __FUNCTION__
, func_get_args() );
319 public function makeList( $a, $mode = self
::LIST_COMMA
) {
320 return $this->__call( __FUNCTION__
, func_get_args() );
323 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
324 return $this->__call( __FUNCTION__
, func_get_args() );
327 public function aggregateValue( $valuedata, $valuename = 'value' ) {
328 return $this->__call( __FUNCTION__
, func_get_args() );
331 public function bitNot( $field ) {
332 return $this->__call( __FUNCTION__
, func_get_args() );
335 public function bitAnd( $fieldLeft, $fieldRight ) {
336 return $this->__call( __FUNCTION__
, func_get_args() );
339 public function bitOr( $fieldLeft, $fieldRight ) {
340 return $this->__call( __FUNCTION__
, func_get_args() );
343 public function buildConcat( $stringList ) {
344 return $this->__call( __FUNCTION__
, func_get_args() );
347 public function buildGroupConcatField(
348 $delim, $table, $field, $conds = '', $join_conds = []
350 return $this->__call( __FUNCTION__
, func_get_args() );
353 public function buildSubstring( $input, $startPosition, $length = null ) {
354 return $this->__call( __FUNCTION__
, func_get_args() );
357 public function buildStringCast( $field ) {
358 return $this->__call( __FUNCTION__
, func_get_args() );
361 public function buildIntegerCast( $field ) {
362 return $this->__call( __FUNCTION__
, func_get_args() );
365 public function buildSelectSubquery(
366 $table, $vars, $conds = '', $fname = __METHOD__
,
367 $options = [], $join_conds = []
369 return $this->__call( __FUNCTION__
, func_get_args() );
372 public function databasesAreIndependent() {
373 return $this->__call( __FUNCTION__
, func_get_args() );
376 public function selectDB( $db ) {
377 return $this->__call( __FUNCTION__
, func_get_args() );
380 public function getDBname() {
381 return $this->__call( __FUNCTION__
, func_get_args() );
384 public function getServer() {
385 return $this->__call( __FUNCTION__
, func_get_args() );
388 public function addQuotes( $s ) {
389 return $this->__call( __FUNCTION__
, func_get_args() );
392 public function buildLike() {
393 return $this->__call( __FUNCTION__
, func_get_args() );
396 public function anyChar() {
397 return $this->__call( __FUNCTION__
, func_get_args() );
400 public function anyString() {
401 return $this->__call( __FUNCTION__
, func_get_args() );
404 public function nextSequenceValue( $seqName ) {
405 return $this->__call( __FUNCTION__
, func_get_args() );
408 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
409 return $this->__call( __FUNCTION__
, func_get_args() );
412 public function upsert(
413 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
415 return $this->__call( __FUNCTION__
, func_get_args() );
418 public function deleteJoin(
419 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
421 return $this->__call( __FUNCTION__
, func_get_args() );
424 public function delete( $table, $conds, $fname = __METHOD__
) {
425 return $this->__call( __FUNCTION__
, func_get_args() );
428 public function insertSelect(
429 $destTable, $srcTable, $varMap, $conds,
430 $fname = __METHOD__
, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
432 return $this->__call( __FUNCTION__
, func_get_args() );
435 public function unionSupportsOrderAndLimit() {
436 return $this->__call( __FUNCTION__
, func_get_args() );
439 public function unionQueries( $sqls, $all ) {
440 return $this->__call( __FUNCTION__
, func_get_args() );
443 public function unionConditionPermutations(
444 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__
,
445 $options = [], $join_conds = []
447 return $this->__call( __FUNCTION__
, func_get_args() );
450 public function conditional( $cond, $trueVal, $falseVal ) {
451 return $this->__call( __FUNCTION__
, func_get_args() );
454 public function strreplace( $orig, $old, $new ) {
455 return $this->__call( __FUNCTION__
, func_get_args() );
458 public function getServerUptime() {
459 return $this->__call( __FUNCTION__
, func_get_args() );
462 public function wasDeadlock() {
463 return $this->__call( __FUNCTION__
, func_get_args() );
466 public function wasLockTimeout() {
467 return $this->__call( __FUNCTION__
, func_get_args() );
470 public function wasConnectionLoss() {
471 return $this->__call( __FUNCTION__
, func_get_args() );
474 public function wasReadOnlyError() {
475 return $this->__call( __FUNCTION__
, func_get_args() );
478 public function wasErrorReissuable() {
479 return $this->__call( __FUNCTION__
, func_get_args() );
482 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
483 return $this->__call( __FUNCTION__
, func_get_args() );
486 public function getReplicaPos() {
487 return $this->__call( __FUNCTION__
, func_get_args() );
490 public function getMasterPos() {
491 return $this->__call( __FUNCTION__
, func_get_args() );
494 public function serverIsReadOnly() {
495 return $this->__call( __FUNCTION__
, func_get_args() );
498 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
499 return $this->__call( __FUNCTION__
, func_get_args() );
502 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
503 return $this->__call( __FUNCTION__
, func_get_args() );
506 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
507 return $this->__call( __FUNCTION__
, func_get_args() );
510 public function setTransactionListener( $name, callable
$callback = null ) {
511 return $this->__call( __FUNCTION__
, func_get_args() );
514 public function startAtomic(
515 $fname = __METHOD__
, $cancelable = IDatabase
::ATOMIC_NOT_CANCELABLE
517 return $this->__call( __FUNCTION__
, func_get_args() );
520 public function endAtomic( $fname = __METHOD__
) {
521 return $this->__call( __FUNCTION__
, func_get_args() );
524 public function cancelAtomic( $fname = __METHOD__
) {
525 return $this->__call( __FUNCTION__
, func_get_args() );
528 public function doAtomicSection( $fname, callable
$callback ) {
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 listTables( $prefix = null, $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
);
652 class_alias( DBConnRef
::class, 'DBConnRef' );