-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiplyConnection.php
61 lines (47 loc) · 1.34 KB
/
MultiplyConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace pframework\db;
use pframework\db\strategy\StrategyInterface;
class MultiplyConnection implements ConnectionInterface
{
/**
* @var StrategyInterface
*/
private $dbShardStrategy;
/**
* @var StrategyInterface[]
*/
private $tableShardStrategy;
private $dbShardValue;
private $tableShardValue;
private $config;
public function __construct($config)
{
$this->config = $config;
}
public function setDbShardStrategy(StrategyInterface $strategy)
{
$this->dbShardStrategy = $strategy;
}
public function addTableShardStrategy(string $table, StrategyInterface $strategy)
{
$this->tableShardStrategy[$table] = $strategy;
}
public function setDbShardValue($dbShardValue)
{
$this->dbShardValue = $dbShardValue;
}
public function setTableShardValue($tableShardValue)
{
$this->tableShardValue = $tableShardValue;
}
public function table($table)
{
$shardDbIndex = $this->dbShardStrategy->shardByValue($this->dbShardValue);
$shardTable = $this->tableShardStrategy[$table]->shardByValue($this->tableShardValue);
return new QueryBuilder($this->getConnection($shardDbIndex), $shardTable);
}
public function getConnection($shardDbIndex)
{
return new Connection();
}
}