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 InvalidArgumentException; |
16
|
|
|
use think\db\Connection; |
17
|
|
|
use think\db\Query; |
18
|
|
|
use think\db\Raw; |
19
|
|
|
use think\exception\DbException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Db |
23
|
|
|
* @package think |
|
|
|
|
24
|
|
|
* @mixin Query |
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
class Db |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* 当前数据库连接对象 |
30
|
|
|
* @var Connection |
31
|
|
|
*/ |
32
|
|
|
protected $connection; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* 数据库连接实例 |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $instance = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Event对象 |
42
|
|
|
* @var Event |
43
|
|
|
*/ |
44
|
|
|
protected $event; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 数据库配置 |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $config = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* SQL监听 |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $listen = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 查询次数 |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
protected $queryTimes = 0; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* 架构函数 |
66
|
|
|
* @param array $config 连接配置 |
|
|
|
|
67
|
|
|
* @access public |
68
|
|
|
*/ |
69
|
1 |
|
public function __construct(array $config = []) |
70
|
|
|
{ |
71
|
1 |
|
$this->config = $config; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
|
|
|
|
75
|
|
|
* @param Event $event |
|
|
|
|
76
|
|
|
* @param Config $config |
|
|
|
|
77
|
|
|
* @return Db |
|
|
|
|
78
|
|
|
* @codeCoverageIgnore |
79
|
|
|
*/ |
80
|
|
|
public static function __make(Event $event, Config $config) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$db = new static($config->get('database')); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$db->setEvent($event); |
85
|
|
|
|
86
|
|
|
return $db; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 切换数据库连接 |
91
|
|
|
* @access public |
92
|
|
|
* @param mixed $config 连接配置 |
|
|
|
|
93
|
|
|
* @param bool|string $name 连接标识 true 强制重新连接 |
|
|
|
|
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function connect($config = [], $name = false) |
97
|
|
|
{ |
98
|
|
|
$this->connection = $this->instance($this->parseConfig($config), $name); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 取得数据库连接类实例 |
104
|
|
|
* @access public |
105
|
|
|
* @param array $config 连接配置 |
|
|
|
|
106
|
|
|
* @param bool|string $name 连接标识 true 强制重新连接 |
|
|
|
|
107
|
|
|
* @return Connection |
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 $value 表达式 |
|
|
|
|
135
|
|
|
* @return Raw |
136
|
|
|
*/ |
137
|
|
|
public function raw(string $value): Raw |
138
|
|
|
{ |
139
|
|
|
return new Raw($value); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 更新查询次数 |
144
|
|
|
* @access public |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function updateQueryTimes(): void |
148
|
|
|
{ |
149
|
|
|
$this->queryTimes++; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* 重置查询次数 |
154
|
|
|
* @access public |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function clearQueryTimes(): void |
158
|
|
|
{ |
159
|
|
|
$this->queryTimes = 0; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* 获得查询次数 |
164
|
|
|
* @access public |
165
|
|
|
* @return integer |
166
|
|
|
*/ |
167
|
|
|
public function getQueryTimes(): int |
168
|
|
|
{ |
169
|
|
|
return $this->queryTimes; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* 数据库连接参数解析 |
174
|
|
|
* @access private |
175
|
|
|
* @param mixed $config |
|
|
|
|
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
private function parseConfig($config): array |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
if (empty($config)) { |
181
|
|
|
$config = $this->config; |
182
|
|
|
} elseif (is_string($config) && isset($this->config[$config])) { |
183
|
|
|
// 支持读取配置参数 |
184
|
|
|
$config = $this->config[$config]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!is_array($config)) { |
188
|
|
|
throw new DbException('database config error:' . $config); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $config; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* 获取数据库的配置参数 |
196
|
|
|
* @access public |
197
|
|
|
* @param string $name 参数名称 |
|
|
|
|
198
|
|
|
* @return mixed |
199
|
|
|
*/ |
200
|
|
|
public function getConfig(string $name = '') |
201
|
|
|
{ |
202
|
|
|
return $name ? ($this->config[$name] ?? null) : $this->config; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* 创建一个新的查询对象 |
207
|
|
|
* @access public |
208
|
|
|
* @param string|array $connection 连接配置信息 |
|
|
|
|
209
|
|
|
* @return mixed |
210
|
|
|
*/ |
211
|
|
|
public function buildQuery($connection = []) |
212
|
|
|
{ |
213
|
|
|
return $this->connect($connection)->newQuery(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* 监听SQL执行 |
218
|
|
|
* @access public |
219
|
|
|
* @param callable $callback 回调方法 |
|
|
|
|
220
|
|
|
* @return void |
221
|
|
|
*/ |
222
|
|
|
public function listen(callable $callback): void |
223
|
|
|
{ |
224
|
|
|
$this->listen[] = $callback; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* 获取监听SQL执行 |
229
|
|
|
* @access public |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
public function getListen(): array |
233
|
|
|
{ |
234
|
|
|
return $this->listen; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* 设置Event对象 |
239
|
|
|
* @param Event $event |
|
|
|
|
240
|
|
|
*/ |
|
|
|
|
241
|
|
|
public function setEvent(Event $event) |
242
|
|
|
{ |
243
|
|
|
$this->event = $event; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* 注册回调方法 |
248
|
|
|
* @access public |
249
|
|
|
* @param string $event 事件名 |
|
|
|
|
250
|
|
|
* @param callable $callback 回调方法 |
|
|
|
|
251
|
|
|
* @return void |
252
|
|
|
*/ |
253
|
|
|
public function event(string $event, callable $callback): void |
254
|
|
|
{ |
255
|
|
|
if ($this->event) { |
256
|
|
|
$this->event->listen('db.' . $event, $callback); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* 触发事件 |
262
|
|
|
* @access public |
263
|
|
|
* @param string $event 事件名 |
|
|
|
|
264
|
|
|
* @param mixed $params 传入参数 |
|
|
|
|
265
|
|
|
* @param bool $once |
|
|
|
|
266
|
|
|
* @return mixed |
267
|
|
|
*/ |
268
|
|
|
public function trigger(string $event, $params = null, bool $once = false) |
269
|
|
|
{ |
270
|
|
|
if ($this->event) { |
271
|
|
|
return $this->event->trigger('db.' . $event, $params, $once); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* 创建一个新的查询对象 |
277
|
|
|
* @access protected |
278
|
|
|
* @param Connection $connection 连接对象 |
|
|
|
|
279
|
|
|
* @return mixed |
280
|
|
|
*/ |
281
|
|
|
protected function newQuery($connection = null) |
282
|
|
|
{ |
283
|
|
|
/** @var Query $query */ |
|
|
|
|
284
|
|
|
if (is_null($connection) && !$this->connection) { |
285
|
|
|
$this->connect($this->config); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$connection = $connection ?: $this->connection; |
289
|
|
|
$class = $connection->getQueryClass(); |
290
|
|
|
$query = new $class($connection); |
291
|
|
|
|
292
|
|
|
$query->setDb($this); |
293
|
|
|
|
294
|
|
|
return $query; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function __call($method, $args) |
|
|
|
|
298
|
|
|
{ |
299
|
|
|
$query = $this->newQuery($this->connection); |
300
|
|
|
|
301
|
|
|
return call_user_func_array([$query, $method], $args); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|