Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
9 / 10 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ConnectionManager | |
90.00% |
9 / 10 |
|
75.00% |
3 / 4 |
6.04 | |
0.00% |
0 / 1 |
| __construct | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| getConnection | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getWriteConnection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getReadConnection | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | namespace Wikimedia\Rdbms; |
| 7 | |
| 8 | use InvalidArgumentException; |
| 9 | |
| 10 | /** |
| 11 | * Database connection manager. |
| 12 | * |
| 13 | * This manages access to primary and replica databases. |
| 14 | * |
| 15 | * @since 1.29 |
| 16 | * @ingroup Database |
| 17 | * @author Addshore |
| 18 | */ |
| 19 | class ConnectionManager { |
| 20 | |
| 21 | /** |
| 22 | * @var ILoadBalancer |
| 23 | */ |
| 24 | private $loadBalancer; |
| 25 | |
| 26 | /** |
| 27 | * The symbolic name of the target database, or false for the local wiki's database. |
| 28 | * |
| 29 | * @var string|false |
| 30 | */ |
| 31 | private $domain; |
| 32 | |
| 33 | /** |
| 34 | * @var string[] |
| 35 | */ |
| 36 | private $groups = []; |
| 37 | |
| 38 | /** |
| 39 | * @param ILoadBalancer $loadBalancer |
| 40 | * @param string|false $domain Optional logical DB name, defaults to current wiki. |
| 41 | * This follows the convention for database names used by $loadBalancer. |
| 42 | * @param string[] $groups see LoadBalancer::getConnection |
| 43 | * |
| 44 | * @throws InvalidArgumentException |
| 45 | */ |
| 46 | public function __construct( ILoadBalancer $loadBalancer, $domain = false, array $groups = [] ) { |
| 47 | if ( !is_string( $domain ) && $domain !== false ) { |
| 48 | throw new InvalidArgumentException( '$dbName must be a string, or false.' ); |
| 49 | } |
| 50 | |
| 51 | $this->loadBalancer = $loadBalancer; |
| 52 | $this->domain = $domain; |
| 53 | $this->groups = $groups; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param int $i |
| 58 | * @param string[]|null $groups |
| 59 | * @param int $flags |
| 60 | * @return IDatabase |
| 61 | */ |
| 62 | private function getConnection( $i, ?array $groups = null, int $flags = 0 ) { |
| 63 | $groups ??= $this->groups; |
| 64 | return $this->loadBalancer->getConnection( $i, $groups, $this->domain, $flags ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns a connection to the primary DB, for updating. |
| 69 | * |
| 70 | * @since 1.29 |
| 71 | * @since 1.37 Added optional $flags parameter |
| 72 | * @param int $flags |
| 73 | * @return IDatabase |
| 74 | */ |
| 75 | public function getWriteConnection( int $flags = 0 ) { |
| 76 | return $this->getConnection( DB_PRIMARY, null, $flags ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns a database connection for reading. |
| 81 | * |
| 82 | * @since 1.29 |
| 83 | * @since 1.37 Added optional $flags parameter |
| 84 | * @param string[]|null $groups |
| 85 | * @param int $flags |
| 86 | * @return IReadableDatabase |
| 87 | */ |
| 88 | public function getReadConnection( ?array $groups = null, int $flags = 0 ) { |
| 89 | $groups ??= $this->groups; |
| 90 | return $this->getConnection( DB_REPLICA, $groups, $flags ); |
| 91 | } |
| 92 | } |