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 isOpen() {
128 return $this->__call( __FUNCTION__
, func_get_args() );
131 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
132 return $this->__call( __FUNCTION__
, func_get_args() );
135 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
136 return $this->__call( __FUNCTION__
, func_get_args() );
139 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
140 return $this->__call( __FUNCTION__
, func_get_args() );
143 public function getFlag( $flag ) {
144 return $this->__call( __FUNCTION__
, func_get_args() );
147 public function getProperty( $name ) {
148 return $this->__call( __FUNCTION__
, func_get_args() );
151 public function getDomainID() {
152 if ( $this->conn
=== null ) {
153 $domain = $this->params
[self
::FLD_DOMAIN
];
154 // Avoid triggering a database connection
155 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
158 return $this->__call( __FUNCTION__
, func_get_args() );
161 public function getWikiID() {
162 return $this->getDomainID();
165 public function getType() {
166 return $this->__call( __FUNCTION__
, func_get_args() );
169 public function open( $server, $user, $password, $dbName ) {
170 return $this->__call( __FUNCTION__
, func_get_args() );
173 public function fetchObject( $res ) {
174 return $this->__call( __FUNCTION__
, func_get_args() );
177 public function fetchRow( $res ) {
178 return $this->__call( __FUNCTION__
, func_get_args() );
181 public function numRows( $res ) {
182 return $this->__call( __FUNCTION__
, func_get_args() );
185 public function numFields( $res ) {
186 return $this->__call( __FUNCTION__
, func_get_args() );
189 public function fieldName( $res, $n ) {
190 return $this->__call( __FUNCTION__
, func_get_args() );
193 public function insertId() {
194 return $this->__call( __FUNCTION__
, func_get_args() );
197 public function dataSeek( $res, $row ) {
198 return $this->__call( __FUNCTION__
, func_get_args() );
201 public function lastErrno() {
202 return $this->__call( __FUNCTION__
, func_get_args() );
205 public function lastError() {
206 return $this->__call( __FUNCTION__
, func_get_args() );
209 public function fieldInfo( $table, $field ) {
210 return $this->__call( __FUNCTION__
, func_get_args() );
213 public function affectedRows() {
214 return $this->__call( __FUNCTION__
, func_get_args() );
217 public function getSoftwareLink() {
218 return $this->__call( __FUNCTION__
, func_get_args() );
221 public function getServerVersion() {
222 return $this->__call( __FUNCTION__
, func_get_args() );
225 public function close() {
226 return $this->__call( __FUNCTION__
, func_get_args() );
229 public function reportConnectionError( $error = 'Unknown error' ) {
230 return $this->__call( __FUNCTION__
, func_get_args() );
233 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
234 return $this->__call( __FUNCTION__
, func_get_args() );
237 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
238 return $this->__call( __FUNCTION__
, func_get_args() );
241 public function freeResult( $res ) {
242 return $this->__call( __FUNCTION__
, func_get_args() );
245 public function selectField(
246 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
248 return $this->__call( __FUNCTION__
, func_get_args() );
251 public function selectFieldValues(
252 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
254 return $this->__call( __FUNCTION__
, func_get_args() );
257 public function select(
258 $table, $vars, $conds = '', $fname = __METHOD__
,
259 $options = [], $join_conds = []
261 return $this->__call( __FUNCTION__
, func_get_args() );
264 public function selectSQLText(
265 $table, $vars, $conds = '', $fname = __METHOD__
,
266 $options = [], $join_conds = []
268 return $this->__call( __FUNCTION__
, func_get_args() );
271 public function selectRow(
272 $table, $vars, $conds, $fname = __METHOD__
,
273 $options = [], $join_conds = []
275 return $this->__call( __FUNCTION__
, func_get_args() );
278 public function estimateRowCount(
279 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
281 return $this->__call( __FUNCTION__
, func_get_args() );
284 public function selectRowCount(
285 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
287 return $this->__call( __FUNCTION__
, func_get_args() );
290 public function fieldExists( $table, $field, $fname = __METHOD__
) {
291 return $this->__call( __FUNCTION__
, func_get_args() );
294 public function indexExists( $table, $index, $fname = __METHOD__
) {
295 return $this->__call( __FUNCTION__
, func_get_args() );
298 public function tableExists( $table, $fname = __METHOD__
) {
299 return $this->__call( __FUNCTION__
, func_get_args() );
302 public function indexUnique( $table, $index ) {
303 return $this->__call( __FUNCTION__
, func_get_args() );
306 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
307 return $this->__call( __FUNCTION__
, func_get_args() );
310 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
311 return $this->__call( __FUNCTION__
, func_get_args() );
314 public function makeList( $a, $mode = self
::LIST_COMMA
) {
315 return $this->__call( __FUNCTION__
, func_get_args() );
318 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
319 return $this->__call( __FUNCTION__
, func_get_args() );
322 public function aggregateValue( $valuedata, $valuename = 'value' ) {
323 return $this->__call( __FUNCTION__
, func_get_args() );
326 public function bitNot( $field ) {
327 return $this->__call( __FUNCTION__
, func_get_args() );
330 public function bitAnd( $fieldLeft, $fieldRight ) {
331 return $this->__call( __FUNCTION__
, func_get_args() );
334 public function bitOr( $fieldLeft, $fieldRight ) {
335 return $this->__call( __FUNCTION__
, func_get_args() );
338 public function buildConcat( $stringList ) {
339 return $this->__call( __FUNCTION__
, func_get_args() );
342 public function buildGroupConcatField(
343 $delim, $table, $field, $conds = '', $join_conds = []
345 return $this->__call( __FUNCTION__
, func_get_args() );
348 public function buildStringCast( $field ) {
349 return $this->__call( __FUNCTION__
, func_get_args() );
352 public function databasesAreIndependent() {
353 return $this->__call( __FUNCTION__
, func_get_args() );
356 public function selectDB( $db ) {
357 return $this->__call( __FUNCTION__
, func_get_args() );
360 public function getDBname() {
361 return $this->__call( __FUNCTION__
, func_get_args() );
364 public function getServer() {
365 return $this->__call( __FUNCTION__
, func_get_args() );
368 public function addQuotes( $s ) {
369 return $this->__call( __FUNCTION__
, func_get_args() );
372 public function buildLike() {
373 return $this->__call( __FUNCTION__
, func_get_args() );
376 public function anyChar() {
377 return $this->__call( __FUNCTION__
, func_get_args() );
380 public function anyString() {
381 return $this->__call( __FUNCTION__
, func_get_args() );
384 public function nextSequenceValue( $seqName ) {
385 return $this->__call( __FUNCTION__
, func_get_args() );
388 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
389 return $this->__call( __FUNCTION__
, func_get_args() );
392 public function upsert(
393 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
395 return $this->__call( __FUNCTION__
, func_get_args() );
398 public function deleteJoin(
399 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
401 return $this->__call( __FUNCTION__
, func_get_args() );
404 public function delete( $table, $conds, $fname = __METHOD__
) {
405 return $this->__call( __FUNCTION__
, func_get_args() );
408 public function insertSelect(
409 $destTable, $srcTable, $varMap, $conds,
410 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
412 return $this->__call( __FUNCTION__
, func_get_args() );
415 public function unionSupportsOrderAndLimit() {
416 return $this->__call( __FUNCTION__
, func_get_args() );
419 public function unionQueries( $sqls, $all ) {
420 return $this->__call( __FUNCTION__
, func_get_args() );
423 public function conditional( $cond, $trueVal, $falseVal ) {
424 return $this->__call( __FUNCTION__
, func_get_args() );
427 public function strreplace( $orig, $old, $new ) {
428 return $this->__call( __FUNCTION__
, func_get_args() );
431 public function getServerUptime() {
432 return $this->__call( __FUNCTION__
, func_get_args() );
435 public function wasDeadlock() {
436 return $this->__call( __FUNCTION__
, func_get_args() );
439 public function wasLockTimeout() {
440 return $this->__call( __FUNCTION__
, func_get_args() );
443 public function wasErrorReissuable() {
444 return $this->__call( __FUNCTION__
, func_get_args() );
447 public function wasReadOnlyError() {
448 return $this->__call( __FUNCTION__
, func_get_args() );
451 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
452 return $this->__call( __FUNCTION__
, func_get_args() );
455 public function getReplicaPos() {
456 return $this->__call( __FUNCTION__
, func_get_args() );
459 public function getMasterPos() {
460 return $this->__call( __FUNCTION__
, func_get_args() );
463 public function serverIsReadOnly() {
464 return $this->__call( __FUNCTION__
, func_get_args() );
467 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
468 return $this->__call( __FUNCTION__
, func_get_args() );
471 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
472 return $this->__call( __FUNCTION__
, func_get_args() );
475 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
476 return $this->__call( __FUNCTION__
, func_get_args() );
479 public function setTransactionListener( $name, callable
$callback = null ) {
480 return $this->__call( __FUNCTION__
, func_get_args() );
483 public function startAtomic( $fname = __METHOD__
) {
484 return $this->__call( __FUNCTION__
, func_get_args() );
487 public function endAtomic( $fname = __METHOD__
) {
488 return $this->__call( __FUNCTION__
, func_get_args() );
491 public function doAtomicSection( $fname, callable
$callback ) {
492 return $this->__call( __FUNCTION__
, func_get_args() );
495 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
496 return $this->__call( __FUNCTION__
, func_get_args() );
499 public function commit( $fname = __METHOD__
, $flush = '' ) {
500 return $this->__call( __FUNCTION__
, func_get_args() );
503 public function rollback( $fname = __METHOD__
, $flush = '' ) {
504 return $this->__call( __FUNCTION__
, func_get_args() );
507 public function flushSnapshot( $fname = __METHOD__
) {
508 return $this->__call( __FUNCTION__
, func_get_args() );
511 public function listTables( $prefix = null, $fname = __METHOD__
) {
512 return $this->__call( __FUNCTION__
, func_get_args() );
515 public function timestamp( $ts = 0 ) {
516 return $this->__call( __FUNCTION__
, func_get_args() );
519 public function timestampOrNull( $ts = null ) {
520 return $this->__call( __FUNCTION__
, func_get_args() );
523 public function ping( &$rtt = null ) {
524 return func_num_args()
525 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
526 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
529 public function getLag() {
530 return $this->__call( __FUNCTION__
, func_get_args() );
533 public function getSessionLagStatus() {
534 return $this->__call( __FUNCTION__
, func_get_args() );
537 public function maxListLen() {
538 return $this->__call( __FUNCTION__
, func_get_args() );
541 public function encodeBlob( $b ) {
542 return $this->__call( __FUNCTION__
, func_get_args() );
545 public function decodeBlob( $b ) {
546 return $this->__call( __FUNCTION__
, func_get_args() );
549 public function setSessionOptions( array $options ) {
550 return $this->__call( __FUNCTION__
, func_get_args() );
553 public function setSchemaVars( $vars ) {
554 return $this->__call( __FUNCTION__
, func_get_args() );
557 public function lockIsFree( $lockName, $method ) {
558 return $this->__call( __FUNCTION__
, func_get_args() );
561 public function lock( $lockName, $method, $timeout = 5 ) {
562 return $this->__call( __FUNCTION__
, func_get_args() );
565 public function unlock( $lockName, $method ) {
566 return $this->__call( __FUNCTION__
, func_get_args() );
569 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
570 return $this->__call( __FUNCTION__
, func_get_args() );
573 public function namedLocksEnqueue() {
574 return $this->__call( __FUNCTION__
, func_get_args() );
577 public function getInfinity() {
578 return $this->__call( __FUNCTION__
, func_get_args() );
581 public function encodeExpiry( $expiry ) {
582 return $this->__call( __FUNCTION__
, func_get_args() );
585 public function decodeExpiry( $expiry, $format = TS_MW
) {
586 return $this->__call( __FUNCTION__
, func_get_args() );
589 public function setBigSelects( $value = true ) {
590 return $this->__call( __FUNCTION__
, func_get_args() );
593 public function isReadOnly() {
594 return $this->__call( __FUNCTION__
, func_get_args() );
597 public function setTableAliases( array $aliases ) {
598 return $this->__call( __FUNCTION__
, func_get_args() );
602 * Clean up the connection when out of scope
604 function __destruct() {
606 $this->lb
->reuseConnection( $this->conn
);
611 class_alias( DBConnRef
::class, 'DBConnRef' );