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