Merge "Drop index oi_name_archive_name on table oldimage"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DBConnRef.php
1 <?php
2
3 use Wikimedia\Rdbms\DatabaseDomain;
4 use Wikimedia\Rdbms\ILoadBalancer;
5 use Wikimedia\Rdbms\DBMasterPos;
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 explicity 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 isOpen() {
128 return $this->__call( __FUNCTION__, func_get_args() );
129 }
130
131 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
132 return $this->__call( __FUNCTION__, func_get_args() );
133 }
134
135 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
136 return $this->__call( __FUNCTION__, func_get_args() );
137 }
138
139 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
140 return $this->__call( __FUNCTION__, func_get_args() );
141 }
142
143 public function getFlag( $flag ) {
144 return $this->__call( __FUNCTION__, func_get_args() );
145 }
146
147 public function getProperty( $name ) {
148 return $this->__call( __FUNCTION__, func_get_args() );
149 }
150
151 public function getDomainID() {
152 if ( $this->conn === null ) {
153 $domain = $this->params[self::FLD_DOMAIN];
154 // Avoid triggering a database connection
155 return $domain instanceof DatabaseDomain ? $domain->getId() : $domain;
156 }
157
158 return $this->__call( __FUNCTION__, func_get_args() );
159 }
160
161 public function getWikiID() {
162 return $this->getDomainID();
163 }
164
165 public function getType() {
166 return $this->__call( __FUNCTION__, func_get_args() );
167 }
168
169 public function open( $server, $user, $password, $dbName ) {
170 return $this->__call( __FUNCTION__, func_get_args() );
171 }
172
173 public function fetchObject( $res ) {
174 return $this->__call( __FUNCTION__, func_get_args() );
175 }
176
177 public function fetchRow( $res ) {
178 return $this->__call( __FUNCTION__, func_get_args() );
179 }
180
181 public function numRows( $res ) {
182 return $this->__call( __FUNCTION__, func_get_args() );
183 }
184
185 public function numFields( $res ) {
186 return $this->__call( __FUNCTION__, func_get_args() );
187 }
188
189 public function fieldName( $res, $n ) {
190 return $this->__call( __FUNCTION__, func_get_args() );
191 }
192
193 public function insertId() {
194 return $this->__call( __FUNCTION__, func_get_args() );
195 }
196
197 public function dataSeek( $res, $row ) {
198 return $this->__call( __FUNCTION__, func_get_args() );
199 }
200
201 public function lastErrno() {
202 return $this->__call( __FUNCTION__, func_get_args() );
203 }
204
205 public function lastError() {
206 return $this->__call( __FUNCTION__, func_get_args() );
207 }
208
209 public function fieldInfo( $table, $field ) {
210 return $this->__call( __FUNCTION__, func_get_args() );
211 }
212
213 public function affectedRows() {
214 return $this->__call( __FUNCTION__, func_get_args() );
215 }
216
217 public function getSoftwareLink() {
218 return $this->__call( __FUNCTION__, func_get_args() );
219 }
220
221 public function getServerVersion() {
222 return $this->__call( __FUNCTION__, func_get_args() );
223 }
224
225 public function close() {
226 return $this->__call( __FUNCTION__, func_get_args() );
227 }
228
229 public function reportConnectionError( $error = 'Unknown error' ) {
230 return $this->__call( __FUNCTION__, func_get_args() );
231 }
232
233 public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
234 return $this->__call( __FUNCTION__, func_get_args() );
235 }
236
237 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
238 return $this->__call( __FUNCTION__, func_get_args() );
239 }
240
241 public function freeResult( $res ) {
242 return $this->__call( __FUNCTION__, func_get_args() );
243 }
244
245 public function selectField(
246 $table, $var, $cond = '', $fname = __METHOD__, $options = []
247 ) {
248 return $this->__call( __FUNCTION__, func_get_args() );
249 }
250
251 public function selectFieldValues(
252 $table, $var, $cond = '', $fname = __METHOD__, $options = []
253 ) {
254 return $this->__call( __FUNCTION__, func_get_args() );
255 }
256
257 public function select(
258 $table, $vars, $conds = '', $fname = __METHOD__,
259 $options = [], $join_conds = []
260 ) {
261 return $this->__call( __FUNCTION__, func_get_args() );
262 }
263
264 public function selectSQLText(
265 $table, $vars, $conds = '', $fname = __METHOD__,
266 $options = [], $join_conds = []
267 ) {
268 return $this->__call( __FUNCTION__, func_get_args() );
269 }
270
271 public function selectRow(
272 $table, $vars, $conds, $fname = __METHOD__,
273 $options = [], $join_conds = []
274 ) {
275 return $this->__call( __FUNCTION__, func_get_args() );
276 }
277
278 public function estimateRowCount(
279 $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = []
280 ) {
281 return $this->__call( __FUNCTION__, func_get_args() );
282 }
283
284 public function selectRowCount(
285 $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
286 ) {
287 return $this->__call( __FUNCTION__, func_get_args() );
288 }
289
290 public function fieldExists( $table, $field, $fname = __METHOD__ ) {
291 return $this->__call( __FUNCTION__, func_get_args() );
292 }
293
294 public function indexExists( $table, $index, $fname = __METHOD__ ) {
295 return $this->__call( __FUNCTION__, func_get_args() );
296 }
297
298 public function tableExists( $table, $fname = __METHOD__ ) {
299 return $this->__call( __FUNCTION__, func_get_args() );
300 }
301
302 public function indexUnique( $table, $index ) {
303 return $this->__call( __FUNCTION__, func_get_args() );
304 }
305
306 public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
307 return $this->__call( __FUNCTION__, func_get_args() );
308 }
309
310 public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
311 return $this->__call( __FUNCTION__, func_get_args() );
312 }
313
314 public function makeList( $a, $mode = self::LIST_COMMA ) {
315 return $this->__call( __FUNCTION__, func_get_args() );
316 }
317
318 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
319 return $this->__call( __FUNCTION__, func_get_args() );
320 }
321
322 public function aggregateValue( $valuedata, $valuename = 'value' ) {
323 return $this->__call( __FUNCTION__, func_get_args() );
324 }
325
326 public function bitNot( $field ) {
327 return $this->__call( __FUNCTION__, func_get_args() );
328 }
329
330 public function bitAnd( $fieldLeft, $fieldRight ) {
331 return $this->__call( __FUNCTION__, func_get_args() );
332 }
333
334 public function bitOr( $fieldLeft, $fieldRight ) {
335 return $this->__call( __FUNCTION__, func_get_args() );
336 }
337
338 public function buildConcat( $stringList ) {
339 return $this->__call( __FUNCTION__, func_get_args() );
340 }
341
342 public function buildGroupConcatField(
343 $delim, $table, $field, $conds = '', $join_conds = []
344 ) {
345 return $this->__call( __FUNCTION__, func_get_args() );
346 }
347
348 public function buildStringCast( $field ) {
349 return $this->__call( __FUNCTION__, func_get_args() );
350 }
351
352 public function selectDB( $db ) {
353 return $this->__call( __FUNCTION__, func_get_args() );
354 }
355
356 public function getDBname() {
357 return $this->__call( __FUNCTION__, func_get_args() );
358 }
359
360 public function getServer() {
361 return $this->__call( __FUNCTION__, func_get_args() );
362 }
363
364 public function addQuotes( $s ) {
365 return $this->__call( __FUNCTION__, func_get_args() );
366 }
367
368 public function buildLike() {
369 return $this->__call( __FUNCTION__, func_get_args() );
370 }
371
372 public function anyChar() {
373 return $this->__call( __FUNCTION__, func_get_args() );
374 }
375
376 public function anyString() {
377 return $this->__call( __FUNCTION__, func_get_args() );
378 }
379
380 public function nextSequenceValue( $seqName ) {
381 return $this->__call( __FUNCTION__, func_get_args() );
382 }
383
384 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
385 return $this->__call( __FUNCTION__, func_get_args() );
386 }
387
388 public function upsert(
389 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
390 ) {
391 return $this->__call( __FUNCTION__, func_get_args() );
392 }
393
394 public function deleteJoin(
395 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
396 ) {
397 return $this->__call( __FUNCTION__, func_get_args() );
398 }
399
400 public function delete( $table, $conds, $fname = __METHOD__ ) {
401 return $this->__call( __FUNCTION__, func_get_args() );
402 }
403
404 public function insertSelect(
405 $destTable, $srcTable, $varMap, $conds,
406 $fname = __METHOD__, $insertOptions = [], $selectOptions = []
407 ) {
408 return $this->__call( __FUNCTION__, func_get_args() );
409 }
410
411 public function unionSupportsOrderAndLimit() {
412 return $this->__call( __FUNCTION__, func_get_args() );
413 }
414
415 public function unionQueries( $sqls, $all ) {
416 return $this->__call( __FUNCTION__, func_get_args() );
417 }
418
419 public function conditional( $cond, $trueVal, $falseVal ) {
420 return $this->__call( __FUNCTION__, func_get_args() );
421 }
422
423 public function strreplace( $orig, $old, $new ) {
424 return $this->__call( __FUNCTION__, func_get_args() );
425 }
426
427 public function getServerUptime() {
428 return $this->__call( __FUNCTION__, func_get_args() );
429 }
430
431 public function wasDeadlock() {
432 return $this->__call( __FUNCTION__, func_get_args() );
433 }
434
435 public function wasLockTimeout() {
436 return $this->__call( __FUNCTION__, func_get_args() );
437 }
438
439 public function wasErrorReissuable() {
440 return $this->__call( __FUNCTION__, func_get_args() );
441 }
442
443 public function wasReadOnlyError() {
444 return $this->__call( __FUNCTION__, func_get_args() );
445 }
446
447 public function masterPosWait( DBMasterPos $pos, $timeout ) {
448 return $this->__call( __FUNCTION__, func_get_args() );
449 }
450
451 public function getReplicaPos() {
452 return $this->__call( __FUNCTION__, func_get_args() );
453 }
454
455 public function getMasterPos() {
456 return $this->__call( __FUNCTION__, func_get_args() );
457 }
458
459 public function serverIsReadOnly() {
460 return $this->__call( __FUNCTION__, func_get_args() );
461 }
462
463 public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
464 return $this->__call( __FUNCTION__, func_get_args() );
465 }
466
467 public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
468 return $this->__call( __FUNCTION__, func_get_args() );
469 }
470
471 public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
472 return $this->__call( __FUNCTION__, func_get_args() );
473 }
474
475 public function setTransactionListener( $name, callable $callback = null ) {
476 return $this->__call( __FUNCTION__, func_get_args() );
477 }
478
479 public function startAtomic( $fname = __METHOD__ ) {
480 return $this->__call( __FUNCTION__, func_get_args() );
481 }
482
483 public function endAtomic( $fname = __METHOD__ ) {
484 return $this->__call( __FUNCTION__, func_get_args() );
485 }
486
487 public function doAtomicSection( $fname, callable $callback ) {
488 return $this->__call( __FUNCTION__, func_get_args() );
489 }
490
491 public function begin( $fname = __METHOD__, $mode = IDatabase::TRANSACTION_EXPLICIT ) {
492 return $this->__call( __FUNCTION__, func_get_args() );
493 }
494
495 public function commit( $fname = __METHOD__, $flush = '' ) {
496 return $this->__call( __FUNCTION__, func_get_args() );
497 }
498
499 public function rollback( $fname = __METHOD__, $flush = '' ) {
500 return $this->__call( __FUNCTION__, func_get_args() );
501 }
502
503 public function flushSnapshot( $fname = __METHOD__ ) {
504 return $this->__call( __FUNCTION__, func_get_args() );
505 }
506
507 public function listTables( $prefix = null, $fname = __METHOD__ ) {
508 return $this->__call( __FUNCTION__, func_get_args() );
509 }
510
511 public function timestamp( $ts = 0 ) {
512 return $this->__call( __FUNCTION__, func_get_args() );
513 }
514
515 public function timestampOrNull( $ts = null ) {
516 return $this->__call( __FUNCTION__, func_get_args() );
517 }
518
519 public function ping( &$rtt = null ) {
520 return func_num_args()
521 ? $this->__call( __FUNCTION__, [ &$rtt ] )
522 : $this->__call( __FUNCTION__, [] ); // method cares about null vs missing
523 }
524
525 public function getLag() {
526 return $this->__call( __FUNCTION__, func_get_args() );
527 }
528
529 public function getSessionLagStatus() {
530 return $this->__call( __FUNCTION__, func_get_args() );
531 }
532
533 public function maxListLen() {
534 return $this->__call( __FUNCTION__, func_get_args() );
535 }
536
537 public function encodeBlob( $b ) {
538 return $this->__call( __FUNCTION__, func_get_args() );
539 }
540
541 public function decodeBlob( $b ) {
542 return $this->__call( __FUNCTION__, func_get_args() );
543 }
544
545 public function setSessionOptions( array $options ) {
546 return $this->__call( __FUNCTION__, func_get_args() );
547 }
548
549 public function setSchemaVars( $vars ) {
550 return $this->__call( __FUNCTION__, func_get_args() );
551 }
552
553 public function lockIsFree( $lockName, $method ) {
554 return $this->__call( __FUNCTION__, func_get_args() );
555 }
556
557 public function lock( $lockName, $method, $timeout = 5 ) {
558 return $this->__call( __FUNCTION__, func_get_args() );
559 }
560
561 public function unlock( $lockName, $method ) {
562 return $this->__call( __FUNCTION__, func_get_args() );
563 }
564
565 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
566 return $this->__call( __FUNCTION__, func_get_args() );
567 }
568
569 public function namedLocksEnqueue() {
570 return $this->__call( __FUNCTION__, func_get_args() );
571 }
572
573 public function getInfinity() {
574 return $this->__call( __FUNCTION__, func_get_args() );
575 }
576
577 public function encodeExpiry( $expiry ) {
578 return $this->__call( __FUNCTION__, func_get_args() );
579 }
580
581 public function decodeExpiry( $expiry, $format = TS_MW ) {
582 return $this->__call( __FUNCTION__, func_get_args() );
583 }
584
585 public function setBigSelects( $value = true ) {
586 return $this->__call( __FUNCTION__, func_get_args() );
587 }
588
589 public function isReadOnly() {
590 return $this->__call( __FUNCTION__, func_get_args() );
591 }
592
593 public function setTableAliases( array $aliases ) {
594 return $this->__call( __FUNCTION__, func_get_args() );
595 }
596
597 /**
598 * Clean up the connection when out of scope
599 */
600 function __destruct() {
601 if ( $this->conn ) {
602 $this->lb->reuseConnection( $this->conn );
603 }
604 }
605 }