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 */
13 /** @var Database|null Live connection handle */
15 /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */
23 * @param ILoadBalancer $lb Connection manager for $conn
24 * @param Database|array $conn New connection handle or (server index, query groups, domain)
26 public function __construct( ILoadBalancer
$lb, $conn ) {
28 if ( $conn instanceof Database
) {
29 $this->conn
= $conn; // live handle
30 } elseif ( count( $conn ) >= 3 && $conn[self
::FLD_DOMAIN
] !== false ) {
31 $this->params
= $conn;
33 throw new InvalidArgumentException( "Missing lazy connection arguments." );
37 function __call( $name, array $arguments ) {
38 if ( $this->conn
=== null ) {
39 list( $db, $groups, $wiki ) = $this->params
;
40 $this->conn
= $this->lb
->getConnection( $db, $groups, $wiki );
43 return call_user_func_array( [ $this->conn
, $name ], $arguments );
46 public function getServerInfo() {
47 return $this->__call( __FUNCTION__
, func_get_args() );
50 public function bufferResults( $buffer = null ) {
51 return $this->__call( __FUNCTION__
, func_get_args() );
54 public function trxLevel() {
55 return $this->__call( __FUNCTION__
, func_get_args() );
58 public function trxTimestamp() {
59 return $this->__call( __FUNCTION__
, func_get_args() );
62 public function explicitTrxActive() {
63 return $this->__call( __FUNCTION__
, func_get_args() );
66 public function tablePrefix( $prefix = null ) {
67 return $this->__call( __FUNCTION__
, func_get_args() );
70 public function dbSchema( $schema = null ) {
71 return $this->__call( __FUNCTION__
, func_get_args() );
74 public function getLBInfo( $name = null ) {
75 return $this->__call( __FUNCTION__
, func_get_args() );
78 public function setLBInfo( $name, $value = null ) {
79 return $this->__call( __FUNCTION__
, func_get_args() );
82 public function setLazyMasterHandle( IDatabase
$conn ) {
83 return $this->__call( __FUNCTION__
, func_get_args() );
86 public function implicitGroupby() {
87 return $this->__call( __FUNCTION__
, func_get_args() );
90 public function implicitOrderby() {
91 return $this->__call( __FUNCTION__
, func_get_args() );
94 public function lastQuery() {
95 return $this->__call( __FUNCTION__
, func_get_args() );
98 public function doneWrites() {
99 return $this->__call( __FUNCTION__
, func_get_args() );
102 public function lastDoneWrites() {
103 return $this->__call( __FUNCTION__
, func_get_args() );
106 public function writesPending() {
107 return $this->__call( __FUNCTION__
, func_get_args() );
110 public function writesOrCallbacksPending() {
111 return $this->__call( __FUNCTION__
, func_get_args() );
114 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
115 return $this->__call( __FUNCTION__
, func_get_args() );
118 public function pendingWriteCallers() {
119 return $this->__call( __FUNCTION__
, func_get_args() );
122 public function isOpen() {
123 return $this->__call( __FUNCTION__
, func_get_args() );
126 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
127 return $this->__call( __FUNCTION__
, func_get_args() );
130 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
131 return $this->__call( __FUNCTION__
, func_get_args() );
134 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
135 return $this->__call( __FUNCTION__
, func_get_args() );
138 public function getFlag( $flag ) {
139 return $this->__call( __FUNCTION__
, func_get_args() );
142 public function getProperty( $name ) {
143 return $this->__call( __FUNCTION__
, func_get_args() );
146 public function getDomainID() {
147 if ( $this->conn
=== null ) {
148 $domain = $this->params
[self
::FLD_DOMAIN
];
149 // Avoid triggering a database connection
150 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
153 return $this->__call( __FUNCTION__
, func_get_args() );
156 public function getWikiID() {
157 return $this->getDomainID();
160 public function getType() {
161 return $this->__call( __FUNCTION__
, func_get_args() );
164 public function open( $server, $user, $password, $dbName ) {
165 return $this->__call( __FUNCTION__
, func_get_args() );
168 public function fetchObject( $res ) {
169 return $this->__call( __FUNCTION__
, func_get_args() );
172 public function fetchRow( $res ) {
173 return $this->__call( __FUNCTION__
, func_get_args() );
176 public function numRows( $res ) {
177 return $this->__call( __FUNCTION__
, func_get_args() );
180 public function numFields( $res ) {
181 return $this->__call( __FUNCTION__
, func_get_args() );
184 public function fieldName( $res, $n ) {
185 return $this->__call( __FUNCTION__
, func_get_args() );
188 public function insertId() {
189 return $this->__call( __FUNCTION__
, func_get_args() );
192 public function dataSeek( $res, $row ) {
193 return $this->__call( __FUNCTION__
, func_get_args() );
196 public function lastErrno() {
197 return $this->__call( __FUNCTION__
, func_get_args() );
200 public function lastError() {
201 return $this->__call( __FUNCTION__
, func_get_args() );
204 public function fieldInfo( $table, $field ) {
205 return $this->__call( __FUNCTION__
, func_get_args() );
208 public function affectedRows() {
209 return $this->__call( __FUNCTION__
, func_get_args() );
212 public function getSoftwareLink() {
213 return $this->__call( __FUNCTION__
, func_get_args() );
216 public function getServerVersion() {
217 return $this->__call( __FUNCTION__
, func_get_args() );
220 public function close() {
221 return $this->__call( __FUNCTION__
, func_get_args() );
224 public function reportConnectionError( $error = 'Unknown error' ) {
225 return $this->__call( __FUNCTION__
, func_get_args() );
228 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
229 return $this->__call( __FUNCTION__
, func_get_args() );
232 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
233 return $this->__call( __FUNCTION__
, func_get_args() );
236 public function freeResult( $res ) {
237 return $this->__call( __FUNCTION__
, func_get_args() );
240 public function selectField(
241 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
243 return $this->__call( __FUNCTION__
, func_get_args() );
246 public function selectFieldValues(
247 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
249 return $this->__call( __FUNCTION__
, func_get_args() );
252 public function select(
253 $table, $vars, $conds = '', $fname = __METHOD__
,
254 $options = [], $join_conds = []
256 return $this->__call( __FUNCTION__
, func_get_args() );
259 public function selectSQLText(
260 $table, $vars, $conds = '', $fname = __METHOD__
,
261 $options = [], $join_conds = []
263 return $this->__call( __FUNCTION__
, func_get_args() );
266 public function selectRow(
267 $table, $vars, $conds, $fname = __METHOD__
,
268 $options = [], $join_conds = []
270 return $this->__call( __FUNCTION__
, func_get_args() );
273 public function estimateRowCount(
274 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
276 return $this->__call( __FUNCTION__
, func_get_args() );
279 public function selectRowCount(
280 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
282 return $this->__call( __FUNCTION__
, func_get_args() );
285 public function fieldExists( $table, $field, $fname = __METHOD__
) {
286 return $this->__call( __FUNCTION__
, func_get_args() );
289 public function indexExists( $table, $index, $fname = __METHOD__
) {
290 return $this->__call( __FUNCTION__
, func_get_args() );
293 public function tableExists( $table, $fname = __METHOD__
) {
294 return $this->__call( __FUNCTION__
, func_get_args() );
297 public function indexUnique( $table, $index ) {
298 return $this->__call( __FUNCTION__
, func_get_args() );
301 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
302 return $this->__call( __FUNCTION__
, func_get_args() );
305 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
306 return $this->__call( __FUNCTION__
, func_get_args() );
309 public function makeList( $a, $mode = self
::LIST_COMMA
) {
310 return $this->__call( __FUNCTION__
, func_get_args() );
313 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
314 return $this->__call( __FUNCTION__
, func_get_args() );
317 public function aggregateValue( $valuedata, $valuename = 'value' ) {
318 return $this->__call( __FUNCTION__
, func_get_args() );
321 public function bitNot( $field ) {
322 return $this->__call( __FUNCTION__
, func_get_args() );
325 public function bitAnd( $fieldLeft, $fieldRight ) {
326 return $this->__call( __FUNCTION__
, func_get_args() );
329 public function bitOr( $fieldLeft, $fieldRight ) {
330 return $this->__call( __FUNCTION__
, func_get_args() );
333 public function buildConcat( $stringList ) {
334 return $this->__call( __FUNCTION__
, func_get_args() );
337 public function buildGroupConcatField(
338 $delim, $table, $field, $conds = '', $join_conds = []
340 return $this->__call( __FUNCTION__
, func_get_args() );
343 public function buildStringCast( $field ) {
344 return $this->__call( __FUNCTION__
, func_get_args() );
347 public function selectDB( $db ) {
348 return $this->__call( __FUNCTION__
, func_get_args() );
351 public function getDBname() {
352 return $this->__call( __FUNCTION__
, func_get_args() );
355 public function getServer() {
356 return $this->__call( __FUNCTION__
, func_get_args() );
359 public function addQuotes( $s ) {
360 return $this->__call( __FUNCTION__
, func_get_args() );
363 public function buildLike() {
364 return $this->__call( __FUNCTION__
, func_get_args() );
367 public function anyChar() {
368 return $this->__call( __FUNCTION__
, func_get_args() );
371 public function anyString() {
372 return $this->__call( __FUNCTION__
, func_get_args() );
375 public function nextSequenceValue( $seqName ) {
376 return $this->__call( __FUNCTION__
, func_get_args() );
379 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
380 return $this->__call( __FUNCTION__
, func_get_args() );
383 public function upsert(
384 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
386 return $this->__call( __FUNCTION__
, func_get_args() );
389 public function deleteJoin(
390 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
392 return $this->__call( __FUNCTION__
, func_get_args() );
395 public function delete( $table, $conds, $fname = __METHOD__
) {
396 return $this->__call( __FUNCTION__
, func_get_args() );
399 public function insertSelect(
400 $destTable, $srcTable, $varMap, $conds,
401 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
403 return $this->__call( __FUNCTION__
, func_get_args() );
406 public function unionSupportsOrderAndLimit() {
407 return $this->__call( __FUNCTION__
, func_get_args() );
410 public function unionQueries( $sqls, $all ) {
411 return $this->__call( __FUNCTION__
, func_get_args() );
414 public function conditional( $cond, $trueVal, $falseVal ) {
415 return $this->__call( __FUNCTION__
, func_get_args() );
418 public function strreplace( $orig, $old, $new ) {
419 return $this->__call( __FUNCTION__
, func_get_args() );
422 public function getServerUptime() {
423 return $this->__call( __FUNCTION__
, func_get_args() );
426 public function wasDeadlock() {
427 return $this->__call( __FUNCTION__
, func_get_args() );
430 public function wasLockTimeout() {
431 return $this->__call( __FUNCTION__
, func_get_args() );
434 public function wasErrorReissuable() {
435 return $this->__call( __FUNCTION__
, func_get_args() );
438 public function wasReadOnlyError() {
439 return $this->__call( __FUNCTION__
, func_get_args() );
442 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
443 return $this->__call( __FUNCTION__
, func_get_args() );
446 public function getReplicaPos() {
447 return $this->__call( __FUNCTION__
, func_get_args() );
450 public function getMasterPos() {
451 return $this->__call( __FUNCTION__
, func_get_args() );
454 public function serverIsReadOnly() {
455 return $this->__call( __FUNCTION__
, func_get_args() );
458 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
459 return $this->__call( __FUNCTION__
, func_get_args() );
462 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
463 return $this->__call( __FUNCTION__
, func_get_args() );
466 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
467 return $this->__call( __FUNCTION__
, func_get_args() );
470 public function setTransactionListener( $name, callable
$callback = null ) {
471 return $this->__call( __FUNCTION__
, func_get_args() );
474 public function startAtomic( $fname = __METHOD__
) {
475 return $this->__call( __FUNCTION__
, func_get_args() );
478 public function endAtomic( $fname = __METHOD__
) {
479 return $this->__call( __FUNCTION__
, func_get_args() );
482 public function doAtomicSection( $fname, callable
$callback ) {
483 return $this->__call( __FUNCTION__
, func_get_args() );
486 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
487 return $this->__call( __FUNCTION__
, func_get_args() );
490 public function commit( $fname = __METHOD__
, $flush = '' ) {
491 return $this->__call( __FUNCTION__
, func_get_args() );
494 public function rollback( $fname = __METHOD__
, $flush = '' ) {
495 return $this->__call( __FUNCTION__
, func_get_args() );
498 public function flushSnapshot( $fname = __METHOD__
) {
499 return $this->__call( __FUNCTION__
, func_get_args() );
502 public function listTables( $prefix = null, $fname = __METHOD__
) {
503 return $this->__call( __FUNCTION__
, func_get_args() );
506 public function timestamp( $ts = 0 ) {
507 return $this->__call( __FUNCTION__
, func_get_args() );
510 public function timestampOrNull( $ts = null ) {
511 return $this->__call( __FUNCTION__
, func_get_args() );
514 public function ping( &$rtt = null ) {
515 return func_num_args()
516 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
517 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
520 public function getLag() {
521 return $this->__call( __FUNCTION__
, func_get_args() );
524 public function getSessionLagStatus() {
525 return $this->__call( __FUNCTION__
, func_get_args() );
528 public function maxListLen() {
529 return $this->__call( __FUNCTION__
, func_get_args() );
532 public function encodeBlob( $b ) {
533 return $this->__call( __FUNCTION__
, func_get_args() );
536 public function decodeBlob( $b ) {
537 return $this->__call( __FUNCTION__
, func_get_args() );
540 public function setSessionOptions( array $options ) {
541 return $this->__call( __FUNCTION__
, func_get_args() );
544 public function setSchemaVars( $vars ) {
545 return $this->__call( __FUNCTION__
, func_get_args() );
548 public function lockIsFree( $lockName, $method ) {
549 return $this->__call( __FUNCTION__
, func_get_args() );
552 public function lock( $lockName, $method, $timeout = 5 ) {
553 return $this->__call( __FUNCTION__
, func_get_args() );
556 public function unlock( $lockName, $method ) {
557 return $this->__call( __FUNCTION__
, func_get_args() );
560 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
561 return $this->__call( __FUNCTION__
, func_get_args() );
564 public function namedLocksEnqueue() {
565 return $this->__call( __FUNCTION__
, func_get_args() );
568 public function getInfinity() {
569 return $this->__call( __FUNCTION__
, func_get_args() );
572 public function encodeExpiry( $expiry ) {
573 return $this->__call( __FUNCTION__
, func_get_args() );
576 public function decodeExpiry( $expiry, $format = TS_MW
) {
577 return $this->__call( __FUNCTION__
, func_get_args() );
580 public function setBigSelects( $value = true ) {
581 return $this->__call( __FUNCTION__
, func_get_args() );
584 public function isReadOnly() {
585 return $this->__call( __FUNCTION__
, func_get_args() );
588 public function setTableAliases( array $aliases ) {
589 return $this->__call( __FUNCTION__
, func_get_args() );
593 * Clean up the connection when out of scope
595 function __destruct() {
597 $this->lb
->reuseConnection( $this->conn
);