Move DBConnRef/MaintainableDBConnRef to Rdbms namespace
[mediawiki.git] / includes / libs / rdbms / connectionmanager / ConnectionManager.php
blob49b2fe685cccc5f056130dfed7293b4bb76108d9
1 <?php
3 namespace Wikimedia\Rdbms;
5 use Database;
6 use InvalidArgumentException;
8 /**
9 * Database connection manager.
11 * This manages access to master and replica databases.
13 * @since 1.29
15 * @license GPL-2.0+
16 * @author Addshore
18 class ConnectionManager {
20 /**
21 * @var LoadBalancer
23 private $loadBalancer;
25 /**
26 * The symbolic name of the target database, or false for the local wiki's database.
28 * @var string|false
30 private $domain;
32 /**
33 * @var string[]
35 private $groups = [];
37 /**
38 * @param LoadBalancer $loadBalancer
39 * @param string|bool $domain Optional logical DB name, defaults to current wiki.
40 * This follows the convention for database names used by $loadBalancer.
41 * @param string[] $groups see LoadBalancer::getConnection
43 * @throws InvalidArgumentException
45 public function __construct( LoadBalancer $loadBalancer, $domain = false, array $groups = [] ) {
46 if ( !is_string( $domain ) && $domain !== false ) {
47 throw new InvalidArgumentException( '$dbName must be a string, or false.' );
50 $this->loadBalancer = $loadBalancer;
51 $this->domain = $domain;
52 $this->groups = $groups;
55 /**
56 * @param int $i
57 * @param string[]|null $groups
59 * @return Database
61 private function getConnection( $i, array $groups = null ) {
62 $groups = $groups === null ? $this->groups : $groups;
63 return $this->loadBalancer->getConnection( $i, $groups, $this->domain );
66 /**
67 * @param int $i
68 * @param string[]|null $groups
70 * @return DBConnRef
72 private function getConnectionRef( $i, array $groups = null ) {
73 $groups = $groups === null ? $this->groups : $groups;
74 return $this->loadBalancer->getConnectionRef( $i, $groups, $this->domain );
77 /**
78 * Returns a connection to the master DB, for updating. The connection should later be released
79 * by calling releaseConnection().
81 * @since 1.29
83 * @return Database
85 public function getWriteConnection() {
86 return $this->getConnection( DB_MASTER );
89 /**
90 * Returns a database connection for reading. The connection should later be released by
91 * calling releaseConnection().
93 * @since 1.29
95 * @param string[]|null $groups
97 * @return Database
99 public function getReadConnection( array $groups = null ) {
100 $groups = $groups === null ? $this->groups : $groups;
101 return $this->getConnection( DB_REPLICA, $groups );
105 * @since 1.29
107 * @param IDatabase $db
109 public function releaseConnection( IDatabase $db ) {
110 $this->loadBalancer->reuseConnection( $db );
114 * Returns a connection ref to the master DB, for updating.
116 * @since 1.29
118 * @return DBConnRef
120 public function getWriteConnectionRef() {
121 return $this->getConnectionRef( DB_MASTER );
125 * Returns a database connection ref for reading.
127 * @since 1.29
129 * @param string[]|null $groups
131 * @return DBConnRef
133 public function getReadConnectionRef( array $groups = null ) {
134 $groups = $groups === null ? $this->groups : $groups;
135 return $this->getConnectionRef( DB_REPLICA, $groups );