1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think; |
14
|
|
|
|
15
|
|
|
use think\db\Connection; |
16
|
|
|
use think\db\Raw; |
17
|
|
|
|
18
|
|
|
class Db |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* 当前数据库连接对象 |
22
|
|
|
* @var Connection |
23
|
|
|
*/ |
24
|
|
|
protected $connection; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* 数据库连接实例 |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $instance = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 配置对象 |
34
|
|
|
* @var Config |
35
|
|
|
*/ |
36
|
|
|
protected $config; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Event对象 |
40
|
|
|
* @var Event |
41
|
|
|
*/ |
42
|
|
|
protected $event; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 数据库配置 |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $option = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 读取主库 |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $readMaster = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 查询次数 |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
protected $queryTimes = 0; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 架构函数 |
64
|
|
|
* @param array $config 连接配置 |
65
|
|
|
* @access public |
66
|
|
|
*/ |
67
|
|
|
public function __construct(array $config = []) |
68
|
|
|
{ |
69
|
|
|
if (empty($config['query'])) { |
70
|
|
|
$config['query'] = '\\think\\db\\Query'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->option = $config; |
74
|
|
|
|
75
|
|
|
$this->connect($config); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function __make(Event $event, Config $config) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$db = new static($config->get('database')); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$db->event = $event; |
83
|
|
|
$db->config = $config; |
84
|
|
|
|
85
|
|
|
return $db; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* 切换数据库连接 |
90
|
|
|
* @access public |
91
|
|
|
* @param mixed $config 连接配置 |
92
|
|
|
* @param bool|string $name 连接标识 true 强制重新连接 |
|
|
|
|
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function connect($config = [], $name = false) |
96
|
|
|
{ |
97
|
|
|
$this->connection = $this->instance($this->parseConfig($config), $name); |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* 取得数据库连接类实例 |
103
|
|
|
* @access public |
104
|
|
|
* @param array $config 连接配置 |
105
|
|
|
* @param bool|string $name 连接标识 true 强制重新连接 |
|
|
|
|
106
|
|
|
* @return Connection |
107
|
|
|
* @throws Exception |
108
|
|
|
*/ |
109
|
|
|
public function instance(array $config = [], $name = false) |
110
|
|
|
{ |
111
|
|
|
if (false === $name) { |
112
|
|
|
$name = md5(serialize($config)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (true === $name || !isset($this->instance[$name])) { |
116
|
|
|
|
117
|
|
|
if (empty($config['type'])) { |
118
|
|
|
throw new InvalidArgumentException('Undefined db type'); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (true === $name) { |
122
|
|
|
$name = md5(serialize($config)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->instance[$name] = App::factory($config['type'], '\\think\\db\\connector\\', $config); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->instance[$name]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* 设置从主库读取数据 |
133
|
|
|
* @access public |
134
|
|
|
* @param string $table 数据表 |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function readMaster(string $table = '*') |
138
|
|
|
{ |
139
|
|
|
$this->readMaster[$table] = true; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* 是否从主库读取数据 |
146
|
|
|
* @access public |
147
|
|
|
* @param string $table 数据表 |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function isReadMaster(string $table): bool |
151
|
|
|
{ |
152
|
|
|
return isset($this->readMaster['*']) || isset($this->readMaster[$table]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* 使用表达式设置数据 |
157
|
|
|
* @access public |
158
|
|
|
* @param string $value 表达式 |
159
|
|
|
* @return Raw |
160
|
|
|
*/ |
161
|
|
|
public function raw(string $value): Raw |
162
|
|
|
{ |
163
|
|
|
return new Raw($value); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* 更新查询次数 |
168
|
|
|
* @access public |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
public function updateQueryTimes(): void |
172
|
|
|
{ |
173
|
|
|
$this->queryTimes++; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* 获得查询次数 |
178
|
|
|
* @access public |
179
|
|
|
* @return integer |
180
|
|
|
*/ |
181
|
|
|
public function getQueryTimes(): int |
182
|
|
|
{ |
183
|
|
|
return $this->queryTimes; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* 数据库连接参数解析 |
188
|
|
|
* @access private |
189
|
|
|
* @param mixed $config |
|
|
|
|
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
private function parseConfig($config): array |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
if (empty($config)) { |
195
|
|
|
$config = $this->option; |
196
|
|
|
} elseif (is_string($config)) { |
197
|
|
|
// 支持读取配置参数 |
198
|
|
|
$config = $this->option[$config] ?? null; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $config; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* 获取数据库的配置参数 |
206
|
|
|
* @access public |
207
|
|
|
* @param string $name 参数名称 |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
|
|
public function getConfig(string $name = '') |
211
|
|
|
{ |
212
|
|
|
return $name ? ($this->option[$name] ?? null) : $this->option; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* 创建一个新的查询对象 |
217
|
|
|
* @access public |
218
|
|
|
* @param string $query 查询对象类名 |
|
|
|
|
219
|
|
|
* @param mixed $connection 连接配置信息 |
|
|
|
|
220
|
|
|
* @return mixed |
221
|
|
|
*/ |
222
|
|
|
public function buildQuery(string $query, $connection) |
223
|
|
|
{ |
224
|
|
|
return $this->connect($connection)->newQuery($query); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* 注册回调方法 |
229
|
|
|
* @access public |
230
|
|
|
* @param string $event 事件名 |
231
|
|
|
* @param callable $callback 回调方法 |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
public function event(string $event, callable $callback): void |
235
|
|
|
{ |
236
|
|
|
$this->event->listen('db.' . $event, $callback); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
|
|
|
|
240
|
|
|
* 创建一个新的查询对象 |
241
|
|
|
* @access public |
242
|
|
|
* @param string $query 查询对象类名 |
|
|
|
|
243
|
|
|
* @param Connection $connection 连接对象 |
|
|
|
|
244
|
|
|
* @return mixed |
245
|
|
|
*/ |
246
|
|
|
protected function newQuery(string $class, $connection = null) |
247
|
|
|
{ |
248
|
|
|
$query = new $class($connection ?: $this->connection); |
249
|
|
|
|
250
|
|
|
$query->setEvent($this->event); |
251
|
|
|
$query->setConfig($this->config); |
252
|
|
|
$query->setDb($this); |
253
|
|
|
|
254
|
|
|
return $query; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function __call($method, $args) |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
$query = $this->newQuery($this->option['query'], $this->connection); |
260
|
|
|
|
261
|
|
|
return call_user_func_array([$query, $method], $args); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|