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