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