Skip to content

Commit

Permalink
Merge pull request doctrine#3571 from jwage/sharding-types
Browse files Browse the repository at this point in the history
Add proper types to the Doctrine\DBAL\Sharding namespace.
  • Loading branch information
jwage authored and morozov committed May 31, 2019
2 parents 43b2149 + f6e3a29 commit 82261fc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 63 deletions.
10 changes: 5 additions & 5 deletions lib/Doctrine/DBAL/Sharding/PoolingShardManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PoolingShardManager implements ShardManager
/** @var ShardChoser */
private $choser;

/** @var string|null */
/** @var mixed */
private $currentDistributionValue;

public function __construct(PoolingShardConnection $conn)
Expand All @@ -31,7 +31,7 @@ public function __construct(PoolingShardConnection $conn)
/**
* {@inheritDoc}
*/
public function selectGlobal()
public function selectGlobal() : void
{
$this->conn->connect(0);
$this->currentDistributionValue = null;
Expand All @@ -40,7 +40,7 @@ public function selectGlobal()
/**
* {@inheritDoc}
*/
public function selectShard($distributionValue)
public function selectShard($distributionValue) : void
{
$shardId = $this->choser->pickShard($distributionValue, $this->conn);
$this->conn->connect($shardId);
Expand All @@ -58,7 +58,7 @@ public function getCurrentDistributionValue()
/**
* {@inheritDoc}
*/
public function getShards()
public function getShards() : array
{
$params = $this->conn->getParams();
$shards = [];
Expand All @@ -75,7 +75,7 @@ public function getShards()
*
* @throws RuntimeException
*/
public function queryAll($sql, array $params, array $types)
public function queryAll(string $sql, array $params, array $types) : array
{
$shards = $this->getShards();
if (! $shards) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function dropAllSchema() : void
/**
* @return Schema[]
*/
private function partitionSchema(Schema $schema)
private function partitionSchema(Schema $schema) : array
{
return [
$this->extractSchemaFederation($schema, false),
Expand All @@ -167,13 +167,9 @@ private function partitionSchema(Schema $schema)
}

/**
* @param bool $isFederation
*
* @return Schema
*
* @throws RuntimeException
*/
private function extractSchemaFederation(Schema $schema, $isFederation)
private function extractSchemaFederation(Schema $schema, bool $isFederation) : Schema
{
$partitionedSchema = clone $schema;

Expand Down Expand Up @@ -204,7 +200,7 @@ private function extractSchemaFederation(Schema $schema, $isFederation)
*
* @return string[]
*/
private function work(Schema $schema, Closure $operation)
private function work(Schema $schema, Closure $operation) : array
{
[$global, $federation] = $this->partitionSchema($schema);
$sql = [];
Expand Down Expand Up @@ -235,10 +231,7 @@ private function work(Schema $schema, Closure $operation)
return $sql;
}

/**
* @return string
*/
private function getFederationTypeDefaultValue()
private function getFederationTypeDefaultValue() : string
{
$federationType = Type::getType($this->shardManager->getDistributionType());

Expand All @@ -259,10 +252,7 @@ private function getFederationTypeDefaultValue()
return $defaultValue;
}

/**
* @return string
*/
private function getCreateFederationStatement()
private function getCreateFederationStatement() : string
{
$federationType = Type::getType($this->shardManager->getDistributionType());
$federationTypeSql = $federationType->getSQLDeclaration([], $this->conn->getDatabasePlatform());
Expand Down
34 changes: 11 additions & 23 deletions lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureShardManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SQLAzureShardManager implements ShardManager
/** @var Connection */
private $conn;

/** @var string|null */
/** @var mixed */
private $currentDistributionValue;

/**
Expand Down Expand Up @@ -66,50 +66,40 @@ public function __construct(Connection $conn)

/**
* Gets the name of the federation.
*
* @return string
*/
public function getFederationName()
public function getFederationName() : string
{
return $this->federationName;
}

/**
* Gets the distribution key.
*
* @return string
*/
public function getDistributionKey()
public function getDistributionKey() : string
{
return $this->distributionKey;
}

/**
* Gets the Doctrine Type name used for the distribution.
*
* @return string
*/
public function getDistributionType()
public function getDistributionType() : string
{
return $this->distributionType;
}

/**
* Sets Enabled/Disable filtering on the fly.
*
* @param bool $flag
*
* @return void
*/
public function setFilteringEnabled($flag)
public function setFilteringEnabled(bool $flag) : void
{
$this->filteringEnabled = (bool) $flag;
$this->filteringEnabled = $flag;
}

/**
* {@inheritDoc}
*/
public function selectGlobal()
public function selectGlobal() : void
{
if ($this->conn->isTransactionActive()) {
throw ActiveTransaction::new();
Expand All @@ -123,7 +113,7 @@ public function selectGlobal()
/**
* {@inheritDoc}
*/
public function selectShard($distributionValue)
public function selectShard($distributionValue) : void
{
if ($this->conn->isTransactionActive()) {
throw ActiveTransaction::new();
Expand Down Expand Up @@ -153,7 +143,7 @@ public function getCurrentDistributionValue()
/**
* {@inheritDoc}
*/
public function getShards()
public function getShards() : array
{
$sql = 'SELECT member_id as id,
distribution_name as distribution_key,
Expand All @@ -169,7 +159,7 @@ public function getShards()
/**
* {@inheritDoc}
*/
public function queryAll($sql, array $params = [], array $types = [])
public function queryAll(string $sql, array $params = [], array $types = []) : array
{
$shards = $this->getShards();
if (! $shards) {
Expand Down Expand Up @@ -199,10 +189,8 @@ public function queryAll($sql, array $params = [], array $types = [])
* Splits Federation at a given distribution value.
*
* @param mixed $splitDistributionValue
*
* @return void
*/
public function splitFederation($splitDistributionValue)
public function splitFederation($splitDistributionValue) : void
{
$type = Type::getType($this->distributionType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ class MultiTenantVisitor implements Visitor
private $distributionName;

/**
* @param string[] $excludedTables
* @param string $tenantColumnName
* @param string|null $distributionName
* @param string[] $excludedTables
*/
public function __construct(array $excludedTables = [], $tenantColumnName = 'tenant_id', $distributionName = null)
public function __construct(array $excludedTables = [], string $tenantColumnName = 'tenant_id', ?string $distributionName = null)
{
$this->excludedTables = $excludedTables;
$this->tenantColumnName = $tenantColumnName;
Expand Down Expand Up @@ -94,13 +92,9 @@ public function acceptTable(Table $table) : void
}

/**
* @param Table $table
*
* @return Index
*
* @throws RuntimeException
*/
private function getClusteredIndex($table)
private function getClusteredIndex(Table $table) : Index
{
foreach ($table->getIndexes() as $index) {
if ($index->isPrimary() && ! $index->hasFlag('nonclustered')) {
Expand Down
17 changes: 6 additions & 11 deletions lib/Doctrine/DBAL/Sharding/ShardManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,22 @@ interface ShardManager
*
* This is the default database that is connected when no shard is
* selected.
*
* @return void
*/
public function selectGlobal();
public function selectGlobal() : void;

/**
* Selects the shard against which the queries after this statement will be issued.
*
* @param string $distributionValue
*
* @return void
* @param mixed $distributionValue
*
* @throws ShardingException If no value is passed as shard identifier.
*/
public function selectShard($distributionValue);
public function selectShard($distributionValue) : void;

/**
* Gets the distribution value currently used for sharding.
*
* @return string|null
* @return mixed
*/
public function getCurrentDistributionValue();

Expand All @@ -58,19 +54,18 @@ public function getCurrentDistributionValue();
*
* @return mixed[][]
*/
public function getShards();
public function getShards() : array;

/**
* Queries all shards in undefined order and return the results appended to
* each other. Restore the previous distribution value after execution.
*
* Using {@link \Doctrine\DBAL\Connection::fetchAll} to retrieve rows internally.
*
* @param string $sql
* @param mixed[] $params
* @param int[]|string[] $types
*
* @return mixed[]
*/
public function queryAll($sql, array $params, array $types);
public function queryAll(string $sql, array $params, array $types) : array;
}

0 comments on commit 82261fc

Please sign in to comment.