Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[lhc/web/wiklou.git] / includes / libs / rdbms / connectionmanager / ConnectionManager.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 use InvalidArgumentException;
6
7 /**
8 * Database connection manager.
9 *
10 * This manages access to master and replica databases.
11 *
12 * @since 1.29
13 *
14 * @license GPL-2.0+
15 * @author Addshore
16 */
17 class ConnectionManager {
18
19 /**
20 * @var LoadBalancer
21 */
22 private $loadBalancer;
23
24 /**
25 * The symbolic name of the target database, or false for the local wiki's database.
26 *
27 * @var string|false
28 */
29 private $domain;
30
31 /**
32 * @var string[]
33 */
34 private $groups = [];
35
36 /**
37 * @param LoadBalancer $loadBalancer
38 * @param string|bool $domain Optional logical DB name, defaults to current wiki.
39 * This follows the convention for database names used by $loadBalancer.
40 * @param string[] $groups see LoadBalancer::getConnection
41 *
42 * @throws InvalidArgumentException
43 */
44 public function __construct( LoadBalancer $loadBalancer, $domain = false, array $groups = [] ) {
45 if ( !is_string( $domain ) && $domain !== false ) {
46 throw new InvalidArgumentException( '$dbName must be a string, or false.' );
47 }
48
49 $this->loadBalancer = $loadBalancer;
50 $this->domain = $domain;
51 $this->groups = $groups;
52 }
53
54 /**
55 * @param int $i
56 * @param string[]|null $groups
57 *
58 * @return Database
59 */
60 private function getConnection( $i, array $groups = null ) {
61 $groups = $groups === null ? $this->groups : $groups;
62 return $this->loadBalancer->getConnection( $i, $groups, $this->domain );
63 }
64
65 /**
66 * @param int $i
67 * @param string[]|null $groups
68 *
69 * @return DBConnRef
70 */
71 private function getConnectionRef( $i, array $groups = null ) {
72 $groups = $groups === null ? $this->groups : $groups;
73 return $this->loadBalancer->getConnectionRef( $i, $groups, $this->domain );
74 }
75
76 /**
77 * Returns a connection to the master DB, for updating. The connection should later be released
78 * by calling releaseConnection().
79 *
80 * @since 1.29
81 *
82 * @return Database
83 */
84 public function getWriteConnection() {
85 return $this->getConnection( DB_MASTER );
86 }
87
88 /**
89 * Returns a database connection for reading. The connection should later be released by
90 * calling releaseConnection().
91 *
92 * @since 1.29
93 *
94 * @param string[]|null $groups
95 *
96 * @return Database
97 */
98 public function getReadConnection( array $groups = null ) {
99 $groups = $groups === null ? $this->groups : $groups;
100 return $this->getConnection( DB_REPLICA, $groups );
101 }
102
103 /**
104 * @since 1.29
105 *
106 * @param IDatabase $db
107 */
108 public function releaseConnection( IDatabase $db ) {
109 $this->loadBalancer->reuseConnection( $db );
110 }
111
112 /**
113 * Returns a connection ref to the master DB, for updating.
114 *
115 * @since 1.29
116 *
117 * @return DBConnRef
118 */
119 public function getWriteConnectionRef() {
120 return $this->getConnectionRef( DB_MASTER );
121 }
122
123 /**
124 * Returns a database connection ref for reading.
125 *
126 * @since 1.29
127 *
128 * @param string[]|null $groups
129 *
130 * @return DBConnRef
131 */
132 public function getReadConnectionRef( array $groups = null ) {
133 $groups = $groups === null ? $this->groups : $groups;
134 return $this->getConnectionRef( DB_REPLICA, $groups );
135 }
136
137 }