SQLAzureShardManager.php 5.42 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\DBAL\Sharding\SQLAzure;

use Doctrine\DBAL\Connection;
6 7
use Doctrine\DBAL\Sharding\ShardingException;
use Doctrine\DBAL\Sharding\ShardManager;
8
use Doctrine\DBAL\Types\Type;
9
use RuntimeException;
10

11
use function sprintf;
12 13 14

/**
 * Sharding using the SQL Azure Federations support.
15 16
 *
 * @deprecated
17 18 19
 */
class SQLAzureShardManager implements ShardManager
{
20
    /** @var string */
21 22
    private $federationName;

23
    /** @var bool */
24 25
    private $filteringEnabled;

26
    /** @var string */
27 28
    private $distributionKey;

29
    /** @var string */
30 31
    private $distributionType;

32
    /** @var Connection */
33 34
    private $conn;

35
    /** @var string|null */
36 37 38
    private $currentDistributionValue;

    /**
39
     * @throws ShardingException
40 41 42 43
     */
    public function __construct(Connection $conn)
    {
        $this->conn = $conn;
44
        $params     = $conn->getParams();
45

46
        if (! isset($params['sharding']['federationName'])) {
47 48 49
            throw ShardingException::missingDefaultFederationName();
        }

50
        if (! isset($params['sharding']['distributionKey'])) {
51 52 53
            throw ShardingException::missingDefaultDistributionKey();
        }

54
        if (! isset($params['sharding']['distributionType'])) {
55 56 57
            throw ShardingException::missingDistributionType();
        }

58 59
        $this->federationName   = $params['sharding']['federationName'];
        $this->distributionKey  = $params['sharding']['distributionKey'];
60
        $this->distributionType = $params['sharding']['distributionType'];
61
        $this->filteringEnabled = (bool) ($params['sharding']['filteringEnabled'] ?? false);
62 63 64
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
65
     * Gets the name of the federation.
66 67 68 69 70 71 72 73 74
     *
     * @return string
     */
    public function getFederationName()
    {
        return $this->federationName;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
75
     * Gets the distribution key.
76 77 78 79 80 81 82 83 84
     *
     * @return string
     */
    public function getDistributionKey()
    {
        return $this->distributionKey;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
85
     * Gets the Doctrine Type name used for the distribution.
86 87 88 89 90 91 92 93 94
     *
     * @return string
     */
    public function getDistributionType()
    {
        return $this->distributionType;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
95 96
     * Sets Enabled/Disable filtering on the fly.
     *
97
     * @param bool $flag
98 99 100 101 102
     *
     * @return void
     */
    public function setFilteringEnabled($flag)
    {
103
        $this->filteringEnabled = (bool) $flag;
104 105 106 107 108 109 110 111 112 113 114
    }

    /**
     * {@inheritDoc}
     */
    public function selectGlobal()
    {
        if ($this->conn->isTransactionActive()) {
            throw ShardingException::activeTransaction();
        }

115
        $sql = 'USE FEDERATION ROOT WITH RESET';
116 117 118 119 120 121 122 123 124 125 126 127 128 129
        $this->conn->exec($sql);
        $this->currentDistributionValue = null;
    }

    /**
     * {@inheritDoc}
     */
    public function selectShard($distributionValue)
    {
        if ($this->conn->isTransactionActive()) {
            throw ShardingException::activeTransaction();
        }

        $platform = $this->conn->getDatabasePlatform();
130 131
        $sql      = sprintf(
            'USE FEDERATION %s (%s = %s) WITH RESET, FILTERING = %s;',
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            $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()
    {
155
        $sql = 'SELECT member_id as id,
156 157 158 159 160
                      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
161
                      WHERE f.name = ' . $this->conn->quote($this->federationName);
162

163
        return $this->conn->fetchAllAssociative($sql);
164 165 166 167 168
    }

     /**
      * {@inheritDoc}
      */
169
    public function queryAll($sql, array $params = [], array $types = [])
170 171
    {
        $shards = $this->getShards();
172 173
        if (! $shards) {
            throw new RuntimeException('No shards found for ' . $this->federationName);
174 175
        }

176
        $result          = [];
177 178 179 180
        $oldDistribution = $this->getCurrentDistributionValue();

        foreach ($shards as $shard) {
            $this->selectShard($shard['rangeLow']);
181
            foreach ($this->conn->fetchAllAssociative($sql, $params, $types) as $row) {
182 183 184 185 186 187 188 189 190 191 192 193 194 195
                $result[] = $row;
            }
        }

        if ($oldDistribution === null) {
            $this->selectGlobal();
        } else {
            $this->selectShard($oldDistribution);
        }

        return $result;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
196
     * Splits Federation at a given distribution value.
197 198
     *
     * @param mixed $splitDistributionValue
Benjamin Morel's avatar
Benjamin Morel committed
199 200
     *
     * @return void
201 202 203 204 205
     */
    public function splitFederation($splitDistributionValue)
    {
        $type = Type::getType($this->distributionType);

206 207 208
        $sql = 'ALTER FEDERATION ' . $this->getFederationName() . ' ' .
               'SPLIT AT (' . $this->getDistributionKey() . ' = ' .
               $this->conn->quote($splitDistributionValue, $type->getBindingType()) . ')';
209 210 211
        $this->conn->exec($sql);
    }
}