Fix order of @var parameter in PHP
[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( $db, $groups, $wiki, $flags ) = $this->params;
50 $this->conn = $this->lb->getConnection( $db, $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 return $this->__call( __FUNCTION__, func_get_args() );
215 }
216
217 public function fetchObject( $res ) {
218 return $this->__call( __FUNCTION__, func_get_args() );
219 }
220
221 public function fetchRow( $res ) {
222 return $this->__call( __FUNCTION__, func_get_args() );
223 }
224
225 public function numRows( $res ) {
226 return $this->__call( __FUNCTION__, func_get_args() );
227 }
228
229 public function numFields( $res ) {
230 return $this->__call( __FUNCTION__, func_get_args() );
231 }
232
233 public function fieldName( $res, $n ) {
234 return $this->__call( __FUNCTION__, func_get_args() );
235 }
236
237 public function insertId() {
238 return $this->__call( __FUNCTION__, func_get_args() );
239 }
240
241 public function dataSeek( $res, $row ) {
242 return $this->__call( __FUNCTION__, func_get_args() );
243 }
244
245 public function lastErrno() {
246 return $this->__call( __FUNCTION__, func_get_args() );
247 }
248
249 public function lastError() {
250 return $this->__call( __FUNCTION__, func_get_args() );
251 }
252
253 public function affectedRows() {
254 return $this->__call( __FUNCTION__, func_get_args() );
255 }
256
257 public function getSoftwareLink() {
258 return $this->__call( __FUNCTION__, func_get_args() );
259 }
260
261 public function getServerVersion() {
262 return $this->__call( __FUNCTION__, func_get_args() );
263 }
264
265 public function close() {
266 throw new DBUnexpectedError( $this->conn, 'Cannot close shared connection.' );
267 }
268
269 public function query( $sql, $fname = __METHOD__, $flags = 0 ) {
270 if ( $this->role !== ILoadBalancer::DB_MASTER ) {
271 $flags |= IDatabase::QUERY_REPLICA_ROLE;
272 }
273
274 return $this->__call( __FUNCTION__, [ $sql, $fname, $flags ] );
275 }
276
277 public function freeResult( $res ) {
278 return $this->__call( __FUNCTION__, func_get_args() );
279 }
280
281 public function selectField(
282 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
283 ) {
284 return $this->__call( __FUNCTION__, func_get_args() );
285 }
286
287 public function selectFieldValues(
288 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
289 ) {
290 return $this->__call( __FUNCTION__, func_get_args() );
291 }
292
293 public function select(
294 $table, $vars, $conds = '', $fname = __METHOD__,
295 $options = [], $join_conds = []
296 ) {
297 return $this->__call( __FUNCTION__, func_get_args() );
298 }
299
300 public function selectSQLText(
301 $table, $vars, $conds = '', $fname = __METHOD__,
302 $options = [], $join_conds = []
303 ) {
304 return $this->__call( __FUNCTION__, func_get_args() );
305 }
306
307 public function selectRow(
308 $table, $vars, $conds, $fname = __METHOD__,
309 $options = [], $join_conds = []
310 ) {
311 return $this->__call( __FUNCTION__, func_get_args() );
312 }
313
314 public function estimateRowCount(
315 $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
316 ) {
317 return $this->__call( __FUNCTION__, func_get_args() );
318 }
319
320 public function selectRowCount(
321 $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
322 ) {
323 return $this->__call( __FUNCTION__, func_get_args() );
324 }
325
326 public function lockForUpdate(
327 $table, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
328 ) {
329 $this->assertRoleAllowsWrites();
330
331 return $this->__call( __FUNCTION__, func_get_args() );
332 }
333
334 public function fieldExists( $table, $field, $fname = __METHOD__ ) {
335 return $this->__call( __FUNCTION__, func_get_args() );
336 }
337
338 public function indexExists( $table, $index, $fname = __METHOD__ ) {
339 return $this->__call( __FUNCTION__, func_get_args() );
340 }
341
342 public function tableExists( $table, $fname = __METHOD__ ) {
343 return $this->__call( __FUNCTION__, func_get_args() );
344 }
345
346 public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
347 $this->assertRoleAllowsWrites();
348
349 return $this->__call( __FUNCTION__, func_get_args() );
350 }
351
352 public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
353 $this->assertRoleAllowsWrites();
354
355 return $this->__call( __FUNCTION__, func_get_args() );
356 }
357
358 public function makeList( $a, $mode = self::LIST_COMMA ) {
359 return $this->__call( __FUNCTION__, func_get_args() );
360 }
361
362 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
363 return $this->__call( __FUNCTION__, func_get_args() );
364 }
365
366 public function aggregateValue( $valuedata, $valuename = 'value' ) {
367 return $this->__call( __FUNCTION__, func_get_args() );
368 }
369
370 public function bitNot( $field ) {
371 return $this->__call( __FUNCTION__, func_get_args() );
372 }
373
374 public function bitAnd( $fieldLeft, $fieldRight ) {
375 return $this->__call( __FUNCTION__, func_get_args() );
376 }
377
378 public function bitOr( $fieldLeft, $fieldRight ) {
379 return $this->__call( __FUNCTION__, func_get_args() );
380 }
381
382 public function buildConcat( $stringList ) {
383 return $this->__call( __FUNCTION__, func_get_args() );
384 }
385
386 public function buildGroupConcatField(
387 $delim, $table, $field, $conds = '', $join_conds = []
388 ) {
389 return $this->__call( __FUNCTION__, func_get_args() );
390 }
391
392 public function buildSubstring( $input, $startPosition, $length = null ) {
393 return $this->__call( __FUNCTION__, func_get_args() );
394 }
395
396 public function buildStringCast( $field ) {
397 return $this->__call( __FUNCTION__, func_get_args() );
398 }
399
400 public function buildIntegerCast( $field ) {
401 return $this->__call( __FUNCTION__, func_get_args() );
402 }
403
404 public function buildSelectSubquery(
405 $table, $vars, $conds = '', $fname = __METHOD__,
406 $options = [], $join_conds = []
407 ) {
408 return $this->__call( __FUNCTION__, func_get_args() );
409 }
410
411 public function databasesAreIndependent() {
412 return $this->__call( __FUNCTION__, func_get_args() );
413 }
414
415 public function selectDB( $db ) {
416 // Disallow things that might confuse the LoadBalancer tracking
417 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
418 }
419
420 public function selectDomain( $domain ) {
421 // Disallow things that might confuse the LoadBalancer tracking
422 throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
423 }
424
425 public function getDBname() {
426 if ( $this->conn === null ) {
427 $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
428 // Avoid triggering a database connection
429 return $domain->getDatabase();
430 }
431
432 return $this->__call( __FUNCTION__, func_get_args() );
433 }
434
435 public function getServer() {
436 return $this->__call( __FUNCTION__, func_get_args() );
437 }
438
439 public function addQuotes( $s ) {
440 return $this->__call( __FUNCTION__, func_get_args() );
441 }
442
443 public function addIdentifierQuotes( $s ) {
444 return $this->__call( __FUNCTION__, func_get_args() );
445 }
446
447 public function buildLike() {
448 return $this->__call( __FUNCTION__, func_get_args() );
449 }
450
451 public function anyChar() {
452 return $this->__call( __FUNCTION__, func_get_args() );
453 }
454
455 public function anyString() {
456 return $this->__call( __FUNCTION__, func_get_args() );
457 }
458
459 public function nextSequenceValue( $seqName ) {
460 $this->assertRoleAllowsWrites();
461
462 return $this->__call( __FUNCTION__, func_get_args() );
463 }
464
465 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
466 $this->assertRoleAllowsWrites();
467
468 return $this->__call( __FUNCTION__, func_get_args() );
469 }
470
471 public function upsert(
472 $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__
473 ) {
474 $this->assertRoleAllowsWrites();
475
476 return $this->__call( __FUNCTION__, func_get_args() );
477 }
478
479 public function deleteJoin(
480 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
481 ) {
482 $this->assertRoleAllowsWrites();
483
484 return $this->__call( __FUNCTION__, func_get_args() );
485 }
486
487 public function delete( $table, $conds, $fname = __METHOD__ ) {
488 $this->assertRoleAllowsWrites();
489
490 return $this->__call( __FUNCTION__, func_get_args() );
491 }
492
493 public function insertSelect(
494 $destTable, $srcTable, $varMap, $conds,
495 $fname = __METHOD__, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
496 ) {
497 $this->assertRoleAllowsWrites();
498
499 return $this->__call( __FUNCTION__, func_get_args() );
500 }
501
502 public function unionSupportsOrderAndLimit() {
503 return $this->__call( __FUNCTION__, func_get_args() );
504 }
505
506 public function unionQueries( $sqls, $all ) {
507 return $this->__call( __FUNCTION__, func_get_args() );
508 }
509
510 public function unionConditionPermutations(
511 $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__,
512 $options = [], $join_conds = []
513 ) {
514 return $this->__call( __FUNCTION__, func_get_args() );
515 }
516
517 public function conditional( $cond, $trueVal, $falseVal ) {
518 return $this->__call( __FUNCTION__, func_get_args() );
519 }
520
521 public function strreplace( $orig, $old, $new ) {
522 return $this->__call( __FUNCTION__, func_get_args() );
523 }
524
525 public function getServerUptime() {
526 return $this->__call( __FUNCTION__, func_get_args() );
527 }
528
529 public function wasDeadlock() {
530 return $this->__call( __FUNCTION__, func_get_args() );
531 }
532
533 public function wasLockTimeout() {
534 return $this->__call( __FUNCTION__, func_get_args() );
535 }
536
537 public function wasConnectionLoss() {
538 return $this->__call( __FUNCTION__, func_get_args() );
539 }
540
541 public function wasReadOnlyError() {
542 return $this->__call( __FUNCTION__, func_get_args() );
543 }
544
545 public function wasErrorReissuable() {
546 return $this->__call( __FUNCTION__, func_get_args() );
547 }
548
549 public function masterPosWait( DBMasterPos $pos, $timeout ) {
550 return $this->__call( __FUNCTION__, func_get_args() );
551 }
552
553 public function getReplicaPos() {
554 return $this->__call( __FUNCTION__, func_get_args() );
555 }
556
557 public function getMasterPos() {
558 return $this->__call( __FUNCTION__, func_get_args() );
559 }
560
561 public function serverIsReadOnly() {
562 return $this->__call( __FUNCTION__, func_get_args() );
563 }
564
565 public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
566 // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
567 return $this->__call( __FUNCTION__, func_get_args() );
568 }
569
570 public function onTransactionCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
571 // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
572 return $this->__call( __FUNCTION__, func_get_args() );
573 }
574
575 public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
576 return $this->onTransactionCommitOrIdle( $callback, $fname );
577 }
578
579 public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
580 // DB_REPLICA role: caller might want to refresh cache after a cache mutex is released
581 return $this->__call( __FUNCTION__, func_get_args() );
582 }
583
584 public function setTransactionListener( $name, callable $callback = null ) {
585 return $this->__call( __FUNCTION__, func_get_args() );
586 }
587
588 public function startAtomic(
589 $fname = __METHOD__, $cancelable = IDatabase::ATOMIC_NOT_CANCELABLE
590 ) {
591 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
592 return $this->__call( __FUNCTION__, func_get_args() );
593 }
594
595 public function endAtomic( $fname = __METHOD__ ) {
596 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
597 return $this->__call( __FUNCTION__, func_get_args() );
598 }
599
600 public function cancelAtomic( $fname = __METHOD__, AtomicSectionIdentifier $sectionId = null ) {
601 // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
602 return $this->__call( __FUNCTION__, func_get_args() );
603 }
604
605 public function doAtomicSection(
606 $fname, callable $callback, $cancelable = self::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 begin( $fname = __METHOD__, $mode = IDatabase::TRANSACTION_EXPLICIT ) {
613 return $this->__call( __FUNCTION__, func_get_args() );
614 }
615
616 public function commit( $fname = __METHOD__, $flush = '' ) {
617 return $this->__call( __FUNCTION__, func_get_args() );
618 }
619
620 public function rollback( $fname = __METHOD__, $flush = '' ) {
621 return $this->__call( __FUNCTION__, func_get_args() );
622 }
623
624 public function flushSnapshot( $fname = __METHOD__ ) {
625 return $this->__call( __FUNCTION__, func_get_args() );
626 }
627
628 public function timestamp( $ts = 0 ) {
629 return $this->__call( __FUNCTION__, func_get_args() );
630 }
631
632 public function timestampOrNull( $ts = null ) {
633 return $this->__call( __FUNCTION__, func_get_args() );
634 }
635
636 public function ping( &$rtt = null ) {
637 return func_num_args()
638 ? $this->__call( __FUNCTION__, [ &$rtt ] )
639 : $this->__call( __FUNCTION__, [] ); // method cares about null vs missing
640 }
641
642 public function getLag() {
643 return $this->__call( __FUNCTION__, func_get_args() );
644 }
645
646 public function getSessionLagStatus() {
647 return $this->__call( __FUNCTION__, func_get_args() );
648 }
649
650 public function maxListLen() {
651 return $this->__call( __FUNCTION__, func_get_args() );
652 }
653
654 public function encodeBlob( $b ) {
655 return $this->__call( __FUNCTION__, func_get_args() );
656 }
657
658 public function decodeBlob( $b ) {
659 return $this->__call( __FUNCTION__, func_get_args() );
660 }
661
662 public function setSessionOptions( array $options ) {
663 return $this->__call( __FUNCTION__, func_get_args() );
664 }
665
666 public function setSchemaVars( $vars ) {
667 return $this->__call( __FUNCTION__, func_get_args() );
668 }
669
670 public function lockIsFree( $lockName, $method ) {
671 $this->assertRoleAllowsWrites();
672
673 return $this->__call( __FUNCTION__, func_get_args() );
674 }
675
676 public function lock( $lockName, $method, $timeout = 5 ) {
677 $this->assertRoleAllowsWrites();
678
679 return $this->__call( __FUNCTION__, func_get_args() );
680 }
681
682 public function unlock( $lockName, $method ) {
683 $this->assertRoleAllowsWrites();
684
685 return $this->__call( __FUNCTION__, func_get_args() );
686 }
687
688 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
689 $this->assertRoleAllowsWrites();
690
691 return $this->__call( __FUNCTION__, func_get_args() );
692 }
693
694 public function namedLocksEnqueue() {
695 return $this->__call( __FUNCTION__, func_get_args() );
696 }
697
698 public function getInfinity() {
699 return $this->__call( __FUNCTION__, func_get_args() );
700 }
701
702 public function encodeExpiry( $expiry ) {
703 return $this->__call( __FUNCTION__, func_get_args() );
704 }
705
706 public function decodeExpiry( $expiry, $format = TS_MW ) {
707 return $this->__call( __FUNCTION__, func_get_args() );
708 }
709
710 public function setBigSelects( $value = true ) {
711 return $this->__call( __FUNCTION__, func_get_args() );
712 }
713
714 public function isReadOnly() {
715 return $this->__call( __FUNCTION__, func_get_args() );
716 }
717
718 public function setTableAliases( array $aliases ) {
719 return $this->__call( __FUNCTION__, func_get_args() );
720 }
721
722 public function setIndexAliases( array $aliases ) {
723 return $this->__call( __FUNCTION__, func_get_args() );
724 }
725
726 /**
727 * Error out if the role is not DB_MASTER
728 *
729 * Note that the underlying connection may or may not itself be read-only.
730 * It could even be to a writable master (both server-side and to the application).
731 * This error is meant for the case when a DB_REPLICA handle was requested but a
732 * a write was attempted on that handle regardless.
733 *
734 * In configurations where the master DB has some generic read load or is the only server,
735 * DB_MASTER/DB_REPLICA will sometimes (or always) use the same connection to the master DB.
736 * This does not effect the role of DBConnRef instances.
737 * @throws DBReadOnlyRoleError
738 */
739 protected function assertRoleAllowsWrites() {
740 // DB_MASTER is "prima facie" writable
741 if ( $this->role !== ILoadBalancer::DB_MASTER ) {
742 throw new DBReadOnlyRoleError( $this->conn, "Cannot write with role DB_REPLICA" );
743 }
744 }
745
746 /**
747 * Clean up the connection when out of scope
748 */
749 function __destruct() {
750 if ( $this->conn ) {
751 $this->lb->reuseConnection( $this->conn );
752 }
753 }
754 }
755
756 /**
757 * @since 1.22
758 * @deprecated since 1.29
759 */
760 class_alias( DBConnRef::class, 'DBConnRef' );