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 $option = []; |
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->option = $config; |
72
|
|
|
|
73
|
1 |
|
$this->connect($config); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
|
|
|
|
77
|
|
|
* @param Event $event |
|
|
|
|
78
|
|
|
* @param Config $config |
|
|
|
|
79
|
|
|
* @return Db |
|
|
|
|
80
|
|
|
* @codeCoverageIgnore |
81
|
|
|
*/ |
82
|
|
|
public static function __make(Event $event, Config $config) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$db = new static($config->get('database')); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$db->setEvent($event); |
87
|
|
|
|
88
|
|
|
return $db; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* 切换数据库连接 |
93
|
|
|
* @access public |
94
|
|
|
* @param mixed $config 连接配置 |
|
|
|
|
95
|
|
|
* @param bool|string $name 连接标识 true 强制重新连接 |
|
|
|
|
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
1 |
|
public function connect($config = [], $name = false) |
99
|
|
|
{ |
100
|
1 |
|
$this->connection = $this->instance($this->parseConfig($config), $name); |
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* 取得数据库连接类实例 |
106
|
|
|
* @access public |
107
|
|
|
* @param array $config 连接配置 |
|
|
|
|
108
|
|
|
* @param bool|string $name 连接标识 true 强制重新连接 |
|
|
|
|
109
|
|
|
* @return Connection |
110
|
|
|
*/ |
111
|
1 |
|
public function instance(array $config = [], $name = false) |
112
|
|
|
{ |
113
|
1 |
|
if (false === $name) { |
114
|
1 |
|
$name = md5(serialize($config)); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
if (true === $name || !isset($this->instance[$name])) { |
118
|
|
|
|
119
|
1 |
|
if (empty($config['type'])) { |
120
|
|
|
throw new InvalidArgumentException('Undefined db type'); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
if (true === $name) { |
124
|
|
|
$name = md5(serialize($config)); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$this->instance[$name] = App::factory($config['type'], '\\think\\db\\connector\\', $config); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $this->instance[$name]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* 使用表达式设置数据 |
135
|
|
|
* @access public |
136
|
|
|
* @param string $value 表达式 |
|
|
|
|
137
|
|
|
* @return Raw |
138
|
|
|
*/ |
139
|
|
|
public function raw(string $value): Raw |
140
|
|
|
{ |
141
|
|
|
return new Raw($value); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* 更新查询次数 |
146
|
|
|
* @access public |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
public function updateQueryTimes(): void |
150
|
|
|
{ |
151
|
|
|
$this->queryTimes++; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* 获得查询次数 |
156
|
|
|
* @access public |
157
|
|
|
* @return integer |
158
|
|
|
*/ |
159
|
|
|
public function getQueryTimes(): int |
160
|
|
|
{ |
161
|
|
|
return $this->queryTimes; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* 数据库连接参数解析 |
166
|
|
|
* @access private |
167
|
|
|
* @param mixed $config |
|
|
|
|
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
1 |
|
private function parseConfig($config): array |
|
|
|
|
171
|
|
|
{ |
172
|
1 |
|
if (empty($config)) { |
173
|
|
|
$config = $this->option; |
174
|
1 |
|
} elseif (is_string($config) && isset($this->option[$config])) { |
175
|
|
|
// 支持读取配置参数 |
176
|
|
|
$config = $this->option[$config]; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
if (!is_array($config)) { |
180
|
|
|
throw new DbException('database config error:' . $config); |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
return $config; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* 获取数据库的配置参数 |
188
|
|
|
* @access public |
189
|
|
|
* @param string $name 参数名称 |
|
|
|
|
190
|
|
|
* @return mixed |
191
|
|
|
*/ |
192
|
|
|
public function getConfig(string $name = '') |
193
|
|
|
{ |
194
|
|
|
return $name ? ($this->option[$name] ?? null) : $this->option; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* 创建一个新的查询对象 |
199
|
|
|
* @access public |
200
|
|
|
* @param string|array $connection 连接配置信息 |
|
|
|
|
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
public function buildQuery($connection = []) |
204
|
|
|
{ |
205
|
|
|
return $this->connect($connection)->newQuery(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* 监听SQL执行 |
210
|
|
|
* @access public |
211
|
|
|
* @param callable $callback 回调方法 |
|
|
|
|
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
public function listen(callable $callback): void |
215
|
|
|
{ |
216
|
|
|
$this->listen[] = $callback; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* 获取监听SQL执行 |
221
|
|
|
* @access public |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
public function getListen(): array |
225
|
|
|
{ |
226
|
|
|
return $this->listen; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* 设置Event对象 |
231
|
|
|
* @param Event $event |
|
|
|
|
232
|
|
|
*/ |
|
|
|
|
233
|
|
|
public function setEvent(Event $event) |
234
|
|
|
{ |
235
|
|
|
$this->event = $event; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* 注册回调方法 |
240
|
|
|
* @access public |
241
|
|
|
* @param string $event 事件名 |
|
|
|
|
242
|
|
|
* @param callable $callback 回调方法 |
|
|
|
|
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
public function event(string $event, callable $callback): void |
246
|
|
|
{ |
247
|
|
|
if ($this->event) { |
248
|
|
|
$this->event->listen('db.' . $event, $callback); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* 触发事件 |
254
|
|
|
* @access public |
255
|
|
|
* @param string $event 事件名 |
|
|
|
|
256
|
|
|
* @param mixed $params 传入参数 |
|
|
|
|
257
|
|
|
* @param bool $once |
|
|
|
|
258
|
|
|
* @return mixed |
259
|
|
|
*/ |
260
|
|
|
public function trigger(string $event, $params = null, bool $once = false) |
261
|
|
|
{ |
262
|
|
|
if ($this->event) { |
263
|
|
|
return $this->event->trigger('db.' . $event, $params, $once); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* 创建一个新的查询对象 |
269
|
|
|
* @access protected |
270
|
|
|
* @param Connection $connection 连接对象 |
|
|
|
|
271
|
|
|
* @return mixed |
272
|
|
|
*/ |
273
|
|
|
protected function newQuery($connection = null) |
274
|
|
|
{ |
275
|
|
|
/** @var Query $query */ |
|
|
|
|
276
|
|
|
$connection = $connection ?: $this->connection; |
277
|
|
|
$class = $connection->getQueryClass(); |
278
|
|
|
$query = new $class($connection); |
279
|
|
|
|
280
|
|
|
$query->setDb($this); |
281
|
|
|
|
282
|
|
|
return $query; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function __call($method, $args) |
|
|
|
|
286
|
|
|
{ |
287
|
|
|
$query = $this->newQuery($this->connection); |
288
|
|
|
|
289
|
|
|
return call_user_func_array([$query, $method], $args); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|