3 use Wikimedia\Rdbms\DatabaseDomain
;
6 * Helper class to handle automatically marking connections as reusable (via RAII pattern)
7 * as well handling deferring the actual network connection until the handle is used
9 * @note: proxy methods are defined explicity to avoid interface errors
13 class DBConnRef
implements IDatabase
{
14 /** @var ILoadBalancer */
16 /** @var Database|null Live connection handle */
18 /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */
26 * @param ILoadBalancer $lb Connection manager for $conn
27 * @param Database|array $conn New connection handle or (server index, query groups, domain)
29 public function __construct( ILoadBalancer
$lb, $conn ) {
31 if ( $conn instanceof Database
) {
32 $this->conn
= $conn; // live handle
33 } elseif ( count( $conn ) >= 3 && $conn[self
::FLD_DOMAIN
] !== false ) {
34 $this->params
= $conn;
36 throw new InvalidArgumentException( "Missing lazy connection arguments." );
40 function __call( $name, array $arguments ) {
41 if ( $this->conn
=== null ) {
42 list( $db, $groups, $wiki ) = $this->params
;
43 $this->conn
= $this->lb
->getConnection( $db, $groups, $wiki );
46 return call_user_func_array( [ $this->conn
, $name ], $arguments );
49 public function getServerInfo() {
50 return $this->__call( __FUNCTION__
, func_get_args() );
53 public function bufferResults( $buffer = null ) {
54 return $this->__call( __FUNCTION__
, func_get_args() );
57 public function trxLevel() {
58 return $this->__call( __FUNCTION__
, func_get_args() );
61 public function trxTimestamp() {
62 return $this->__call( __FUNCTION__
, func_get_args() );
65 public function explicitTrxActive() {
66 return $this->__call( __FUNCTION__
, func_get_args() );
69 public function tablePrefix( $prefix = null ) {
70 return $this->__call( __FUNCTION__
, func_get_args() );
73 public function dbSchema( $schema = null ) {
74 return $this->__call( __FUNCTION__
, func_get_args() );
77 public function getLBInfo( $name = null ) {
78 return $this->__call( __FUNCTION__
, func_get_args() );
81 public function setLBInfo( $name, $value = null ) {
82 return $this->__call( __FUNCTION__
, func_get_args() );
85 public function setLazyMasterHandle( IDatabase
$conn ) {
86 return $this->__call( __FUNCTION__
, func_get_args() );
89 public function implicitGroupby() {
90 return $this->__call( __FUNCTION__
, func_get_args() );
93 public function implicitOrderby() {
94 return $this->__call( __FUNCTION__
, func_get_args() );
97 public function lastQuery() {
98 return $this->__call( __FUNCTION__
, func_get_args() );
101 public function doneWrites() {
102 return $this->__call( __FUNCTION__
, func_get_args() );
105 public function lastDoneWrites() {
106 return $this->__call( __FUNCTION__
, func_get_args() );
109 public function writesPending() {
110 return $this->__call( __FUNCTION__
, func_get_args() );
113 public function writesOrCallbacksPending() {
114 return $this->__call( __FUNCTION__
, func_get_args() );
117 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
118 return $this->__call( __FUNCTION__
, func_get_args() );
121 public function pendingWriteCallers() {
122 return $this->__call( __FUNCTION__
, func_get_args() );
125 public function isOpen() {
126 return $this->__call( __FUNCTION__
, func_get_args() );
129 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
130 return $this->__call( __FUNCTION__
, func_get_args() );
133 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
134 return $this->__call( __FUNCTION__
, func_get_args() );
137 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
138 return $this->__call( __FUNCTION__
, func_get_args() );
141 public function getFlag( $flag ) {
142 return $this->__call( __FUNCTION__
, func_get_args() );
145 public function getProperty( $name ) {
146 return $this->__call( __FUNCTION__
, func_get_args() );
149 public function getDomainID() {
150 if ( $this->conn
=== null ) {
151 $domain = $this->params
[self
::FLD_DOMAIN
];
152 // Avoid triggering a database connection
153 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
156 return $this->__call( __FUNCTION__
, func_get_args() );
159 public function getWikiID() {
160 return $this->getDomainID();
163 public function getType() {
164 return $this->__call( __FUNCTION__
, func_get_args() );
167 public function open( $server, $user, $password, $dbName ) {
168 return $this->__call( __FUNCTION__
, func_get_args() );
171 public function fetchObject( $res ) {
172 return $this->__call( __FUNCTION__
, func_get_args() );
175 public function fetchRow( $res ) {
176 return $this->__call( __FUNCTION__
, func_get_args() );
179 public function numRows( $res ) {
180 return $this->__call( __FUNCTION__
, func_get_args() );
183 public function numFields( $res ) {
184 return $this->__call( __FUNCTION__
, func_get_args() );
187 public function fieldName( $res, $n ) {
188 return $this->__call( __FUNCTION__
, func_get_args() );
191 public function insertId() {
192 return $this->__call( __FUNCTION__
, func_get_args() );
195 public function dataSeek( $res, $row ) {
196 return $this->__call( __FUNCTION__
, func_get_args() );
199 public function lastErrno() {
200 return $this->__call( __FUNCTION__
, func_get_args() );
203 public function lastError() {
204 return $this->__call( __FUNCTION__
, func_get_args() );
207 public function fieldInfo( $table, $field ) {
208 return $this->__call( __FUNCTION__
, func_get_args() );
211 public function affectedRows() {
212 return $this->__call( __FUNCTION__
, func_get_args() );
215 public function getSoftwareLink() {
216 return $this->__call( __FUNCTION__
, func_get_args() );
219 public function getServerVersion() {
220 return $this->__call( __FUNCTION__
, func_get_args() );
223 public function close() {
224 return $this->__call( __FUNCTION__
, func_get_args() );
227 public function reportConnectionError( $error = 'Unknown error' ) {
228 return $this->__call( __FUNCTION__
, func_get_args() );
231 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
232 return $this->__call( __FUNCTION__
, func_get_args() );
235 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
236 return $this->__call( __FUNCTION__
, func_get_args() );
239 public function freeResult( $res ) {
240 return $this->__call( __FUNCTION__
, func_get_args() );
243 public function selectField(
244 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
246 return $this->__call( __FUNCTION__
, func_get_args() );
249 public function selectFieldValues(
250 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
252 return $this->__call( __FUNCTION__
, func_get_args() );
255 public function select(
256 $table, $vars, $conds = '', $fname = __METHOD__
,
257 $options = [], $join_conds = []
259 return $this->__call( __FUNCTION__
, func_get_args() );
262 public function selectSQLText(
263 $table, $vars, $conds = '', $fname = __METHOD__
,
264 $options = [], $join_conds = []
266 return $this->__call( __FUNCTION__
, func_get_args() );
269 public function selectRow(
270 $table, $vars, $conds, $fname = __METHOD__
,
271 $options = [], $join_conds = []
273 return $this->__call( __FUNCTION__
, func_get_args() );
276 public function estimateRowCount(
277 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
279 return $this->__call( __FUNCTION__
, func_get_args() );
282 public function selectRowCount(
283 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
285 return $this->__call( __FUNCTION__
, func_get_args() );
288 public function fieldExists( $table, $field, $fname = __METHOD__
) {
289 return $this->__call( __FUNCTION__
, func_get_args() );
292 public function indexExists( $table, $index, $fname = __METHOD__
) {
293 return $this->__call( __FUNCTION__
, func_get_args() );
296 public function tableExists( $table, $fname = __METHOD__
) {
297 return $this->__call( __FUNCTION__
, func_get_args() );
300 public function indexUnique( $table, $index ) {
301 return $this->__call( __FUNCTION__
, func_get_args() );
304 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
305 return $this->__call( __FUNCTION__
, func_get_args() );
308 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
309 return $this->__call( __FUNCTION__
, func_get_args() );
312 public function makeList( $a, $mode = self
::LIST_COMMA
) {
313 return $this->__call( __FUNCTION__
, func_get_args() );
316 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
317 return $this->__call( __FUNCTION__
, func_get_args() );
320 public function aggregateValue( $valuedata, $valuename = 'value' ) {
321 return $this->__call( __FUNCTION__
, func_get_args() );
324 public function bitNot( $field ) {
325 return $this->__call( __FUNCTION__
, func_get_args() );
328 public function bitAnd( $fieldLeft, $fieldRight ) {
329 return $this->__call( __FUNCTION__
, func_get_args() );
332 public function bitOr( $fieldLeft, $fieldRight ) {
333 return $this->__call( __FUNCTION__
, func_get_args() );
336 public function buildConcat( $stringList ) {
337 return $this->__call( __FUNCTION__
, func_get_args() );
340 public function buildGroupConcatField(
341 $delim, $table, $field, $conds = '', $join_conds = []
343 return $this->__call( __FUNCTION__
, func_get_args() );
346 public function buildStringCast( $field ) {
347 return $this->__call( __FUNCTION__
, func_get_args() );
350 public function selectDB( $db ) {
351 return $this->__call( __FUNCTION__
, func_get_args() );
354 public function getDBname() {
355 return $this->__call( __FUNCTION__
, func_get_args() );
358 public function getServer() {
359 return $this->__call( __FUNCTION__
, func_get_args() );
362 public function addQuotes( $s ) {
363 return $this->__call( __FUNCTION__
, func_get_args() );
366 public function buildLike() {
367 return $this->__call( __FUNCTION__
, func_get_args() );
370 public function anyChar() {
371 return $this->__call( __FUNCTION__
, func_get_args() );
374 public function anyString() {
375 return $this->__call( __FUNCTION__
, func_get_args() );
378 public function nextSequenceValue( $seqName ) {
379 return $this->__call( __FUNCTION__
, func_get_args() );
382 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
383 return $this->__call( __FUNCTION__
, func_get_args() );
386 public function upsert(
387 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
389 return $this->__call( __FUNCTION__
, func_get_args() );
392 public function deleteJoin(
393 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
395 return $this->__call( __FUNCTION__
, func_get_args() );
398 public function delete( $table, $conds, $fname = __METHOD__
) {
399 return $this->__call( __FUNCTION__
, func_get_args() );
402 public function insertSelect(
403 $destTable, $srcTable, $varMap, $conds,
404 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
406 return $this->__call( __FUNCTION__
, func_get_args() );
409 public function unionSupportsOrderAndLimit() {
410 return $this->__call( __FUNCTION__
, func_get_args() );
413 public function unionQueries( $sqls, $all ) {
414 return $this->__call( __FUNCTION__
, func_get_args() );
417 public function conditional( $cond, $trueVal, $falseVal ) {
418 return $this->__call( __FUNCTION__
, func_get_args() );
421 public function strreplace( $orig, $old, $new ) {
422 return $this->__call( __FUNCTION__
, func_get_args() );
425 public function getServerUptime() {
426 return $this->__call( __FUNCTION__
, func_get_args() );
429 public function wasDeadlock() {
430 return $this->__call( __FUNCTION__
, func_get_args() );
433 public function wasLockTimeout() {
434 return $this->__call( __FUNCTION__
, func_get_args() );
437 public function wasErrorReissuable() {
438 return $this->__call( __FUNCTION__
, func_get_args() );
441 public function wasReadOnlyError() {
442 return $this->__call( __FUNCTION__
, func_get_args() );
445 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
446 return $this->__call( __FUNCTION__
, func_get_args() );
449 public function getReplicaPos() {
450 return $this->__call( __FUNCTION__
, func_get_args() );
453 public function getMasterPos() {
454 return $this->__call( __FUNCTION__
, func_get_args() );
457 public function serverIsReadOnly() {
458 return $this->__call( __FUNCTION__
, func_get_args() );
461 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
462 return $this->__call( __FUNCTION__
, func_get_args() );
465 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
466 return $this->__call( __FUNCTION__
, func_get_args() );
469 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
470 return $this->__call( __FUNCTION__
, func_get_args() );
473 public function setTransactionListener( $name, callable
$callback = null ) {
474 return $this->__call( __FUNCTION__
, func_get_args() );
477 public function startAtomic( $fname = __METHOD__
) {
478 return $this->__call( __FUNCTION__
, func_get_args() );
481 public function endAtomic( $fname = __METHOD__
) {
482 return $this->__call( __FUNCTION__
, func_get_args() );
485 public function doAtomicSection( $fname, callable
$callback ) {
486 return $this->__call( __FUNCTION__
, func_get_args() );
489 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
490 return $this->__call( __FUNCTION__
, func_get_args() );
493 public function commit( $fname = __METHOD__
, $flush = '' ) {
494 return $this->__call( __FUNCTION__
, func_get_args() );
497 public function rollback( $fname = __METHOD__
, $flush = '' ) {
498 return $this->__call( __FUNCTION__
, func_get_args() );
501 public function flushSnapshot( $fname = __METHOD__
) {
502 return $this->__call( __FUNCTION__
, func_get_args() );
505 public function listTables( $prefix = null, $fname = __METHOD__
) {
506 return $this->__call( __FUNCTION__
, func_get_args() );
509 public function timestamp( $ts = 0 ) {
510 return $this->__call( __FUNCTION__
, func_get_args() );
513 public function timestampOrNull( $ts = null ) {
514 return $this->__call( __FUNCTION__
, func_get_args() );
517 public function ping( &$rtt = null ) {
518 return func_num_args()
519 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
520 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
523 public function getLag() {
524 return $this->__call( __FUNCTION__
, func_get_args() );
527 public function getSessionLagStatus() {
528 return $this->__call( __FUNCTION__
, func_get_args() );
531 public function maxListLen() {
532 return $this->__call( __FUNCTION__
, func_get_args() );
535 public function encodeBlob( $b ) {
536 return $this->__call( __FUNCTION__
, func_get_args() );
539 public function decodeBlob( $b ) {
540 return $this->__call( __FUNCTION__
, func_get_args() );
543 public function setSessionOptions( array $options ) {
544 return $this->__call( __FUNCTION__
, func_get_args() );
547 public function setSchemaVars( $vars ) {
548 return $this->__call( __FUNCTION__
, func_get_args() );
551 public function lockIsFree( $lockName, $method ) {
552 return $this->__call( __FUNCTION__
, func_get_args() );
555 public function lock( $lockName, $method, $timeout = 5 ) {
556 return $this->__call( __FUNCTION__
, func_get_args() );
559 public function unlock( $lockName, $method ) {
560 return $this->__call( __FUNCTION__
, func_get_args() );
563 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
564 return $this->__call( __FUNCTION__
, func_get_args() );
567 public function namedLocksEnqueue() {
568 return $this->__call( __FUNCTION__
, func_get_args() );
571 public function getInfinity() {
572 return $this->__call( __FUNCTION__
, func_get_args() );
575 public function encodeExpiry( $expiry ) {
576 return $this->__call( __FUNCTION__
, func_get_args() );
579 public function decodeExpiry( $expiry, $format = TS_MW
) {
580 return $this->__call( __FUNCTION__
, func_get_args() );
583 public function setBigSelects( $value = true ) {
584 return $this->__call( __FUNCTION__
, func_get_args() );
587 public function isReadOnly() {
588 return $this->__call( __FUNCTION__
, func_get_args() );
591 public function setTableAliases( array $aliases ) {
592 return $this->__call( __FUNCTION__
, func_get_args() );
596 * Clean up the connection when out of scope
598 function __destruct() {
600 $this->lb
->reuseConnection( $this->conn
);