3 use Wikimedia\Rdbms\DatabaseDomain
;
4 use Wikimedia\Rdbms\ILoadBalancer
;
7 * Helper class to handle automatically marking connections as reusable (via RAII pattern)
8 * as well handling deferring the actual network connection until the handle is used
10 * @note: proxy methods are defined explicity to avoid interface errors
14 class DBConnRef
implements IDatabase
{
15 /** @var ILoadBalancer */
17 /** @var Database|null Live connection handle */
19 /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */
27 * @param ILoadBalancer $lb Connection manager for $conn
28 * @param Database|array $conn New connection handle or (server index, query groups, domain)
30 public function __construct( ILoadBalancer
$lb, $conn ) {
32 if ( $conn instanceof Database
) {
33 $this->conn
= $conn; // live handle
34 } elseif ( count( $conn ) >= 3 && $conn[self
::FLD_DOMAIN
] !== false ) {
35 $this->params
= $conn;
37 throw new InvalidArgumentException( "Missing lazy connection arguments." );
41 function __call( $name, array $arguments ) {
42 if ( $this->conn
=== null ) {
43 list( $db, $groups, $wiki ) = $this->params
;
44 $this->conn
= $this->lb
->getConnection( $db, $groups, $wiki );
47 return call_user_func_array( [ $this->conn
, $name ], $arguments );
50 public function getServerInfo() {
51 return $this->__call( __FUNCTION__
, func_get_args() );
54 public function bufferResults( $buffer = null ) {
55 return $this->__call( __FUNCTION__
, func_get_args() );
58 public function trxLevel() {
59 return $this->__call( __FUNCTION__
, func_get_args() );
62 public function trxTimestamp() {
63 return $this->__call( __FUNCTION__
, func_get_args() );
66 public function explicitTrxActive() {
67 return $this->__call( __FUNCTION__
, func_get_args() );
70 public function tablePrefix( $prefix = null ) {
71 return $this->__call( __FUNCTION__
, func_get_args() );
74 public function dbSchema( $schema = null ) {
75 return $this->__call( __FUNCTION__
, func_get_args() );
78 public function getLBInfo( $name = null ) {
79 return $this->__call( __FUNCTION__
, func_get_args() );
82 public function setLBInfo( $name, $value = null ) {
83 return $this->__call( __FUNCTION__
, func_get_args() );
86 public function setLazyMasterHandle( IDatabase
$conn ) {
87 return $this->__call( __FUNCTION__
, func_get_args() );
90 public function implicitGroupby() {
91 return $this->__call( __FUNCTION__
, func_get_args() );
94 public function implicitOrderby() {
95 return $this->__call( __FUNCTION__
, func_get_args() );
98 public function lastQuery() {
99 return $this->__call( __FUNCTION__
, func_get_args() );
102 public function doneWrites() {
103 return $this->__call( __FUNCTION__
, func_get_args() );
106 public function lastDoneWrites() {
107 return $this->__call( __FUNCTION__
, func_get_args() );
110 public function writesPending() {
111 return $this->__call( __FUNCTION__
, func_get_args() );
114 public function writesOrCallbacksPending() {
115 return $this->__call( __FUNCTION__
, func_get_args() );
118 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
119 return $this->__call( __FUNCTION__
, func_get_args() );
122 public function pendingWriteCallers() {
123 return $this->__call( __FUNCTION__
, func_get_args() );
126 public function isOpen() {
127 return $this->__call( __FUNCTION__
, func_get_args() );
130 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
131 return $this->__call( __FUNCTION__
, func_get_args() );
134 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
135 return $this->__call( __FUNCTION__
, func_get_args() );
138 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
139 return $this->__call( __FUNCTION__
, func_get_args() );
142 public function getFlag( $flag ) {
143 return $this->__call( __FUNCTION__
, func_get_args() );
146 public function getProperty( $name ) {
147 return $this->__call( __FUNCTION__
, func_get_args() );
150 public function getDomainID() {
151 if ( $this->conn
=== null ) {
152 $domain = $this->params
[self
::FLD_DOMAIN
];
153 // Avoid triggering a database connection
154 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
157 return $this->__call( __FUNCTION__
, func_get_args() );
160 public function getWikiID() {
161 return $this->getDomainID();
164 public function getType() {
165 return $this->__call( __FUNCTION__
, func_get_args() );
168 public function open( $server, $user, $password, $dbName ) {
169 return $this->__call( __FUNCTION__
, func_get_args() );
172 public function fetchObject( $res ) {
173 return $this->__call( __FUNCTION__
, func_get_args() );
176 public function fetchRow( $res ) {
177 return $this->__call( __FUNCTION__
, func_get_args() );
180 public function numRows( $res ) {
181 return $this->__call( __FUNCTION__
, func_get_args() );
184 public function numFields( $res ) {
185 return $this->__call( __FUNCTION__
, func_get_args() );
188 public function fieldName( $res, $n ) {
189 return $this->__call( __FUNCTION__
, func_get_args() );
192 public function insertId() {
193 return $this->__call( __FUNCTION__
, func_get_args() );
196 public function dataSeek( $res, $row ) {
197 return $this->__call( __FUNCTION__
, func_get_args() );
200 public function lastErrno() {
201 return $this->__call( __FUNCTION__
, func_get_args() );
204 public function lastError() {
205 return $this->__call( __FUNCTION__
, func_get_args() );
208 public function fieldInfo( $table, $field ) {
209 return $this->__call( __FUNCTION__
, func_get_args() );
212 public function affectedRows() {
213 return $this->__call( __FUNCTION__
, func_get_args() );
216 public function getSoftwareLink() {
217 return $this->__call( __FUNCTION__
, func_get_args() );
220 public function getServerVersion() {
221 return $this->__call( __FUNCTION__
, func_get_args() );
224 public function close() {
225 return $this->__call( __FUNCTION__
, func_get_args() );
228 public function reportConnectionError( $error = 'Unknown error' ) {
229 return $this->__call( __FUNCTION__
, func_get_args() );
232 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
233 return $this->__call( __FUNCTION__
, func_get_args() );
236 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
237 return $this->__call( __FUNCTION__
, func_get_args() );
240 public function freeResult( $res ) {
241 return $this->__call( __FUNCTION__
, func_get_args() );
244 public function selectField(
245 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
247 return $this->__call( __FUNCTION__
, func_get_args() );
250 public function selectFieldValues(
251 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
253 return $this->__call( __FUNCTION__
, func_get_args() );
256 public function select(
257 $table, $vars, $conds = '', $fname = __METHOD__
,
258 $options = [], $join_conds = []
260 return $this->__call( __FUNCTION__
, func_get_args() );
263 public function selectSQLText(
264 $table, $vars, $conds = '', $fname = __METHOD__
,
265 $options = [], $join_conds = []
267 return $this->__call( __FUNCTION__
, func_get_args() );
270 public function selectRow(
271 $table, $vars, $conds, $fname = __METHOD__
,
272 $options = [], $join_conds = []
274 return $this->__call( __FUNCTION__
, func_get_args() );
277 public function estimateRowCount(
278 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
280 return $this->__call( __FUNCTION__
, func_get_args() );
283 public function selectRowCount(
284 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
286 return $this->__call( __FUNCTION__
, func_get_args() );
289 public function fieldExists( $table, $field, $fname = __METHOD__
) {
290 return $this->__call( __FUNCTION__
, func_get_args() );
293 public function indexExists( $table, $index, $fname = __METHOD__
) {
294 return $this->__call( __FUNCTION__
, func_get_args() );
297 public function tableExists( $table, $fname = __METHOD__
) {
298 return $this->__call( __FUNCTION__
, func_get_args() );
301 public function indexUnique( $table, $index ) {
302 return $this->__call( __FUNCTION__
, func_get_args() );
305 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
306 return $this->__call( __FUNCTION__
, func_get_args() );
309 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
310 return $this->__call( __FUNCTION__
, func_get_args() );
313 public function makeList( $a, $mode = self
::LIST_COMMA
) {
314 return $this->__call( __FUNCTION__
, func_get_args() );
317 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
318 return $this->__call( __FUNCTION__
, func_get_args() );
321 public function aggregateValue( $valuedata, $valuename = 'value' ) {
322 return $this->__call( __FUNCTION__
, func_get_args() );
325 public function bitNot( $field ) {
326 return $this->__call( __FUNCTION__
, func_get_args() );
329 public function bitAnd( $fieldLeft, $fieldRight ) {
330 return $this->__call( __FUNCTION__
, func_get_args() );
333 public function bitOr( $fieldLeft, $fieldRight ) {
334 return $this->__call( __FUNCTION__
, func_get_args() );
337 public function buildConcat( $stringList ) {
338 return $this->__call( __FUNCTION__
, func_get_args() );
341 public function buildGroupConcatField(
342 $delim, $table, $field, $conds = '', $join_conds = []
344 return $this->__call( __FUNCTION__
, func_get_args() );
347 public function buildStringCast( $field ) {
348 return $this->__call( __FUNCTION__
, func_get_args() );
351 public function selectDB( $db ) {
352 return $this->__call( __FUNCTION__
, func_get_args() );
355 public function getDBname() {
356 return $this->__call( __FUNCTION__
, func_get_args() );
359 public function getServer() {
360 return $this->__call( __FUNCTION__
, func_get_args() );
363 public function addQuotes( $s ) {
364 return $this->__call( __FUNCTION__
, func_get_args() );
367 public function buildLike() {
368 return $this->__call( __FUNCTION__
, func_get_args() );
371 public function anyChar() {
372 return $this->__call( __FUNCTION__
, func_get_args() );
375 public function anyString() {
376 return $this->__call( __FUNCTION__
, func_get_args() );
379 public function nextSequenceValue( $seqName ) {
380 return $this->__call( __FUNCTION__
, func_get_args() );
383 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
384 return $this->__call( __FUNCTION__
, func_get_args() );
387 public function upsert(
388 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
390 return $this->__call( __FUNCTION__
, func_get_args() );
393 public function deleteJoin(
394 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
396 return $this->__call( __FUNCTION__
, func_get_args() );
399 public function delete( $table, $conds, $fname = __METHOD__
) {
400 return $this->__call( __FUNCTION__
, func_get_args() );
403 public function insertSelect(
404 $destTable, $srcTable, $varMap, $conds,
405 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
407 return $this->__call( __FUNCTION__
, func_get_args() );
410 public function unionSupportsOrderAndLimit() {
411 return $this->__call( __FUNCTION__
, func_get_args() );
414 public function unionQueries( $sqls, $all ) {
415 return $this->__call( __FUNCTION__
, func_get_args() );
418 public function conditional( $cond, $trueVal, $falseVal ) {
419 return $this->__call( __FUNCTION__
, func_get_args() );
422 public function strreplace( $orig, $old, $new ) {
423 return $this->__call( __FUNCTION__
, func_get_args() );
426 public function getServerUptime() {
427 return $this->__call( __FUNCTION__
, func_get_args() );
430 public function wasDeadlock() {
431 return $this->__call( __FUNCTION__
, func_get_args() );
434 public function wasLockTimeout() {
435 return $this->__call( __FUNCTION__
, func_get_args() );
438 public function wasErrorReissuable() {
439 return $this->__call( __FUNCTION__
, func_get_args() );
442 public function wasReadOnlyError() {
443 return $this->__call( __FUNCTION__
, func_get_args() );
446 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
447 return $this->__call( __FUNCTION__
, func_get_args() );
450 public function getReplicaPos() {
451 return $this->__call( __FUNCTION__
, func_get_args() );
454 public function getMasterPos() {
455 return $this->__call( __FUNCTION__
, func_get_args() );
458 public function serverIsReadOnly() {
459 return $this->__call( __FUNCTION__
, func_get_args() );
462 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
463 return $this->__call( __FUNCTION__
, func_get_args() );
466 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
467 return $this->__call( __FUNCTION__
, func_get_args() );
470 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
471 return $this->__call( __FUNCTION__
, func_get_args() );
474 public function setTransactionListener( $name, callable
$callback = null ) {
475 return $this->__call( __FUNCTION__
, func_get_args() );
478 public function startAtomic( $fname = __METHOD__
) {
479 return $this->__call( __FUNCTION__
, func_get_args() );
482 public function endAtomic( $fname = __METHOD__
) {
483 return $this->__call( __FUNCTION__
, func_get_args() );
486 public function doAtomicSection( $fname, callable
$callback ) {
487 return $this->__call( __FUNCTION__
, func_get_args() );
490 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
491 return $this->__call( __FUNCTION__
, func_get_args() );
494 public function commit( $fname = __METHOD__
, $flush = '' ) {
495 return $this->__call( __FUNCTION__
, func_get_args() );
498 public function rollback( $fname = __METHOD__
, $flush = '' ) {
499 return $this->__call( __FUNCTION__
, func_get_args() );
502 public function flushSnapshot( $fname = __METHOD__
) {
503 return $this->__call( __FUNCTION__
, func_get_args() );
506 public function listTables( $prefix = null, $fname = __METHOD__
) {
507 return $this->__call( __FUNCTION__
, func_get_args() );
510 public function timestamp( $ts = 0 ) {
511 return $this->__call( __FUNCTION__
, func_get_args() );
514 public function timestampOrNull( $ts = null ) {
515 return $this->__call( __FUNCTION__
, func_get_args() );
518 public function ping( &$rtt = null ) {
519 return func_num_args()
520 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
521 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
524 public function getLag() {
525 return $this->__call( __FUNCTION__
, func_get_args() );
528 public function getSessionLagStatus() {
529 return $this->__call( __FUNCTION__
, func_get_args() );
532 public function maxListLen() {
533 return $this->__call( __FUNCTION__
, func_get_args() );
536 public function encodeBlob( $b ) {
537 return $this->__call( __FUNCTION__
, func_get_args() );
540 public function decodeBlob( $b ) {
541 return $this->__call( __FUNCTION__
, func_get_args() );
544 public function setSessionOptions( array $options ) {
545 return $this->__call( __FUNCTION__
, func_get_args() );
548 public function setSchemaVars( $vars ) {
549 return $this->__call( __FUNCTION__
, func_get_args() );
552 public function lockIsFree( $lockName, $method ) {
553 return $this->__call( __FUNCTION__
, func_get_args() );
556 public function lock( $lockName, $method, $timeout = 5 ) {
557 return $this->__call( __FUNCTION__
, func_get_args() );
560 public function unlock( $lockName, $method ) {
561 return $this->__call( __FUNCTION__
, func_get_args() );
564 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
565 return $this->__call( __FUNCTION__
, func_get_args() );
568 public function namedLocksEnqueue() {
569 return $this->__call( __FUNCTION__
, func_get_args() );
572 public function getInfinity() {
573 return $this->__call( __FUNCTION__
, func_get_args() );
576 public function encodeExpiry( $expiry ) {
577 return $this->__call( __FUNCTION__
, func_get_args() );
580 public function decodeExpiry( $expiry, $format = TS_MW
) {
581 return $this->__call( __FUNCTION__
, func_get_args() );
584 public function setBigSelects( $value = true ) {
585 return $this->__call( __FUNCTION__
, func_get_args() );
588 public function isReadOnly() {
589 return $this->__call( __FUNCTION__
, func_get_args() );
592 public function setTableAliases( array $aliases ) {
593 return $this->__call( __FUNCTION__
, func_get_args() );
597 * Clean up the connection when out of scope
599 function __destruct() {
601 $this->lb
->reuseConnection( $this->conn
);