Merge "Special:Newpages feed now shows first revision instead of latest revision"
[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
27 /**
28 * @param ILoadBalancer $lb Connection manager for $conn
29 * @param Database|array $conn New connection handle or (server index, query groups, domain)
30 */
31 public function __construct( ILoadBalancer $lb, $conn ) {
32 $this->lb = $lb;
33 if ( $conn instanceof Database ) {
34 $this->conn = $conn; // live handle
35 } elseif ( count( $conn ) >= 3 && $conn[self::FLD_DOMAIN] !== false ) {
36 $this->params = $conn;
37 } else {
38 throw new InvalidArgumentException( "Missing lazy connection arguments." );
39 }
40 }
41
42 function __call( $name, array $arguments ) {
43 if ( $this->conn === null ) {
44 list( $db, $groups, $wiki ) = $this->params;
45 $this->conn = $this->lb->getConnection( $db, $groups, $wiki );
46 }
47
48 return call_user_func_array( [ $this->conn, $name ], $arguments );
49 }
50
51 public function getServerInfo() {
52 return $this->__call( __FUNCTION__, func_get_args() );
53 }
54
55 public function bufferResults( $buffer = null ) {
56 return $this->__call( __FUNCTION__, func_get_args() );
57 }
58
59 public function trxLevel() {
60 return $this->__call( __FUNCTION__, func_get_args() );
61 }
62
63 public function trxTimestamp() {
64 return $this->__call( __FUNCTION__, func_get_args() );
65 }
66
67 public function explicitTrxActive() {
68 return $this->__call( __FUNCTION__, func_get_args() );
69 }
70
71 public function tablePrefix( $prefix = null ) {
72 return $this->__call( __FUNCTION__, func_get_args() );
73 }
74
75 public function dbSchema( $schema = null ) {
76 return $this->__call( __FUNCTION__, func_get_args() );
77 }
78
79 public function getLBInfo( $name = null ) {
80 return $this->__call( __FUNCTION__, func_get_args() );
81 }
82
83 public function setLBInfo( $name, $value = null ) {
84 return $this->__call( __FUNCTION__, func_get_args() );
85 }
86
87 public function setLazyMasterHandle( IDatabase $conn ) {
88 return $this->__call( __FUNCTION__, func_get_args() );
89 }
90
91 public function implicitGroupby() {
92 return $this->__call( __FUNCTION__, func_get_args() );
93 }
94
95 public function implicitOrderby() {
96 return $this->__call( __FUNCTION__, func_get_args() );
97 }
98
99 public function lastQuery() {
100 return $this->__call( __FUNCTION__, func_get_args() );
101 }
102
103 public function doneWrites() {
104 return $this->__call( __FUNCTION__, func_get_args() );
105 }
106
107 public function lastDoneWrites() {
108 return $this->__call( __FUNCTION__, func_get_args() );
109 }
110
111 public function writesPending() {
112 return $this->__call( __FUNCTION__, func_get_args() );
113 }
114
115 public function writesOrCallbacksPending() {
116 return $this->__call( __FUNCTION__, func_get_args() );
117 }
118
119 public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
120 return $this->__call( __FUNCTION__, func_get_args() );
121 }
122
123 public function pendingWriteCallers() {
124 return $this->__call( __FUNCTION__, func_get_args() );
125 }
126
127 public function pendingWriteRowsAffected() {
128 return $this->__call( __FUNCTION__, func_get_args() );
129 }
130
131 public function isOpen() {
132 return $this->__call( __FUNCTION__, func_get_args() );
133 }
134
135 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
136 return $this->__call( __FUNCTION__, func_get_args() );
137 }
138
139 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
140 return $this->__call( __FUNCTION__, func_get_args() );
141 }
142
143 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
144 return $this->__call( __FUNCTION__, func_get_args() );
145 }
146
147 public function getFlag( $flag ) {
148 return $this->__call( __FUNCTION__, func_get_args() );
149 }
150
151 public function getProperty( $name ) {
152 return $this->__call( __FUNCTION__, func_get_args() );
153 }
154
155 public function getDomainID() {
156 if ( $this->conn === null ) {
157 $domain = $this->params[self::FLD_DOMAIN];
158 // Avoid triggering a database connection
159 return $domain instanceof DatabaseDomain ? $domain->getId() : $domain;
160 }
161
162 return $this->__call( __FUNCTION__, func_get_args() );
163 }
164
165 public function getWikiID() {
166 return $this->getDomainID();
167 }
168
169 public function getType() {
170 return $this->__call( __FUNCTION__, func_get_args() );
171 }
172
173 public function open( $server, $user, $password, $dbName ) {
174 return $this->__call( __FUNCTION__, func_get_args() );
175 }
176
177 public function fetchObject( $res ) {
178 return $this->__call( __FUNCTION__, func_get_args() );
179 }
180
181 public function fetchRow( $res ) {
182 return $this->__call( __FUNCTION__, func_get_args() );
183 }
184
185 public function numRows( $res ) {
186 return $this->__call( __FUNCTION__, func_get_args() );
187 }
188
189 public function numFields( $res ) {
190 return $this->__call( __FUNCTION__, func_get_args() );
191 }
192
193 public function fieldName( $res, $n ) {
194 return $this->__call( __FUNCTION__, func_get_args() );
195 }
196
197 public function insertId() {
198 return $this->__call( __FUNCTION__, func_get_args() );
199 }
200
201 public function dataSeek( $res, $row ) {
202 return $this->__call( __FUNCTION__, func_get_args() );
203 }
204
205 public function lastErrno() {
206 return $this->__call( __FUNCTION__, func_get_args() );
207 }
208
209 public function lastError() {
210 return $this->__call( __FUNCTION__, func_get_args() );
211 }
212
213 public function fieldInfo( $table, $field ) {
214 return $this->__call( __FUNCTION__, func_get_args() );
215 }
216
217 public function affectedRows() {
218 return $this->__call( __FUNCTION__, func_get_args() );
219 }
220
221 public function getSoftwareLink() {
222 return $this->__call( __FUNCTION__, func_get_args() );
223 }
224
225 public function getServerVersion() {
226 return $this->__call( __FUNCTION__, func_get_args() );
227 }
228
229 public function close() {
230 return $this->__call( __FUNCTION__, func_get_args() );
231 }
232
233 public function reportConnectionError( $error = 'Unknown error' ) {
234 return $this->__call( __FUNCTION__, func_get_args() );
235 }
236
237 public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
238 return $this->__call( __FUNCTION__, func_get_args() );
239 }
240
241 public function reportQueryError( $error, $errno, $sql, $fname, $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 = []
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 fieldExists( $table, $field, $fname = __METHOD__ ) {
295 return $this->__call( __FUNCTION__, func_get_args() );
296 }
297
298 public function indexExists( $table, $index, $fname = __METHOD__ ) {
299 return $this->__call( __FUNCTION__, func_get_args() );
300 }
301
302 public function tableExists( $table, $fname = __METHOD__ ) {
303 return $this->__call( __FUNCTION__, func_get_args() );
304 }
305
306 public function indexUnique( $table, $index ) {
307 return $this->__call( __FUNCTION__, func_get_args() );
308 }
309
310 public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
311 return $this->__call( __FUNCTION__, func_get_args() );
312 }
313
314 public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
315 return $this->__call( __FUNCTION__, func_get_args() );
316 }
317
318 public function makeList( $a, $mode = self::LIST_COMMA ) {
319 return $this->__call( __FUNCTION__, func_get_args() );
320 }
321
322 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
323 return $this->__call( __FUNCTION__, func_get_args() );
324 }
325
326 public function aggregateValue( $valuedata, $valuename = 'value' ) {
327 return $this->__call( __FUNCTION__, func_get_args() );
328 }
329
330 public function bitNot( $field ) {
331 return $this->__call( __FUNCTION__, func_get_args() );
332 }
333
334 public function bitAnd( $fieldLeft, $fieldRight ) {
335 return $this->__call( __FUNCTION__, func_get_args() );
336 }
337
338 public function bitOr( $fieldLeft, $fieldRight ) {
339 return $this->__call( __FUNCTION__, func_get_args() );
340 }
341
342 public function buildConcat( $stringList ) {
343 return $this->__call( __FUNCTION__, func_get_args() );
344 }
345
346 public function buildGroupConcatField(
347 $delim, $table, $field, $conds = '', $join_conds = []
348 ) {
349 return $this->__call( __FUNCTION__, func_get_args() );
350 }
351
352 public function buildStringCast( $field ) {
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 wasErrorReissuable() {
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 masterPosWait( DBMasterPos $pos, $timeout ) {
463 return $this->__call( __FUNCTION__, func_get_args() );
464 }
465
466 public function getReplicaPos() {
467 return $this->__call( __FUNCTION__, func_get_args() );
468 }
469
470 public function getMasterPos() {
471 return $this->__call( __FUNCTION__, func_get_args() );
472 }
473
474 public function serverIsReadOnly() {
475 return $this->__call( __FUNCTION__, func_get_args() );
476 }
477
478 public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
479 return $this->__call( __FUNCTION__, func_get_args() );
480 }
481
482 public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
483 return $this->__call( __FUNCTION__, func_get_args() );
484 }
485
486 public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
487 return $this->__call( __FUNCTION__, func_get_args() );
488 }
489
490 public function setTransactionListener( $name, callable $callback = null ) {
491 return $this->__call( __FUNCTION__, func_get_args() );
492 }
493
494 public function startAtomic( $fname = __METHOD__ ) {
495 return $this->__call( __FUNCTION__, func_get_args() );
496 }
497
498 public function endAtomic( $fname = __METHOD__ ) {
499 return $this->__call( __FUNCTION__, func_get_args() );
500 }
501
502 public function doAtomicSection( $fname, callable $callback ) {
503 return $this->__call( __FUNCTION__, func_get_args() );
504 }
505
506 public function begin( $fname = __METHOD__, $mode = IDatabase::TRANSACTION_EXPLICIT ) {
507 return $this->__call( __FUNCTION__, func_get_args() );
508 }
509
510 public function commit( $fname = __METHOD__, $flush = '' ) {
511 return $this->__call( __FUNCTION__, func_get_args() );
512 }
513
514 public function rollback( $fname = __METHOD__, $flush = '' ) {
515 return $this->__call( __FUNCTION__, func_get_args() );
516 }
517
518 public function flushSnapshot( $fname = __METHOD__ ) {
519 return $this->__call( __FUNCTION__, func_get_args() );
520 }
521
522 public function listTables( $prefix = null, $fname = __METHOD__ ) {
523 return $this->__call( __FUNCTION__, func_get_args() );
524 }
525
526 public function timestamp( $ts = 0 ) {
527 return $this->__call( __FUNCTION__, func_get_args() );
528 }
529
530 public function timestampOrNull( $ts = null ) {
531 return $this->__call( __FUNCTION__, func_get_args() );
532 }
533
534 public function ping( &$rtt = null ) {
535 return func_num_args()
536 ? $this->__call( __FUNCTION__, [ &$rtt ] )
537 : $this->__call( __FUNCTION__, [] ); // method cares about null vs missing
538 }
539
540 public function getLag() {
541 return $this->__call( __FUNCTION__, func_get_args() );
542 }
543
544 public function getSessionLagStatus() {
545 return $this->__call( __FUNCTION__, func_get_args() );
546 }
547
548 public function maxListLen() {
549 return $this->__call( __FUNCTION__, func_get_args() );
550 }
551
552 public function encodeBlob( $b ) {
553 return $this->__call( __FUNCTION__, func_get_args() );
554 }
555
556 public function decodeBlob( $b ) {
557 return $this->__call( __FUNCTION__, func_get_args() );
558 }
559
560 public function setSessionOptions( array $options ) {
561 return $this->__call( __FUNCTION__, func_get_args() );
562 }
563
564 public function setSchemaVars( $vars ) {
565 return $this->__call( __FUNCTION__, func_get_args() );
566 }
567
568 public function lockIsFree( $lockName, $method ) {
569 return $this->__call( __FUNCTION__, func_get_args() );
570 }
571
572 public function lock( $lockName, $method, $timeout = 5 ) {
573 return $this->__call( __FUNCTION__, func_get_args() );
574 }
575
576 public function unlock( $lockName, $method ) {
577 return $this->__call( __FUNCTION__, func_get_args() );
578 }
579
580 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
581 return $this->__call( __FUNCTION__, func_get_args() );
582 }
583
584 public function namedLocksEnqueue() {
585 return $this->__call( __FUNCTION__, func_get_args() );
586 }
587
588 public function getInfinity() {
589 return $this->__call( __FUNCTION__, func_get_args() );
590 }
591
592 public function encodeExpiry( $expiry ) {
593 return $this->__call( __FUNCTION__, func_get_args() );
594 }
595
596 public function decodeExpiry( $expiry, $format = TS_MW ) {
597 return $this->__call( __FUNCTION__, func_get_args() );
598 }
599
600 public function setBigSelects( $value = true ) {
601 return $this->__call( __FUNCTION__, func_get_args() );
602 }
603
604 public function isReadOnly() {
605 return $this->__call( __FUNCTION__, func_get_args() );
606 }
607
608 public function setTableAliases( array $aliases ) {
609 return $this->__call( __FUNCTION__, func_get_args() );
610 }
611
612 /**
613 * Clean up the connection when out of scope
614 */
615 function __destruct() {
616 if ( $this->conn ) {
617 $this->lb->reuseConnection( $this->conn );
618 }
619 }
620 }
621
622 class_alias( DBConnRef::class, 'DBConnRef' );