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