Completed
Pull Request — develop (#3576)
by Jonathan
64:38 queued 61:53
created

getCurrentDistributionValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Sharding\SQLAzure;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Sharding\Exception\ActiveTransaction;
9
use Doctrine\DBAL\Sharding\Exception\MissingDefaultDistributionKey;
10
use Doctrine\DBAL\Sharding\Exception\MissingDefaultFederationName;
11
use Doctrine\DBAL\Sharding\Exception\MissingDistributionType;
12
use Doctrine\DBAL\Sharding\ShardingException;
13
use Doctrine\DBAL\Sharding\ShardManager;
14
use Doctrine\DBAL\Types\Type;
15
use RuntimeException;
16
use function sprintf;
17
18
/**
19
 * Sharding using the SQL Azure Federations support.
20
 */
21
class SQLAzureShardManager implements ShardManager
22
{
23
    /** @var string */
24
    private $federationName;
25
26
    /** @var bool */
27
    private $filteringEnabled;
28
29
    /** @var string */
30
    private $distributionKey;
31
32
    /** @var string */
33
    private $distributionType;
34
35
    /** @var Connection */
36
    private $conn;
37
38
    /** @var mixed */
39
    private $currentDistributionValue;
40
41
    /**
42
     * @throws ShardingException
43
     */
44 163
    public function __construct(Connection $conn)
45
    {
46 163
        $this->conn = $conn;
47 163
        $params     = $conn->getParams();
48
49 163
        if (! isset($params['sharding']['federationName'])) {
50 157
            throw MissingDefaultFederationName::new();
51
        }
52
53 136
        if (! isset($params['sharding']['distributionKey'])) {
54 1
            throw MissingDefaultDistributionKey::new();
55
        }
56
57 135
        if (! isset($params['sharding']['distributionType'])) {
58 131
            throw MissingDistributionType::new();
59
        }
60
61 108
        $this->federationName   = $params['sharding']['federationName'];
62 108
        $this->distributionKey  = $params['sharding']['distributionKey'];
63 108
        $this->distributionType = $params['sharding']['distributionType'];
64 108
        $this->filteringEnabled = (bool) ($params['sharding']['filteringEnabled'] ?? false);
65 108
    }
66
67
    /**
68
     * Gets the name of the federation.
69
     */
70
    public function getFederationName() : string
71
    {
72
        return $this->federationName;
73
    }
74
75
    /**
76
     * Gets the distribution key.
77
     */
78
    public function getDistributionKey() : string
79
    {
80
        return $this->distributionKey;
81
    }
82
83
    /**
84
     * Gets the Doctrine Type name used for the distribution.
85
     */
86
    public function getDistributionType() : string
87
    {
88
        return $this->distributionType;
89
    }
90
91
    /**
92
     * Sets Enabled/Disable filtering on the fly.
93
     */
94
    public function setFilteringEnabled(bool $flag) : void
95
    {
96
        $this->filteringEnabled = $flag;
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102 80
    public function selectGlobal() : void
103
    {
104 80
        if ($this->conn->isTransactionActive()) {
105 79
            throw ActiveTransaction::new();
106
        }
107
108 53
        $sql = 'USE FEDERATION ROOT WITH RESET';
109 53
        $this->conn->exec($sql);
110 53
        $this->currentDistributionValue = null;
111 53
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116 27
    public function selectShard($distributionValue) : void
117
    {
118 27
        if ($this->conn->isTransactionActive()) {
119 27
            throw ActiveTransaction::new();
120
        }
121
122
        $platform = $this->conn->getDatabasePlatform();
123
        $sql      = sprintf(
124
            'USE FEDERATION %s (%s = %s) WITH RESET, FILTERING = %s;',
125
            $platform->quoteIdentifier($this->federationName),
126
            $platform->quoteIdentifier($this->distributionKey),
127
            $this->conn->quote($distributionValue),
128
            ($this->filteringEnabled ? 'ON' : 'OFF')
129
        );
130
131
        $this->conn->exec($sql);
132
        $this->currentDistributionValue = $distributionValue;
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138 105
    public function getCurrentDistributionValue()
139
    {
140 105
        return $this->currentDistributionValue;
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function getShards() : array
147
    {
148
        $sql = 'SELECT member_id as id,
149
                      distribution_name as distribution_key,
150
                      CAST(range_low AS CHAR) AS rangeLow,
151
                      CAST(range_high AS CHAR) AS rangeHigh
152
                      FROM sys.federation_member_distributions d
153
                      INNER JOIN sys.federations f ON f.federation_id = d.federation_id
154
                      WHERE f.name = ' . $this->conn->quote($this->federationName);
155
156
        return $this->conn->fetchAll($sql);
157
    }
158
159
     /**
160
      * {@inheritDoc}
161
      */
162
    public function queryAll(string $sql, array $params = [], array $types = []) : array
163
    {
164
        $shards = $this->getShards();
165
        if (! $shards) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $shards of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
166
            throw new RuntimeException(sprintf('No shards found for "%s".', $this->federationName));
167
        }
168
169
        $result          = [];
170
        $oldDistribution = $this->getCurrentDistributionValue();
171
172
        foreach ($shards as $shard) {
173
            $this->selectShard($shard['rangeLow']);
174
            foreach ($this->conn->fetchAll($sql, $params, $types) as $row) {
175
                $result[] = $row;
176
            }
177
        }
178
179
        if ($oldDistribution === null) {
180
            $this->selectGlobal();
181
        } else {
182
            $this->selectShard($oldDistribution);
183
        }
184
185
        return $result;
186
    }
187
188
    /**
189
     * Splits Federation at a given distribution value.
190
     *
191
     * @param mixed $splitDistributionValue
192
     */
193
    public function splitFederation($splitDistributionValue) : void
194
    {
195
        $type = Type::getType($this->distributionType);
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
196
197
        $sql = 'ALTER FEDERATION ' . $this->getFederationName() . ' ' .
198
               'SPLIT AT (' . $this->getDistributionKey() . ' = ' .
199
               $this->conn->quote($splitDistributionValue) . ')';
200
        $this->conn->exec($sql);
201
    }
202
}
203