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 LoadBalancer */
14 /** @var DatabaseBase|null */
17 /** @var array|null */
21 * @param LoadBalancer $lb
22 * @param DatabaseBase|array $conn Connection or (server index, group, wiki ID) array
24 public function __construct( LoadBalancer
$lb, $conn ) {
26 if ( $conn instanceof DatabaseBase
) {
29 $this->params
= $conn;
33 function __call( $name, array $arguments ) {
34 if ( $this->conn
=== null ) {
35 list( $db, $groups, $wiki ) = $this->params
;
36 $this->conn
= $this->lb
->getConnection( $db, $groups, $wiki );
39 return call_user_func_array( [ $this->conn
, $name ], $arguments );
42 public function getServerInfo() {
43 return $this->__call( __FUNCTION__
, func_get_args() );
46 public function bufferResults( $buffer = null ) {
47 return $this->__call( __FUNCTION__
, func_get_args() );
50 public function trxLevel() {
51 return $this->__call( __FUNCTION__
, func_get_args() );
54 public function trxTimestamp() {
55 return $this->__call( __FUNCTION__
, func_get_args() );
58 public function explicitTrxActive() {
59 return $this->__call( __FUNCTION__
, func_get_args() );
62 public function tablePrefix( $prefix = null ) {
63 return $this->__call( __FUNCTION__
, func_get_args() );
66 public function dbSchema( $schema = null ) {
67 return $this->__call( __FUNCTION__
, func_get_args() );
70 public function getLBInfo( $name = null ) {
71 return $this->__call( __FUNCTION__
, func_get_args() );
74 public function setLBInfo( $name, $value = null ) {
75 return $this->__call( __FUNCTION__
, func_get_args() );
78 public function implicitGroupby() {
79 return $this->__call( __FUNCTION__
, func_get_args() );
82 public function implicitOrderby() {
83 return $this->__call( __FUNCTION__
, func_get_args() );
86 public function lastQuery() {
87 return $this->__call( __FUNCTION__
, func_get_args() );
90 public function doneWrites() {
91 return $this->__call( __FUNCTION__
, func_get_args() );
94 public function lastDoneWrites() {
95 return $this->__call( __FUNCTION__
, func_get_args() );
98 public function writesPending() {
99 return $this->__call( __FUNCTION__
, func_get_args() );
102 public function writesOrCallbacksPending() {
103 return $this->__call( __FUNCTION__
, func_get_args() );
106 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
107 return $this->__call( __FUNCTION__
, func_get_args() );
110 public function pendingWriteCallers() {
111 return $this->__call( __FUNCTION__
, func_get_args() );
114 public function isOpen() {
115 return $this->__call( __FUNCTION__
, func_get_args() );
118 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
119 return $this->__call( __FUNCTION__
, func_get_args() );
122 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
123 return $this->__call( __FUNCTION__
, func_get_args() );
126 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
127 return $this->__call( __FUNCTION__
, func_get_args() );
130 public function getFlag( $flag ) {
131 return $this->__call( __FUNCTION__
, func_get_args() );
134 public function getProperty( $name ) {
135 return $this->__call( __FUNCTION__
, func_get_args() );
138 public function getWikiID() {
139 return $this->__call( __FUNCTION__
, func_get_args() );
142 public function getType() {
143 return $this->__call( __FUNCTION__
, func_get_args() );
146 public function open( $server, $user, $password, $dbName ) {
147 return $this->__call( __FUNCTION__
, func_get_args() );
150 public function fetchObject( $res ) {
151 return $this->__call( __FUNCTION__
, func_get_args() );
154 public function fetchRow( $res ) {
155 return $this->__call( __FUNCTION__
, func_get_args() );
158 public function numRows( $res ) {
159 return $this->__call( __FUNCTION__
, func_get_args() );
162 public function numFields( $res ) {
163 return $this->__call( __FUNCTION__
, func_get_args() );
166 public function fieldName( $res, $n ) {
167 return $this->__call( __FUNCTION__
, func_get_args() );
170 public function insertId() {
171 return $this->__call( __FUNCTION__
, func_get_args() );
174 public function dataSeek( $res, $row ) {
175 return $this->__call( __FUNCTION__
, func_get_args() );
178 public function lastErrno() {
179 return $this->__call( __FUNCTION__
, func_get_args() );
182 public function lastError() {
183 return $this->__call( __FUNCTION__
, func_get_args() );
186 public function fieldInfo( $table, $field ) {
187 return $this->__call( __FUNCTION__
, func_get_args() );
190 public function affectedRows() {
191 return $this->__call( __FUNCTION__
, func_get_args() );
194 public function getSoftwareLink() {
195 return $this->__call( __FUNCTION__
, func_get_args() );
198 public function getServerVersion() {
199 return $this->__call( __FUNCTION__
, func_get_args() );
202 public function close() {
203 return $this->__call( __FUNCTION__
, func_get_args() );
206 public function reportConnectionError( $error = 'Unknown error' ) {
207 return $this->__call( __FUNCTION__
, func_get_args() );
210 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
211 return $this->__call( __FUNCTION__
, func_get_args() );
214 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
215 return $this->__call( __FUNCTION__
, func_get_args() );
218 public function freeResult( $res ) {
219 return $this->__call( __FUNCTION__
, func_get_args() );
222 public function selectField(
223 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
225 return $this->__call( __FUNCTION__
, func_get_args() );
228 public function selectFieldValues(
229 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
231 return $this->__call( __FUNCTION__
, func_get_args() );
234 public function select(
235 $table, $vars, $conds = '', $fname = __METHOD__
,
236 $options = [], $join_conds = []
238 return $this->__call( __FUNCTION__
, func_get_args() );
241 public function selectSQLText(
242 $table, $vars, $conds = '', $fname = __METHOD__
,
243 $options = [], $join_conds = []
245 return $this->__call( __FUNCTION__
, func_get_args() );
248 public function selectRow(
249 $table, $vars, $conds, $fname = __METHOD__
,
250 $options = [], $join_conds = []
252 return $this->__call( __FUNCTION__
, func_get_args() );
255 public function estimateRowCount(
256 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
258 return $this->__call( __FUNCTION__
, func_get_args() );
261 public function selectRowCount(
262 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
264 return $this->__call( __FUNCTION__
, func_get_args() );
267 public function fieldExists( $table, $field, $fname = __METHOD__
) {
268 return $this->__call( __FUNCTION__
, func_get_args() );
271 public function indexExists( $table, $index, $fname = __METHOD__
) {
272 return $this->__call( __FUNCTION__
, func_get_args() );
275 public function tableExists( $table, $fname = __METHOD__
) {
276 return $this->__call( __FUNCTION__
, func_get_args() );
279 public function indexUnique( $table, $index ) {
280 return $this->__call( __FUNCTION__
, func_get_args() );
283 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
284 return $this->__call( __FUNCTION__
, func_get_args() );
287 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
288 return $this->__call( __FUNCTION__
, func_get_args() );
291 public function makeList( $a, $mode = LIST_COMMA
) {
292 return $this->__call( __FUNCTION__
, func_get_args() );
295 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
296 return $this->__call( __FUNCTION__
, func_get_args() );
299 public function bitNot( $field ) {
300 return $this->__call( __FUNCTION__
, func_get_args() );
303 public function bitAnd( $fieldLeft, $fieldRight ) {
304 return $this->__call( __FUNCTION__
, func_get_args() );
307 public function bitOr( $fieldLeft, $fieldRight ) {
308 return $this->__call( __FUNCTION__
, func_get_args() );
311 public function buildConcat( $stringList ) {
312 return $this->__call( __FUNCTION__
, func_get_args() );
315 public function buildGroupConcatField(
316 $delim, $table, $field, $conds = '', $join_conds = []
318 return $this->__call( __FUNCTION__
, func_get_args() );
321 public function selectDB( $db ) {
322 return $this->__call( __FUNCTION__
, func_get_args() );
325 public function getDBname() {
326 return $this->__call( __FUNCTION__
, func_get_args() );
329 public function getServer() {
330 return $this->__call( __FUNCTION__
, func_get_args() );
333 public function addQuotes( $s ) {
334 return $this->__call( __FUNCTION__
, func_get_args() );
337 public function buildLike() {
338 return $this->__call( __FUNCTION__
, func_get_args() );
341 public function anyChar() {
342 return $this->__call( __FUNCTION__
, func_get_args() );
345 public function anyString() {
346 return $this->__call( __FUNCTION__
, func_get_args() );
349 public function nextSequenceValue( $seqName ) {
350 return $this->__call( __FUNCTION__
, func_get_args() );
353 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
354 return $this->__call( __FUNCTION__
, func_get_args() );
357 public function upsert(
358 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
360 return $this->__call( __FUNCTION__
, func_get_args() );
363 public function deleteJoin(
364 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
366 return $this->__call( __FUNCTION__
, func_get_args() );
369 public function delete( $table, $conds, $fname = __METHOD__
) {
370 return $this->__call( __FUNCTION__
, func_get_args() );
373 public function insertSelect(
374 $destTable, $srcTable, $varMap, $conds,
375 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
377 return $this->__call( __FUNCTION__
, func_get_args() );
380 public function unionSupportsOrderAndLimit() {
381 return $this->__call( __FUNCTION__
, func_get_args() );
384 public function unionQueries( $sqls, $all ) {
385 return $this->__call( __FUNCTION__
, func_get_args() );
388 public function conditional( $cond, $trueVal, $falseVal ) {
389 return $this->__call( __FUNCTION__
, func_get_args() );
392 public function strreplace( $orig, $old, $new ) {
393 return $this->__call( __FUNCTION__
, func_get_args() );
396 public function getServerUptime() {
397 return $this->__call( __FUNCTION__
, func_get_args() );
400 public function wasDeadlock() {
401 return $this->__call( __FUNCTION__
, func_get_args() );
404 public function wasLockTimeout() {
405 return $this->__call( __FUNCTION__
, func_get_args() );
408 public function wasErrorReissuable() {
409 return $this->__call( __FUNCTION__
, func_get_args() );
412 public function wasReadOnlyError() {
413 return $this->__call( __FUNCTION__
, func_get_args() );
416 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
417 return $this->__call( __FUNCTION__
, func_get_args() );
420 public function getSlavePos() {
421 return $this->__call( __FUNCTION__
, func_get_args() );
424 public function getMasterPos() {
425 return $this->__call( __FUNCTION__
, func_get_args() );
428 public function serverIsReadOnly() {
429 return $this->__call( __FUNCTION__
, func_get_args() );
432 public function onTransactionResolution( callable
$callback ) {
433 return $this->__call( __FUNCTION__
, func_get_args() );
436 public function onTransactionIdle( callable
$callback ) {
437 return $this->__call( __FUNCTION__
, func_get_args() );
440 public function onTransactionPreCommitOrIdle( callable
$callback ) {
441 return $this->__call( __FUNCTION__
, func_get_args() );
444 public function setTransactionListener( $name, callable
$callback = null ) {
445 return $this->__call( __FUNCTION__
, func_get_args() );
448 public function startAtomic( $fname = __METHOD__
) {
449 return $this->__call( __FUNCTION__
, func_get_args() );
452 public function endAtomic( $fname = __METHOD__
) {
453 return $this->__call( __FUNCTION__
, func_get_args() );
456 public function doAtomicSection( $fname, callable
$callback ) {
457 return $this->__call( __FUNCTION__
, func_get_args() );
460 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
461 return $this->__call( __FUNCTION__
, func_get_args() );
464 public function commit( $fname = __METHOD__
, $flush = '' ) {
465 return $this->__call( __FUNCTION__
, func_get_args() );
468 public function rollback( $fname = __METHOD__
, $flush = '' ) {
469 return $this->__call( __FUNCTION__
, func_get_args() );
472 public function listTables( $prefix = null, $fname = __METHOD__
) {
473 return $this->__call( __FUNCTION__
, func_get_args() );
476 public function timestamp( $ts = 0 ) {
477 return $this->__call( __FUNCTION__
, func_get_args() );
480 public function timestampOrNull( $ts = null ) {
481 return $this->__call( __FUNCTION__
, func_get_args() );
484 public function ping( &$rtt = null ) {
485 return func_num_args()
486 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
487 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
490 public function getLag() {
491 return $this->__call( __FUNCTION__
, func_get_args() );
494 public function getSessionLagStatus() {
495 return $this->__call( __FUNCTION__
, func_get_args() );
498 public function maxListLen() {
499 return $this->__call( __FUNCTION__
, func_get_args() );
502 public function encodeBlob( $b ) {
503 return $this->__call( __FUNCTION__
, func_get_args() );
506 public function decodeBlob( $b ) {
507 return $this->__call( __FUNCTION__
, func_get_args() );
510 public function setSessionOptions( array $options ) {
511 return $this->__call( __FUNCTION__
, func_get_args() );
514 public function setSchemaVars( $vars ) {
515 return $this->__call( __FUNCTION__
, func_get_args() );
518 public function lockIsFree( $lockName, $method ) {
519 return $this->__call( __FUNCTION__
, func_get_args() );
522 public function lock( $lockName, $method, $timeout = 5 ) {
523 return $this->__call( __FUNCTION__
, func_get_args() );
526 public function unlock( $lockName, $method ) {
527 return $this->__call( __FUNCTION__
, func_get_args() );
530 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
531 return $this->__call( __FUNCTION__
, func_get_args() );
534 public function namedLocksEnqueue() {
535 return $this->__call( __FUNCTION__
, func_get_args() );
538 public function getInfinity() {
539 return $this->__call( __FUNCTION__
, func_get_args() );
542 public function encodeExpiry( $expiry ) {
543 return $this->__call( __FUNCTION__
, func_get_args() );
546 public function decodeExpiry( $expiry, $format = TS_MW
) {
547 return $this->__call( __FUNCTION__
, func_get_args() );
550 public function setBigSelects( $value = true ) {
551 return $this->__call( __FUNCTION__
, func_get_args() );
554 public function isReadOnly() {
555 return $this->__call( __FUNCTION__
, func_get_args() );
559 * Clean up the connection when out of scope
561 function __destruct() {
562 if ( $this->conn
!== null ) {
563 $this->lb
->reuseConnection( $this->conn
);