3 * Helper class to handle automatically marking connections as reusable (via RAII pattern)
4 * as well handling deferring the actual network connection until the handle is used
6 * @note: proxy methods are defined explicity to avoid interface errors
10 class DBConnRef
implements IDatabase
{
11 /** @var ILoadBalancer */
14 /** @var IDatabase|null Live connection handle */
17 /** @var array|null */
25 * @param ILoadBalancer $lb
26 * @param IDatabase|array $conn Connection or (server index, group, wiki ID)
28 public function __construct( ILoadBalancer
$lb, $conn ) {
30 if ( $conn instanceof IDatabase
) {
31 $this->conn
= $conn; // live handle
32 } elseif ( count( $conn ) >= 3 && $conn[self
::FLD_WIKI
] !== false ) {
33 $this->params
= $conn;
35 throw new InvalidArgumentException( "Missing lazy connection arguments." );
39 function __call( $name, array $arguments ) {
40 if ( $this->conn
=== null ) {
41 list( $db, $groups, $wiki ) = $this->params
;
42 $this->conn
= $this->lb
->getConnection( $db, $groups, $wiki );
45 return call_user_func_array( [ $this->conn
, $name ], $arguments );
48 public function getServerInfo() {
49 return $this->__call( __FUNCTION__
, func_get_args() );
52 public function bufferResults( $buffer = null ) {
53 return $this->__call( __FUNCTION__
, func_get_args() );
56 public function trxLevel() {
57 return $this->__call( __FUNCTION__
, func_get_args() );
60 public function trxTimestamp() {
61 return $this->__call( __FUNCTION__
, func_get_args() );
64 public function explicitTrxActive() {
65 return $this->__call( __FUNCTION__
, func_get_args() );
68 public function tablePrefix( $prefix = null ) {
69 return $this->__call( __FUNCTION__
, func_get_args() );
72 public function dbSchema( $schema = null ) {
73 return $this->__call( __FUNCTION__
, func_get_args() );
76 public function getLBInfo( $name = null ) {
77 return $this->__call( __FUNCTION__
, func_get_args() );
80 public function setLBInfo( $name, $value = null ) {
81 return $this->__call( __FUNCTION__
, func_get_args() );
84 public function setLazyMasterHandle( IDatabase
$conn ) {
85 return $this->__call( __FUNCTION__
, func_get_args() );
88 public function implicitGroupby() {
89 return $this->__call( __FUNCTION__
, func_get_args() );
92 public function implicitOrderby() {
93 return $this->__call( __FUNCTION__
, func_get_args() );
96 public function lastQuery() {
97 return $this->__call( __FUNCTION__
, func_get_args() );
100 public function doneWrites() {
101 return $this->__call( __FUNCTION__
, func_get_args() );
104 public function lastDoneWrites() {
105 return $this->__call( __FUNCTION__
, func_get_args() );
108 public function writesPending() {
109 return $this->__call( __FUNCTION__
, func_get_args() );
112 public function writesOrCallbacksPending() {
113 return $this->__call( __FUNCTION__
, func_get_args() );
116 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
117 return $this->__call( __FUNCTION__
, func_get_args() );
120 public function pendingWriteCallers() {
121 return $this->__call( __FUNCTION__
, func_get_args() );
124 public function isOpen() {
125 return $this->__call( __FUNCTION__
, func_get_args() );
128 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
129 return $this->__call( __FUNCTION__
, func_get_args() );
132 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
133 return $this->__call( __FUNCTION__
, func_get_args() );
136 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
137 return $this->__call( __FUNCTION__
, func_get_args() );
140 public function getFlag( $flag ) {
141 return $this->__call( __FUNCTION__
, func_get_args() );
144 public function getProperty( $name ) {
145 return $this->__call( __FUNCTION__
, func_get_args() );
148 public function getWikiID() {
149 if ( $this->conn
=== null ) {
150 // Avoid triggering a connection
151 return $this->params
[self
::FLD_WIKI
];
154 return $this->__call( __FUNCTION__
, func_get_args() );
157 public function getType() {
158 return $this->__call( __FUNCTION__
, func_get_args() );
161 public function open( $server, $user, $password, $dbName ) {
162 return $this->__call( __FUNCTION__
, func_get_args() );
165 public function fetchObject( $res ) {
166 return $this->__call( __FUNCTION__
, func_get_args() );
169 public function fetchRow( $res ) {
170 return $this->__call( __FUNCTION__
, func_get_args() );
173 public function numRows( $res ) {
174 return $this->__call( __FUNCTION__
, func_get_args() );
177 public function numFields( $res ) {
178 return $this->__call( __FUNCTION__
, func_get_args() );
181 public function fieldName( $res, $n ) {
182 return $this->__call( __FUNCTION__
, func_get_args() );
185 public function insertId() {
186 return $this->__call( __FUNCTION__
, func_get_args() );
189 public function dataSeek( $res, $row ) {
190 return $this->__call( __FUNCTION__
, func_get_args() );
193 public function lastErrno() {
194 return $this->__call( __FUNCTION__
, func_get_args() );
197 public function lastError() {
198 return $this->__call( __FUNCTION__
, func_get_args() );
201 public function fieldInfo( $table, $field ) {
202 return $this->__call( __FUNCTION__
, func_get_args() );
205 public function affectedRows() {
206 return $this->__call( __FUNCTION__
, func_get_args() );
209 public function getSoftwareLink() {
210 return $this->__call( __FUNCTION__
, func_get_args() );
213 public function getServerVersion() {
214 return $this->__call( __FUNCTION__
, func_get_args() );
217 public function close() {
218 return $this->__call( __FUNCTION__
, func_get_args() );
221 public function reportConnectionError( $error = 'Unknown error' ) {
222 return $this->__call( __FUNCTION__
, func_get_args() );
225 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
226 return $this->__call( __FUNCTION__
, func_get_args() );
229 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
230 return $this->__call( __FUNCTION__
, func_get_args() );
233 public function freeResult( $res ) {
234 return $this->__call( __FUNCTION__
, func_get_args() );
237 public function selectField(
238 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
240 return $this->__call( __FUNCTION__
, func_get_args() );
243 public function selectFieldValues(
244 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
246 return $this->__call( __FUNCTION__
, func_get_args() );
249 public function select(
250 $table, $vars, $conds = '', $fname = __METHOD__
,
251 $options = [], $join_conds = []
253 return $this->__call( __FUNCTION__
, func_get_args() );
256 public function selectSQLText(
257 $table, $vars, $conds = '', $fname = __METHOD__
,
258 $options = [], $join_conds = []
260 return $this->__call( __FUNCTION__
, func_get_args() );
263 public function selectRow(
264 $table, $vars, $conds, $fname = __METHOD__
,
265 $options = [], $join_conds = []
267 return $this->__call( __FUNCTION__
, func_get_args() );
270 public function estimateRowCount(
271 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
273 return $this->__call( __FUNCTION__
, func_get_args() );
276 public function selectRowCount(
277 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
279 return $this->__call( __FUNCTION__
, func_get_args() );
282 public function fieldExists( $table, $field, $fname = __METHOD__
) {
283 return $this->__call( __FUNCTION__
, func_get_args() );
286 public function indexExists( $table, $index, $fname = __METHOD__
) {
287 return $this->__call( __FUNCTION__
, func_get_args() );
290 public function tableExists( $table, $fname = __METHOD__
) {
291 return $this->__call( __FUNCTION__
, func_get_args() );
294 public function indexUnique( $table, $index ) {
295 return $this->__call( __FUNCTION__
, func_get_args() );
298 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
299 return $this->__call( __FUNCTION__
, func_get_args() );
302 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
303 return $this->__call( __FUNCTION__
, func_get_args() );
306 public function makeList( $a, $mode = LIST_COMMA
) {
307 return $this->__call( __FUNCTION__
, func_get_args() );
310 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
311 return $this->__call( __FUNCTION__
, func_get_args() );
314 public function bitNot( $field ) {
315 return $this->__call( __FUNCTION__
, func_get_args() );
318 public function bitAnd( $fieldLeft, $fieldRight ) {
319 return $this->__call( __FUNCTION__
, func_get_args() );
322 public function bitOr( $fieldLeft, $fieldRight ) {
323 return $this->__call( __FUNCTION__
, func_get_args() );
326 public function buildConcat( $stringList ) {
327 return $this->__call( __FUNCTION__
, func_get_args() );
330 public function buildGroupConcatField(
331 $delim, $table, $field, $conds = '', $join_conds = []
333 return $this->__call( __FUNCTION__
, func_get_args() );
336 public function selectDB( $db ) {
337 return $this->__call( __FUNCTION__
, func_get_args() );
340 public function getDBname() {
341 return $this->__call( __FUNCTION__
, func_get_args() );
344 public function getServer() {
345 return $this->__call( __FUNCTION__
, func_get_args() );
348 public function addQuotes( $s ) {
349 return $this->__call( __FUNCTION__
, func_get_args() );
352 public function buildLike() {
353 return $this->__call( __FUNCTION__
, func_get_args() );
356 public function anyChar() {
357 return $this->__call( __FUNCTION__
, func_get_args() );
360 public function anyString() {
361 return $this->__call( __FUNCTION__
, func_get_args() );
364 public function nextSequenceValue( $seqName ) {
365 return $this->__call( __FUNCTION__
, func_get_args() );
368 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
369 return $this->__call( __FUNCTION__
, func_get_args() );
372 public function upsert(
373 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
375 return $this->__call( __FUNCTION__
, func_get_args() );
378 public function deleteJoin(
379 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
381 return $this->__call( __FUNCTION__
, func_get_args() );
384 public function delete( $table, $conds, $fname = __METHOD__
) {
385 return $this->__call( __FUNCTION__
, func_get_args() );
388 public function insertSelect(
389 $destTable, $srcTable, $varMap, $conds,
390 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
392 return $this->__call( __FUNCTION__
, func_get_args() );
395 public function unionSupportsOrderAndLimit() {
396 return $this->__call( __FUNCTION__
, func_get_args() );
399 public function unionQueries( $sqls, $all ) {
400 return $this->__call( __FUNCTION__
, func_get_args() );
403 public function conditional( $cond, $trueVal, $falseVal ) {
404 return $this->__call( __FUNCTION__
, func_get_args() );
407 public function strreplace( $orig, $old, $new ) {
408 return $this->__call( __FUNCTION__
, func_get_args() );
411 public function getServerUptime() {
412 return $this->__call( __FUNCTION__
, func_get_args() );
415 public function wasDeadlock() {
416 return $this->__call( __FUNCTION__
, func_get_args() );
419 public function wasLockTimeout() {
420 return $this->__call( __FUNCTION__
, func_get_args() );
423 public function wasErrorReissuable() {
424 return $this->__call( __FUNCTION__
, func_get_args() );
427 public function wasReadOnlyError() {
428 return $this->__call( __FUNCTION__
, func_get_args() );
431 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
432 return $this->__call( __FUNCTION__
, func_get_args() );
435 public function getSlavePos() {
436 return $this->__call( __FUNCTION__
, func_get_args() );
439 public function getMasterPos() {
440 return $this->__call( __FUNCTION__
, func_get_args() );
443 public function serverIsReadOnly() {
444 return $this->__call( __FUNCTION__
, func_get_args() );
447 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
448 return $this->__call( __FUNCTION__
, func_get_args() );
451 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
452 return $this->__call( __FUNCTION__
, func_get_args() );
455 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
456 return $this->__call( __FUNCTION__
, func_get_args() );
459 public function setTransactionListener( $name, callable
$callback = null ) {
460 return $this->__call( __FUNCTION__
, func_get_args() );
463 public function startAtomic( $fname = __METHOD__
) {
464 return $this->__call( __FUNCTION__
, func_get_args() );
467 public function endAtomic( $fname = __METHOD__
) {
468 return $this->__call( __FUNCTION__
, func_get_args() );
471 public function doAtomicSection( $fname, callable
$callback ) {
472 return $this->__call( __FUNCTION__
, func_get_args() );
475 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
476 return $this->__call( __FUNCTION__
, func_get_args() );
479 public function commit( $fname = __METHOD__
, $flush = '' ) {
480 return $this->__call( __FUNCTION__
, func_get_args() );
483 public function rollback( $fname = __METHOD__
, $flush = '' ) {
484 return $this->__call( __FUNCTION__
, func_get_args() );
487 public function flushSnapshot( $fname = __METHOD__
) {
488 return $this->__call( __FUNCTION__
, func_get_args() );
491 public function listTables( $prefix = null, $fname = __METHOD__
) {
492 return $this->__call( __FUNCTION__
, func_get_args() );
495 public function timestamp( $ts = 0 ) {
496 return $this->__call( __FUNCTION__
, func_get_args() );
499 public function timestampOrNull( $ts = null ) {
500 return $this->__call( __FUNCTION__
, func_get_args() );
503 public function ping( &$rtt = null ) {
504 return func_num_args()
505 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
506 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
509 public function getLag() {
510 return $this->__call( __FUNCTION__
, func_get_args() );
513 public function getSessionLagStatus() {
514 return $this->__call( __FUNCTION__
, func_get_args() );
517 public function maxListLen() {
518 return $this->__call( __FUNCTION__
, func_get_args() );
521 public function encodeBlob( $b ) {
522 return $this->__call( __FUNCTION__
, func_get_args() );
525 public function decodeBlob( $b ) {
526 return $this->__call( __FUNCTION__
, func_get_args() );
529 public function setSessionOptions( array $options ) {
530 return $this->__call( __FUNCTION__
, func_get_args() );
533 public function setSchemaVars( $vars ) {
534 return $this->__call( __FUNCTION__
, func_get_args() );
537 public function lockIsFree( $lockName, $method ) {
538 return $this->__call( __FUNCTION__
, func_get_args() );
541 public function lock( $lockName, $method, $timeout = 5 ) {
542 return $this->__call( __FUNCTION__
, func_get_args() );
545 public function unlock( $lockName, $method ) {
546 return $this->__call( __FUNCTION__
, func_get_args() );
549 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
550 return $this->__call( __FUNCTION__
, func_get_args() );
553 public function namedLocksEnqueue() {
554 return $this->__call( __FUNCTION__
, func_get_args() );
557 public function getInfinity() {
558 return $this->__call( __FUNCTION__
, func_get_args() );
561 public function encodeExpiry( $expiry ) {
562 return $this->__call( __FUNCTION__
, func_get_args() );
565 public function decodeExpiry( $expiry, $format = TS_MW
) {
566 return $this->__call( __FUNCTION__
, func_get_args() );
569 public function setBigSelects( $value = true ) {
570 return $this->__call( __FUNCTION__
, func_get_args() );
573 public function isReadOnly() {
574 return $this->__call( __FUNCTION__
, func_get_args() );
577 public function setTableAliases( array $aliases ) {
578 return $this->__call( __FUNCTION__
, func_get_args() );
582 * Clean up the connection when out of scope
584 function __destruct() {
585 if ( $this->conn
!== null ) {
586 $this->lb
->reuseConnection( $this->conn
);