Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| LoadBalancerSingle | |
0.00% |
0 / 31 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
12 | |||
| newFromConnection | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| sanitizeConnectionFlags | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| reallyOpenConnection | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| __destruct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | namespace Wikimedia\Rdbms; |
| 7 | |
| 8 | use InvalidArgumentException; |
| 9 | |
| 10 | /** |
| 11 | * Trivial LoadBalancer that always returns an injected connection handle. |
| 12 | * |
| 13 | * @ingroup Database |
| 14 | */ |
| 15 | class LoadBalancerSingle extends LoadBalancer { |
| 16 | /** @var Database */ |
| 17 | private $conn; |
| 18 | |
| 19 | /** |
| 20 | * You probably want to use {@link newFromConnection} instead. |
| 21 | * |
| 22 | * @param array $params An associative array with one member: |
| 23 | * - connection: An IDatabase connection object |
| 24 | */ |
| 25 | public function __construct( array $params ) { |
| 26 | /** @var Database $conn */ |
| 27 | $conn = $params['connection'] ?? null; |
| 28 | if ( !$conn ) { |
| 29 | throw new InvalidArgumentException( "Missing 'connection' argument." ); |
| 30 | } |
| 31 | |
| 32 | $this->conn = $conn; |
| 33 | |
| 34 | parent::__construct( [ |
| 35 | 'servers' => [ [ |
| 36 | 'type' => $conn->getType(), |
| 37 | 'host' => $conn->getServer(), |
| 38 | 'dbname' => $conn->getDBname(), |
| 39 | 'load' => 1, |
| 40 | ] ], |
| 41 | 'trxProfiler' => $params['trxProfiler'] ?? null, |
| 42 | 'srvCache' => $params['srvCache'] ?? null, |
| 43 | 'wanCache' => $params['wanCache'] ?? null, |
| 44 | 'localDomain' => $params['localDomain'] ?? $this->conn->getDomainID(), |
| 45 | 'readOnlyReason' => $params['readOnlyReason'] ?? false, |
| 46 | 'clusterName' => $params['clusterName'] ?? null, |
| 47 | ] ); |
| 48 | |
| 49 | if ( isset( $params['readOnlyReason'] ) ) { |
| 50 | $conn->setLBInfo( $conn::LB_READ_ONLY_REASON, $params['readOnlyReason'] ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param IDatabase $db Live connection handle |
| 56 | * @param array $params Parameter map to LoadBalancerSingle::__constructs() |
| 57 | * @since 1.28 |
| 58 | */ |
| 59 | public static function newFromConnection( IDatabase $db, array $params = [] ): static { |
| 60 | return new static( [ |
| 61 | 'localDomain' => $db->getDomainID(), |
| 62 | ...$params, |
| 63 | 'connection' => $db, |
| 64 | ] ); |
| 65 | } |
| 66 | |
| 67 | /** @inheritDoc */ |
| 68 | protected function sanitizeConnectionFlags( $flags, $domain ) { |
| 69 | // There is only one underlying connection handle. Also, this class is only meant to |
| 70 | // be used during situations like site installation, where there should be no contenting |
| 71 | // connections, and integration testing, where everything uses temporary tables. |
| 72 | $flags &= ~self::CONN_TRX_AUTOCOMMIT; |
| 73 | |
| 74 | return $flags; |
| 75 | } |
| 76 | |
| 77 | /** @inheritDoc */ |
| 78 | protected function reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo ) { |
| 79 | foreach ( $lbInfo as $k => $v ) { |
| 80 | $this->conn->setLBInfo( $k, $v ); |
| 81 | } |
| 82 | |
| 83 | return $this->conn; |
| 84 | } |
| 85 | |
| 86 | public function __destruct() { |
| 87 | // do nothing since the connection was injected |
| 88 | } |
| 89 | } |