Skip to content

Commit

Permalink
Merge pull request #50 from ezweb/public
Browse files Browse the repository at this point in the history
make masterConnection / slaveConnection public
  • Loading branch information
gregorg authored Nov 9, 2018
2 parents c26747f + 607fdbb commit 589519d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public function tableCanBeCreatedAutomaticallyOn($tableName, $connectionName)
// drop table first ??
$retryStrategy = $this->connections[$connectionName]['params']['retryStrategy'];
$this->wrpdcnx($connectionName)->exec('DROP TABLE IF EXISTS ' . $tableName);
$retryStrategy->addHandler(function (Exception $exception,RetryConnection $connection) use ($tableName) {
$retryStrategy->addHandler(function (Exception $exception, RetryConnection $connection) use ($tableName) {
if (strpos($exception->getMessage(), $tableName) !== false) {
$connection->exec("CREATE TABLE {$tableName} (id INT)");
return true;
Expand Down
33 changes: 24 additions & 9 deletions src/Driver/Connection/MasterSlavesConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ public function __construct(array $master, array $slaves, $cache = null)
$this->cache = $cache;
}

public function disableCache() {
public function disableCache(): void
{
$this->cache->disableCache();
}

/**
* @param array $slaves
* @return Slave[]
*/
private function filteredSlaves(array $slaves)
private function filteredSlaves(array $slaves): array
{
// keep slave with weight > 0
return array_filter(array_map(function ($slave) {
Expand All @@ -79,7 +80,7 @@ private function filteredSlaves(array $slaves)
* @return Connection
* @throws \Doctrine\DBAL\DBALException
*/
private function masterConnection(bool $forced = null): Connection
public function masterConnection(bool $forced = null): Connection
{
if ($forced !== null) {
$this->forceMaster = $forced;
Expand All @@ -92,7 +93,7 @@ private function masterConnection(bool $forced = null): Connection
* @return Connection
* @throws \Doctrine\DBAL\DBALException
*/
private function slaveConnection(): ?Connection
public function slaveConnection(): ?Connection
{
if (empty($this->slaves)) {
return $this->masterConnection();
Expand All @@ -117,17 +118,20 @@ private function slaveConnection(): ?Connection

/**
* @param Slave[] $slaves
* @return Slave
* @return Slave|null
*/
private function randomSlave(array $slaves): Slave
private function randomSlave(array $slaves): ?Slave
{
$weights = [];
foreach ($slaves as $slaveIndex => $slave) {
if (!$this->isSlaveOk($slave)) {
if (!$this->isSlaveOK($slave)) {
continue;
}
$weights = array_merge($weights, array_fill(0, $slave->getWeight(), $slaveIndex));
}
if (!\count($weights)) {
return null;
}
return $slaves[$weights[array_rand($weights)]];
}

Expand All @@ -153,6 +157,7 @@ public function slaves(): ?array
* @param string $prepareString
*
* @return \Doctrine\DBAL\Driver\Statement
* @throws \Doctrine\DBAL\DBALException
*/
public function prepare($prepareString)
{
Expand Down Expand Up @@ -186,6 +191,7 @@ public function forceMaster(bool $force): void
* Executes an SQL statement, returning a result set as a Statement object.
*
* @return \Doctrine\DBAL\Driver\Statement
* @throws \Doctrine\DBAL\DBALException
*/
public function query()
{
Expand All @@ -196,10 +202,11 @@ public function query()
/**
* Quotes a string for use in a query.
*
* @param string $input
* @param string $input
* @param integer $type
*
* @return string
* @throws \Doctrine\DBAL\DBALException
*/
public function quote($input, $type = \PDO::PARAM_STR)
{
Expand All @@ -212,6 +219,7 @@ public function quote($input, $type = \PDO::PARAM_STR)
* @param string $statement
*
* @return integer
* @throws \Doctrine\DBAL\DBALException
*/
public function exec($statement)
{
Expand All @@ -224,6 +232,7 @@ public function exec($statement)
* @param string|null $name
*
* @return string
* @throws \Doctrine\DBAL\DBALException
*/
public function lastInsertId($name = null)
{
Expand All @@ -234,6 +243,7 @@ public function lastInsertId($name = null)
* Initiates a transaction.
*
* @return boolean TRUE on success or FALSE on failure.
* @throws \Doctrine\DBAL\DBALException
*/
public function beginTransaction()
{
Expand All @@ -244,6 +254,7 @@ public function beginTransaction()
* Commits a transaction.
*
* @return boolean TRUE on success or FALSE on failure.
* @throws \Doctrine\DBAL\DBALException
*/
public function commit()
{
Expand All @@ -254,6 +265,7 @@ public function commit()
* Rolls back the current transaction, as initiated by beginTransaction().
*
* @return boolean TRUE on success or FALSE on failure.
* @throws \Doctrine\DBAL\DBALException
*/
public function rollBack()
{
Expand All @@ -264,6 +276,7 @@ public function rollBack()
* Returns the error code associated with the last operation on the database handle.
*
* @return string|null The error code, or null if no operation has been run on the database handle.
* @throws \Doctrine\DBAL\DBALException
*/
public function errorCode()
{
Expand All @@ -274,6 +287,7 @@ public function errorCode()
* Returns extended error information associated with the last operation on the database handle.
*
* @return array
* @throws \Doctrine\DBAL\DBALException
*/
public function errorInfo()
{
Expand All @@ -288,7 +302,8 @@ private function getCacheKey(Slave $slave) {
return 'Slave_' . md5(serialize($slave->dbalConfig()));
}

public function setSlaveStatus(Slave $slave, bool $running, int $delay = null) {
public function setSlaveStatus(Slave $slave, bool $running, int $delay = null): array
{
if ($this->hasCache()) {
$this->cache->setCacheItem($this->getCacheKey($slave), ['running' => $running, 'delay' => $delay], $this->slaveStatusCacheTtl);
}
Expand Down

0 comments on commit 589519d

Please sign in to comment.