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 N-tuple of (server index, group, DatabaseDomain|string) */
25 * @param ILoadBalancer $lb
26 * @param IDatabase|array $conn Connection or (server index, group, DatabaseDomain|string)
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_DOMAIN
] !== 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 getDomainID() {
149 if ( $this->conn
=== null ) {
150 $domain = $this->params
[self
::FLD_DOMAIN
];
151 // Avoid triggering a database connection
152 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
155 return $this->__call( __FUNCTION__
, func_get_args() );
158 public function getWikiID() {
159 return $this->getDomainID();
162 public function getType() {
163 return $this->__call( __FUNCTION__
, func_get_args() );
166 public function open( $server, $user, $password, $dbName ) {
167 return $this->__call( __FUNCTION__
, func_get_args() );
170 public function fetchObject( $res ) {
171 return $this->__call( __FUNCTION__
, func_get_args() );
174 public function fetchRow( $res ) {
175 return $this->__call( __FUNCTION__
, func_get_args() );
178 public function numRows( $res ) {
179 return $this->__call( __FUNCTION__
, func_get_args() );
182 public function numFields( $res ) {
183 return $this->__call( __FUNCTION__
, func_get_args() );
186 public function fieldName( $res, $n ) {
187 return $this->__call( __FUNCTION__
, func_get_args() );
190 public function insertId() {
191 return $this->__call( __FUNCTION__
, func_get_args() );
194 public function dataSeek( $res, $row ) {
195 return $this->__call( __FUNCTION__
, func_get_args() );
198 public function lastErrno() {
199 return $this->__call( __FUNCTION__
, func_get_args() );
202 public function lastError() {
203 return $this->__call( __FUNCTION__
, func_get_args() );
206 public function fieldInfo( $table, $field ) {
207 return $this->__call( __FUNCTION__
, func_get_args() );
210 public function affectedRows() {
211 return $this->__call( __FUNCTION__
, func_get_args() );
214 public function getSoftwareLink() {
215 return $this->__call( __FUNCTION__
, func_get_args() );
218 public function getServerVersion() {
219 return $this->__call( __FUNCTION__
, func_get_args() );
222 public function close() {
223 return $this->__call( __FUNCTION__
, func_get_args() );
226 public function reportConnectionError( $error = 'Unknown error' ) {
227 return $this->__call( __FUNCTION__
, func_get_args() );
230 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
231 return $this->__call( __FUNCTION__
, func_get_args() );
234 public function reportQueryError( $error, $errno, $sql, $fname, $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 = []
245 return $this->__call( __FUNCTION__
, func_get_args() );
248 public function selectFieldValues(
249 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
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 = []
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 fieldExists( $table, $field, $fname = __METHOD__
) {
288 return $this->__call( __FUNCTION__
, func_get_args() );
291 public function indexExists( $table, $index, $fname = __METHOD__
) {
292 return $this->__call( __FUNCTION__
, func_get_args() );
295 public function tableExists( $table, $fname = __METHOD__
) {
296 return $this->__call( __FUNCTION__
, func_get_args() );
299 public function indexUnique( $table, $index ) {
300 return $this->__call( __FUNCTION__
, func_get_args() );
303 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
304 return $this->__call( __FUNCTION__
, func_get_args() );
307 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
308 return $this->__call( __FUNCTION__
, func_get_args() );
311 public function makeList( $a, $mode = self
::LIST_COMMA
) {
312 return $this->__call( __FUNCTION__
, func_get_args() );
315 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
316 return $this->__call( __FUNCTION__
, func_get_args() );
319 public function aggregateValue( $valuedata, $valuename = 'value' ) {
320 return $this->__call( __FUNCTION__
, func_get_args() );
323 public function bitNot( $field ) {
324 return $this->__call( __FUNCTION__
, func_get_args() );
327 public function bitAnd( $fieldLeft, $fieldRight ) {
328 return $this->__call( __FUNCTION__
, func_get_args() );
331 public function bitOr( $fieldLeft, $fieldRight ) {
332 return $this->__call( __FUNCTION__
, func_get_args() );
335 public function buildConcat( $stringList ) {
336 return $this->__call( __FUNCTION__
, func_get_args() );
339 public function buildGroupConcatField(
340 $delim, $table, $field, $conds = '', $join_conds = []
342 return $this->__call( __FUNCTION__
, func_get_args() );
345 public function buildStringCast( $field ) {
346 return $this->__call( __FUNCTION__
, func_get_args() );
349 public function selectDB( $db ) {
350 return $this->__call( __FUNCTION__
, func_get_args() );
353 public function getDBname() {
354 return $this->__call( __FUNCTION__
, func_get_args() );
357 public function getServer() {
358 return $this->__call( __FUNCTION__
, func_get_args() );
361 public function addQuotes( $s ) {
362 return $this->__call( __FUNCTION__
, func_get_args() );
365 public function buildLike() {
366 return $this->__call( __FUNCTION__
, func_get_args() );
369 public function anyChar() {
370 return $this->__call( __FUNCTION__
, func_get_args() );
373 public function anyString() {
374 return $this->__call( __FUNCTION__
, func_get_args() );
377 public function nextSequenceValue( $seqName ) {
378 return $this->__call( __FUNCTION__
, func_get_args() );
381 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
382 return $this->__call( __FUNCTION__
, func_get_args() );
385 public function upsert(
386 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
388 return $this->__call( __FUNCTION__
, func_get_args() );
391 public function deleteJoin(
392 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
394 return $this->__call( __FUNCTION__
, func_get_args() );
397 public function delete( $table, $conds, $fname = __METHOD__
) {
398 return $this->__call( __FUNCTION__
, func_get_args() );
401 public function insertSelect(
402 $destTable, $srcTable, $varMap, $conds,
403 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
405 return $this->__call( __FUNCTION__
, func_get_args() );
408 public function unionSupportsOrderAndLimit() {
409 return $this->__call( __FUNCTION__
, func_get_args() );
412 public function unionQueries( $sqls, $all ) {
413 return $this->__call( __FUNCTION__
, func_get_args() );
416 public function conditional( $cond, $trueVal, $falseVal ) {
417 return $this->__call( __FUNCTION__
, func_get_args() );
420 public function strreplace( $orig, $old, $new ) {
421 return $this->__call( __FUNCTION__
, func_get_args() );
424 public function getServerUptime() {
425 return $this->__call( __FUNCTION__
, func_get_args() );
428 public function wasDeadlock() {
429 return $this->__call( __FUNCTION__
, func_get_args() );
432 public function wasLockTimeout() {
433 return $this->__call( __FUNCTION__
, func_get_args() );
436 public function wasErrorReissuable() {
437 return $this->__call( __FUNCTION__
, func_get_args() );
440 public function wasReadOnlyError() {
441 return $this->__call( __FUNCTION__
, func_get_args() );
444 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
445 return $this->__call( __FUNCTION__
, func_get_args() );
448 public function getReplicaPos() {
449 return $this->__call( __FUNCTION__
, func_get_args() );
452 public function getMasterPos() {
453 return $this->__call( __FUNCTION__
, func_get_args() );
456 public function serverIsReadOnly() {
457 return $this->__call( __FUNCTION__
, func_get_args() );
460 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
461 return $this->__call( __FUNCTION__
, func_get_args() );
464 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
465 return $this->__call( __FUNCTION__
, func_get_args() );
468 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
469 return $this->__call( __FUNCTION__
, func_get_args() );
472 public function setTransactionListener( $name, callable
$callback = null ) {
473 return $this->__call( __FUNCTION__
, func_get_args() );
476 public function startAtomic( $fname = __METHOD__
) {
477 return $this->__call( __FUNCTION__
, func_get_args() );
480 public function endAtomic( $fname = __METHOD__
) {
481 return $this->__call( __FUNCTION__
, func_get_args() );
484 public function doAtomicSection( $fname, callable
$callback ) {
485 return $this->__call( __FUNCTION__
, func_get_args() );
488 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
489 return $this->__call( __FUNCTION__
, func_get_args() );
492 public function commit( $fname = __METHOD__
, $flush = '' ) {
493 return $this->__call( __FUNCTION__
, func_get_args() );
496 public function rollback( $fname = __METHOD__
, $flush = '' ) {
497 return $this->__call( __FUNCTION__
, func_get_args() );
500 public function flushSnapshot( $fname = __METHOD__
) {
501 return $this->__call( __FUNCTION__
, func_get_args() );
504 public function listTables( $prefix = null, $fname = __METHOD__
) {
505 return $this->__call( __FUNCTION__
, func_get_args() );
508 public function timestamp( $ts = 0 ) {
509 return $this->__call( __FUNCTION__
, func_get_args() );
512 public function timestampOrNull( $ts = null ) {
513 return $this->__call( __FUNCTION__
, func_get_args() );
516 public function ping( &$rtt = null ) {
517 return func_num_args()
518 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
519 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
522 public function getLag() {
523 return $this->__call( __FUNCTION__
, func_get_args() );
526 public function getSessionLagStatus() {
527 return $this->__call( __FUNCTION__
, func_get_args() );
530 public function maxListLen() {
531 return $this->__call( __FUNCTION__
, func_get_args() );
534 public function encodeBlob( $b ) {
535 return $this->__call( __FUNCTION__
, func_get_args() );
538 public function decodeBlob( $b ) {
539 return $this->__call( __FUNCTION__
, func_get_args() );
542 public function setSessionOptions( array $options ) {
543 return $this->__call( __FUNCTION__
, func_get_args() );
546 public function setSchemaVars( $vars ) {
547 return $this->__call( __FUNCTION__
, func_get_args() );
550 public function lockIsFree( $lockName, $method ) {
551 return $this->__call( __FUNCTION__
, func_get_args() );
554 public function lock( $lockName, $method, $timeout = 5 ) {
555 return $this->__call( __FUNCTION__
, func_get_args() );
558 public function unlock( $lockName, $method ) {
559 return $this->__call( __FUNCTION__
, func_get_args() );
562 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
563 return $this->__call( __FUNCTION__
, func_get_args() );
566 public function namedLocksEnqueue() {
567 return $this->__call( __FUNCTION__
, func_get_args() );
570 public function getInfinity() {
571 return $this->__call( __FUNCTION__
, func_get_args() );
574 public function encodeExpiry( $expiry ) {
575 return $this->__call( __FUNCTION__
, func_get_args() );
578 public function decodeExpiry( $expiry, $format = TS_MW
) {
579 return $this->__call( __FUNCTION__
, func_get_args() );
582 public function setBigSelects( $value = true ) {
583 return $this->__call( __FUNCTION__
, func_get_args() );
586 public function isReadOnly() {
587 return $this->__call( __FUNCTION__
, func_get_args() );
590 public function setTableAliases( array $aliases ) {
591 return $this->__call( __FUNCTION__
, func_get_args() );
594 public function declareUsageSectionStart( $id ) {
595 return $this->__call( __FUNCTION__
, func_get_args() );
598 public function declareUsageSectionEnd( $id ) {
599 return $this->__call( __FUNCTION__
, func_get_args() );
603 * Clean up the connection when out of scope
605 function __destruct() {
606 if ( $this->conn
!== null ) {
607 $this->lb
->reuseConnection( $this->conn
);