1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\orm; |
4
|
|
|
|
5
|
|
|
use function in_array; |
6
|
|
|
use suda\orm\connection\Connection; |
7
|
|
|
use suda\orm\exception\SQLException; |
8
|
|
|
use suda\orm\connection\MySQLConnection; |
9
|
|
|
use suda\orm\connection\SQLiteConnection; |
10
|
|
|
|
11
|
|
|
class DataSource |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* 读连接 |
15
|
|
|
* |
16
|
|
|
* @var Connection[] |
17
|
|
|
*/ |
18
|
|
|
protected $write = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* 写连接 |
22
|
|
|
* |
23
|
|
|
* @var Connection[] |
24
|
|
|
*/ |
25
|
|
|
protected $read = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 当前写数据库 |
29
|
|
|
* |
30
|
|
|
* @var Connection|null |
31
|
|
|
*/ |
32
|
|
|
protected $master; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* 当前读数据库 |
36
|
|
|
* |
37
|
|
|
* @var Connection|null |
38
|
|
|
*/ |
39
|
|
|
protected $slave; |
40
|
|
|
|
41
|
|
|
protected static $type = [ |
42
|
|
|
'mysql' => MySQLConnection::class, |
43
|
|
|
'sqlite' => SQLiteConnection::class, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* DataSource constructor. |
49
|
|
|
* @param Connection[]|Connection $connections |
50
|
|
|
*/ |
51
|
|
|
public function __construct($connections) |
52
|
|
|
{ |
53
|
|
|
if (is_array($connections)) { |
54
|
|
|
foreach ($connections as $connection) { |
55
|
|
|
$this->add($connection); |
56
|
|
|
} |
57
|
|
|
} else { |
58
|
|
|
$this->add($connections); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 添加连接 |
64
|
|
|
* |
65
|
|
|
* @param Connection $connection |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function add(Connection $connection) |
69
|
|
|
{ |
70
|
|
|
$this->addRead($connection); |
71
|
|
|
$this->addWrite($connection); |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* 添加读连接 |
77
|
|
|
* |
78
|
|
|
* @param Connection $connection |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function addRead(Connection $connection) |
82
|
|
|
{ |
83
|
|
|
if (!in_array($connection, $this->read)) { |
84
|
|
|
$this->read[] = $connection; |
85
|
|
|
} |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* 添加写连接 |
92
|
|
|
* |
93
|
|
|
* @param Connection $connection |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function addWrite(Connection $connection) |
97
|
|
|
{ |
98
|
|
|
if (!in_array($connection, $this->write)) { |
99
|
|
|
$this->write[] = $connection; |
100
|
|
|
} |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* 获取写连接 |
106
|
|
|
* |
107
|
|
|
* @return Connection |
108
|
|
|
*/ |
109
|
|
|
public function read(): Connection |
110
|
|
|
{ |
111
|
|
|
$this->selectReadConnection(); |
112
|
|
|
return $this->slave; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* 获取读连接 |
117
|
|
|
* |
118
|
|
|
* @return Connection |
119
|
|
|
*/ |
120
|
|
|
public function write(): Connection |
121
|
|
|
{ |
122
|
|
|
$this->selectWriteConnection(); |
123
|
|
|
return $this->master; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* 创建连接 |
128
|
|
|
* |
129
|
|
|
* @param string $type |
130
|
|
|
* @param array $config |
131
|
|
|
* @param string|null $name |
132
|
|
|
* @return Connection |
133
|
|
|
* @throws SQLException |
134
|
|
|
*/ |
135
|
|
|
public static function new(string $type, array $config, ?string $name = null): Connection |
136
|
|
|
{ |
137
|
|
|
if (array_key_exists($type, static::$type)) { |
138
|
|
|
return new static::$type[$type]($config, $name); |
139
|
|
|
} else { |
140
|
|
|
throw new SQLException(sprintf('no connection type of %s', $type)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* 读数据库选择 |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
protected function selectReadConnection() |
150
|
|
|
{ |
151
|
|
|
$postion = mt_rand(0, count($this->read) - 1); |
152
|
|
|
$this->slave = $this->read[$postion]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* 写数据库选择 |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
protected function selectWriteConnection() |
161
|
|
|
{ |
162
|
|
|
if ($this->master === null) { |
163
|
|
|
$position = mt_rand(0, count($this->write) - 1); |
164
|
|
|
$this->master = $this->write[$position]; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|