Skip to content

Commit 589519d

Browse files
authored
Merge pull request #50 from ezweb/public
make masterConnection / slaveConnection public
2 parents c26747f + 607fdbb commit 589519d

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

‎features/bootstrap/FeatureContext.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ public function tableCanBeCreatedAutomaticallyOn($tableName, $connectionName)
511511
// drop table first ??
512512
$retryStrategy = $this->connections[$connectionName]['params']['retryStrategy'];
513513
$this->wrpdcnx($connectionName)->exec('DROP TABLE IF EXISTS ' . $tableName);
514-
$retryStrategy->addHandler(function (Exception $exception,RetryConnection $connection) use ($tableName) {
514+
$retryStrategy->addHandler(function (Exception $exception, RetryConnection $connection) use ($tableName) {
515515
if (strpos($exception->getMessage(), $tableName) !== false) {
516516
$connection->exec("CREATE TABLE {$tableName} (id INT)");
517517
return true;

‎src/Driver/Connection/MasterSlavesConnection.php‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ public function __construct(array $master, array $slaves, $cache = null)
5555
$this->cache = $cache;
5656
}
5757

58-
public function disableCache() {
58+
public function disableCache(): void
59+
{
5960
$this->cache->disableCache();
6061
}
6162

6263
/**
6364
* @param array $slaves
6465
* @return Slave[]
6566
*/
66-
private function filteredSlaves(array $slaves)
67+
private function filteredSlaves(array $slaves): array
6768
{
6869
// keep slave with weight > 0
6970
return array_filter(array_map(function ($slave) {
@@ -79,7 +80,7 @@ private function filteredSlaves(array $slaves)
7980
* @return Connection
8081
* @throws \Doctrine\DBAL\DBALException
8182
*/
82-
private function masterConnection(bool $forced = null): Connection
83+
public function masterConnection(bool $forced = null): Connection
8384
{
8485
if ($forced !== null) {
8586
$this->forceMaster = $forced;
@@ -92,7 +93,7 @@ private function masterConnection(bool $forced = null): Connection
9293
* @return Connection
9394
* @throws \Doctrine\DBAL\DBALException
9495
*/
95-
private function slaveConnection(): ?Connection
96+
public function slaveConnection(): ?Connection
9697
{
9798
if (empty($this->slaves)) {
9899
return $this->masterConnection();
@@ -117,17 +118,20 @@ private function slaveConnection(): ?Connection
117118

118119
/**
119120
* @param Slave[] $slaves
120-
* @return Slave
121+
* @return Slave|null
121122
*/
122-
private function randomSlave(array $slaves): Slave
123+
private function randomSlave(array $slaves): ?Slave
123124
{
124125
$weights = [];
125126
foreach ($slaves as $slaveIndex => $slave) {
126-
if (!$this->isSlaveOk($slave)) {
127+
if (!$this->isSlaveOK($slave)) {
127128
continue;
128129
}
129130
$weights = array_merge($weights, array_fill(0, $slave->getWeight(), $slaveIndex));
130131
}
132+
if (!\count($weights)) {
133+
return null;
134+
}
131135
return $slaves[$weights[array_rand($weights)]];
132136
}
133137

@@ -153,6 +157,7 @@ public function slaves(): ?array
153157
* @param string $prepareString
154158
*
155159
* @return \Doctrine\DBAL\Driver\Statement
160+
* @throws \Doctrine\DBAL\DBALException
156161
*/
157162
public function prepare($prepareString)
158163
{
@@ -186,6 +191,7 @@ public function forceMaster(bool $force): void
186191
* Executes an SQL statement, returning a result set as a Statement object.
187192
*
188193
* @return \Doctrine\DBAL\Driver\Statement
194+
* @throws \Doctrine\DBAL\DBALException
189195
*/
190196
public function query()
191197
{
@@ -196,10 +202,11 @@ public function query()
196202
/**
197203
* Quotes a string for use in a query.
198204
*
199-
* @param string $input
205+
* @param string $input
200206
* @param integer $type
201207
*
202208
* @return string
209+
* @throws \Doctrine\DBAL\DBALException
203210
*/
204211
public function quote($input, $type = \PDO::PARAM_STR)
205212
{
@@ -212,6 +219,7 @@ public function quote($input, $type = \PDO::PARAM_STR)
212219
* @param string $statement
213220
*
214221
* @return integer
222+
* @throws \Doctrine\DBAL\DBALException
215223
*/
216224
public function exec($statement)
217225
{
@@ -224,6 +232,7 @@ public function exec($statement)
224232
* @param string|null $name
225233
*
226234
* @return string
235+
* @throws \Doctrine\DBAL\DBALException
227236
*/
228237
public function lastInsertId($name = null)
229238
{
@@ -234,6 +243,7 @@ public function lastInsertId($name = null)
234243
* Initiates a transaction.
235244
*
236245
* @return boolean TRUE on success or FALSE on failure.
246+
* @throws \Doctrine\DBAL\DBALException
237247
*/
238248
public function beginTransaction()
239249
{
@@ -244,6 +254,7 @@ public function beginTransaction()
244254
* Commits a transaction.
245255
*
246256
* @return boolean TRUE on success or FALSE on failure.
257+
* @throws \Doctrine\DBAL\DBALException
247258
*/
248259
public function commit()
249260
{
@@ -254,6 +265,7 @@ public function commit()
254265
* Rolls back the current transaction, as initiated by beginTransaction().
255266
*
256267
* @return boolean TRUE on success or FALSE on failure.
268+
* @throws \Doctrine\DBAL\DBALException
257269
*/
258270
public function rollBack()
259271
{
@@ -264,6 +276,7 @@ public function rollBack()
264276
* Returns the error code associated with the last operation on the database handle.
265277
*
266278
* @return string|null The error code, or null if no operation has been run on the database handle.
279+
* @throws \Doctrine\DBAL\DBALException
267280
*/
268281
public function errorCode()
269282
{
@@ -274,6 +287,7 @@ public function errorCode()
274287
* Returns extended error information associated with the last operation on the database handle.
275288
*
276289
* @return array
290+
* @throws \Doctrine\DBAL\DBALException
277291
*/
278292
public function errorInfo()
279293
{
@@ -288,7 +302,8 @@ private function getCacheKey(Slave $slave) {
288302
return 'Slave_' . md5(serialize($slave->dbalConfig()));
289303
}
290304

291-
public function setSlaveStatus(Slave $slave, bool $running, int $delay = null) {
305+
public function setSlaveStatus(Slave $slave, bool $running, int $delay = null): array
306+
{
292307
if ($this->hasCache()) {
293308
$this->cache->setCacheItem($this->getCacheKey($slave), ['running' => $running, 'delay' => $delay], $this->slaveStatusCacheTtl);
294309
}

0 commit comments

Comments
 (0)