1 | <?php |
||
11 | class MasterSlavesConnection implements Connection, ConnectionWrapper |
||
12 | { |
||
13 | private $master; |
||
14 | private $slaves; |
||
15 | private $connection; |
||
16 | private $driver; |
||
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->connection = null; |
||
41 | } |
||
42 | |||
43 | public function connectToSlave() |
||
44 | { |
||
45 | $this->currentConnectionParams = null; |
||
46 | $this->currentSlave = null; |
||
47 | $this->connection = null; |
||
48 | } |
||
49 | |||
50 | public function isConnectedToMaster() |
||
51 | { |
||
52 | return $this->currentSlave === null && $this->currentConnectionParams !== null; |
||
53 | } |
||
54 | |||
55 | private function connection() |
||
62 | |||
63 | /** |
||
64 | * @inherit |
||
65 | */ |
||
66 | public function getCurrentConnection() |
||
70 | |||
71 | /** |
||
72 | * @inherit |
||
73 | */ |
||
74 | public function wrappedConnection() |
||
81 | |||
82 | public function wrappedDriver() |
||
89 | |||
90 | private function wrap() |
||
100 | |||
101 | private function chooseASlave() |
||
115 | |||
116 | private function totalSlavesWeight() |
||
124 | |||
125 | public function disableCurrentSlave() |
||
126 | { |
||
127 | if ($this->currentSlave !== null) { |
||
128 | array_splice($this->slaves, $this->currentSlave, 1); |
||
129 | $this->currentSlave = null; |
||
134 | |||
135 | public function slaves() |
||
139 | |||
140 | /** |
||
141 | * Prepares a statement for execution and returns a Statement object. |
||
142 | * |
||
143 | * @param string $prepareString |
||
144 | * |
||
145 | * @return \Doctrine\DBAL\Driver\Statement |
||
146 | */ |
||
147 | public function prepare($prepareString) |
||
152 | |||
153 | /** |
||
154 | * Executes an SQL statement, returning a result set as a Statement object. |
||
155 | * |
||
156 | * @return \Doctrine\DBAL\Driver\Statement |
||
157 | */ |
||
158 | public function query() |
||
162 | |||
163 | /** |
||
164 | * Quotes a string for use in a query. |
||
165 | * |
||
166 | * @param string $input |
||
167 | * @param integer $type |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function quote($input, $type = \PDO::PARAM_STR) |
||
175 | |||
176 | /** |
||
177 | * Executes an SQL statement and return the number of affected rows. |
||
178 | * |
||
179 | * @param string $statement |
||
180 | * |
||
181 | * @return integer |
||
182 | */ |
||
183 | public function exec($statement) |
||
188 | |||
189 | /** |
||
190 | * Returns the ID of the last inserted row or sequence value. |
||
191 | * |
||
192 | * @param string|null $name |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function lastInsertId($name = null) |
||
200 | |||
201 | /** |
||
202 | * Initiates a transaction. |
||
203 | * |
||
204 | * @return boolean TRUE on success or FALSE on failure. |
||
205 | */ |
||
206 | public function beginTransaction() |
||
211 | |||
212 | /** |
||
213 | * Commits a transaction. |
||
214 | * |
||
215 | * @return boolean TRUE on success or FALSE on failure. |
||
216 | */ |
||
217 | public function commit() |
||
222 | |||
223 | /** |
||
224 | * Rolls back the current transaction, as initiated by beginTransaction(). |
||
225 | * |
||
226 | * @return boolean TRUE on success or FALSE on failure. |
||
227 | */ |
||
228 | public function rollBack() |
||
233 | |||
234 | /** |
||
235 | * Returns the error code associated with the last operation on the database handle. |
||
236 | * |
||
237 | * @return string|null The error code, or null if no operation has been run on the database handle. |
||
238 | */ |
||
239 | public function errorCode() |
||
243 | |||
244 | /** |
||
245 | * Returns extended error information associated with the last operation on the database handle. |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | public function errorInfo() |
||
253 | } |
||
254 |