Move IDatabase/IMaintainableDatabase to Rdbms namespace
[lhc/web/wiklou.git] / includes / libs / rdbms / connectionmanager / ConnectionManager.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 use Database;
6 use DBConnRef;
7 use InvalidArgumentException;
8
9 /**
10 * Database connection manager.
11 *
12 * This manages access to master and replica databases.
13 *
14 * @since 1.29
15 *
16 * @license GPL-2.0+
17 * @author Addshore
18 */
19 class ConnectionManager {
20
21 /**
22 * @var LoadBalancer
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 LoadBalancer $loadBalancer
40 * @param string|bool $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( LoadBalancer $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 *
60 * @return Database
61 */
62 private function getConnection( $i, array $groups = null ) {
63 $groups = $groups === null ? $this->groups : $groups;
64 return $this->loadBalancer->getConnection( $i, $groups, $this->domain );
65 }
66
67 /**
68 * @param int $i
69 * @param string[]|null $groups
70 *
71 * @return DBConnRef
72 */
73 private function getConnectionRef( $i, array $groups = null ) {
74 $groups = $groups === null ? $this->groups : $groups;
75 return $this->loadBalancer->getConnectionRef( $i, $groups, $this->domain );
76 }
77
78 /**
79 * Returns a connection to the master DB, for updating. The connection should later be released
80 * by calling releaseConnection().
81 *
82 * @since 1.29
83 *
84 * @return Database
85 */
86 public function getWriteConnection() {
87 return $this->getConnection( DB_MASTER );
88 }
89
90 /**
91 * Returns a database connection for reading. The connection should later be released by
92 * calling releaseConnection().
93 *
94 * @since 1.29
95 *
96 * @param string[]|null $groups
97 *
98 * @return Database
99 */
100 public function getReadConnection( array $groups = null ) {
101 $groups = $groups === null ? $this->groups : $groups;
102 return $this->getConnection( DB_REPLICA, $groups );
103 }
104
105 /**
106 * @since 1.29
107 *
108 * @param IDatabase $db
109 */
110 public function releaseConnection( IDatabase $db ) {
111 $this->loadBalancer->reuseConnection( $db );
112 }
113
114 /**
115 * Returns a connection ref to the master DB, for updating.
116 *
117 * @since 1.29
118 *
119 * @return DBConnRef
120 */
121 public function getWriteConnectionRef() {
122 return $this->getConnectionRef( DB_MASTER );
123 }
124
125 /**
126 * Returns a database connection ref for reading.
127 *
128 * @since 1.29
129 *
130 * @param string[]|null $groups
131 *
132 * @return DBConnRef
133 */
134 public function getReadConnectionRef( array $groups = null ) {
135 $groups = $groups === null ? $this->groups : $groups;
136 return $this->getConnectionRef( DB_REPLICA, $groups );
137 }
138
139 }