3 namespace Wikimedia\Rdbms
;
5 use InvalidArgumentException
;
8 * Helper class to handle automatically marking connections as reusable (via RAII pattern)
9 * as well handling deferring the actual network connection until the handle is used
11 * @note: proxy methods are defined explicitly to avoid interface errors
15 class DBConnRef
implements IDatabase
{
16 /** @var ILoadBalancer */
18 /** @var Database|null Live connection handle */
20 /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */
28 * @param ILoadBalancer $lb Connection manager for $conn
29 * @param Database|array $conn New connection handle or (server index, query groups, domain)
31 public function __construct( ILoadBalancer
$lb, $conn ) {
33 if ( $conn instanceof Database
) {
34 $this->conn
= $conn; // live handle
35 } elseif ( count( $conn ) >= 3 && $conn[self
::FLD_DOMAIN
] !== false ) {
36 $this->params
= $conn;
38 throw new InvalidArgumentException( "Missing lazy connection arguments." );
42 function __call( $name, array $arguments ) {
43 if ( $this->conn
=== null ) {
44 list( $db, $groups, $wiki ) = $this->params
;
45 $this->conn
= $this->lb
->getConnection( $db, $groups, $wiki );
48 return call_user_func_array( [ $this->conn
, $name ], $arguments );
51 public function getServerInfo() {
52 return $this->__call( __FUNCTION__
, func_get_args() );
55 public function bufferResults( $buffer = null ) {
56 return $this->__call( __FUNCTION__
, func_get_args() );
59 public function trxLevel() {
60 return $this->__call( __FUNCTION__
, func_get_args() );
63 public function trxTimestamp() {
64 return $this->__call( __FUNCTION__
, func_get_args() );
67 public function explicitTrxActive() {
68 return $this->__call( __FUNCTION__
, func_get_args() );
71 public function tablePrefix( $prefix = null ) {
72 return $this->__call( __FUNCTION__
, func_get_args() );
75 public function dbSchema( $schema = null ) {
76 return $this->__call( __FUNCTION__
, func_get_args() );
79 public function getLBInfo( $name = null ) {
80 return $this->__call( __FUNCTION__
, func_get_args() );
83 public function setLBInfo( $name, $value = null ) {
84 return $this->__call( __FUNCTION__
, func_get_args() );
87 public function setLazyMasterHandle( IDatabase
$conn ) {
88 return $this->__call( __FUNCTION__
, func_get_args() );
91 public function implicitGroupby() {
92 return $this->__call( __FUNCTION__
, func_get_args() );
95 public function implicitOrderby() {
96 return $this->__call( __FUNCTION__
, func_get_args() );
99 public function lastQuery() {
100 return $this->__call( __FUNCTION__
, func_get_args() );
103 public function doneWrites() {
104 return $this->__call( __FUNCTION__
, func_get_args() );
107 public function lastDoneWrites() {
108 return $this->__call( __FUNCTION__
, func_get_args() );
111 public function writesPending() {
112 return $this->__call( __FUNCTION__
, func_get_args() );
115 public function writesOrCallbacksPending() {
116 return $this->__call( __FUNCTION__
, func_get_args() );
119 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
120 return $this->__call( __FUNCTION__
, func_get_args() );
123 public function pendingWriteCallers() {
124 return $this->__call( __FUNCTION__
, func_get_args() );
127 public function pendingWriteRowsAffected() {
128 return $this->__call( __FUNCTION__
, func_get_args() );
131 public function isOpen() {
132 return $this->__call( __FUNCTION__
, func_get_args() );
135 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
136 return $this->__call( __FUNCTION__
, func_get_args() );
139 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
140 return $this->__call( __FUNCTION__
, func_get_args() );
143 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
144 return $this->__call( __FUNCTION__
, func_get_args() );
147 public function getFlag( $flag ) {
148 return $this->__call( __FUNCTION__
, func_get_args() );
151 public function getProperty( $name ) {
152 return $this->__call( __FUNCTION__
, func_get_args() );
155 public function getDomainID() {
156 if ( $this->conn
=== null ) {
157 $domain = $this->params
[self
::FLD_DOMAIN
];
158 // Avoid triggering a database connection
159 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
162 return $this->__call( __FUNCTION__
, func_get_args() );
165 public function getWikiID() {
166 return $this->getDomainID();
169 public function getType() {
170 return $this->__call( __FUNCTION__
, func_get_args() );
173 public function open( $server, $user, $password, $dbName ) {
174 return $this->__call( __FUNCTION__
, func_get_args() );
177 public function fetchObject( $res ) {
178 return $this->__call( __FUNCTION__
, func_get_args() );
181 public function fetchRow( $res ) {
182 return $this->__call( __FUNCTION__
, func_get_args() );
185 public function numRows( $res ) {
186 return $this->__call( __FUNCTION__
, func_get_args() );
189 public function numFields( $res ) {
190 return $this->__call( __FUNCTION__
, func_get_args() );
193 public function fieldName( $res, $n ) {
194 return $this->__call( __FUNCTION__
, func_get_args() );
197 public function insertId() {
198 return $this->__call( __FUNCTION__
, func_get_args() );
201 public function dataSeek( $res, $row ) {
202 return $this->__call( __FUNCTION__
, func_get_args() );
205 public function lastErrno() {
206 return $this->__call( __FUNCTION__
, func_get_args() );
209 public function lastError() {
210 return $this->__call( __FUNCTION__
, func_get_args() );
213 public function fieldInfo( $table, $field ) {
214 return $this->__call( __FUNCTION__
, func_get_args() );
217 public function affectedRows() {
218 return $this->__call( __FUNCTION__
, func_get_args() );
221 public function getSoftwareLink() {
222 return $this->__call( __FUNCTION__
, func_get_args() );
225 public function getServerVersion() {
226 return $this->__call( __FUNCTION__
, func_get_args() );
229 public function close() {
230 return $this->__call( __FUNCTION__
, func_get_args() );
233 public function reportConnectionError( $error = 'Unknown error' ) {
234 return $this->__call( __FUNCTION__
, func_get_args() );
237 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
238 return $this->__call( __FUNCTION__
, func_get_args() );
241 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
242 return $this->__call( __FUNCTION__
, func_get_args() );
245 public function freeResult( $res ) {
246 return $this->__call( __FUNCTION__
, func_get_args() );
249 public function selectField(
250 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
252 return $this->__call( __FUNCTION__
, func_get_args() );
255 public function selectFieldValues(
256 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
258 return $this->__call( __FUNCTION__
, func_get_args() );
261 public function select(
262 $table, $vars, $conds = '', $fname = __METHOD__
,
263 $options = [], $join_conds = []
265 return $this->__call( __FUNCTION__
, func_get_args() );
268 public function selectSQLText(
269 $table, $vars, $conds = '', $fname = __METHOD__
,
270 $options = [], $join_conds = []
272 return $this->__call( __FUNCTION__
, func_get_args() );
275 public function selectRow(
276 $table, $vars, $conds, $fname = __METHOD__
,
277 $options = [], $join_conds = []
279 return $this->__call( __FUNCTION__
, func_get_args() );
282 public function estimateRowCount(
283 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
285 return $this->__call( __FUNCTION__
, func_get_args() );
288 public function selectRowCount(
289 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
291 return $this->__call( __FUNCTION__
, func_get_args() );
294 public function fieldExists( $table, $field, $fname = __METHOD__
) {
295 return $this->__call( __FUNCTION__
, func_get_args() );
298 public function indexExists( $table, $index, $fname = __METHOD__
) {
299 return $this->__call( __FUNCTION__
, func_get_args() );
302 public function tableExists( $table, $fname = __METHOD__
) {
303 return $this->__call( __FUNCTION__
, func_get_args() );
306 public function indexUnique( $table, $index ) {
307 return $this->__call( __FUNCTION__
, func_get_args() );
310 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
311 return $this->__call( __FUNCTION__
, func_get_args() );
314 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
315 return $this->__call( __FUNCTION__
, func_get_args() );
318 public function makeList( $a, $mode = self
::LIST_COMMA
) {
319 return $this->__call( __FUNCTION__
, func_get_args() );
322 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
323 return $this->__call( __FUNCTION__
, func_get_args() );
326 public function aggregateValue( $valuedata, $valuename = 'value' ) {
327 return $this->__call( __FUNCTION__
, func_get_args() );
330 public function bitNot( $field ) {
331 return $this->__call( __FUNCTION__
, func_get_args() );
334 public function bitAnd( $fieldLeft, $fieldRight ) {
335 return $this->__call( __FUNCTION__
, func_get_args() );
338 public function bitOr( $fieldLeft, $fieldRight ) {
339 return $this->__call( __FUNCTION__
, func_get_args() );
342 public function buildConcat( $stringList ) {
343 return $this->__call( __FUNCTION__
, func_get_args() );
346 public function buildGroupConcatField(
347 $delim, $table, $field, $conds = '', $join_conds = []
349 return $this->__call( __FUNCTION__
, func_get_args() );
352 public function buildStringCast( $field ) {
353 return $this->__call( __FUNCTION__
, func_get_args() );
356 public function databasesAreIndependent() {
357 return $this->__call( __FUNCTION__
, func_get_args() );
360 public function selectDB( $db ) {
361 return $this->__call( __FUNCTION__
, func_get_args() );
364 public function getDBname() {
365 return $this->__call( __FUNCTION__
, func_get_args() );
368 public function getServer() {
369 return $this->__call( __FUNCTION__
, func_get_args() );
372 public function addQuotes( $s ) {
373 return $this->__call( __FUNCTION__
, func_get_args() );
376 public function buildLike() {
377 return $this->__call( __FUNCTION__
, func_get_args() );
380 public function anyChar() {
381 return $this->__call( __FUNCTION__
, func_get_args() );
384 public function anyString() {
385 return $this->__call( __FUNCTION__
, func_get_args() );
388 public function nextSequenceValue( $seqName ) {
389 return $this->__call( __FUNCTION__
, func_get_args() );
392 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
393 return $this->__call( __FUNCTION__
, func_get_args() );
396 public function upsert(
397 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
399 return $this->__call( __FUNCTION__
, func_get_args() );
402 public function deleteJoin(
403 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
405 return $this->__call( __FUNCTION__
, func_get_args() );
408 public function delete( $table, $conds, $fname = __METHOD__
) {
409 return $this->__call( __FUNCTION__
, func_get_args() );
412 public function insertSelect(
413 $destTable, $srcTable, $varMap, $conds,
414 $fname = __METHOD__
, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
416 return $this->__call( __FUNCTION__
, func_get_args() );
419 public function unionSupportsOrderAndLimit() {
420 return $this->__call( __FUNCTION__
, func_get_args() );
423 public function unionQueries( $sqls, $all ) {
424 return $this->__call( __FUNCTION__
, func_get_args() );
427 public function conditional( $cond, $trueVal, $falseVal ) {
428 return $this->__call( __FUNCTION__
, func_get_args() );
431 public function strreplace( $orig, $old, $new ) {
432 return $this->__call( __FUNCTION__
, func_get_args() );
435 public function getServerUptime() {
436 return $this->__call( __FUNCTION__
, func_get_args() );
439 public function wasDeadlock() {
440 return $this->__call( __FUNCTION__
, func_get_args() );
443 public function wasLockTimeout() {
444 return $this->__call( __FUNCTION__
, func_get_args() );
447 public function wasErrorReissuable() {
448 return $this->__call( __FUNCTION__
, func_get_args() );
451 public function wasReadOnlyError() {
452 return $this->__call( __FUNCTION__
, func_get_args() );
455 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
456 return $this->__call( __FUNCTION__
, func_get_args() );
459 public function getReplicaPos() {
460 return $this->__call( __FUNCTION__
, func_get_args() );
463 public function getMasterPos() {
464 return $this->__call( __FUNCTION__
, func_get_args() );
467 public function serverIsReadOnly() {
468 return $this->__call( __FUNCTION__
, func_get_args() );
471 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
472 return $this->__call( __FUNCTION__
, func_get_args() );
475 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
476 return $this->__call( __FUNCTION__
, func_get_args() );
479 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
480 return $this->__call( __FUNCTION__
, func_get_args() );
483 public function setTransactionListener( $name, callable
$callback = null ) {
484 return $this->__call( __FUNCTION__
, func_get_args() );
487 public function startAtomic( $fname = __METHOD__
) {
488 return $this->__call( __FUNCTION__
, func_get_args() );
491 public function endAtomic( $fname = __METHOD__
) {
492 return $this->__call( __FUNCTION__
, func_get_args() );
495 public function doAtomicSection( $fname, callable
$callback ) {
496 return $this->__call( __FUNCTION__
, func_get_args() );
499 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
500 return $this->__call( __FUNCTION__
, func_get_args() );
503 public function commit( $fname = __METHOD__
, $flush = '' ) {
504 return $this->__call( __FUNCTION__
, func_get_args() );
507 public function rollback( $fname = __METHOD__
, $flush = '' ) {
508 return $this->__call( __FUNCTION__
, func_get_args() );
511 public function flushSnapshot( $fname = __METHOD__
) {
512 return $this->__call( __FUNCTION__
, func_get_args() );
515 public function listTables( $prefix = null, $fname = __METHOD__
) {
516 return $this->__call( __FUNCTION__
, func_get_args() );
519 public function timestamp( $ts = 0 ) {
520 return $this->__call( __FUNCTION__
, func_get_args() );
523 public function timestampOrNull( $ts = null ) {
524 return $this->__call( __FUNCTION__
, func_get_args() );
527 public function ping( &$rtt = null ) {
528 return func_num_args()
529 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
530 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
533 public function getLag() {
534 return $this->__call( __FUNCTION__
, func_get_args() );
537 public function getSessionLagStatus() {
538 return $this->__call( __FUNCTION__
, func_get_args() );
541 public function maxListLen() {
542 return $this->__call( __FUNCTION__
, func_get_args() );
545 public function encodeBlob( $b ) {
546 return $this->__call( __FUNCTION__
, func_get_args() );
549 public function decodeBlob( $b ) {
550 return $this->__call( __FUNCTION__
, func_get_args() );
553 public function setSessionOptions( array $options ) {
554 return $this->__call( __FUNCTION__
, func_get_args() );
557 public function setSchemaVars( $vars ) {
558 return $this->__call( __FUNCTION__
, func_get_args() );
561 public function lockIsFree( $lockName, $method ) {
562 return $this->__call( __FUNCTION__
, func_get_args() );
565 public function lock( $lockName, $method, $timeout = 5 ) {
566 return $this->__call( __FUNCTION__
, func_get_args() );
569 public function unlock( $lockName, $method ) {
570 return $this->__call( __FUNCTION__
, func_get_args() );
573 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
574 return $this->__call( __FUNCTION__
, func_get_args() );
577 public function namedLocksEnqueue() {
578 return $this->__call( __FUNCTION__
, func_get_args() );
581 public function getInfinity() {
582 return $this->__call( __FUNCTION__
, func_get_args() );
585 public function encodeExpiry( $expiry ) {
586 return $this->__call( __FUNCTION__
, func_get_args() );
589 public function decodeExpiry( $expiry, $format = TS_MW
) {
590 return $this->__call( __FUNCTION__
, func_get_args() );
593 public function setBigSelects( $value = true ) {
594 return $this->__call( __FUNCTION__
, func_get_args() );
597 public function isReadOnly() {
598 return $this->__call( __FUNCTION__
, func_get_args() );
601 public function setTableAliases( array $aliases ) {
602 return $this->__call( __FUNCTION__
, func_get_args() );
606 * Clean up the connection when out of scope
608 function __destruct() {
610 $this->lb
->reuseConnection( $this->conn
);
615 class_alias( DBConnRef
::class, 'DBConnRef' );