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 */
25 * @param LoadBalancer $lb
26 * @param DatabaseBase|array $conn Connection or (server index, group, wiki ID)
28 public function __construct( LoadBalancer
$lb, $conn ) {
30 if ( $conn instanceof DatabaseBase
) {
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 implicitGroupby() {
85 return $this->__call( __FUNCTION__
, func_get_args() );
88 public function implicitOrderby() {
89 return $this->__call( __FUNCTION__
, func_get_args() );
92 public function lastQuery() {
93 return $this->__call( __FUNCTION__
, func_get_args() );
96 public function doneWrites() {
97 return $this->__call( __FUNCTION__
, func_get_args() );
100 public function lastDoneWrites() {
101 return $this->__call( __FUNCTION__
, func_get_args() );
104 public function writesPending() {
105 return $this->__call( __FUNCTION__
, func_get_args() );
108 public function writesOrCallbacksPending() {
109 return $this->__call( __FUNCTION__
, func_get_args() );
112 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
113 return $this->__call( __FUNCTION__
, func_get_args() );
116 public function pendingWriteCallers() {
117 return $this->__call( __FUNCTION__
, func_get_args() );
120 public function isOpen() {
121 return $this->__call( __FUNCTION__
, func_get_args() );
124 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
125 return $this->__call( __FUNCTION__
, func_get_args() );
128 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
129 return $this->__call( __FUNCTION__
, func_get_args() );
132 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
133 return $this->__call( __FUNCTION__
, func_get_args() );
136 public function getFlag( $flag ) {
137 return $this->__call( __FUNCTION__
, func_get_args() );
140 public function getProperty( $name ) {
141 return $this->__call( __FUNCTION__
, func_get_args() );
144 public function getWikiID() {
145 if ( $this->conn
=== null ) {
146 // Avoid triggering a connection
147 return $this->params
[self
::FLD_WIKI
];
150 return $this->__call( __FUNCTION__
, func_get_args() );
153 public function getType() {
154 return $this->__call( __FUNCTION__
, func_get_args() );
157 public function open( $server, $user, $password, $dbName ) {
158 return $this->__call( __FUNCTION__
, func_get_args() );
161 public function fetchObject( $res ) {
162 return $this->__call( __FUNCTION__
, func_get_args() );
165 public function fetchRow( $res ) {
166 return $this->__call( __FUNCTION__
, func_get_args() );
169 public function numRows( $res ) {
170 return $this->__call( __FUNCTION__
, func_get_args() );
173 public function numFields( $res ) {
174 return $this->__call( __FUNCTION__
, func_get_args() );
177 public function fieldName( $res, $n ) {
178 return $this->__call( __FUNCTION__
, func_get_args() );
181 public function insertId() {
182 return $this->__call( __FUNCTION__
, func_get_args() );
185 public function dataSeek( $res, $row ) {
186 return $this->__call( __FUNCTION__
, func_get_args() );
189 public function lastErrno() {
190 return $this->__call( __FUNCTION__
, func_get_args() );
193 public function lastError() {
194 return $this->__call( __FUNCTION__
, func_get_args() );
197 public function fieldInfo( $table, $field ) {
198 return $this->__call( __FUNCTION__
, func_get_args() );
201 public function affectedRows() {
202 return $this->__call( __FUNCTION__
, func_get_args() );
205 public function getSoftwareLink() {
206 return $this->__call( __FUNCTION__
, func_get_args() );
209 public function getServerVersion() {
210 return $this->__call( __FUNCTION__
, func_get_args() );
213 public function close() {
214 return $this->__call( __FUNCTION__
, func_get_args() );
217 public function reportConnectionError( $error = 'Unknown error' ) {
218 return $this->__call( __FUNCTION__
, func_get_args() );
221 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
222 return $this->__call( __FUNCTION__
, func_get_args() );
225 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
226 return $this->__call( __FUNCTION__
, func_get_args() );
229 public function freeResult( $res ) {
230 return $this->__call( __FUNCTION__
, func_get_args() );
233 public function selectField(
234 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
236 return $this->__call( __FUNCTION__
, func_get_args() );
239 public function selectFieldValues(
240 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
242 return $this->__call( __FUNCTION__
, func_get_args() );
245 public function select(
246 $table, $vars, $conds = '', $fname = __METHOD__
,
247 $options = [], $join_conds = []
249 return $this->__call( __FUNCTION__
, func_get_args() );
252 public function selectSQLText(
253 $table, $vars, $conds = '', $fname = __METHOD__
,
254 $options = [], $join_conds = []
256 return $this->__call( __FUNCTION__
, func_get_args() );
259 public function selectRow(
260 $table, $vars, $conds, $fname = __METHOD__
,
261 $options = [], $join_conds = []
263 return $this->__call( __FUNCTION__
, func_get_args() );
266 public function estimateRowCount(
267 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
269 return $this->__call( __FUNCTION__
, func_get_args() );
272 public function selectRowCount(
273 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
275 return $this->__call( __FUNCTION__
, func_get_args() );
278 public function fieldExists( $table, $field, $fname = __METHOD__
) {
279 return $this->__call( __FUNCTION__
, func_get_args() );
282 public function indexExists( $table, $index, $fname = __METHOD__
) {
283 return $this->__call( __FUNCTION__
, func_get_args() );
286 public function tableExists( $table, $fname = __METHOD__
) {
287 return $this->__call( __FUNCTION__
, func_get_args() );
290 public function indexUnique( $table, $index ) {
291 return $this->__call( __FUNCTION__
, func_get_args() );
294 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
295 return $this->__call( __FUNCTION__
, func_get_args() );
298 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
299 return $this->__call( __FUNCTION__
, func_get_args() );
302 public function makeList( $a, $mode = LIST_COMMA
) {
303 return $this->__call( __FUNCTION__
, func_get_args() );
306 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
307 return $this->__call( __FUNCTION__
, func_get_args() );
310 public function bitNot( $field ) {
311 return $this->__call( __FUNCTION__
, func_get_args() );
314 public function bitAnd( $fieldLeft, $fieldRight ) {
315 return $this->__call( __FUNCTION__
, func_get_args() );
318 public function bitOr( $fieldLeft, $fieldRight ) {
319 return $this->__call( __FUNCTION__
, func_get_args() );
322 public function buildConcat( $stringList ) {
323 return $this->__call( __FUNCTION__
, func_get_args() );
326 public function buildGroupConcatField(
327 $delim, $table, $field, $conds = '', $join_conds = []
329 return $this->__call( __FUNCTION__
, func_get_args() );
332 public function selectDB( $db ) {
333 return $this->__call( __FUNCTION__
, func_get_args() );
336 public function getDBname() {
337 return $this->__call( __FUNCTION__
, func_get_args() );
340 public function getServer() {
341 return $this->__call( __FUNCTION__
, func_get_args() );
344 public function addQuotes( $s ) {
345 return $this->__call( __FUNCTION__
, func_get_args() );
348 public function buildLike() {
349 return $this->__call( __FUNCTION__
, func_get_args() );
352 public function anyChar() {
353 return $this->__call( __FUNCTION__
, func_get_args() );
356 public function anyString() {
357 return $this->__call( __FUNCTION__
, func_get_args() );
360 public function nextSequenceValue( $seqName ) {
361 return $this->__call( __FUNCTION__
, func_get_args() );
364 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
365 return $this->__call( __FUNCTION__
, func_get_args() );
368 public function upsert(
369 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
371 return $this->__call( __FUNCTION__
, func_get_args() );
374 public function deleteJoin(
375 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
377 return $this->__call( __FUNCTION__
, func_get_args() );
380 public function delete( $table, $conds, $fname = __METHOD__
) {
381 return $this->__call( __FUNCTION__
, func_get_args() );
384 public function insertSelect(
385 $destTable, $srcTable, $varMap, $conds,
386 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
388 return $this->__call( __FUNCTION__
, func_get_args() );
391 public function unionSupportsOrderAndLimit() {
392 return $this->__call( __FUNCTION__
, func_get_args() );
395 public function unionQueries( $sqls, $all ) {
396 return $this->__call( __FUNCTION__
, func_get_args() );
399 public function conditional( $cond, $trueVal, $falseVal ) {
400 return $this->__call( __FUNCTION__
, func_get_args() );
403 public function strreplace( $orig, $old, $new ) {
404 return $this->__call( __FUNCTION__
, func_get_args() );
407 public function getServerUptime() {
408 return $this->__call( __FUNCTION__
, func_get_args() );
411 public function wasDeadlock() {
412 return $this->__call( __FUNCTION__
, func_get_args() );
415 public function wasLockTimeout() {
416 return $this->__call( __FUNCTION__
, func_get_args() );
419 public function wasErrorReissuable() {
420 return $this->__call( __FUNCTION__
, func_get_args() );
423 public function wasReadOnlyError() {
424 return $this->__call( __FUNCTION__
, func_get_args() );
427 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
428 return $this->__call( __FUNCTION__
, func_get_args() );
431 public function getSlavePos() {
432 return $this->__call( __FUNCTION__
, func_get_args() );
435 public function getMasterPos() {
436 return $this->__call( __FUNCTION__
, func_get_args() );
439 public function serverIsReadOnly() {
440 return $this->__call( __FUNCTION__
, func_get_args() );
443 public function onTransactionResolution( callable
$callback ) {
444 return $this->__call( __FUNCTION__
, func_get_args() );
447 public function onTransactionIdle( callable
$callback ) {
448 return $this->__call( __FUNCTION__
, func_get_args() );
451 public function onTransactionPreCommitOrIdle( callable
$callback ) {
452 return $this->__call( __FUNCTION__
, func_get_args() );
455 public function setTransactionListener( $name, callable
$callback = null ) {
456 return $this->__call( __FUNCTION__
, func_get_args() );
459 public function startAtomic( $fname = __METHOD__
) {
460 return $this->__call( __FUNCTION__
, func_get_args() );
463 public function endAtomic( $fname = __METHOD__
) {
464 return $this->__call( __FUNCTION__
, func_get_args() );
467 public function doAtomicSection( $fname, callable
$callback ) {
468 return $this->__call( __FUNCTION__
, func_get_args() );
471 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
472 return $this->__call( __FUNCTION__
, func_get_args() );
475 public function commit( $fname = __METHOD__
, $flush = '' ) {
476 return $this->__call( __FUNCTION__
, func_get_args() );
479 public function rollback( $fname = __METHOD__
, $flush = '' ) {
480 return $this->__call( __FUNCTION__
, func_get_args() );
483 public function flushSnapshot( $fname = __METHOD__
) {
484 return $this->__call( __FUNCTION__
, func_get_args() );
487 public function listTables( $prefix = null, $fname = __METHOD__
) {
488 return $this->__call( __FUNCTION__
, func_get_args() );
491 public function timestamp( $ts = 0 ) {
492 return $this->__call( __FUNCTION__
, func_get_args() );
495 public function timestampOrNull( $ts = null ) {
496 return $this->__call( __FUNCTION__
, func_get_args() );
499 public function ping( &$rtt = null ) {
500 return func_num_args()
501 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
502 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
505 public function getLag() {
506 return $this->__call( __FUNCTION__
, func_get_args() );
509 public function getSessionLagStatus() {
510 return $this->__call( __FUNCTION__
, func_get_args() );
513 public function maxListLen() {
514 return $this->__call( __FUNCTION__
, func_get_args() );
517 public function encodeBlob( $b ) {
518 return $this->__call( __FUNCTION__
, func_get_args() );
521 public function decodeBlob( $b ) {
522 return $this->__call( __FUNCTION__
, func_get_args() );
525 public function setSessionOptions( array $options ) {
526 return $this->__call( __FUNCTION__
, func_get_args() );
529 public function setSchemaVars( $vars ) {
530 return $this->__call( __FUNCTION__
, func_get_args() );
533 public function lockIsFree( $lockName, $method ) {
534 return $this->__call( __FUNCTION__
, func_get_args() );
537 public function lock( $lockName, $method, $timeout = 5 ) {
538 return $this->__call( __FUNCTION__
, func_get_args() );
541 public function unlock( $lockName, $method ) {
542 return $this->__call( __FUNCTION__
, func_get_args() );
545 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
546 return $this->__call( __FUNCTION__
, func_get_args() );
549 public function namedLocksEnqueue() {
550 return $this->__call( __FUNCTION__
, func_get_args() );
553 public function getInfinity() {
554 return $this->__call( __FUNCTION__
, func_get_args() );
557 public function encodeExpiry( $expiry ) {
558 return $this->__call( __FUNCTION__
, func_get_args() );
561 public function decodeExpiry( $expiry, $format = TS_MW
) {
562 return $this->__call( __FUNCTION__
, func_get_args() );
565 public function setBigSelects( $value = true ) {
566 return $this->__call( __FUNCTION__
, func_get_args() );
569 public function isReadOnly() {
570 return $this->__call( __FUNCTION__
, func_get_args() );
574 * Clean up the connection when out of scope
576 function __destruct() {
577 if ( $this->conn
!== null ) {
578 $this->lb
->reuseConnection( $this->conn
);