rdbms: optimize DBConnRef::getType() to avoid connections when possible
[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 * @ingroup Database
12 * @since 1.22
13 */
14 class DBConnRef implements IDatabase {
15 /** @var ILoadBalancer */
16 private $lb;
17 /** @var Database|null Live connection handle */
18 private $conn;
19 /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */
20 private $params;
21 /** @var int One of DB_MASTER/DB_REPLICA */
22 private $role;
23
24 const FLD_INDEX = 0;
25 const FLD_GROUP = 1;
26 const FLD_DOMAIN = 2;
27 const FLD_FLAGS = 3;
28
29 /**
30 * @param ILoadBalancer $lb Connection manager for $conn
31 * @param Database|array $conn Database or (server index, query groups, domain, flags)
32 * @param int $role The type of connection asked for; one of DB_MASTER/DB_REPLICA
33 * @internal This method should not be called outside of LoadBalancer
34 */
35 public function __construct( ILoadBalancer $lb, $conn, $role ) {
36 $this->lb = $lb;
37 $this->role = $role;
38 if ( $conn instanceof Database ) {
39 $this->conn = $conn; // live handle
40 } elseif ( is_array( $conn ) && count( $conn ) >= 4 && $conn[self::FLD_DOMAIN] !== false ) {
41 $this->params = $conn;
42 } else {
43 throw new InvalidArgumentException( "Missing lazy connection arguments." );
44 }
45 }
46
47 function __call( $name, array $arguments ) {
48 if ( $this->conn === null ) {
49 list( $index, $groups, $wiki, $flags ) = $this->params;
50 $this->conn = $this->lb->getConnection( $index, $groups, $wiki, $flags );
51 }
52
53 return $this->conn->$name( ...$arguments );
54 }
55
56 /**
57 * @return int DB_MASTER when this *requires* the master DB, otherwise DB_REPLICA
58 * @since 1.33
59 */
60 public function getReferenceRole() {
61 return $this->role;
62 }
63
64 public function getServerInfo() {
65 return $this->__call( __FUNCTION__, func_get_args() );
66 }
67
68 public function bufferResults( $buffer = null ) {
69 return $this->__call( __FUNCTION__, func_get_args() );
70 }
71
72 public function trxLevel() {
73 return $this->__call( __FUNCTION__, func_get_args() );
74 }
75
76 public function trxTimestamp() {
77 return $this->__call( __FUNCTION__, func_get_args() );
78 }
79
80 public function explicitTrxActive() {
81 return $this->__call( __FUNCTION__, func_get_args() );
82 }
83
84 public function assertNoOpenTransactions() {
85 return $this->__call( __FUNCTION__, func_get_args() );
86 }
87
88 public function tablePrefix( $prefix = null ) {
89 if ( $this->conn === null && $prefix === null ) {
90 $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
91 // Avoid triggering a database connection
92 return $domain->getTablePrefix();
93 } elseif ( $this->conn !== null && $prefix === null ) {
94 // This will just return the prefix
95 return $this->__call( __FUNCTION__, func_get_args() );
96 }
97 // Disallow things that might confuse the LoadBalancer tracking
98 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
99 }
100
101 public function dbSchema( $schema = null ) {
102 if ( $this->conn === null && $schema === null ) {
103 $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
104 // Avoid triggering a database connection
105 return $domain->getSchema();
106 } elseif ( $this->conn !== null && $schema === null ) {
107 // This will just return the schema
108 return $this->__call( __FUNCTION__, func_get_args() );
109 }
110 // Disallow things that might confuse the LoadBalancer tracking
111 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
112 }
113
114 public function getLBInfo( $name = null ) {
115 return $this->__call( __FUNCTION__, func_get_args() );
116 }
117
118 public function setLBInfo( $name, $value = null ) {
119 // Disallow things that might confuse the LoadBalancer tracking
120 throw new DBUnexpectedError( $this, "Changing LB info is disallowed to enable reuse." );
121 }
122
123 public function setLazyMasterHandle( IDatabase $conn ) {
124 // Disallow things that might confuse the LoadBalancer tracking
125 throw new DBUnexpectedError( $this, "Database injection is disallowed to enable reuse." );
126 }
127
128 public function implicitGroupby() {
129 return $this->__call( __FUNCTION__, func_get_args() );
130 }
131
132 public function implicitOrderby() {
133 return $this->__call( __FUNCTION__, func_get_args() );
134 }
135
136 public function lastQuery() {
137 return $this->__call( __FUNCTION__, func_get_args() );
138 }
139
140 public function doneWrites() {
141 return $this->__call( __FUNCTION__, func_get_args() );
142 }
143
144 public function lastDoneWrites() {
145 return $this->__call( __FUNCTION__, func_get_args() );
146 }
147
148 public function writesPending() {
149 return $this->__call( __FUNCTION__, func_get_args() );
150 }
151
152 public function preCommitCallbacksPending() {
153 return $this->__call( __FUNCTION__, func_get_args() );
154 }
155
156 public function writesOrCallbacksPending() {
157 return $this->__call( __FUNCTION__, func_get_args() );
158 }
159
160 public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
161 return $this->__call( __FUNCTION__, func_get_args() );
162 }
163
164 public function pendingWriteCallers() {
165 return $this->__call( __FUNCTION__, func_get_args() );
166 }
167
168 public function pendingWriteRowsAffected() {
169 return $this->__call( __FUNCTION__, func_get_args() );
170 }
171
172 public function isOpen() {
173 return $this->__call( __FUNCTION__, func_get_args() );
174 }
175
176 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
177 return $this->__call( __FUNCTION__, func_get_args() );
178 }
179
180 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
181 return $this->__call( __FUNCTION__, func_get_args() );
182 }
183
184 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
185 return $this->__call( __FUNCTION__, func_get_args() );
186 }
187
188 public function getFlag( $flag ) {
189 return $this->__call( __FUNCTION__, func_get_args() );
190 }
191
192 public function getProperty( $name ) {
193 return $this->__call( __FUNCTION__, func_get_args() );
194 }
195
196 public function getDomainID() {
197 if ( $this->conn === null ) {
198 $domain = $this->params[self::FLD_DOMAIN];
199 // Avoid triggering a database connection
200 return $domain instanceof DatabaseDomain ? $domain->getId() : $domain;
201 }
202
203 return $this->__call( __FUNCTION__, func_get_args() );
204 }
205
206 /**
207 * @codeCoverageIgnore
208 */
209 public function getWikiID() {
210 return $this->getDomainID();
211 }
212
213 public function getType() {
214 if ( $this->conn === null ) {
215 // Avoid triggering a database connection
216 if ( $this->params[self::FLD_INDEX] === ILoadBalancer::DB_MASTER ) {
217 $index = $this->lb->getWriterIndex();
218 } else {
219 $index = $this->params[self::FLD_INDEX];
220 }
221 if ( $index >= 0 ) {
222 // In theory, if $index is DB_REPLICA, the type could vary
223 return $this->lb->getServerType( $index );
224 }
225 }
226
227 return $this->__call( __FUNCTION__, func_get_args() );
228 }
229
230 public function fetchObject( $res ) {
231 return $this->__call( __FUNCTION__, func_get_args() );
232 }
233
234 public function fetchRow( $res ) {
235 return $this->__call( __FUNCTION__, func_get_args() );
236 }
237
238 public function numRows( $res ) {
239 return $this->__call( __FUNCTION__, func_get_args() );
240 }
241
242 public function numFields( $res ) {
243 return $this->__call( __FUNCTION__, func_get_args() );
244 }
245
246 public function fieldName( $res, $n ) {
247 return $this->__call( __FUNCTION__, func_get_args() );
248 }
249
250 public function insertId() {
251 return $this->__call( __FUNCTION__, func_get_args() );
252 }
253
254 public function dataSeek( $res, $row ) {
255 return $this->__call( __FUNCTION__, func_get_args() );
256 }
257
258 public function lastErrno() {
259 return $this->__call( __FUNCTION__, func_get_args() );
260 }
261
262 public function lastError() {
263 return $this->__call( __FUNCTION__, func_get_args() );
264 }
265
266 public function affectedRows() {
267 return $this->__call( __FUNCTION__, func_get_args() );
268 }
269
270 public function getSoftwareLink() {
271 return $this->__call( __FUNCTION__, func_get_args() );
272 }
273
274 public function getServerVersion() {
275 return $this->__call( __FUNCTION__, func_get_args() );
276 }
277
278 public function close() {
279 throw new DBUnexpectedError( $this->conn, 'Cannot close shared connection.' );
280 }
281
282 public function query( $sql, $fname = __METHOD__, $flags = 0 ) {
283 if ( $this->role !== ILoadBalancer::DB_MASTER ) {
284 $flags |= IDatabase::QUERY_REPLICA_ROLE;
285 }
286
287 return $this->__call( __FUNCTION__, [ $sql, $fname, $flags ] );
288 }
289
290 public function freeResult( $res ) {
291 return $this->__call( __FUNCTION__, func_get_args() );
292 }
293
294 public function selectField(
295 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
296 ) {
297 return $this->__call( __FUNCTION__, func_get_args() );
298 }
299
300 public function selectFieldValues(
301 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
302 ) {
303 return $this->__call( __FUNCTION__, func_get_args() );
304 }
305
306 public function select(
307 $table, $vars, $conds = '', $fname = __METHOD__,
308 $options = [], $join_conds = []
309 ) {
310 return $this->__call( __FUNCTION__, func_get_args() );
311 }
312
313 public function selectSQLText(
314 $table, $vars, $conds = '', $fname = __METHOD__,
315 $options = [], $join_conds = []
316 ) {
317 return $this->__call( __FUNCTION__, func_get_args() );
318 }
319
320 public function limitResult( $sql, $limit, $offset = false ) {
321 return $this->__call( __FUNCTION__, func_get_args() );
322 }
323
324 public function selectRow(
325 $table, $vars, $conds, $fname = __METHOD__,
326 $options = [], $join_conds = []
327 ) {
328 return $this->__call( __FUNCTION__, func_get_args() );
329 }
330
331 public function estimateRowCount(
332 $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
333 ) {
334 return $this->__call( __FUNCTION__, func_get_args() );
335 }
336
337 public function selectRowCount(
338 $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
339 ) {
340 return $this->__call( __FUNCTION__, func_get_args() );
341 }
342
343 public function lockForUpdate(
344 $table, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
345 ) {
346 $this->assertRoleAllowsWrites();
347
348 return $this->__call( __FUNCTION__, func_get_args() );
349 }
350
351 public function fieldExists( $table, $field, $fname = __METHOD__ ) {
352 return $this->__call( __FUNCTION__, func_get_args() );
353 }
354
355 public function indexExists( $table, $index, $fname = __METHOD__ ) {
356 return $this->__call( __FUNCTION__, func_get_args() );
357 }
358
359 public function tableExists( $table, $fname = __METHOD__ ) {
360 return $this->__call( __FUNCTION__, func_get_args() );
361 }
362
363 public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
364 $this->assertRoleAllowsWrites();
365
366 return $this->__call( __FUNCTION__, func_get_args() );
367 }
368
369 public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
370 $this->assertRoleAllowsWrites();
371
372 return $this->__call( __FUNCTION__, func_get_args() );
373 }
374
375 public function makeList( $a, $mode = self::LIST_COMMA ) {
376 return $this->__call( __FUNCTION__, func_get_args() );
377 }
378
379 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
380 return $this->__call( __FUNCTION__, func_get_args() );
381 }
382
383 public function aggregateValue( $valuedata, $valuename = 'value' ) {
384 return $this->__call( __FUNCTION__, func_get_args() );
385 }
386
387 public function bitNot( $field ) {
388 return $this->__call( __FUNCTION__, func_get_args() );
389 }
390
391 public function bitAnd( $fieldLeft, $fieldRight ) {
392 return $this->__call( __FUNCTION__, func_get_args() );
393 }
394
395 public function bitOr( $fieldLeft, $fieldRight ) {
396 return $this->__call( __FUNCTION__, func_get_args() );
397 }
398
399 public function buildConcat( $stringList ) {
400 return $this->__call( __FUNCTION__, func_get_args() );
401 }
402
403 public function buildGroupConcatField(
404 $delim, $table, $field, $conds = '', $join_conds = []
405 ) {
406 return $this->__call( __FUNCTION__, func_get_args() );
407 }
408
409 public function buildSubstring( $input, $startPosition, $length = null ) {
410 return $this->__call( __FUNCTION__, func_get_args() );
411 }
412
413 public function buildStringCast( $field ) {
414 return $this->__call( __FUNCTION__, func_get_args() );
415 }
416
417 public function buildIntegerCast( $field ) {
418 return $this->__call( __FUNCTION__, func_get_args() );
419 }
420
421 public function buildSelectSubquery(
422 $table, $vars, $conds = '', $fname = __METHOD__,
423 $options = [], $join_conds = []
424 ) {
425 return $this->__call( __FUNCTION__, func_get_args() );
426 }
427
428 public function databasesAreIndependent() {
429 return $this->__call( __FUNCTION__, func_get_args() );
430 }
431
432 public function selectDB( $db ) {
433 // Disallow things that might confuse the LoadBalancer tracking
434 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
435 }
436
437 public function selectDomain( $domain ) {
438 // Disallow things that might confuse the LoadBalancer tracking
439 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
440 }
441
442 public function getDBname() {
443 if ( $this->conn === null ) {
444 $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
445 // Avoid triggering a database connection
446 return $domain->getDatabase();
447 }
448
449 return $this->__call( __FUNCTION__, func_get_args() );
450 }
451
452 public function getServer() {
453 return $this->__call( __FUNCTION__, func_get_args() );
454 }
455
456 public function addQuotes( $s ) {
457 return $this->__call( __FUNCTION__, func_get_args() );
458 }
459
460 public function addIdentifierQuotes( $s ) {
461 return $this->__call( __FUNCTION__, func_get_args() );
462 }
463
464 public function buildLike() {
465 return $this->__call( __FUNCTION__, func_get_args() );
466 }
467
468 public function anyChar() {
469 return $this->__call( __FUNCTION__, func_get_args() );
470 }
471
472 public function anyString() {
473 return $this->__call( __FUNCTION__, func_get_args() );
474 }
475
476 public function nextSequenceValue( $seqName ) {
477 $this->assertRoleAllowsWrites();
478
479 return $this->__call( __FUNCTION__, func_get_args() );
480 }
481
482 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
483 $this->assertRoleAllowsWrites();
484
485 return $this->__call( __FUNCTION__, func_get_args() );
486 }
487
488 public function upsert(
489 $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__
490 ) {
491 $this->assertRoleAllowsWrites();
492
493 return $this->__call( __FUNCTION__, func_get_args() );
494 }
495
496 public function deleteJoin(
497 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
498 ) {
499 $this->assertRoleAllowsWrites();
500
501 return $this->__call( __FUNCTION__, func_get_args() );
502 }
503
504 public function delete( $table, $conds, $fname = __METHOD__ ) {
505 $this->assertRoleAllowsWrites();
506
507 return $this->__call( __FUNCTION__, func_get_args() );
508 }
509
510 public function insertSelect(
511 $destTable, $srcTable, $varMap, $conds,
512 $fname = __METHOD__, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
513 ) {
514 $this->assertRoleAllowsWrites();
515
516 return $this->__call( __FUNCTION__, func_get_args() );
517 }
518
519 public function unionSupportsOrderAndLimit() {
520 return $this->__call( __FUNCTION__, func_get_args() );
521 }
522
523 public function unionQueries( $sqls, $all ) {
524 return $this->__call( __FUNCTION__, func_get_args() );
525 }
526
527 public function unionConditionPermutations(
528 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__,
529 $options = [], $join_conds = []
530 ) {
531 return $this->__call( __FUNCTION__, func_get_args() );
532 }
533
534 public function conditional( $cond, $trueVal, $falseVal ) {
535 return $this->__call( __FUNCTION__, func_get_args() );
536 }
537
538 public function strreplace( $orig, $old, $new ) {
539 return $this->__call( __FUNCTION__, func_get_args() );
540 }
541
542 public function getServerUptime() {
543 return $this->__call( __FUNCTION__, func_get_args() );
544 }
545
546 public function wasDeadlock() {
547 return $this->__call( __FUNCTION__, func_get_args() );
548 }
549
550 public function wasLockTimeout() {
551 return $this->__call( __FUNCTION__, func_get_args() );
552 }
553
554 public function wasConnectionLoss() {
555 return $this->__call( __FUNCTION__, func_get_args() );
556 }
557
558 public function wasReadOnlyError() {
559 return $this->__call( __FUNCTION__, func_get_args() );
560 }
561
562 public function wasErrorReissuable() {
563 return $this->__call( __FUNCTION__, func_get_args() );
564 }
565
566 public function masterPosWait( DBMasterPos $pos, $timeout ) {
567 return $this->__call( __FUNCTION__, func_get_args() );
568 }
569
570 public function getReplicaPos() {
571 return $this->__call( __FUNCTION__, func_get_args() );
572 }
573
574 public function getMasterPos() {
575 return $this->__call( __FUNCTION__, func_get_args() );
576 }
577
578 public function serverIsReadOnly() {
579 return $this->__call( __FUNCTION__, func_get_args() );
580 }
581
582 public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
583 // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
584 return $this->__call( __FUNCTION__, func_get_args() );
585 }
586
587 public function onTransactionCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
588 // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
589 return $this->__call( __FUNCTION__, func_get_args() );
590 }
591
592 public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
593 return $this->onTransactionCommitOrIdle( $callback, $fname );
594 }
595
596 public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
597 // DB_REPLICA role: caller might want to refresh cache after a cache mutex is released
598 return $this->__call( __FUNCTION__, func_get_args() );
599 }
600
601 public function setTransactionListener( $name, callable $callback = null ) {
602 return $this->__call( __FUNCTION__, func_get_args() );
603 }
604
605 public function startAtomic(
606 $fname = __METHOD__, $cancelable = IDatabase::ATOMIC_NOT_CANCELABLE
607 ) {
608 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
609 return $this->__call( __FUNCTION__, func_get_args() );
610 }
611
612 public function endAtomic( $fname = __METHOD__ ) {
613 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
614 return $this->__call( __FUNCTION__, func_get_args() );
615 }
616
617 public function cancelAtomic( $fname = __METHOD__, AtomicSectionIdentifier $sectionId = null ) {
618 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
619 return $this->__call( __FUNCTION__, func_get_args() );
620 }
621
622 public function doAtomicSection(
623 $fname, callable $callback, $cancelable = self::ATOMIC_NOT_CANCELABLE
624 ) {
625 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
626 return $this->__call( __FUNCTION__, func_get_args() );
627 }
628
629 public function begin( $fname = __METHOD__, $mode = IDatabase::TRANSACTION_EXPLICIT ) {
630 return $this->__call( __FUNCTION__, func_get_args() );
631 }
632
633 public function commit( $fname = __METHOD__, $flush = '' ) {
634 return $this->__call( __FUNCTION__, func_get_args() );
635 }
636
637 public function rollback( $fname = __METHOD__, $flush = '' ) {
638 return $this->__call( __FUNCTION__, func_get_args() );
639 }
640
641 public function flushSnapshot( $fname = __METHOD__ ) {
642 return $this->__call( __FUNCTION__, func_get_args() );
643 }
644
645 public function timestamp( $ts = 0 ) {
646 return $this->__call( __FUNCTION__, func_get_args() );
647 }
648
649 public function timestampOrNull( $ts = null ) {
650 return $this->__call( __FUNCTION__, func_get_args() );
651 }
652
653 public function ping( &$rtt = null ) {
654 return func_num_args()
655 ? $this->__call( __FUNCTION__, [ &$rtt ] )
656 : $this->__call( __FUNCTION__, [] ); // method cares about null vs missing
657 }
658
659 public function getLag() {
660 return $this->__call( __FUNCTION__, func_get_args() );
661 }
662
663 public function getSessionLagStatus() {
664 return $this->__call( __FUNCTION__, func_get_args() );
665 }
666
667 public function maxListLen() {
668 return $this->__call( __FUNCTION__, func_get_args() );
669 }
670
671 public function encodeBlob( $b ) {
672 return $this->__call( __FUNCTION__, func_get_args() );
673 }
674
675 public function decodeBlob( $b ) {
676 return $this->__call( __FUNCTION__, func_get_args() );
677 }
678
679 public function setSessionOptions( array $options ) {
680 return $this->__call( __FUNCTION__, func_get_args() );
681 }
682
683 public function setSchemaVars( $vars ) {
684 return $this->__call( __FUNCTION__, func_get_args() );
685 }
686
687 public function lockIsFree( $lockName, $method ) {
688 $this->assertRoleAllowsWrites();
689
690 return $this->__call( __FUNCTION__, func_get_args() );
691 }
692
693 public function lock( $lockName, $method, $timeout = 5 ) {
694 $this->assertRoleAllowsWrites();
695
696 return $this->__call( __FUNCTION__, func_get_args() );
697 }
698
699 public function unlock( $lockName, $method ) {
700 $this->assertRoleAllowsWrites();
701
702 return $this->__call( __FUNCTION__, func_get_args() );
703 }
704
705 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
706 $this->assertRoleAllowsWrites();
707
708 return $this->__call( __FUNCTION__, func_get_args() );
709 }
710
711 public function namedLocksEnqueue() {
712 return $this->__call( __FUNCTION__, func_get_args() );
713 }
714
715 public function getInfinity() {
716 return $this->__call( __FUNCTION__, func_get_args() );
717 }
718
719 public function encodeExpiry( $expiry ) {
720 return $this->__call( __FUNCTION__, func_get_args() );
721 }
722
723 public function decodeExpiry( $expiry, $format = TS_MW ) {
724 return $this->__call( __FUNCTION__, func_get_args() );
725 }
726
727 public function setBigSelects( $value = true ) {
728 return $this->__call( __FUNCTION__, func_get_args() );
729 }
730
731 public function isReadOnly() {
732 return $this->__call( __FUNCTION__, func_get_args() );
733 }
734
735 public function setTableAliases( array $aliases ) {
736 return $this->__call( __FUNCTION__, func_get_args() );
737 }
738
739 public function setIndexAliases( array $aliases ) {
740 return $this->__call( __FUNCTION__, func_get_args() );
741 }
742
743 /**
744 * Error out if the role is not DB_MASTER
745 *
746 * Note that the underlying connection may or may not itself be read-only.
747 * It could even be to a writable master (both server-side and to the application).
748 * This error is meant for the case when a DB_REPLICA handle was requested but a
749 * a write was attempted on that handle regardless.
750 *
751 * In configurations where the master DB has some generic read load or is the only server,
752 * DB_MASTER/DB_REPLICA will sometimes (or always) use the same connection to the master DB.
753 * This does not effect the role of DBConnRef instances.
754 * @throws DBReadOnlyRoleError
755 */
756 protected function assertRoleAllowsWrites() {
757 // DB_MASTER is "prima facie" writable
758 if ( $this->role !== ILoadBalancer::DB_MASTER ) {
759 throw new DBReadOnlyRoleError( $this->conn, "Cannot write with role DB_REPLICA" );
760 }
761 }
762
763 /**
764 * Clean up the connection when out of scope
765 */
766 function __destruct() {
767 if ( $this->conn ) {
768 $this->lb->reuseConnection( $this->conn );
769 }
770 }
771 }
772
773 /**
774 * @since 1.22
775 * @deprecated since 1.29
776 */
777 class_alias( DBConnRef::class, 'DBConnRef' );