1 | <?php |
||
11 | class MasterSlavesConnection implements Connection, ConnectionWrapper |
||
|
|||
12 | { |
||
13 | use ConnectionWrapperTrait; |
||
14 | |||
15 | private $master; |
||
16 | private $slaves; |
||
17 | private $currentConnectionParams; |
||
18 | private $currentSlave; |
||
19 | |||
20 | public function __construct(array $master, array $slaves) |
||
21 | { |
||
22 | $this->master = $master; |
||
23 | $this->checkSlaves($slaves); |
||
24 | $this->slaves = $slaves; |
||
25 | } |
||
26 | |||
27 | private function checkSlaves(array $slaves) |
||
28 | { |
||
29 | foreach ($slaves as $slave) { |
||
30 | if ((int)$slave['weight'] < 0) { |
||
31 | throw new Exception('Slave weight must be >= 0'); |
||
32 | } |
||
33 | } |
||
34 | } |
||
35 | |||
36 | public function connectToMaster() |
||
37 | { |
||
38 | $this->currentConnectionParams = $this->master; |
||
39 | $this->currentSlave = null; |
||
40 | $this->wrappedConnection = null; |
||
41 | } |
||
42 | |||
43 | public function connectToSlave() |
||
44 | { |
||
45 | $this->currentConnectionParams = null; |
||
46 | $this->currentSlave = null; |
||
47 | $this->wrappedConnection = null; |
||
48 | } |
||
49 | |||
50 | public function isConnectedToMaster() |
||
51 | { |
||
52 | return $this->currentSlave === null && $this->currentConnectionParams !== null; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @inherit |
||
57 | */ |
||
58 | public function getCurrentConnection() |
||
62 | |||
63 | protected function wrap() |
||
64 | { |
||
65 | if ($this->currentConnectionParams === null) { |
||
66 | $this->currentSlave = $this->chooseASlave(); |
||
67 | $this->currentConnectionParams = $this->currentSlave ? $this->slaves[$this->currentSlave] : $this->master; |
||
73 | |||
74 | private function chooseASlave() |
||
88 | |||
89 | private function totalSlavesWeight() |
||
97 | |||
98 | public function disableCurrentSlave() |
||
107 | |||
108 | public function slaves() |
||
112 | |||
113 | /** |
||
114 | * Prepares a statement for execution and returns a Statement object. |
||
115 | * |
||
116 | * @param string $prepareString |
||
117 | * |
||
118 | * @return \Doctrine\DBAL\Driver\Statement |
||
119 | */ |
||
120 | public function prepare($prepareString) |
||
125 | |||
126 | /** |
||
127 | * Executes an SQL statement, returning a result set as a Statement object. |
||
128 | * |
||
129 | * @return \Doctrine\DBAL\Driver\Statement |
||
130 | */ |
||
131 | public function query() |
||
135 | |||
136 | /** |
||
137 | * Quotes a string for use in a query. |
||
138 | * |
||
139 | * @param string $input |
||
140 | * @param integer $type |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | public function quote($input, $type = \PDO::PARAM_STR) |
||
148 | |||
149 | /** |
||
150 | * Executes an SQL statement and return the number of affected rows. |
||
151 | * |
||
152 | * @param string $statement |
||
153 | * |
||
154 | * @return integer |
||
155 | */ |
||
156 | public function exec($statement) |
||
161 | |||
162 | /** |
||
163 | * Returns the ID of the last inserted row or sequence value. |
||
164 | * |
||
165 | * @param string|null $name |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function lastInsertId($name = null) |
||
173 | |||
174 | /** |
||
175 | * Initiates a transaction. |
||
176 | * |
||
177 | * @return boolean TRUE on success or FALSE on failure. |
||
178 | */ |
||
179 | public function beginTransaction() |
||
184 | |||
185 | /** |
||
186 | * Commits a transaction. |
||
187 | * |
||
188 | * @return boolean TRUE on success or FALSE on failure. |
||
189 | */ |
||
190 | public function commit() |
||
195 | |||
196 | /** |
||
197 | * Rolls back the current transaction, as initiated by beginTransaction(). |
||
198 | * |
||
199 | * @return boolean TRUE on success or FALSE on failure. |
||
200 | */ |
||
201 | public function rollBack() |
||
206 | |||
207 | /** |
||
208 | * Returns the error code associated with the last operation on the database handle. |
||
209 | * |
||
210 | * @return string|null The error code, or null if no operation has been run on the database handle. |
||
211 | */ |
||
212 | public function errorCode() |
||
216 | |||
217 | /** |
||
218 | * Returns extended error information associated with the last operation on the database handle. |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | public function errorInfo() |
||
226 | } |
||
227 |