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) */
29 * @param ILoadBalancer $lb Connection manager for $conn
30 * @param Database|array $conn Database handle or (server index, query groups, domain, flags)
32 public function __construct( ILoadBalancer
$lb, $conn ) {
34 if ( $conn instanceof Database
) {
35 $this->conn
= $conn; // live handle
36 } elseif ( is_array( $conn ) && count( $conn ) >= 4 && $conn[self
::FLD_DOMAIN
] !== false ) {
37 $this->params
= $conn;
39 throw new InvalidArgumentException( "Missing lazy connection arguments." );
43 function __call( $name, array $arguments ) {
44 if ( $this->conn
=== null ) {
45 list( $db, $groups, $wiki, $flags ) = $this->params
;
46 $this->conn
= $this->lb
->getConnection( $db, $groups, $wiki, $flags );
49 return call_user_func_array( [ $this->conn
, $name ], $arguments );
52 public function getServerInfo() {
53 return $this->__call( __FUNCTION__
, func_get_args() );
56 public function bufferResults( $buffer = null ) {
57 return $this->__call( __FUNCTION__
, func_get_args() );
60 public function trxLevel() {
61 return $this->__call( __FUNCTION__
, func_get_args() );
64 public function trxTimestamp() {
65 return $this->__call( __FUNCTION__
, func_get_args() );
68 public function explicitTrxActive() {
69 return $this->__call( __FUNCTION__
, func_get_args() );
72 public function tablePrefix( $prefix = null ) {
73 return $this->__call( __FUNCTION__
, func_get_args() );
76 public function dbSchema( $schema = null ) {
77 return $this->__call( __FUNCTION__
, func_get_args() );
80 public function getLBInfo( $name = null ) {
81 return $this->__call( __FUNCTION__
, func_get_args() );
84 public function setLBInfo( $name, $value = null ) {
85 return $this->__call( __FUNCTION__
, func_get_args() );
88 public function setLazyMasterHandle( IDatabase
$conn ) {
89 return $this->__call( __FUNCTION__
, func_get_args() );
92 public function implicitGroupby() {
93 return $this->__call( __FUNCTION__
, func_get_args() );
96 public function implicitOrderby() {
97 return $this->__call( __FUNCTION__
, func_get_args() );
100 public function lastQuery() {
101 return $this->__call( __FUNCTION__
, func_get_args() );
104 public function doneWrites() {
105 return $this->__call( __FUNCTION__
, func_get_args() );
108 public function lastDoneWrites() {
109 return $this->__call( __FUNCTION__
, func_get_args() );
112 public function writesPending() {
113 return $this->__call( __FUNCTION__
, func_get_args() );
116 public function preCommitCallbacksPending() {
117 return $this->__call( __FUNCTION__
, func_get_args() );
120 public function writesOrCallbacksPending() {
121 return $this->__call( __FUNCTION__
, func_get_args() );
124 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
125 return $this->__call( __FUNCTION__
, func_get_args() );
128 public function pendingWriteCallers() {
129 return $this->__call( __FUNCTION__
, func_get_args() );
132 public function pendingWriteRowsAffected() {
133 return $this->__call( __FUNCTION__
, func_get_args() );
136 public function isOpen() {
137 return $this->__call( __FUNCTION__
, func_get_args() );
140 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
141 return $this->__call( __FUNCTION__
, func_get_args() );
144 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
145 return $this->__call( __FUNCTION__
, func_get_args() );
148 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
149 return $this->__call( __FUNCTION__
, func_get_args() );
152 public function getFlag( $flag ) {
153 return $this->__call( __FUNCTION__
, func_get_args() );
156 public function getProperty( $name ) {
157 return $this->__call( __FUNCTION__
, func_get_args() );
160 public function getDomainID() {
161 if ( $this->conn
=== null ) {
162 $domain = $this->params
[self
::FLD_DOMAIN
];
163 // Avoid triggering a database connection
164 return $domain instanceof DatabaseDomain ?
$domain->getId() : $domain;
167 return $this->__call( __FUNCTION__
, func_get_args() );
170 public function getWikiID() {
171 return $this->getDomainID();
174 public function getType() {
175 return $this->__call( __FUNCTION__
, func_get_args() );
178 public function open( $server, $user, $password, $dbName ) {
179 return $this->__call( __FUNCTION__
, func_get_args() );
182 public function fetchObject( $res ) {
183 return $this->__call( __FUNCTION__
, func_get_args() );
186 public function fetchRow( $res ) {
187 return $this->__call( __FUNCTION__
, func_get_args() );
190 public function numRows( $res ) {
191 return $this->__call( __FUNCTION__
, func_get_args() );
194 public function numFields( $res ) {
195 return $this->__call( __FUNCTION__
, func_get_args() );
198 public function fieldName( $res, $n ) {
199 return $this->__call( __FUNCTION__
, func_get_args() );
202 public function insertId() {
203 return $this->__call( __FUNCTION__
, func_get_args() );
206 public function dataSeek( $res, $row ) {
207 return $this->__call( __FUNCTION__
, func_get_args() );
210 public function lastErrno() {
211 return $this->__call( __FUNCTION__
, func_get_args() );
214 public function lastError() {
215 return $this->__call( __FUNCTION__
, func_get_args() );
218 public function affectedRows() {
219 return $this->__call( __FUNCTION__
, func_get_args() );
222 public function getSoftwareLink() {
223 return $this->__call( __FUNCTION__
, func_get_args() );
226 public function getServerVersion() {
227 return $this->__call( __FUNCTION__
, func_get_args() );
230 public function close() {
231 return $this->__call( __FUNCTION__
, func_get_args() );
234 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
235 return $this->__call( __FUNCTION__
, func_get_args() );
238 public function freeResult( $res ) {
239 return $this->__call( __FUNCTION__
, func_get_args() );
242 public function selectField(
243 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
245 return $this->__call( __FUNCTION__
, func_get_args() );
248 public function selectFieldValues(
249 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
251 return $this->__call( __FUNCTION__
, func_get_args() );
254 public function select(
255 $table, $vars, $conds = '', $fname = __METHOD__
,
256 $options = [], $join_conds = []
258 return $this->__call( __FUNCTION__
, func_get_args() );
261 public function selectSQLText(
262 $table, $vars, $conds = '', $fname = __METHOD__
,
263 $options = [], $join_conds = []
265 return $this->__call( __FUNCTION__
, func_get_args() );
268 public function selectRow(
269 $table, $vars, $conds, $fname = __METHOD__
,
270 $options = [], $join_conds = []
272 return $this->__call( __FUNCTION__
, func_get_args() );
275 public function estimateRowCount(
276 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
278 return $this->__call( __FUNCTION__
, func_get_args() );
281 public function selectRowCount(
282 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
284 return $this->__call( __FUNCTION__
, func_get_args() );
287 public function fieldExists( $table, $field, $fname = __METHOD__
) {
288 return $this->__call( __FUNCTION__
, func_get_args() );
291 public function indexExists( $table, $index, $fname = __METHOD__
) {
292 return $this->__call( __FUNCTION__
, func_get_args() );
295 public function tableExists( $table, $fname = __METHOD__
) {
296 return $this->__call( __FUNCTION__
, func_get_args() );
299 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
300 return $this->__call( __FUNCTION__
, func_get_args() );
303 public function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
304 return $this->__call( __FUNCTION__
, func_get_args() );
307 public function makeList( $a, $mode = self
::LIST_COMMA
) {
308 return $this->__call( __FUNCTION__
, func_get_args() );
311 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
312 return $this->__call( __FUNCTION__
, func_get_args() );
315 public function aggregateValue( $valuedata, $valuename = 'value' ) {
316 return $this->__call( __FUNCTION__
, func_get_args() );
319 public function bitNot( $field ) {
320 return $this->__call( __FUNCTION__
, func_get_args() );
323 public function bitAnd( $fieldLeft, $fieldRight ) {
324 return $this->__call( __FUNCTION__
, func_get_args() );
327 public function bitOr( $fieldLeft, $fieldRight ) {
328 return $this->__call( __FUNCTION__
, func_get_args() );
331 public function buildConcat( $stringList ) {
332 return $this->__call( __FUNCTION__
, func_get_args() );
335 public function buildGroupConcatField(
336 $delim, $table, $field, $conds = '', $join_conds = []
338 return $this->__call( __FUNCTION__
, func_get_args() );
341 public function buildSubstring( $input, $startPosition, $length = null ) {
342 return $this->__call( __FUNCTION__
, func_get_args() );
345 public function buildStringCast( $field ) {
346 return $this->__call( __FUNCTION__
, func_get_args() );
349 public function buildIntegerCast( $field ) {
350 return $this->__call( __FUNCTION__
, func_get_args() );
353 public function buildSelectSubquery(
354 $table, $vars, $conds = '', $fname = __METHOD__
,
355 $options = [], $join_conds = []
357 return $this->__call( __FUNCTION__
, func_get_args() );
360 public function databasesAreIndependent() {
361 return $this->__call( __FUNCTION__
, func_get_args() );
364 public function selectDB( $db ) {
365 return $this->__call( __FUNCTION__
, func_get_args() );
368 public function getDBname() {
369 return $this->__call( __FUNCTION__
, func_get_args() );
372 public function getServer() {
373 return $this->__call( __FUNCTION__
, func_get_args() );
376 public function addQuotes( $s ) {
377 return $this->__call( __FUNCTION__
, func_get_args() );
380 public function buildLike() {
381 return $this->__call( __FUNCTION__
, func_get_args() );
384 public function anyChar() {
385 return $this->__call( __FUNCTION__
, func_get_args() );
388 public function anyString() {
389 return $this->__call( __FUNCTION__
, func_get_args() );
392 public function nextSequenceValue( $seqName ) {
393 return $this->__call( __FUNCTION__
, func_get_args() );
396 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
397 return $this->__call( __FUNCTION__
, func_get_args() );
400 public function upsert(
401 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
403 return $this->__call( __FUNCTION__
, func_get_args() );
406 public function deleteJoin(
407 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
409 return $this->__call( __FUNCTION__
, func_get_args() );
412 public function delete( $table, $conds, $fname = __METHOD__
) {
413 return $this->__call( __FUNCTION__
, func_get_args() );
416 public function insertSelect(
417 $destTable, $srcTable, $varMap, $conds,
418 $fname = __METHOD__
, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
420 return $this->__call( __FUNCTION__
, func_get_args() );
423 public function unionSupportsOrderAndLimit() {
424 return $this->__call( __FUNCTION__
, func_get_args() );
427 public function unionQueries( $sqls, $all ) {
428 return $this->__call( __FUNCTION__
, func_get_args() );
431 public function unionConditionPermutations(
432 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__
,
433 $options = [], $join_conds = []
435 return $this->__call( __FUNCTION__
, func_get_args() );
438 public function conditional( $cond, $trueVal, $falseVal ) {
439 return $this->__call( __FUNCTION__
, func_get_args() );
442 public function strreplace( $orig, $old, $new ) {
443 return $this->__call( __FUNCTION__
, func_get_args() );
446 public function getServerUptime() {
447 return $this->__call( __FUNCTION__
, func_get_args() );
450 public function wasDeadlock() {
451 return $this->__call( __FUNCTION__
, func_get_args() );
454 public function wasLockTimeout() {
455 return $this->__call( __FUNCTION__
, func_get_args() );
458 public function wasConnectionLoss() {
459 return $this->__call( __FUNCTION__
, func_get_args() );
462 public function wasReadOnlyError() {
463 return $this->__call( __FUNCTION__
, func_get_args() );
466 public function wasErrorReissuable() {
467 return $this->__call( __FUNCTION__
, func_get_args() );
470 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
471 return $this->__call( __FUNCTION__
, func_get_args() );
474 public function getReplicaPos() {
475 return $this->__call( __FUNCTION__
, func_get_args() );
478 public function getMasterPos() {
479 return $this->__call( __FUNCTION__
, func_get_args() );
482 public function serverIsReadOnly() {
483 return $this->__call( __FUNCTION__
, func_get_args() );
486 public function onTransactionResolution( callable
$callback, $fname = __METHOD__
) {
487 return $this->__call( __FUNCTION__
, func_get_args() );
490 public function onTransactionCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
491 return $this->__call( __FUNCTION__
, func_get_args() );
494 public function onTransactionIdle( callable
$callback, $fname = __METHOD__
) {
495 return $this->__call( __FUNCTION__
, func_get_args() );
498 public function onTransactionPreCommitOrIdle( callable
$callback, $fname = __METHOD__
) {
499 return $this->__call( __FUNCTION__
, func_get_args() );
502 public function setTransactionListener( $name, callable
$callback = null ) {
503 return $this->__call( __FUNCTION__
, func_get_args() );
506 public function startAtomic(
507 $fname = __METHOD__
, $cancelable = IDatabase
::ATOMIC_NOT_CANCELABLE
509 return $this->__call( __FUNCTION__
, func_get_args() );
512 public function endAtomic( $fname = __METHOD__
) {
513 return $this->__call( __FUNCTION__
, func_get_args() );
516 public function cancelAtomic( $fname = __METHOD__
, AtomicSectionIdentifier
$sectionId = null ) {
517 return $this->__call( __FUNCTION__
, func_get_args() );
520 public function doAtomicSection(
521 $fname, callable
$callback, $cancelable = self
::ATOMIC_NOT_CANCELABLE
523 return $this->__call( __FUNCTION__
, func_get_args() );
526 public function begin( $fname = __METHOD__
, $mode = IDatabase
::TRANSACTION_EXPLICIT
) {
527 return $this->__call( __FUNCTION__
, func_get_args() );
530 public function commit( $fname = __METHOD__
, $flush = '' ) {
531 return $this->__call( __FUNCTION__
, func_get_args() );
534 public function rollback( $fname = __METHOD__
, $flush = '' ) {
535 return $this->__call( __FUNCTION__
, func_get_args() );
538 public function flushSnapshot( $fname = __METHOD__
) {
539 return $this->__call( __FUNCTION__
, func_get_args() );
542 public function timestamp( $ts = 0 ) {
543 return $this->__call( __FUNCTION__
, func_get_args() );
546 public function timestampOrNull( $ts = null ) {
547 return $this->__call( __FUNCTION__
, func_get_args() );
550 public function ping( &$rtt = null ) {
551 return func_num_args()
552 ?
$this->__call( __FUNCTION__
, [ &$rtt ] )
553 : $this->__call( __FUNCTION__
, [] ); // method cares about null vs missing
556 public function getLag() {
557 return $this->__call( __FUNCTION__
, func_get_args() );
560 public function getSessionLagStatus() {
561 return $this->__call( __FUNCTION__
, func_get_args() );
564 public function maxListLen() {
565 return $this->__call( __FUNCTION__
, func_get_args() );
568 public function encodeBlob( $b ) {
569 return $this->__call( __FUNCTION__
, func_get_args() );
572 public function decodeBlob( $b ) {
573 return $this->__call( __FUNCTION__
, func_get_args() );
576 public function setSessionOptions( array $options ) {
577 return $this->__call( __FUNCTION__
, func_get_args() );
580 public function setSchemaVars( $vars ) {
581 return $this->__call( __FUNCTION__
, func_get_args() );
584 public function lockIsFree( $lockName, $method ) {
585 return $this->__call( __FUNCTION__
, func_get_args() );
588 public function lock( $lockName, $method, $timeout = 5 ) {
589 return $this->__call( __FUNCTION__
, func_get_args() );
592 public function unlock( $lockName, $method ) {
593 return $this->__call( __FUNCTION__
, func_get_args() );
596 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
597 return $this->__call( __FUNCTION__
, func_get_args() );
600 public function namedLocksEnqueue() {
601 return $this->__call( __FUNCTION__
, func_get_args() );
604 public function getInfinity() {
605 return $this->__call( __FUNCTION__
, func_get_args() );
608 public function encodeExpiry( $expiry ) {
609 return $this->__call( __FUNCTION__
, func_get_args() );
612 public function decodeExpiry( $expiry, $format = TS_MW
) {
613 return $this->__call( __FUNCTION__
, func_get_args() );
616 public function setBigSelects( $value = true ) {
617 return $this->__call( __FUNCTION__
, func_get_args() );
620 public function isReadOnly() {
621 return $this->__call( __FUNCTION__
, func_get_args() );
624 public function setTableAliases( array $aliases ) {
625 return $this->__call( __FUNCTION__
, func_get_args() );
628 public function setIndexAliases( array $aliases ) {
629 return $this->__call( __FUNCTION__
, func_get_args() );
633 * Clean up the connection when out of scope
635 function __destruct() {
637 $this->lb
->reuseConnection( $this->conn
);
644 * @deprecated since 1.29
646 class_alias( DBConnRef
::class, 'DBConnRef' );