Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Sharding / PoolingShardManager.php
1 <?php
2 /*
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.
14  *
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>.
18  */
19
20 namespace Doctrine\DBAL\Sharding;
21
22 use Doctrine\DBAL\Sharding\ShardChoser\ShardChoser;
23
24 /**
25  * Shard Manager for the Connection Pooling Shard Strategy
26  *
27  * @author Benjamin Eberlei <kontakt@beberlei.de>
28  */
29 class PoolingShardManager implements ShardManager
30 {
31     private $conn;
32     private $choser;
33     private $currentDistributionValue;
34
35     public function __construct(PoolingShardConnection $conn)
36     {
37         $params       = $conn->getParams();
38         $this->conn   = $conn;
39         $this->choser = $params['shardChoser'];
40     }
41
42     public function selectGlobal()
43     {
44         $this->conn->connect(0);
45         $this->currentDistributionValue = null;
46     }
47
48     public function selectShard($distributionValue)
49     {
50         $shardId = $this->choser->pickShard($distributionValue, $this->conn);
51         $this->conn->connect($shardId);
52         $this->currentDistributionValue = $distributionValue;
53     }
54
55     public function getCurrentDistributionValue()
56     {
57         return $this->currentDistributionValue;
58     }
59
60     public function getShards()
61     {
62         $params = $this->conn->getParams();
63         $shards = array();
64
65         foreach ($params['shards'] as $shard) {
66             $shards[] = array('id' => $shard['id']);
67         }
68
69         return $shards;
70     }
71
72     public function queryAll($sql, array $params, array $types)
73     {
74         $shards = $this->getShards();
75         if (!$shards) {
76             throw new \RuntimeException("No shards found.");
77         }
78
79         $result = array();
80         $oldDistribution = $this->getCurrentDistributionValue();
81
82         foreach ($shards as $shard) {
83             $this->selectShard($shard['id']);
84             foreach ($this->conn->fetchAll($sql, $params, $types) as $row) {
85                 $result[] = $row;
86             }
87         }
88
89         if ($oldDistribution === null) {
90             $this->selectGlobal();
91         } else {
92             $this->selectShard($oldDistribution);
93         }
94
95         return $result;
96     }
97 }
98