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