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