Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
23.00% covered (danger)
23.00%
49 / 213
7.75% covered (danger)
7.75%
11 / 142
CRAP
0.00% covered (danger)
0.00%
0 / 1
DBConnRef
23.00% covered (danger)
23.00%
49 / 213
7.75% covered (danger)
7.75%
11 / 142
12591.84
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 ensureConnection
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
6
 __call
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getReferenceRole
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getServerInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 trxLevel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 trxTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 explicitTrxActive
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 tablePrefix
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 dbSchema
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getLBInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLBInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 implicitOrderby
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 lastDoneWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 writesPending
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 writesOrCallbacksPending
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 pendingWriteQueryDuration
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 pendingWriteCallers
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isOpen
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setFlag
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 clearFlag
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 restoreFlags
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFlag
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDomainID
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 getType
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 insertId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 lastErrno
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 lastError
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 affectedRows
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSoftwareLink
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getServerVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 close
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 query
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 newSelectQueryBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newUnionQueryBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newUpdateQueryBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newDeleteQueryBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newInsertQueryBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newReplaceQueryBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 selectField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 selectFieldValues
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 select
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 selectSQLText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 limitResult
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 selectRow
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 estimateRowCount
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 selectRowCount
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 lockForUpdate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 fieldExists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 indexExists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPrimaryKeyColumns
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 tableExists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 insert
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 update
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 buildComparison
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeList
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeWhereFrom2d
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 factorConds
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 bitNot
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 bitAnd
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 bitOr
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildConcat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildGroupConcat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildGroupConcatField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildGreatest
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildLeast
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildSubstring
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildStringCast
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildIntegerCast
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildExcludedValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildSelectSubquery
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 databasesAreIndependent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 selectDomain
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDBname
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getServer
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getServerName
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 addQuotes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 expr
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 andExpr
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 orExpr
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addIdentifierQuotes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildLike
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 anyChar
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 anyString
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 replace
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 upsert
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 deleteJoin
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 delete
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 insertSelect
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 unionSupportsOrderAndLimit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 unionQueries
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 conditional
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 strreplace
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 primaryPosWait
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPrimaryPos
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 serverIsReadOnly
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onTransactionResolution
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onTransactionCommitOrIdle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onTransactionPreCommitOrIdle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTransactionListener
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 startAtomic
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 endAtomic
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 cancelAtomic
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doAtomicSection
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 begin
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 commit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 rollback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 flushSession
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 flushSnapshot
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 timestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 timestampOrNull
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 ping
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getLag
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSessionLagStatus
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 encodeBlob
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 decodeBlob
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSessionOptions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSchemaVars
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 lock
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 unlock
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 getScopedLockAndFlush
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
1.12
 getInfinity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 encodeExpiry
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 decodeExpiry
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isReadOnly
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTableAliases
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTableAliases
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 tableName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 tableNamesN
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sourceFile
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 sourceStream
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 dropTable
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 truncateTable
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 streamStatementEnd
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 duplicateTableStructure
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 indexUnique
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 listTables
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fieldInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __toString
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 assertRoleAllowsWrites
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getDomainChangeException
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 normalizeServerIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use InvalidArgumentException;
6use Stringable;
7use Wikimedia\ScopedCallback;
8use Wikimedia\Timestamp\TimestampFormat as TS;
9
10/**
11 * Helper class used for automatically re-using IDatabase connections and lazily
12 * establishing the actual network connection to a database host.
13 *
14 * It does this by deferring to ILoadBalancer::getConnectionInternal, which in
15 * turn ensures we share and re-use a single connection for a given database
16 * wherever possible.
17 *
18 * This class previously used an RAII-style pattern where connections would be
19 * claimed from a pool, and then added back to the pool for re-use only after
20 * the calling code's variable for this object went out of scope (a __destruct
21 * got called when the calling function returns or throws). This is no longer
22 * needed today as LoadBalancer now permits re-use internally even for
23 * overlapping callers, where two pieces of code may both obtain their own
24 * DBConnRef object and where both are used alternatingly, and yet still share
25 * the same connection.
26 *
27 * @par Example:
28 * @code
29 *     function getRowData() {
30 *         $conn = $this->lb->getConnection( DB_REPLICA );
31 *         $row = $conn->select( ... );
32 *         return $row ? (array)$row : false;
33 *     }
34 * @endcode
35 *
36 * @ingroup Database
37 * @since 1.22
38 */
39class DBConnRef implements Stringable, IMaintainableDatabase, IDatabaseForOwner {
40    /** @var ILoadBalancer */
41    private $lb;
42    /** @var IDatabase|null Live connection handle */
43    private $conn;
44    /**
45     * @var array Map of (DBConnRef::FLD_* constant => connection parameter)
46     * @phan-var array{0:int,1:array|string|false,2:DatabaseDomain,3:int}
47     */
48    private $params;
49    /** @var int One of DB_PRIMARY/DB_REPLICA */
50    private $role;
51
52    /**
53     * @var int Reference to the $modcount passed to the constructor.
54     *      $conn is valid if $modCountRef and $modCountFix are the same.
55     */
56    private $modCountRef;
57
58    /**
59     * @var int Last known good value of $modCountRef
60     *      $conn is valid if $modCountRef and $modCountFix are the same.
61     */
62    private $modCountFix;
63
64    private const FLD_INDEX = 0;
65    private const FLD_GROUPS = 1;
66    private const FLD_DOMAIN = 2;
67    private const FLD_FLAGS = 3;
68
69    /**
70     * @internal May not be used outside Rdbms LoadBalancer
71     * @param ILoadBalancer $lb Connection manager for $conn
72     * @param array $params [server index, query groups, domain, flags]
73     * @param int $role The type of connection asked for; one of DB_PRIMARY/DB_REPLICA
74     * @param null|int &$modcount Reference to a modification counter. This is for
75     *  LoadBalancer::reconfigure to indicate that a new connection should be acquired.
76     */
77    public function __construct( ILoadBalancer $lb, $params, $role, &$modcount = 0 ) {
78        if ( !is_array( $params ) || count( $params ) < 4 || $params[self::FLD_DOMAIN] === false ) {
79            throw new InvalidArgumentException( "Missing lazy connection arguments." );
80        }
81
82        $params[self::FLD_DOMAIN] = DatabaseDomain::newFromId( $params[self::FLD_DOMAIN] );
83
84        $this->lb = $lb;
85        $this->params = $params;
86        $this->role = $role;
87
88        // $this->conn is valid as long as $this->modCountRef and $this->modCountFix are the same.
89        $this->modCountRef = &$modcount; // remember reference
90        $this->modCountFix = $modcount;  // remember current value
91    }
92
93    /**
94     * Connect to the database if we are not already connected.
95     */
96    public function ensureConnection() {
97        if ( $this->modCountFix !== $this->modCountRef ) {
98            // Discard existing connection, unless we are in an ongoing transaction.
99            // This is triggered by LoadBalancer::reconfigure(), to allow changed settings
100            // to take effect. The primary use case are replica servers being taken out of
101            // rotation, or the primary database changing.
102            if ( $this->conn && !$this->conn->trxLevel() ) {
103                $this->conn->close( wfGetCaller() );
104                $this->conn = null;
105            }
106        }
107
108        if ( $this->conn === null ) {
109            $this->conn = $this->lb->getConnectionInternal(
110                $this->params[self::FLD_INDEX],
111                $this->params[self::FLD_GROUPS],
112                $this->params[self::FLD_DOMAIN]->getId(),
113                $this->params[self::FLD_FLAGS]
114            );
115            $this->modCountFix = $this->modCountRef;
116        }
117
118        if ( !$this->params[self::FLD_DOMAIN]->equals( $this->conn->getDomainID() ) ) {
119            // The underlying connection handle is likely being shared by other DBConnRef
120            // instances in a load balancer. Make sure that each one routes queries by their
121            // owner function to the domain that the owner expects.
122            $this->conn->selectDomain( $this->params[self::FLD_DOMAIN] );
123        }
124    }
125
126    public function __call( $name, array $arguments ) {
127        $this->ensureConnection();
128
129        return $this->conn->$name( ...$arguments );
130    }
131
132    /**
133     * @return int DB_PRIMARY when this *requires* the primary DB, otherwise DB_REPLICA
134     * @since 1.33
135     */
136    public function getReferenceRole() {
137        return $this->role;
138    }
139
140    /** @inheritDoc */
141    public function getServerInfo() {
142        return $this->__call( __FUNCTION__, func_get_args() );
143    }
144
145    /** @inheritDoc */
146    public function trxLevel() {
147        return $this->__call( __FUNCTION__, func_get_args() );
148    }
149
150    /** @inheritDoc */
151    public function trxTimestamp() {
152        return $this->__call( __FUNCTION__, func_get_args() );
153    }
154
155    /** @inheritDoc */
156    public function explicitTrxActive() {
157        return $this->__call( __FUNCTION__, func_get_args() );
158    }
159
160    /** @inheritDoc */
161    public function tablePrefix( $prefix = null ) {
162        if ( $prefix !== null ) {
163            // Disallow things that might confuse the LoadBalancer tracking
164            throw $this->getDomainChangeException();
165        }
166
167        if ( $this->conn === null ) {
168            // Avoid triggering a database connection
169            $prefix = $this->params[self::FLD_DOMAIN]->getTablePrefix();
170        } else {
171            // This will just return the prefix
172            $prefix = $this->__call( __FUNCTION__, func_get_args() );
173        }
174
175        return $prefix;
176    }
177
178    /** @inheritDoc */
179    public function dbSchema( $schema = null ) {
180        if ( $schema !== null ) {
181            // Disallow things that might confuse the LoadBalancer tracking
182            throw $this->getDomainChangeException();
183        }
184
185        if ( $this->conn === null ) {
186            // Avoid triggering a database connection
187            $schema = (string)( $this->params[self::FLD_DOMAIN]->getSchema() );
188        } else {
189            // This will just return the schema
190            $schema = $this->__call( __FUNCTION__, func_get_args() );
191        }
192
193        return $schema;
194    }
195
196    /** @inheritDoc */
197    public function getLBInfo( $name = null ) {
198        return $this->__call( __FUNCTION__, func_get_args() );
199    }
200
201    /** @inheritDoc */
202    public function setLBInfo( $nameOrArray, $value = null ) {
203        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
204        // Disallow things that might confuse the LoadBalancer tracking
205        throw $this->getDomainChangeException();
206    }
207
208    /** @inheritDoc */
209    public function implicitOrderby() {
210        return $this->__call( __FUNCTION__, func_get_args() );
211    }
212
213    /** @inheritDoc */
214    public function lastDoneWrites() {
215        return $this->__call( __FUNCTION__, func_get_args() );
216    }
217
218    /** @inheritDoc */
219    public function writesPending() {
220        return $this->__call( __FUNCTION__, func_get_args() );
221    }
222
223    /** @inheritDoc */
224    public function writesOrCallbacksPending() {
225        return $this->__call( __FUNCTION__, func_get_args() );
226    }
227
228    /** @inheritDoc */
229    public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
230        return $this->__call( __FUNCTION__, func_get_args() );
231    }
232
233    /** @inheritDoc */
234    public function pendingWriteCallers() {
235        return $this->__call( __FUNCTION__, func_get_args() );
236    }
237
238    /** @inheritDoc */
239    public function isOpen() {
240        return $this->__call( __FUNCTION__, func_get_args() );
241    }
242
243    /** @inheritDoc */
244    public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
245        return $this->__call( __FUNCTION__, func_get_args() );
246    }
247
248    /** @inheritDoc */
249    public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
250        return $this->__call( __FUNCTION__, func_get_args() );
251    }
252
253    /** @inheritDoc */
254    public function restoreFlags( $state = self::RESTORE_PRIOR ) {
255        return $this->__call( __FUNCTION__, func_get_args() );
256    }
257
258    /** @inheritDoc */
259    public function getFlag( $flag ) {
260        return $this->__call( __FUNCTION__, func_get_args() );
261    }
262
263    /** @inheritDoc */
264    public function getDomainID() {
265        if ( $this->conn === null ) {
266            // Avoid triggering a database connection
267            return $this->params[self::FLD_DOMAIN]->getId();
268        }
269
270        return $this->__call( __FUNCTION__, func_get_args() );
271    }
272
273    /** @inheritDoc */
274    public function getType() {
275        if ( $this->conn === null ) {
276            // Avoid triggering a database connection
277            $index = $this->normalizeServerIndex( $this->params[self::FLD_INDEX] );
278            if ( $index >= 0 ) {
279                // In theory, if $index is DB_REPLICA, the type could vary
280                return $this->lb->getServerType( $index );
281            }
282        }
283
284        return $this->__call( __FUNCTION__, func_get_args() );
285    }
286
287    /** @inheritDoc */
288    public function insertId() {
289        return $this->__call( __FUNCTION__, func_get_args() );
290    }
291
292    /** @inheritDoc */
293    public function lastErrno() {
294        return $this->__call( __FUNCTION__, func_get_args() );
295    }
296
297    /** @inheritDoc */
298    public function lastError() {
299        return $this->__call( __FUNCTION__, func_get_args() );
300    }
301
302    /** @inheritDoc */
303    public function affectedRows() {
304        return $this->__call( __FUNCTION__, func_get_args() );
305    }
306
307    /** @inheritDoc */
308    public function getSoftwareLink() {
309        return $this->__call( __FUNCTION__, func_get_args() );
310    }
311
312    /** @inheritDoc */
313    public function getServerVersion() {
314        return $this->__call( __FUNCTION__, func_get_args() );
315    }
316
317    /** @inheritDoc */
318    public function close( $fname = __METHOD__ ) {
319        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
320        throw new DBUnexpectedError( $this->conn, 'Cannot close shared connection.' );
321    }
322
323    /** @inheritDoc */
324    public function query( $sql, $fname = __METHOD__, $flags = 0 ) {
325        if ( $this->role !== ILoadBalancer::DB_PRIMARY ) {
326            $flags |= IDatabase::QUERY_REPLICA_ROLE;
327        }
328
329        return $this->__call( __FUNCTION__, [ $sql, $fname, $flags ] );
330    }
331
332    public function newSelectQueryBuilder(): SelectQueryBuilder {
333        // Use $this not $this->conn so that the domain is preserved (T326377)
334        return new SelectQueryBuilder( $this );
335    }
336
337    public function newUnionQueryBuilder(): UnionQueryBuilder {
338        // Use $this not $this->conn so that the domain is preserved (T326377)
339        return new UnionQueryBuilder( $this );
340    }
341
342    public function newUpdateQueryBuilder(): UpdateQueryBuilder {
343        // Use $this not $this->conn so that the domain is preserved (T326377)
344        return new UpdateQueryBuilder( $this );
345    }
346
347    public function newDeleteQueryBuilder(): DeleteQueryBuilder {
348        // Use $this not $this->conn so that the domain is preserved (T326377)
349        return new DeleteQueryBuilder( $this );
350    }
351
352    public function newInsertQueryBuilder(): InsertQueryBuilder {
353        // Use $this not $this->conn so that the domain is preserved (T326377)
354        return new InsertQueryBuilder( $this );
355    }
356
357    public function newReplaceQueryBuilder(): ReplaceQueryBuilder {
358        // Use $this not $this->conn so that the domain is preserved (T326377)
359        return new ReplaceQueryBuilder( $this );
360    }
361
362    /** @inheritDoc */
363    public function selectField(
364        $tables, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
365    ) {
366        return $this->__call( __FUNCTION__, func_get_args() );
367    }
368
369    /** @inheritDoc */
370    public function selectFieldValues(
371        $tables, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
372    ): array {
373        return $this->__call( __FUNCTION__, func_get_args() );
374    }
375
376    /** @inheritDoc */
377    public function select(
378        $tables, $vars, $conds = '', $fname = __METHOD__,
379        $options = [], $join_conds = []
380    ) {
381        return $this->__call( __FUNCTION__, func_get_args() );
382    }
383
384    /** @inheritDoc */
385    public function selectSQLText(
386        $tables, $vars, $conds = '', $fname = __METHOD__,
387        $options = [], $join_conds = []
388    ) {
389        return $this->__call( __FUNCTION__, func_get_args() );
390    }
391
392    /** @inheritDoc */
393    public function limitResult( $sql, $limit, $offset = false ) {
394        return $this->__call( __FUNCTION__, func_get_args() );
395    }
396
397    /** @inheritDoc */
398    public function selectRow(
399        $tables, $vars, $conds, $fname = __METHOD__,
400        $options = [], $join_conds = []
401    ) {
402        return $this->__call( __FUNCTION__, func_get_args() );
403    }
404
405    /** @inheritDoc */
406    public function estimateRowCount(
407        $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
408    ): int {
409        return $this->__call( __FUNCTION__, func_get_args() );
410    }
411
412    /** @inheritDoc */
413    public function selectRowCount(
414        $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
415    ): int {
416        return $this->__call( __FUNCTION__, func_get_args() );
417    }
418
419    /** @inheritDoc */
420    public function lockForUpdate(
421        $table, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
422    ) {
423        $this->assertRoleAllowsWrites();
424
425        return $this->__call( __FUNCTION__, func_get_args() );
426    }
427
428    /** @inheritDoc */
429    public function fieldExists( $table, $field, $fname = __METHOD__ ) {
430        return $this->__call( __FUNCTION__, func_get_args() );
431    }
432
433    /** @inheritDoc */
434    public function indexExists( $table, $index, $fname = __METHOD__ ) {
435        return $this->__call( __FUNCTION__, func_get_args() );
436    }
437
438    /** @inheritDoc */
439    public function getPrimaryKeyColumns( $table, $fname = __METHOD__ ) {
440        return $this->__call( __FUNCTION__, func_get_args() );
441    }
442
443    /** @inheritDoc */
444    public function tableExists( $table, $fname = __METHOD__ ) {
445        return $this->__call( __FUNCTION__, func_get_args() );
446    }
447
448    /** @inheritDoc */
449    public function insert( $table, $rows, $fname = __METHOD__, $options = [] ) {
450        $this->assertRoleAllowsWrites();
451
452        return $this->__call( __FUNCTION__, func_get_args() );
453    }
454
455    /** @inheritDoc */
456    public function update( $table, $set, $conds, $fname = __METHOD__, $options = [] ) {
457        $this->assertRoleAllowsWrites();
458
459        return $this->__call( __FUNCTION__, func_get_args() );
460    }
461
462    /** @inheritDoc */
463    public function buildComparison( string $op, array $conds ): string {
464        return $this->__call( __FUNCTION__, func_get_args() );
465    }
466
467    /** @inheritDoc */
468    public function makeList( array $a, $mode = self::LIST_COMMA ) {
469        return $this->__call( __FUNCTION__, func_get_args() );
470    }
471
472    /** @inheritDoc */
473    public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
474        return $this->__call( __FUNCTION__, func_get_args() );
475    }
476
477    /** @inheritDoc */
478    public function factorConds( $condsArray ) {
479        return $this->__call( __FUNCTION__, func_get_args() );
480    }
481
482    /** @inheritDoc */
483    public function bitNot( $field ) {
484        return $this->__call( __FUNCTION__, func_get_args() );
485    }
486
487    /** @inheritDoc */
488    public function bitAnd( $fieldLeft, $fieldRight ) {
489        return $this->__call( __FUNCTION__, func_get_args() );
490    }
491
492    /** @inheritDoc */
493    public function bitOr( $fieldLeft, $fieldRight ) {
494        return $this->__call( __FUNCTION__, func_get_args() );
495    }
496
497    /** @inheritDoc */
498    public function buildConcat( $stringList ) {
499        return $this->__call( __FUNCTION__, func_get_args() );
500    }
501
502    /** @inheritDoc */
503    public function buildGroupConcat( $field, $delim ): string {
504        return $this->__call( __FUNCTION__, func_get_args() );
505    }
506
507    /** @inheritDoc */
508    public function buildGroupConcatField(
509        $delim, $tables, $field, $conds = '', $join_conds = []
510    ) {
511        return $this->__call( __FUNCTION__, func_get_args() );
512    }
513
514    /** @inheritDoc */
515    public function buildGreatest( $fields, $values ) {
516        return $this->__call( __FUNCTION__, func_get_args() );
517    }
518
519    /** @inheritDoc */
520    public function buildLeast( $fields, $values ) {
521        return $this->__call( __FUNCTION__, func_get_args() );
522    }
523
524    /** @inheritDoc */
525    public function buildSubstring( $input, $startPosition, $length = null ) {
526        return $this->__call( __FUNCTION__, func_get_args() );
527    }
528
529    /** @inheritDoc */
530    public function buildStringCast( $field ) {
531        return $this->__call( __FUNCTION__, func_get_args() );
532    }
533
534    /** @inheritDoc */
535    public function buildIntegerCast( $field ) {
536        return $this->__call( __FUNCTION__, func_get_args() );
537    }
538
539    /** @inheritDoc */
540    public function buildExcludedValue( $column ) {
541        return $this->__call( __FUNCTION__, func_get_args() );
542    }
543
544    /** @inheritDoc */
545    public function buildSelectSubquery(
546        $tables, $vars, $conds = '', $fname = __METHOD__,
547        $options = [], $join_conds = []
548    ) {
549        return $this->__call( __FUNCTION__, func_get_args() );
550    }
551
552    /** @inheritDoc */
553    public function databasesAreIndependent() {
554        return $this->__call( __FUNCTION__, func_get_args() );
555    }
556
557    /** @inheritDoc */
558    public function selectDomain( $domain ) {
559        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
560        // Disallow things that might confuse the LoadBalancer tracking
561        throw $this->getDomainChangeException();
562    }
563
564    /** @inheritDoc */
565    public function getDBname() {
566        if ( $this->conn === null ) {
567            // Avoid triggering a database connection
568            return $this->params[self::FLD_DOMAIN]->getDatabase();
569        }
570
571        return $this->__call( __FUNCTION__, func_get_args() );
572    }
573
574    /** @inheritDoc */
575    public function getServer() {
576        return $this->__call( __FUNCTION__, func_get_args() );
577    }
578
579    /** @inheritDoc */
580    public function getServerName() {
581        if ( $this->conn === null ) {
582            // Avoid triggering a database connection
583            $index = $this->normalizeServerIndex( $this->params[self::FLD_INDEX] );
584            if ( $index >= 0 ) {
585                // If $index is DB_REPLICA, the server name could vary
586                return $this->lb->getServerName( $index );
587            }
588        }
589
590        return $this->__call( __FUNCTION__, func_get_args() );
591    }
592
593    /** @inheritDoc */
594    public function addQuotes( $s ) {
595        return $this->__call( __FUNCTION__, func_get_args() );
596    }
597
598    /** @inheritDoc */
599    public function expr( string $field, string $op, $value ): Expression {
600        // Does not use __call here to delay creating the db connection
601        return new Expression( $field, $op, $value );
602    }
603
604    /** @inheritDoc */
605    public function andExpr( array $conds ): AndExpressionGroup {
606        // Does not use __call here to delay creating the db connection
607        return AndExpressionGroup::newFromArray( $conds );
608    }
609
610    /** @inheritDoc */
611    public function orExpr( array $conds ): OrExpressionGroup {
612        // Does not use __call here to delay creating the db connection
613        return OrExpressionGroup::newFromArray( $conds );
614    }
615
616    /** @inheritDoc */
617    public function addIdentifierQuotes( $s ) {
618        return $this->__call( __FUNCTION__, func_get_args() );
619    }
620
621    /** @inheritDoc */
622    public function buildLike( $param, ...$params ) {
623        return $this->__call( __FUNCTION__, func_get_args() );
624    }
625
626    /** @inheritDoc */
627    public function anyChar() {
628        return $this->__call( __FUNCTION__, func_get_args() );
629    }
630
631    /** @inheritDoc */
632    public function anyString() {
633        return $this->__call( __FUNCTION__, func_get_args() );
634    }
635
636    /** @inheritDoc */
637    public function replace( $table, $uniqueKeys, $rows, $fname = __METHOD__ ) {
638        $this->assertRoleAllowsWrites();
639
640        return $this->__call( __FUNCTION__, func_get_args() );
641    }
642
643    /** @inheritDoc */
644    public function upsert(
645        $table, array $rows, $uniqueKeys, array $set, $fname = __METHOD__
646    ) {
647        $this->assertRoleAllowsWrites();
648
649        return $this->__call( __FUNCTION__, func_get_args() );
650    }
651
652    /** @inheritDoc */
653    public function deleteJoin(
654        $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
655    ) {
656        $this->assertRoleAllowsWrites();
657
658        $this->__call( __FUNCTION__, func_get_args() );
659    }
660
661    /** @inheritDoc */
662    public function delete( $table, $conds, $fname = __METHOD__ ) {
663        $this->assertRoleAllowsWrites();
664
665        return $this->__call( __FUNCTION__, func_get_args() );
666    }
667
668    /** @inheritDoc */
669    public function insertSelect(
670        $destTable, $srcTable, $varMap, $conds,
671        $fname = __METHOD__, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
672    ) {
673        $this->assertRoleAllowsWrites();
674
675        return $this->__call( __FUNCTION__, func_get_args() );
676    }
677
678    /** @inheritDoc */
679    public function unionSupportsOrderAndLimit() {
680        return $this->__call( __FUNCTION__, func_get_args() );
681    }
682
683    /** @inheritDoc */
684    public function unionQueries( $sqls, $all, $options = [] ) {
685        return $this->__call( __FUNCTION__, func_get_args() );
686    }
687
688    /** @inheritDoc */
689    public function conditional( $cond, $caseTrueExpression, $caseFalseExpression ) {
690        return $this->__call( __FUNCTION__, func_get_args() );
691    }
692
693    /** @inheritDoc */
694    public function strreplace( $orig, $old, $new ) {
695        return $this->__call( __FUNCTION__, func_get_args() );
696    }
697
698    /** @inheritDoc */
699    public function primaryPosWait( DBPrimaryPos $pos, $timeout ) {
700        return $this->__call( __FUNCTION__, func_get_args() );
701    }
702
703    /** @inheritDoc */
704    public function getPrimaryPos() {
705        return $this->__call( __FUNCTION__, func_get_args() );
706    }
707
708    /** @inheritDoc */
709    public function serverIsReadOnly() {
710        return $this->__call( __FUNCTION__, func_get_args() );
711    }
712
713    /** @inheritDoc */
714    public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
715        // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
716        return $this->__call( __FUNCTION__, func_get_args() );
717    }
718
719    /** @inheritDoc */
720    public function onTransactionCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
721        // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
722        return $this->__call( __FUNCTION__, func_get_args() );
723    }
724
725    /** @inheritDoc */
726    public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
727        // DB_REPLICA role: caller might want to refresh cache after a cache mutex is released
728        return $this->__call( __FUNCTION__, func_get_args() );
729    }
730
731    /** @inheritDoc */
732    public function setTransactionListener( $name, ?callable $callback = null ) {
733        return $this->__call( __FUNCTION__, func_get_args() );
734    }
735
736    /** @inheritDoc */
737    public function startAtomic(
738        $fname = __METHOD__, $cancelable = IDatabase::ATOMIC_NOT_CANCELABLE
739    ) {
740        // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
741        return $this->__call( __FUNCTION__, func_get_args() );
742    }
743
744    /** @inheritDoc */
745    public function endAtomic( $fname = __METHOD__ ) {
746        // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
747        return $this->__call( __FUNCTION__, func_get_args() );
748    }
749
750    /** @inheritDoc */
751    public function cancelAtomic( $fname = __METHOD__, ?AtomicSectionIdentifier $sectionId = null ) {
752        // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
753        return $this->__call( __FUNCTION__, func_get_args() );
754    }
755
756    /** @inheritDoc */
757    public function doAtomicSection(
758        $fname, callable $callback, $cancelable = self::ATOMIC_NOT_CANCELABLE
759    ) {
760        // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
761        return $this->__call( __FUNCTION__, func_get_args() );
762    }
763
764    /** @inheritDoc */
765    public function begin( $fname = __METHOD__, $mode = IDatabase::TRANSACTION_EXPLICIT ) {
766        return $this->__call( __FUNCTION__, func_get_args() );
767    }
768
769    /** @inheritDoc */
770    public function commit( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
771        return $this->__call( __FUNCTION__, func_get_args() );
772    }
773
774    /** @inheritDoc */
775    public function rollback( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
776        return $this->__call( __FUNCTION__, func_get_args() );
777    }
778
779    /** @inheritDoc */
780    public function flushSession( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
781        return $this->__call( __FUNCTION__, func_get_args() );
782    }
783
784    /** @inheritDoc */
785    public function flushSnapshot( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
786        return $this->__call( __FUNCTION__, func_get_args() );
787    }
788
789    /** @inheritDoc */
790    public function timestamp( $ts = 0 ) {
791        return $this->__call( __FUNCTION__, func_get_args() );
792    }
793
794    /** @inheritDoc */
795    public function timestampOrNull( $ts = null ) {
796        return $this->__call( __FUNCTION__, func_get_args() );
797    }
798
799    /** @inheritDoc */
800    public function ping( &$rtt = null ) {
801        return func_num_args()
802            ? $this->__call( __FUNCTION__, [ &$rtt ] )
803            : $this->__call( __FUNCTION__, [] ); // method cares about null vs missing
804    }
805
806    /** @inheritDoc */
807    public function getLag() {
808        return $this->__call( __FUNCTION__, func_get_args() );
809    }
810
811    /** @inheritDoc */
812    public function getSessionLagStatus() {
813        return $this->__call( __FUNCTION__, func_get_args() );
814    }
815
816    /** @inheritDoc */
817    public function encodeBlob( $b ) {
818        return $this->__call( __FUNCTION__, func_get_args() );
819    }
820
821    /** @inheritDoc */
822    public function decodeBlob( $b ) {
823        return $this->__call( __FUNCTION__, func_get_args() );
824    }
825
826    /** @inheritDoc */
827    public function setSessionOptions( array $options ) {
828        $this->__call( __FUNCTION__, func_get_args() );
829    }
830
831    /** @inheritDoc */
832    public function setSchemaVars( $vars ) {
833        return $this->__call( __FUNCTION__, func_get_args() );
834    }
835
836    /** @inheritDoc */
837    public function lock( $lockName, $method, $timeout = 5, $flags = 0 ) {
838        $this->assertRoleAllowsWrites();
839
840        return $this->__call( __FUNCTION__, func_get_args() );
841    }
842
843    /** @inheritDoc */
844    public function unlock( $lockName, $method ) {
845        $this->assertRoleAllowsWrites();
846
847        return $this->__call( __FUNCTION__, func_get_args() );
848    }
849
850    /** @inheritDoc */
851    #[\NoDiscard]
852    public function getScopedLockAndFlush( $lockKey, $fname, $timeout ): ?ScopedCallback {
853        $this->assertRoleAllowsWrites();
854
855        return $this->__call( __FUNCTION__, func_get_args() );
856    }
857
858    /** @inheritDoc */
859    public function getInfinity() {
860        return $this->__call( __FUNCTION__, func_get_args() );
861    }
862
863    /** @inheritDoc */
864    public function encodeExpiry( $expiry ) {
865        return $this->__call( __FUNCTION__, func_get_args() );
866    }
867
868    /** @inheritDoc */
869    public function decodeExpiry( $expiry, $format = TS::MW ) {
870        return $this->__call( __FUNCTION__, func_get_args() );
871    }
872
873    /** @inheritDoc */
874    public function isReadOnly() {
875        return $this->__call( __FUNCTION__, func_get_args() );
876    }
877
878    /** @inheritDoc */
879    public function setTableAliases( array $aliases ) {
880        return $this->__call( __FUNCTION__, func_get_args() );
881    }
882
883    /** @inheritDoc */
884    public function getTableAliases() {
885        return $this->__call( __FUNCTION__, func_get_args() );
886    }
887
888    /** @inheritDoc */
889    public function tableName( $name, $format = 'quoted' ) {
890        return $this->__call( __FUNCTION__, func_get_args() );
891    }
892
893    /** @inheritDoc */
894    public function tableNamesN( ...$tables ) {
895        return $this->__call( __FUNCTION__, func_get_args() );
896    }
897
898    /** @inheritDoc */
899    public function sourceFile(
900        $filename,
901        ?callable $lineCallback = null,
902        ?callable $resultCallback = null,
903        $fname = false,
904        ?callable $inputCallback = null
905    ) {
906        $this->assertRoleAllowsWrites();
907
908        return $this->__call( __FUNCTION__, func_get_args() );
909    }
910
911    /** @inheritDoc */
912    public function sourceStream(
913        $fp,
914        ?callable $lineCallback = null,
915        ?callable $resultCallback = null,
916        $fname = __METHOD__,
917        ?callable $inputCallback = null
918    ) {
919        $this->assertRoleAllowsWrites();
920
921        return $this->__call( __FUNCTION__, func_get_args() );
922    }
923
924    /** @inheritDoc */
925    public function dropTable( $table, $fname = __METHOD__ ) {
926        $this->assertRoleAllowsWrites();
927
928        return $this->__call( __FUNCTION__, func_get_args() );
929    }
930
931    /** @inheritDoc */
932    public function truncateTable( $table, $fname = __METHOD__ ) {
933        $this->assertRoleAllowsWrites();
934
935        return $this->__call( __FUNCTION__, func_get_args() );
936    }
937
938    /** @inheritDoc */
939    public function streamStatementEnd( &$sql, &$newLine ) {
940        return $this->__call( __FUNCTION__, [ &$sql, &$newLine ] );
941    }
942
943    /** @inheritDoc */
944    public function duplicateTableStructure(
945        $oldName, $newName, $temporary = false, $fname = __METHOD__
946    ) {
947        $this->assertRoleAllowsWrites();
948
949        return $this->__call( __FUNCTION__, func_get_args() );
950    }
951
952    /** @inheritDoc */
953    public function indexUnique( $table, $index, $fname = __METHOD__ ) {
954        return $this->__call( __FUNCTION__, func_get_args() );
955    }
956
957    /** @inheritDoc */
958    public function listTables( $prefix = null, $fname = __METHOD__ ) {
959        return $this->__call( __FUNCTION__, func_get_args() );
960    }
961
962    /** @inheritDoc */
963    public function fieldInfo( $table, $field ) {
964        return $this->__call( __FUNCTION__, func_get_args() );
965    }
966
967    public function __toString() {
968        if ( $this->conn === null ) {
969            return $this->getType() . ' object #' . spl_object_id( $this );
970        }
971
972        return $this->__call( __FUNCTION__, func_get_args() );
973    }
974
975    /**
976     * Error out if the role is not DB_PRIMARY
977     *
978     * Note that the underlying connection may or may not itself be read-only.
979     * It could even be to a writable primary (both server-side and to the application).
980     * This error is meant for the case when a DB_REPLICA handle was requested but a
981     * a write was attempted on that handle regardless.
982     *
983     * In configurations where the primary DB has some generic read load or is the only server,
984     * DB_PRIMARY/DB_REPLICA will sometimes (or always) use the same connection to the primary DB.
985     * This does not effect the role of DBConnRef instances.
986     * @throws DBReadOnlyRoleError
987     */
988    protected function assertRoleAllowsWrites() {
989        // DB_PRIMARY is "prima facie" writable
990        if ( $this->role !== ILoadBalancer::DB_PRIMARY ) {
991            throw new DBReadOnlyRoleError( $this->conn, "Cannot write with role DB_REPLICA" );
992        }
993    }
994
995    /**
996     * @return DBUnexpectedError
997     */
998    protected function getDomainChangeException() {
999        return new DBUnexpectedError(
1000            $this,
1001            "Cannot directly change the selected DB domain; any underlying connection handle " .
1002            "is owned by a LoadBalancer instance and possibly shared with other callers. " .
1003            "LoadBalancer automatically manages DB domain re-selection of unused handles."
1004        );
1005    }
1006
1007    /**
1008     * @param int $i Specific or virtual (DB_PRIMARY/DB_REPLICA) server index
1009     * @return int|mixed
1010     */
1011    protected function normalizeServerIndex( $i ) {
1012        return ( $i === ILoadBalancer::DB_PRIMARY ) ? ServerInfo::WRITER_INDEX : $i;
1013    }
1014}