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 unionConditionPermutations(
428 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__
,
429 $options = [], $join_conds = []
431 return $this->__call( __FUNCTION__
, func_get_args() );
434 public function conditional( $cond, $trueVal, $falseVal ) {
435 return $this->__call( __FUNCTION__
, func_get_args() );
438 public function strreplace( $orig, $old, $new ) {
439 return $this->__call( __FUNCTION__
, func_get_args() );
442 public function getServerUptime() {
443 return $this->__call( __FUNCTION__
, func_get_args() );
446 public function wasDeadlock() {
447 return $this->__call( __FUNCTION__
, func_get_args() );
450 public function wasLockTimeout() {
451 return $this->__call( __FUNCTION__
, func_get_args() );
454 public function wasErrorReissuable() {
455 return $this->__call( __FUNCTION__
, func_get_args() );
458 public function wasReadOnlyError() {
459 return $this->__call( __FUNCTION__
, func_get_args() );
462 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
463 return $this->__call( __FUNCTION__
, func_get_args() );
466 public function getReplicaPos() {
467 return $this->__call( __FUNCTION__
, func_get_args() );
470 public function getMasterPos() {
471 return $this->__call( __FUNCTION__
, func_get_args() );
474 public function serverIsReadOnly() {
475 return $this->__call( __FUNCTION__
, func_get_args() );
478 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
479 return $this->__call( __FUNCTION__
, func_get_args() );
482 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
483 return $this->__call( __FUNCTION__
, func_get_args() );
486 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
487 return $this->__call( __FUNCTION__
, func_get_args() );
490 public function setTransactionListener( $name, callable
$callback = null ) {
491 return $this->__call( __FUNCTION__
, func_get_args() );
494 public function startAtomic( $fname = __METHOD__
) {
495 return $this->__call( __FUNCTION__
, func_get_args() );
498 public function endAtomic( $fname = __METHOD__
) {
499 return $this->__call( __FUNCTION__
, func_get_args() );
502 public function doAtomicSection( $fname, callable
$callback ) {
503 return $this->__call( __FUNCTION__
, func_get_args() );
506 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
507 return $this->__call( __FUNCTION__
, func_get_args() );
510 public function commit( $fname = __METHOD__
, $flush = '' ) {
511 return $this->__call( __FUNCTION__
, func_get_args() );
514 public function rollback( $fname = __METHOD__
, $flush = '' ) {
515 return $this->__call( __FUNCTION__
, func_get_args() );
518 public function flushSnapshot( $fname = __METHOD__
) {
519 return $this->__call( __FUNCTION__
, func_get_args() );
522 public function listTables( $prefix = null, $fname = __METHOD__
) {
523 return $this->__call( __FUNCTION__
, func_get_args() );
526 public function timestamp( $ts = 0 ) {
527 return $this->__call( __FUNCTION__
, func_get_args() );
530 public function timestampOrNull( $ts = null ) {
531 return $this->__call( __FUNCTION__
, func_get_args() );
534 public function ping( &$rtt = null ) {
535 return func_num_args()
536 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
537 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
540 public function getLag() {
541 return $this->__call( __FUNCTION__
, func_get_args() );
544 public function getSessionLagStatus() {
545 return $this->__call( __FUNCTION__
, func_get_args() );
548 public function maxListLen() {
549 return $this->__call( __FUNCTION__
, func_get_args() );
552 public function encodeBlob( $b ) {
553 return $this->__call( __FUNCTION__
, func_get_args() );
556 public function decodeBlob( $b ) {
557 return $this->__call( __FUNCTION__
, func_get_args() );
560 public function setSessionOptions( array $options ) {
561 return $this->__call( __FUNCTION__
, func_get_args() );
564 public function setSchemaVars( $vars ) {
565 return $this->__call( __FUNCTION__
, func_get_args() );
568 public function lockIsFree( $lockName, $method ) {
569 return $this->__call( __FUNCTION__
, func_get_args() );
572 public function lock( $lockName, $method, $timeout = 5 ) {
573 return $this->__call( __FUNCTION__
, func_get_args() );
576 public function unlock( $lockName, $method ) {
577 return $this->__call( __FUNCTION__
, func_get_args() );
580 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
581 return $this->__call( __FUNCTION__
, func_get_args() );
584 public function namedLocksEnqueue() {
585 return $this->__call( __FUNCTION__
, func_get_args() );
588 public function getInfinity() {
589 return $this->__call( __FUNCTION__
, func_get_args() );
592 public function encodeExpiry( $expiry ) {
593 return $this->__call( __FUNCTION__
, func_get_args() );
596 public function decodeExpiry( $expiry, $format = TS_MW
) {
597 return $this->__call( __FUNCTION__
, func_get_args() );
600 public function setBigSelects( $value = true ) {
601 return $this->__call( __FUNCTION__
, func_get_args() );
604 public function isReadOnly() {
605 return $this->__call( __FUNCTION__
, func_get_args() );
608 public function setTableAliases( array $aliases ) {
609 return $this->__call( __FUNCTION__
, func_get_args() );
613 * Clean up the connection when out of scope
615 function __destruct() {
617 $this->lb
->reuseConnection( $this->conn
);
622 class_alias( DBConnRef
::class, 'DBConnRef' );