Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,264 @@
<?php
namespace Doctrine\DBAL\Sharding;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Sharding\ShardChoser\ShardChoser;
use InvalidArgumentException;
use function array_merge;
use function is_numeric;
use function is_string;
/**
* Sharding implementation that pools many different connections
* internally and serves data from the currently active connection.
*
* The internals of this class are:
*
* - All sharding clients are specified and given a shard-id during
* configuration.
* - By default, the global shard is selected. If no global shard is configured
* an exception is thrown on access.
* - Selecting a shard by distribution value delegates the mapping
* "distributionValue" => "client" to the ShardChoser interface.
* - An exception is thrown if trying to switch shards during an open
* transaction.
*
* Instantiation through the DriverManager looks like:
*
* @deprecated
*
* @example
*
* $conn = DriverManager::getConnection(array(
* 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
* 'driver' => 'pdo_mysql',
* 'global' => array('user' => '', 'password' => '', 'host' => '', 'dbname' => ''),
* 'shards' => array(
* array('id' => 1, 'user' => 'slave1', 'password', 'host' => '', 'dbname' => ''),
* array('id' => 2, 'user' => 'slave2', 'password', 'host' => '', 'dbname' => ''),
* ),
* 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
* ));
* $shardManager = $conn->getShardManager();
* $shardManager->selectGlobal();
* $shardManager->selectShard($value);
*/
class PoolingShardConnection extends Connection
{
/** @var DriverConnection[] */
private $activeConnections = [];
/** @var string|int|null */
private $activeShardId;
/** @var mixed[] */
private $connectionParameters = [];
/**
* {@inheritDoc}
*
* @internal The connection can be only instantiated by the driver manager.
*
* @throws InvalidArgumentException
*/
public function __construct(
array $params,
Driver $driver,
?Configuration $config = null,
?EventManager $eventManager = null
) {
if (! isset($params['global'], $params['shards'])) {
throw new InvalidArgumentException("Connection Parameters require 'global' and 'shards' configurations.");
}
if (! isset($params['shardChoser'])) {
throw new InvalidArgumentException("Missing Shard Choser configuration 'shardChoser'");
}
if (is_string($params['shardChoser'])) {
$params['shardChoser'] = new $params['shardChoser']();
}
if (! ($params['shardChoser'] instanceof ShardChoser)) {
throw new InvalidArgumentException(
"The 'shardChoser' configuration is not a valid instance of " . ShardChoser::class
);
}
$this->connectionParameters[0] = array_merge($params, $params['global']);
foreach ($params['shards'] as $shard) {
if (! isset($shard['id'])) {
throw new InvalidArgumentException(
"Missing 'id' for one configured shard. Please specify a unique shard-id."
);
}
if (! is_numeric($shard['id']) || $shard['id'] < 1) {
throw new InvalidArgumentException('Shard Id has to be a non-negative number.');
}
if (isset($this->connectionParameters[$shard['id']])) {
throw new InvalidArgumentException('Shard ' . $shard['id'] . ' is duplicated in the configuration.');
}
$this->connectionParameters[$shard['id']] = array_merge($params, $shard);
}
parent::__construct($params, $driver, $config, $eventManager);
}
/**
* Get active shard id.
*
* @return string|int|null
*/
public function getActiveShardId()
{
return $this->activeShardId;
}
/**
* {@inheritdoc}
*/
public function getParams()
{
return $this->activeShardId
? $this->connectionParameters[$this->activeShardId]
: $this->connectionParameters[0];
}
/**
* {@inheritdoc}
*/
public function getHost()
{
$params = $this->getParams();
return $params['host'] ?? parent::getHost();
}
/**
* {@inheritdoc}
*/
public function getPort()
{
$params = $this->getParams();
return $params['port'] ?? parent::getPort();
}
/**
* {@inheritdoc}
*/
public function getUsername()
{
$params = $this->getParams();
return $params['user'] ?? parent::getUsername();
}
/**
* {@inheritdoc}
*/
public function getPassword()
{
$params = $this->getParams();
return $params['password'] ?? parent::getPassword();
}
/**
* Connects to a given shard.
*
* @param string|int|null $shardId
*
* @return bool
*
* @throws ShardingException
*/
public function connect($shardId = null)
{
if ($shardId === null && $this->_conn) {
return false;
}
if ($shardId !== null && $shardId === $this->activeShardId) {
return false;
}
if ($this->getTransactionNestingLevel() > 0) {
throw new ShardingException('Cannot switch shard when transaction is active.');
}
$activeShardId = $this->activeShardId = (int) $shardId;
if (isset($this->activeConnections[$activeShardId])) {
$this->_conn = $this->activeConnections[$activeShardId];
return false;
}
$this->_conn = $this->activeConnections[$activeShardId] = $this->connectTo($activeShardId);
if ($this->_eventManager->hasListeners(Events::postConnect)) {
$eventArgs = new ConnectionEventArgs($this);
$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
}
return true;
}
/**
* Connects to a specific connection.
*
* @param string|int $shardId
*
* @return \Doctrine\DBAL\Driver\Connection
*/
protected function connectTo($shardId)
{
$params = $this->getParams();
$driverOptions = $params['driverOptions'] ?? [];
$connectionParams = $this->connectionParameters[$shardId];
$user = $connectionParams['user'] ?? null;
$password = $connectionParams['password'] ?? null;
return $this->_driver->connect($connectionParams, $user, $password, $driverOptions);
}
/**
* @param string|int|null $shardId
*
* @return bool
*/
public function isConnected($shardId = null)
{
if ($shardId === null) {
return $this->_conn !== null;
}
return isset($this->activeConnections[$shardId]);
}
/**
* @return void
*/
public function close()
{
$this->_conn = null;
$this->activeConnections = [];
$this->activeShardId = null;
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Doctrine\DBAL\Sharding;
use Doctrine\DBAL\Sharding\ShardChoser\ShardChoser;
use Doctrine\Deprecations\Deprecation;
use RuntimeException;
/**
* Shard Manager for the Connection Pooling Shard Strategy
*
* @deprecated
*/
class PoolingShardManager implements ShardManager
{
/** @var PoolingShardConnection */
private $conn;
/** @var ShardChoser */
private $choser;
/** @var string|null */
private $currentDistributionValue;
public function __construct(PoolingShardConnection $conn)
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3595',
'Native Sharding support in DBAL is removed without replacement.'
);
$params = $conn->getParams();
$this->conn = $conn;
$this->choser = $params['shardChoser'];
}
/**
* {@inheritDoc}
*/
public function selectGlobal()
{
$this->conn->connect(0);
$this->currentDistributionValue = null;
}
/**
* {@inheritDoc}
*/
public function selectShard($distributionValue)
{
$shardId = $this->choser->pickShard($distributionValue, $this->conn);
$this->conn->connect($shardId);
$this->currentDistributionValue = $distributionValue;
}
/**
* {@inheritDoc}
*/
public function getCurrentDistributionValue()
{
return $this->currentDistributionValue;
}
/**
* {@inheritDoc}
*/
public function getShards()
{
$params = $this->conn->getParams();
$shards = [];
foreach ($params['shards'] as $shard) {
$shards[] = ['id' => $shard['id']];
}
return $shards;
}
/**
* {@inheritDoc}
*
* @throws RuntimeException
*/
public function queryAll($sql, array $params, array $types)
{
$shards = $this->getShards();
if (! $shards) {
throw new RuntimeException('No shards found.');
}
$result = [];
$oldDistribution = $this->getCurrentDistributionValue();
foreach ($shards as $shard) {
$this->conn->connect($shard['id']);
foreach ($this->conn->fetchAllAssociative($sql, $params, $types) as $row) {
$result[] = $row;
}
}
if ($oldDistribution === null) {
$this->selectGlobal();
} else {
$this->selectShard($oldDistribution);
}
return $result;
}
}

View File

@@ -0,0 +1,284 @@
<?php
namespace Doctrine\DBAL\Sharding\SQLAzure;
use Closure;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Synchronizer\AbstractSchemaSynchronizer;
use Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer;
use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use RuntimeException;
use function array_merge;
/**
* SQL Azure Schema Synchronizer.
*
* Will iterate over all shards when performing schema operations. This is done
* by partitioning the passed schema into subschemas for the federation and the
* global database and then applying the operations step by step using the
* {@see \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer}.
*
* @deprecated
*/
class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
{
public const FEDERATION_TABLE_FEDERATED = 'azure.federated';
public const FEDERATION_DISTRIBUTION_NAME = 'azure.federatedOnDistributionName';
/** @var SQLAzureShardManager */
private $shardManager;
/** @var SchemaSynchronizer */
private $synchronizer;
public function __construct(Connection $conn, SQLAzureShardManager $shardManager, ?SchemaSynchronizer $sync = null)
{
parent::__construct($conn);
$this->shardManager = $shardManager;
$this->synchronizer = $sync ?: new SingleDatabaseSynchronizer($conn);
}
/**
* {@inheritdoc}
*/
public function getCreateSchema(Schema $createSchema)
{
$sql = [];
[$global, $federation] = $this->partitionSchema($createSchema);
$globalSql = $this->synchronizer->getCreateSchema($global);
if ($globalSql) {
$sql[] = "-- Create Root Federation\n" .
'USE FEDERATION ROOT WITH RESET;';
$sql = array_merge($sql, $globalSql);
}
$federationSql = $this->synchronizer->getCreateSchema($federation);
if ($federationSql) {
$defaultValue = $this->getFederationTypeDefaultValue();
$sql[] = $this->getCreateFederationStatement();
$sql[] = 'USE FEDERATION ' . $this->shardManager->getFederationName()
. ' (' . $this->shardManager->getDistributionKey() . ' = ' . $defaultValue . ')'
. ' WITH RESET, FILTERING = OFF;';
$sql = array_merge($sql, $federationSql);
}
return $sql;
}
/**
* {@inheritdoc}
*/
public function getUpdateSchema(Schema $toSchema, $noDrops = false)
{
return $this->work($toSchema, static function ($synchronizer, $schema) use ($noDrops) {
return $synchronizer->getUpdateSchema($schema, $noDrops);
});
}
/**
* {@inheritdoc}
*/
public function getDropSchema(Schema $dropSchema)
{
return $this->work($dropSchema, static function ($synchronizer, $schema) {
return $synchronizer->getDropSchema($schema);
});
}
/**
* {@inheritdoc}
*/
public function createSchema(Schema $createSchema)
{
$this->processSql($this->getCreateSchema($createSchema));
}
/**
* {@inheritdoc}
*/
public function updateSchema(Schema $toSchema, $noDrops = false)
{
$this->processSql($this->getUpdateSchema($toSchema, $noDrops));
}
/**
* {@inheritdoc}
*/
public function dropSchema(Schema $dropSchema)
{
$this->processSqlSafely($this->getDropSchema($dropSchema));
}
/**
* {@inheritdoc}
*/
public function getDropAllSchema()
{
$this->shardManager->selectGlobal();
$globalSql = $this->synchronizer->getDropAllSchema();
$sql = [];
if ($globalSql) {
$sql[] = "-- Work on Root Federation\nUSE FEDERATION ROOT WITH RESET;";
$sql = array_merge($sql, $globalSql);
}
$shards = $this->shardManager->getShards();
foreach ($shards as $shard) {
$this->shardManager->selectShard($shard['rangeLow']);
$federationSql = $this->synchronizer->getDropAllSchema();
if (! $federationSql) {
continue;
}
$sql[] = '-- Work on Federation ID ' . $shard['id'] . "\n" .
'USE FEDERATION ' . $this->shardManager->getFederationName()
. ' (' . $this->shardManager->getDistributionKey() . ' = ' . $shard['rangeLow'] . ')'
. ' WITH RESET, FILTERING = OFF;';
$sql = array_merge($sql, $federationSql);
}
$sql[] = 'USE FEDERATION ROOT WITH RESET;';
$sql[] = 'DROP FEDERATION ' . $this->shardManager->getFederationName();
return $sql;
}
/**
* {@inheritdoc}
*/
public function dropAllSchema()
{
$this->processSqlSafely($this->getDropAllSchema());
}
/**
* @return Schema[]
*/
private function partitionSchema(Schema $schema)
{
return [
$this->extractSchemaFederation($schema, false),
$this->extractSchemaFederation($schema, true),
];
}
/**
* @param bool $isFederation
*
* @return Schema
*
* @throws RuntimeException
*/
private function extractSchemaFederation(Schema $schema, $isFederation)
{
$partitionedSchema = clone $schema;
foreach ($partitionedSchema->getTables() as $table) {
if ($isFederation) {
$table->addOption(self::FEDERATION_DISTRIBUTION_NAME, $this->shardManager->getDistributionKey());
}
if ($table->hasOption(self::FEDERATION_TABLE_FEDERATED) !== $isFederation) {
$partitionedSchema->dropTable($table->getName());
} else {
foreach ($table->getForeignKeys() as $fk) {
$foreignTable = $schema->getTable($fk->getForeignTableName());
if ($foreignTable->hasOption(self::FEDERATION_TABLE_FEDERATED) !== $isFederation) {
throw new RuntimeException('Cannot have foreign key between global/federation.');
}
}
}
}
return $partitionedSchema;
}
/**
* Work on the Global/Federation based on currently existing shards and
* perform the given operation on the underlying schema synchronizer given
* the different partitioned schema instances.
*
* @return string[]
*/
private function work(Schema $schema, Closure $operation)
{
[$global, $federation] = $this->partitionSchema($schema);
$sql = [];
$this->shardManager->selectGlobal();
$globalSql = $operation($this->synchronizer, $global);
if ($globalSql) {
$sql[] = "-- Work on Root Federation\nUSE FEDERATION ROOT WITH RESET;";
$sql = array_merge($sql, $globalSql);
}
$shards = $this->shardManager->getShards();
foreach ($shards as $shard) {
$this->shardManager->selectShard($shard['rangeLow']);
$federationSql = $operation($this->synchronizer, $federation);
if (! $federationSql) {
continue;
}
$sql[] = '-- Work on Federation ID ' . $shard['id'] . "\n"
. 'USE FEDERATION ' . $this->shardManager->getFederationName()
. ' (' . $this->shardManager->getDistributionKey() . ' = ' . $shard['rangeLow'] . ')'
. ' WITH RESET, FILTERING = OFF;';
$sql = array_merge($sql, $federationSql);
}
return $sql;
}
/**
* @return string
*/
private function getFederationTypeDefaultValue()
{
$federationType = Type::getType($this->shardManager->getDistributionType());
switch ($federationType->getName()) {
case Types::GUID:
$defaultValue = '00000000-0000-0000-0000-000000000000';
break;
case Types::INTEGER:
case Types::SMALLINT:
case Types::BIGINT:
$defaultValue = '0';
break;
default:
$defaultValue = '';
break;
}
return $defaultValue;
}
/**
* @return string
*/
private function getCreateFederationStatement()
{
$federationType = Type::getType($this->shardManager->getDistributionType());
$federationTypeSql = $federationType->getSQLDeclaration([], $this->conn->getDatabasePlatform());
return "--Create Federation\n"
. 'CREATE FEDERATION ' . $this->shardManager->getFederationName()
. ' (' . $this->shardManager->getDistributionKey()
. ' ' . $federationTypeSql . ' RANGE)';
}
}

View File

@@ -0,0 +1,218 @@
<?php
namespace Doctrine\DBAL\Sharding\SQLAzure;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Sharding\ShardingException;
use Doctrine\DBAL\Sharding\ShardManager;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
use RuntimeException;
use function sprintf;
/**
* Sharding using the SQL Azure Federations support.
*
* @deprecated
*/
class SQLAzureShardManager implements ShardManager
{
/** @var string */
private $federationName;
/** @var bool */
private $filteringEnabled;
/** @var string */
private $distributionKey;
/** @var string */
private $distributionType;
/** @var Connection */
private $conn;
/** @var string|null */
private $currentDistributionValue;
/**
* @throws ShardingException
*/
public function __construct(Connection $conn)
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3595',
'Native Sharding support in DBAL is removed without replacement.'
);
$this->conn = $conn;
$params = $conn->getParams();
if (! isset($params['sharding']['federationName'])) {
throw ShardingException::missingDefaultFederationName();
}
if (! isset($params['sharding']['distributionKey'])) {
throw ShardingException::missingDefaultDistributionKey();
}
if (! isset($params['sharding']['distributionType'])) {
throw ShardingException::missingDistributionType();
}
$this->federationName = $params['sharding']['federationName'];
$this->distributionKey = $params['sharding']['distributionKey'];
$this->distributionType = $params['sharding']['distributionType'];
$this->filteringEnabled = (bool) ($params['sharding']['filteringEnabled'] ?? false);
}
/**
* Gets the name of the federation.
*
* @return string
*/
public function getFederationName()
{
return $this->federationName;
}
/**
* Gets the distribution key.
*
* @return string
*/
public function getDistributionKey()
{
return $this->distributionKey;
}
/**
* Gets the Doctrine Type name used for the distribution.
*
* @return string
*/
public function getDistributionType()
{
return $this->distributionType;
}
/**
* Sets Enabled/Disable filtering on the fly.
*
* @param bool $flag
*
* @return void
*/
public function setFilteringEnabled($flag)
{
$this->filteringEnabled = (bool) $flag;
}
/**
* {@inheritDoc}
*/
public function selectGlobal()
{
if ($this->conn->isTransactionActive()) {
throw ShardingException::activeTransaction();
}
$sql = 'USE FEDERATION ROOT WITH RESET';
$this->conn->exec($sql);
$this->currentDistributionValue = null;
}
/**
* {@inheritDoc}
*/
public function selectShard($distributionValue)
{
if ($this->conn->isTransactionActive()) {
throw ShardingException::activeTransaction();
}
$platform = $this->conn->getDatabasePlatform();
$sql = sprintf(
'USE FEDERATION %s (%s = %s) WITH RESET, FILTERING = %s;',
$platform->quoteIdentifier($this->federationName),
$platform->quoteIdentifier($this->distributionKey),
$this->conn->quote($distributionValue),
($this->filteringEnabled ? 'ON' : 'OFF')
);
$this->conn->exec($sql);
$this->currentDistributionValue = $distributionValue;
}
/**
* {@inheritDoc}
*/
public function getCurrentDistributionValue()
{
return $this->currentDistributionValue;
}
/**
* {@inheritDoc}
*/
public function getShards()
{
$sql = 'SELECT member_id as id,
distribution_name as distribution_key,
CAST(range_low AS CHAR) AS rangeLow,
CAST(range_high AS CHAR) AS rangeHigh
FROM sys.federation_member_distributions d
INNER JOIN sys.federations f ON f.federation_id = d.federation_id
WHERE f.name = ' . $this->conn->quote($this->federationName);
return $this->conn->fetchAllAssociative($sql);
}
/**
* {@inheritDoc}
*/
public function queryAll($sql, array $params = [], array $types = [])
{
$shards = $this->getShards();
if (! $shards) {
throw new RuntimeException('No shards found for ' . $this->federationName);
}
$result = [];
$oldDistribution = $this->getCurrentDistributionValue();
foreach ($shards as $shard) {
$this->selectShard($shard['rangeLow']);
foreach ($this->conn->fetchAllAssociative($sql, $params, $types) as $row) {
$result[] = $row;
}
}
if ($oldDistribution === null) {
$this->selectGlobal();
} else {
$this->selectShard($oldDistribution);
}
return $result;
}
/**
* Splits Federation at a given distribution value.
*
* @param mixed $splitDistributionValue
*
* @return void
*/
public function splitFederation($splitDistributionValue)
{
$type = Type::getType($this->distributionType);
$sql = 'ALTER FEDERATION ' . $this->getFederationName() . ' ' .
'SPLIT AT (' . $this->getDistributionKey() . ' = ' .
$this->conn->quote($splitDistributionValue, $type->getBindingType()) . ')';
$this->conn->exec($sql);
}
}

View File

@@ -0,0 +1,152 @@
<?php
namespace Doctrine\DBAL\Sharding\SQLAzure\Schema;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Visitor\Visitor;
use RuntimeException;
use function in_array;
/**
* Converts a single tenant schema into a multi-tenant schema for SQL Azure
* Federations under the following assumptions:
*
* - Every table is part of the multi-tenant application, only explicitly
* excluded tables are non-federated. The behavior of the tables being in
* global or federated database is undefined. It depends on you selecting a
* federation before DDL statements or not.
* - Every Primary key of a federated table is extended by another column
* 'tenant_id' with a default value of the SQLAzure function
* `federation_filtering_value('tenant_id')`.
* - You always have to work with `filtering=On` when using federations with this
* multi-tenant approach.
* - Primary keys are either using globally unique ids (GUID, Table Generator)
* or you explicitly add the tenant_id in every UPDATE or DELETE statement
* (otherwise they will affect the same-id rows from other tenants as well).
* SQLAzure throws errors when you try to create IDENTIY columns on federated
* tables.
*
* @deprecated
*/
class MultiTenantVisitor implements Visitor
{
/** @var string[] */
private $excludedTables = [];
/** @var string */
private $tenantColumnName;
/** @var string */
private $tenantColumnType = 'integer';
/**
* Name of the federation distribution, defaulting to the tenantColumnName
* if not specified.
*
* @var string
*/
private $distributionName;
/**
* @param string[] $excludedTables
* @param string $tenantColumnName
* @param string|null $distributionName
*/
public function __construct(array $excludedTables = [], $tenantColumnName = 'tenant_id', $distributionName = null)
{
$this->excludedTables = $excludedTables;
$this->tenantColumnName = $tenantColumnName;
$this->distributionName = $distributionName ?: $tenantColumnName;
}
/**
* {@inheritdoc}
*/
public function acceptTable(Table $table)
{
if (in_array($table->getName(), $this->excludedTables)) {
return;
}
$table->addColumn($this->tenantColumnName, $this->tenantColumnType, [
'default' => "federation_filtering_value('" . $this->distributionName . "')",
]);
$clusteredIndex = $this->getClusteredIndex($table);
$indexColumns = $clusteredIndex->getColumns();
$indexColumns[] = $this->tenantColumnName;
if ($clusteredIndex->isPrimary()) {
$table->dropPrimaryKey();
$table->setPrimaryKey($indexColumns);
} else {
$table->dropIndex($clusteredIndex->getName());
$table->addIndex($indexColumns, $clusteredIndex->getName());
$table->getIndex($clusteredIndex->getName())->addFlag('clustered');
}
}
/**
* @param Table $table
*
* @return Index
*
* @throws RuntimeException
*/
private function getClusteredIndex($table)
{
foreach ($table->getIndexes() as $index) {
if ($index->isPrimary() && ! $index->hasFlag('nonclustered')) {
return $index;
}
if ($index->hasFlag('clustered')) {
return $index;
}
}
throw new RuntimeException('No clustered index found on table ' . $table->getName());
}
/**
* {@inheritdoc}
*/
public function acceptSchema(Schema $schema)
{
}
/**
* {@inheritdoc}
*/
public function acceptColumn(Table $table, Column $column)
{
}
/**
* {@inheritdoc}
*/
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
{
}
/**
* {@inheritdoc}
*/
public function acceptIndex(Table $table, Index $index)
{
}
/**
* {@inheritdoc}
*/
public function acceptSequence(Sequence $sequence)
{
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Doctrine\DBAL\Sharding\ShardChoser;
use Doctrine\DBAL\Sharding\PoolingShardConnection;
/**
* The MultiTenant Shard choser assumes that the distribution value directly
* maps to the shard id.
*
* @deprecated
*/
class MultiTenantShardChoser implements ShardChoser
{
/**
* {@inheritdoc}
*/
public function pickShard($distributionValue, PoolingShardConnection $conn)
{
return $distributionValue;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Doctrine\DBAL\Sharding\ShardChoser;
use Doctrine\DBAL\Sharding\PoolingShardConnection;
/**
* Given a distribution value this shard-choser strategy will pick the shard to
* connect to for retrieving rows with the distribution value.
*
* @deprecated
*/
interface ShardChoser
{
/**
* Picks a shard for the given distribution value.
*
* @param string|int $distributionValue
*
* @return string|int
*/
public function pickShard($distributionValue, PoolingShardConnection $conn);
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Doctrine\DBAL\Sharding;
/**
* Sharding Manager gives access to APIs to implementing sharding on top of
* Doctrine\DBAL\Connection instances.
*
* For simplicity and developer ease-of-use (and understanding) the sharding
* API only covers single shard queries, no fan-out support. It is primarily
* suited for multi-tenant applications.
*
* The assumption about sharding here
* is that a distribution value can be found that gives access to all the
* necessary data for all use-cases. Switching between shards should be done with
* caution, especially if lazy loading is implemented. Any query is always
* executed against the last shard that was selected. If a query is created for
* a shard Y but then a shard X is selected when its actually executed you
* will hit the wrong shard.
*
* @deprecated
*/
interface ShardManager
{
/**
* Selects global database with global data.
*
* This is the default database that is connected when no shard is
* selected.
*
* @return void
*/
public function selectGlobal();
/**
* Selects the shard against which the queries after this statement will be issued.
*
* @param string $distributionValue
*
* @return void
*
* @throws ShardingException If no value is passed as shard identifier.
*/
public function selectShard($distributionValue);
/**
* Gets the distribution value currently used for sharding.
*
* @return string|null
*/
public function getCurrentDistributionValue();
/**
* Gets information about the amount of shards and other details.
*
* Format is implementation specific, each shard is one element and has an
* 'id' attribute at least.
*
* @return mixed[][]
*/
public function getShards();
/**
* Queries all shards in undefined order and return the results appended to
* each other. Restore the previous distribution value after execution.
*
* Using {@link 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);
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Doctrine\DBAL\Sharding;
use Doctrine\DBAL\Exception;
/**
* Sharding related Exceptions
*
* @deprecated
*
* @psalm-immutable
*/
class ShardingException extends Exception
{
/**
* @return ShardingException
*/
public static function notImplemented()
{
return new self('This functionality is not implemented with this sharding provider.', 1331557937);
}
/**
* @return ShardingException
*/
public static function missingDefaultFederationName()
{
return new self('SQLAzure requires a federation name to be set during sharding configuration.', 1332141280);
}
/**
* @return ShardingException
*/
public static function missingDefaultDistributionKey()
{
return new self('SQLAzure requires a distribution key to be set during sharding configuration.', 1332141329);
}
/**
* @return ShardingException
*/
public static function activeTransaction()
{
return new self('Cannot switch shard during an active transaction.', 1332141766);
}
/**
* @return ShardingException
*/
public static function noShardDistributionValue()
{
return new self('You have to specify a string or integer as shard distribution value.', 1332142103);
}
/**
* @return ShardingException
*/
public static function missingDistributionType()
{
return new self("You have to specify a sharding distribution type such as 'integer', 'string', 'guid'.");
}
}