rdbms: IDatabase interface cleanups
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DBConnRef.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 use InvalidArgumentException;
6
7 /**
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
10 *
11 * @note: proxy methods are defined explicitly to avoid interface errors
12 * @ingroup Database
13 * @since 1.22
14 */
15 class DBConnRef implements IDatabase {
16 /** @var ILoadBalancer */
17 private $lb;
18 /** @var Database|null Live connection handle */
19 private $conn;
20 /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */
21 private $params;
22
23 const FLD_INDEX = 0;
24 const FLD_GROUP = 1;
25 const FLD_DOMAIN = 2;
26 const FLD_FLAGS = 3;
27
28 /**
29 * @param ILoadBalancer $lb Connection manager for $conn
30 * @param Database|array $conn Database handle or (server index, query groups, domain, flags)
31 */
32 public function __construct( ILoadBalancer $lb, $conn ) {
33 $this->lb = $lb;
34 if ( $conn instanceof Database ) {
35 $this->conn = $conn; // live handle
36 } elseif ( count( $conn ) >= 4 && $conn[self::FLD_DOMAIN] !== false ) {
37 $this->params = $conn;
38 } else {
39 throw new InvalidArgumentException( "Missing lazy connection arguments." );
40 }
41 }
42
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 );
47 }
48
49 return call_user_func_array( [ $this->conn, $name ], $arguments );
50 }
51
52 public function getServerInfo() {
53 return $this->__call( __FUNCTION__, func_get_args() );
54 }
55
56 public function bufferResults( $buffer = null ) {
57 return $this->__call( __FUNCTION__, func_get_args() );
58 }
59
60 public function trxLevel() {
61 return $this->__call( __FUNCTION__, func_get_args() );
62 }
63
64 public function trxTimestamp() {
65 return $this->__call( __FUNCTION__, func_get_args() );
66 }
67
68 public function explicitTrxActive() {
69 return $this->__call( __FUNCTION__, func_get_args() );
70 }
71
72 public function tablePrefix( $prefix = null ) {
73 return $this->__call( __FUNCTION__, func_get_args() );
74 }
75
76 public function dbSchema( $schema = null ) {
77 return $this->__call( __FUNCTION__, func_get_args() );
78 }
79
80 public function getLBInfo( $name = null ) {
81 return $this->__call( __FUNCTION__, func_get_args() );
82 }
83
84 public function setLBInfo( $name, $value = null ) {
85 return $this->__call( __FUNCTION__, func_get_args() );
86 }
87
88 public function setLazyMasterHandle( IDatabase $conn ) {
89 return $this->__call( __FUNCTION__, func_get_args() );
90 }
91
92 public function implicitGroupby() {
93 return $this->__call( __FUNCTION__, func_get_args() );
94 }
95
96 public function implicitOrderby() {
97 return $this->__call( __FUNCTION__, func_get_args() );
98 }
99
100 public function lastQuery() {
101 return $this->__call( __FUNCTION__, func_get_args() );
102 }
103
104 public function doneWrites() {
105 return $this->__call( __FUNCTION__, func_get_args() );
106 }
107
108 public function lastDoneWrites() {
109 return $this->__call( __FUNCTION__, func_get_args() );
110 }
111
112 public function writesPending() {
113 return $this->__call( __FUNCTION__, func_get_args() );
114 }
115
116 public function writesOrCallbacksPending() {
117 return $this->__call( __FUNCTION__, func_get_args() );
118 }
119
120 public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
121 return $this->__call( __FUNCTION__, func_get_args() );
122 }
123
124 public function pendingWriteCallers() {
125 return $this->__call( __FUNCTION__, func_get_args() );
126 }
127
128 public function pendingWriteRowsAffected() {
129 return $this->__call( __FUNCTION__, func_get_args() );
130 }
131
132 public function isOpen() {
133 return $this->__call( __FUNCTION__, func_get_args() );
134 }
135
136 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
137 return $this->__call( __FUNCTION__, func_get_args() );
138 }
139
140 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
141 return $this->__call( __FUNCTION__, func_get_args() );
142 }
143
144 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
145 return $this->__call( __FUNCTION__, func_get_args() );
146 }
147
148 public function getFlag( $flag ) {
149 return $this->__call( __FUNCTION__, func_get_args() );
150 }
151
152 public function getProperty( $name ) {
153 return $this->__call( __FUNCTION__, func_get_args() );
154 }
155
156 public function getDomainID() {
157 if ( $this->conn === null ) {
158 $domain = $this->params[self::FLD_DOMAIN];
159 // Avoid triggering a database connection
160 return $domain instanceof DatabaseDomain ? $domain->getId() : $domain;
161 }
162
163 return $this->__call( __FUNCTION__, func_get_args() );
164 }
165
166 public function getWikiID() {
167 return $this->getDomainID();
168 }
169
170 public function getType() {
171 return $this->__call( __FUNCTION__, func_get_args() );
172 }
173
174 public function open( $server, $user, $password, $dbName ) {
175 return $this->__call( __FUNCTION__, func_get_args() );
176 }
177
178 public function fetchObject( $res ) {
179 return $this->__call( __FUNCTION__, func_get_args() );
180 }
181
182 public function fetchRow( $res ) {
183 return $this->__call( __FUNCTION__, func_get_args() );
184 }
185
186 public function numRows( $res ) {
187 return $this->__call( __FUNCTION__, func_get_args() );
188 }
189
190 public function numFields( $res ) {
191 return $this->__call( __FUNCTION__, func_get_args() );
192 }
193
194 public function fieldName( $res, $n ) {
195 return $this->__call( __FUNCTION__, func_get_args() );
196 }
197
198 public function insertId() {
199 return $this->__call( __FUNCTION__, func_get_args() );
200 }
201
202 public function dataSeek( $res, $row ) {
203 return $this->__call( __FUNCTION__, func_get_args() );
204 }
205
206 public function lastErrno() {
207 return $this->__call( __FUNCTION__, func_get_args() );
208 }
209
210 public function lastError() {
211 return $this->__call( __FUNCTION__, func_get_args() );
212 }
213
214 public function affectedRows() {
215 return $this->__call( __FUNCTION__, func_get_args() );
216 }
217
218 public function getSoftwareLink() {
219 return $this->__call( __FUNCTION__, func_get_args() );
220 }
221
222 public function getServerVersion() {
223 return $this->__call( __FUNCTION__, func_get_args() );
224 }
225
226 public function close() {
227 return $this->__call( __FUNCTION__, func_get_args() );
228 }
229
230 public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
231 return $this->__call( __FUNCTION__, func_get_args() );
232 }
233
234 public function freeResult( $res ) {
235 return $this->__call( __FUNCTION__, func_get_args() );
236 }
237
238 public function selectField(
239 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
240 ) {
241 return $this->__call( __FUNCTION__, func_get_args() );
242 }
243
244 public function selectFieldValues(
245 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
246 ) {
247 return $this->__call( __FUNCTION__, func_get_args() );
248 }
249
250 public function select(
251 $table, $vars, $conds = '', $fname = __METHOD__,
252 $options = [], $join_conds = []
253 ) {
254 return $this->__call( __FUNCTION__, func_get_args() );
255 }
256
257 public function selectSQLText(
258 $table, $vars, $conds = '', $fname = __METHOD__,
259 $options = [], $join_conds = []
260 ) {
261 return $this->__call( __FUNCTION__, func_get_args() );
262 }
263
264 public function selectRow(
265 $table, $vars, $conds, $fname = __METHOD__,
266 $options = [], $join_conds = []
267 ) {
268 return $this->__call( __FUNCTION__, func_get_args() );
269 }
270
271 public function estimateRowCount(
272 $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
273 ) {
274 return $this->__call( __FUNCTION__, func_get_args() );
275 }
276
277 public function selectRowCount(
278 $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
279 ) {
280 return $this->__call( __FUNCTION__, func_get_args() );
281 }
282
283 public function fieldExists( $table, $field, $fname = __METHOD__ ) {
284 return $this->__call( __FUNCTION__, func_get_args() );
285 }
286
287 public function indexExists( $table, $index, $fname = __METHOD__ ) {
288 return $this->__call( __FUNCTION__, func_get_args() );
289 }
290
291 public function tableExists( $table, $fname = __METHOD__ ) {
292 return $this->__call( __FUNCTION__, func_get_args() );
293 }
294
295 public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
296 return $this->__call( __FUNCTION__, func_get_args() );
297 }
298
299 public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
300 return $this->__call( __FUNCTION__, func_get_args() );
301 }
302
303 public function makeList( $a, $mode = self::LIST_COMMA ) {
304 return $this->__call( __FUNCTION__, func_get_args() );
305 }
306
307 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
308 return $this->__call( __FUNCTION__, func_get_args() );
309 }
310
311 public function aggregateValue( $valuedata, $valuename = 'value' ) {
312 return $this->__call( __FUNCTION__, func_get_args() );
313 }
314
315 public function bitNot( $field ) {
316 return $this->__call( __FUNCTION__, func_get_args() );
317 }
318
319 public function bitAnd( $fieldLeft, $fieldRight ) {
320 return $this->__call( __FUNCTION__, func_get_args() );
321 }
322
323 public function bitOr( $fieldLeft, $fieldRight ) {
324 return $this->__call( __FUNCTION__, func_get_args() );
325 }
326
327 public function buildConcat( $stringList ) {
328 return $this->__call( __FUNCTION__, func_get_args() );
329 }
330
331 public function buildGroupConcatField(
332 $delim, $table, $field, $conds = '', $join_conds = []
333 ) {
334 return $this->__call( __FUNCTION__, func_get_args() );
335 }
336
337 public function buildSubstring( $input, $startPosition, $length = null ) {
338 return $this->__call( __FUNCTION__, func_get_args() );
339 }
340
341 public function buildStringCast( $field ) {
342 return $this->__call( __FUNCTION__, func_get_args() );
343 }
344
345 public function buildIntegerCast( $field ) {
346 return $this->__call( __FUNCTION__, func_get_args() );
347 }
348
349 public function buildSelectSubquery(
350 $table, $vars, $conds = '', $fname = __METHOD__,
351 $options = [], $join_conds = []
352 ) {
353 return $this->__call( __FUNCTION__, func_get_args() );
354 }
355
356 public function databasesAreIndependent() {
357 return $this->__call( __FUNCTION__, func_get_args() );
358 }
359
360 public function selectDB( $db ) {
361 return $this->__call( __FUNCTION__, func_get_args() );
362 }
363
364 public function getDBname() {
365 return $this->__call( __FUNCTION__, func_get_args() );
366 }
367
368 public function getServer() {
369 return $this->__call( __FUNCTION__, func_get_args() );
370 }
371
372 public function addQuotes( $s ) {
373 return $this->__call( __FUNCTION__, func_get_args() );
374 }
375
376 public function buildLike() {
377 return $this->__call( __FUNCTION__, func_get_args() );
378 }
379
380 public function anyChar() {
381 return $this->__call( __FUNCTION__, func_get_args() );
382 }
383
384 public function anyString() {
385 return $this->__call( __FUNCTION__, func_get_args() );
386 }
387
388 public function nextSequenceValue( $seqName ) {
389 return $this->__call( __FUNCTION__, func_get_args() );
390 }
391
392 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
393 return $this->__call( __FUNCTION__, func_get_args() );
394 }
395
396 public function upsert(
397 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
398 ) {
399 return $this->__call( __FUNCTION__, func_get_args() );
400 }
401
402 public function deleteJoin(
403 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
404 ) {
405 return $this->__call( __FUNCTION__, func_get_args() );
406 }
407
408 public function delete( $table, $conds, $fname = __METHOD__ ) {
409 return $this->__call( __FUNCTION__, func_get_args() );
410 }
411
412 public function insertSelect(
413 $destTable, $srcTable, $varMap, $conds,
414 $fname = __METHOD__, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
415 ) {
416 return $this->__call( __FUNCTION__, func_get_args() );
417 }
418
419 public function unionSupportsOrderAndLimit() {
420 return $this->__call( __FUNCTION__, func_get_args() );
421 }
422
423 public function unionQueries( $sqls, $all ) {
424 return $this->__call( __FUNCTION__, func_get_args() );
425 }
426
427 public function unionConditionPermutations(
428 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__,
429 $options = [], $join_conds = []
430 ) {
431 return $this->__call( __FUNCTION__, func_get_args() );
432 }
433
434 public function conditional( $cond, $trueVal, $falseVal ) {
435 return $this->__call( __FUNCTION__, func_get_args() );
436 }
437
438 public function strreplace( $orig, $old, $new ) {
439 return $this->__call( __FUNCTION__, func_get_args() );
440 }
441
442 public function getServerUptime() {
443 return $this->__call( __FUNCTION__, func_get_args() );
444 }
445
446 public function wasDeadlock() {
447 return $this->__call( __FUNCTION__, func_get_args() );
448 }
449
450 public function wasLockTimeout() {
451 return $this->__call( __FUNCTION__, func_get_args() );
452 }
453
454 public function wasConnectionLoss() {
455 return $this->__call( __FUNCTION__, func_get_args() );
456 }
457
458 public function wasReadOnlyError() {
459 return $this->__call( __FUNCTION__, func_get_args() );
460 }
461
462 public function wasErrorReissuable() {
463 return $this->__call( __FUNCTION__, func_get_args() );
464 }
465
466 public function masterPosWait( DBMasterPos $pos, $timeout ) {
467 return $this->__call( __FUNCTION__, func_get_args() );
468 }
469
470 public function getReplicaPos() {
471 return $this->__call( __FUNCTION__, func_get_args() );
472 }
473
474 public function getMasterPos() {
475 return $this->__call( __FUNCTION__, func_get_args() );
476 }
477
478 public function serverIsReadOnly() {
479 return $this->__call( __FUNCTION__, func_get_args() );
480 }
481
482 public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
483 return $this->__call( __FUNCTION__, func_get_args() );
484 }
485
486 public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
487 return $this->__call( __FUNCTION__, func_get_args() );
488 }
489
490 public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
491 return $this->__call( __FUNCTION__, func_get_args() );
492 }
493
494 public function setTransactionListener( $name, callable $callback = null ) {
495 return $this->__call( __FUNCTION__, func_get_args() );
496 }
497
498 public function startAtomic( $fname = __METHOD__ ) {
499 return $this->__call( __FUNCTION__, func_get_args() );
500 }
501
502 public function endAtomic( $fname = __METHOD__ ) {
503 return $this->__call( __FUNCTION__, func_get_args() );
504 }
505
506 public function cancelAtomic( $fname = __METHOD__ ) {
507 return $this->__call( __FUNCTION__, func_get_args() );
508 }
509
510 public function doAtomicSection( $fname, callable $callback ) {
511 return $this->__call( __FUNCTION__, func_get_args() );
512 }
513
514 public function begin( $fname = __METHOD__, $mode = IDatabase::TRANSACTION_EXPLICIT ) {
515 return $this->__call( __FUNCTION__, func_get_args() );
516 }
517
518 public function commit( $fname = __METHOD__, $flush = '' ) {
519 return $this->__call( __FUNCTION__, func_get_args() );
520 }
521
522 public function rollback( $fname = __METHOD__, $flush = '' ) {
523 return $this->__call( __FUNCTION__, func_get_args() );
524 }
525
526 public function flushSnapshot( $fname = __METHOD__ ) {
527 return $this->__call( __FUNCTION__, func_get_args() );
528 }
529
530 public function timestamp( $ts = 0 ) {
531 return $this->__call( __FUNCTION__, func_get_args() );
532 }
533
534 public function timestampOrNull( $ts = null ) {
535 return $this->__call( __FUNCTION__, func_get_args() );
536 }
537
538 public function ping( &$rtt = null ) {
539 return func_num_args()
540 ? $this->__call( __FUNCTION__, [ &$rtt ] )
541 : $this->__call( __FUNCTION__, [] ); // method cares about null vs missing
542 }
543
544 public function getLag() {
545 return $this->__call( __FUNCTION__, func_get_args() );
546 }
547
548 public function getSessionLagStatus() {
549 return $this->__call( __FUNCTION__, func_get_args() );
550 }
551
552 public function maxListLen() {
553 return $this->__call( __FUNCTION__, func_get_args() );
554 }
555
556 public function encodeBlob( $b ) {
557 return $this->__call( __FUNCTION__, func_get_args() );
558 }
559
560 public function decodeBlob( $b ) {
561 return $this->__call( __FUNCTION__, func_get_args() );
562 }
563
564 public function setSessionOptions( array $options ) {
565 return $this->__call( __FUNCTION__, func_get_args() );
566 }
567
568 public function setSchemaVars( $vars ) {
569 return $this->__call( __FUNCTION__, func_get_args() );
570 }
571
572 public function lockIsFree( $lockName, $method ) {
573 return $this->__call( __FUNCTION__, func_get_args() );
574 }
575
576 public function lock( $lockName, $method, $timeout = 5 ) {
577 return $this->__call( __FUNCTION__, func_get_args() );
578 }
579
580 public function unlock( $lockName, $method ) {
581 return $this->__call( __FUNCTION__, func_get_args() );
582 }
583
584 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
585 return $this->__call( __FUNCTION__, func_get_args() );
586 }
587
588 public function namedLocksEnqueue() {
589 return $this->__call( __FUNCTION__, func_get_args() );
590 }
591
592 public function getInfinity() {
593 return $this->__call( __FUNCTION__, func_get_args() );
594 }
595
596 public function encodeExpiry( $expiry ) {
597 return $this->__call( __FUNCTION__, func_get_args() );
598 }
599
600 public function decodeExpiry( $expiry, $format = TS_MW ) {
601 return $this->__call( __FUNCTION__, func_get_args() );
602 }
603
604 public function setBigSelects( $value = true ) {
605 return $this->__call( __FUNCTION__, func_get_args() );
606 }
607
608 public function isReadOnly() {
609 return $this->__call( __FUNCTION__, func_get_args() );
610 }
611
612 public function setTableAliases( array $aliases ) {
613 return $this->__call( __FUNCTION__, func_get_args() );
614 }
615
616 public function setIndexAliases( array $aliases ) {
617 return $this->__call( __FUNCTION__, func_get_args() );
618 }
619
620 /**
621 * Clean up the connection when out of scope
622 */
623 function __destruct() {
624 if ( $this->conn ) {
625 $this->lb->reuseConnection( $this->conn );
626 }
627 }
628 }
629
630 class_alias( DBConnRef::class, 'DBConnRef' );