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