3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Sharding;
22 use Doctrine\DBAL\Sharding\ShardChoser\ShardChoser;
25 * Shard Manager for the Connection Pooling Shard Strategy
27 * @author Benjamin Eberlei <kontakt@beberlei.de>
29 class PoolingShardManager implements ShardManager
33 private $currentDistributionValue;
35 public function __construct(PoolingShardConnection $conn)
37 $params = $conn->getParams();
39 $this->choser = $params['shardChoser'];
42 public function selectGlobal()
44 $this->conn->connect(0);
45 $this->currentDistributionValue = null;
48 public function selectShard($distributionValue)
50 $shardId = $this->choser->pickShard($distributionValue, $this->conn);
51 $this->conn->connect($shardId);
52 $this->currentDistributionValue = $distributionValue;
55 public function getCurrentDistributionValue()
57 return $this->currentDistributionValue;
60 public function getShards()
62 $params = $this->conn->getParams();
65 foreach ($params['shards'] as $shard) {
66 $shards[] = array('id' => $shard['id']);
72 public function queryAll($sql, array $params, array $types)
74 $shards = $this->getShards();
76 throw new \RuntimeException("No shards found.");
80 $oldDistribution = $this->getCurrentDistributionValue();
82 foreach ($shards as $shard) {
83 $this->selectShard($shard['id']);
84 foreach ($this->conn->fetchAll($sql, $params, $types) as $row) {
89 if ($oldDistribution === null) {
90 $this->selectGlobal();
92 $this->selectShard($oldDistribution);