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 Config |
29
|
|
|
*/ |
30
|
|
|
protected $config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Event对象 |
34
|
|
|
* @var Event |
35
|
|
|
*/ |
36
|
|
|
protected $event; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 数据库配置 |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $option = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 读取主库 |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $readMaster = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 查询次数 |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
protected $queryTimes = 0; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 架构函数 |
58
|
|
|
* @param array $config 连接配置 |
59
|
|
|
* @access public |
60
|
|
|
*/ |
61
|
|
|
public function __construct(array $config = []) |
62
|
|
|
{ |
63
|
|
|
if (empty($config['query'])) { |
64
|
|
|
$config['query'] = '\\think\\db\\Query'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->option = $config; |
68
|
|
|
|
69
|
|
|
$this->connection = $this->connect($config); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function __make(Event $event, Config $config) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$db = new static($config->get('database')); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$db->event = $event; |
77
|
|
|
$db->config = $config; |
78
|
|
|
|
79
|
|
|
return $db; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* 切换数据库连接 |
84
|
|
|
* @access public |
85
|
|
|
* @param mixed $config 连接配置 |
86
|
|
|
* @param bool|string $name 连接标识 true 强制重新连接 |
|
|
|
|
87
|
|
|
* @return object |
88
|
|
|
* @throws Exception |
89
|
|
|
*/ |
90
|
|
|
public function connect($config = [], $name = false) |
91
|
|
|
{ |
92
|
|
|
return Connection::instance($this->parseConfig($config), $name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* 设置从主库读取数据 |
97
|
|
|
* @access public |
98
|
|
|
* @param string $table 数据表 |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function readMaster(string $table = '*') |
102
|
|
|
{ |
103
|
|
|
$this->readMaster[$table] = true; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* 是否从主库读取数据 |
110
|
|
|
* @access public |
111
|
|
|
* @param string $table 数据表 |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function isReadMaster(string $table): bool |
115
|
|
|
{ |
116
|
|
|
return isset($this->readMaster['*']) || isset($this->readMaster[$table]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* 使用表达式设置数据 |
121
|
|
|
* @access public |
122
|
|
|
* @param string $value 表达式 |
123
|
|
|
* @return Raw |
124
|
|
|
*/ |
125
|
|
|
public function raw(string $value): Raw |
126
|
|
|
{ |
127
|
|
|
return new Raw($value); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* 更新查询次数 |
132
|
|
|
* @access public |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
public function updateQueryTimes(): void |
136
|
|
|
{ |
137
|
|
|
$this->queryTimes++; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* 获得查询次数 |
142
|
|
|
* @access public |
143
|
|
|
* @return integer |
144
|
|
|
*/ |
145
|
|
|
public function getQueryTimes(): int |
146
|
|
|
{ |
147
|
|
|
return $this->queryTimes; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* 数据库连接参数解析 |
152
|
|
|
* @access private |
153
|
|
|
* @param mixed $config |
|
|
|
|
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
private function parseConfig($config): array |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
if (empty($config)) { |
159
|
|
|
$config = $this->option; |
160
|
|
|
} elseif (is_string($config)) { |
161
|
|
|
// 支持读取配置参数 |
162
|
|
|
$config = $this->option[$config] ?? null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $config; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* 获取数据库的配置参数 |
170
|
|
|
* @access public |
171
|
|
|
* @param string $name 参数名称 |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
|
|
public function getConfig(string $name = '') |
175
|
|
|
{ |
176
|
|
|
return $name ? ($this->option[$name] ?? null) : $this->option; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* 创建一个新的查询对象 |
181
|
|
|
* @access public |
182
|
|
|
* @param string $query 查询对象类名 |
|
|
|
|
183
|
|
|
* @param mixed $connection 连接配置信息 |
|
|
|
|
184
|
|
|
* @return mixed |
185
|
|
|
*/ |
186
|
|
|
public function buildQuery(string $query, $connection) |
187
|
|
|
{ |
188
|
|
|
$connection = $this->connect($connection); |
189
|
|
|
return $this->newQuery($query, $connection); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* 注册回调方法 |
194
|
|
|
* @access public |
195
|
|
|
* @param string $event 事件名 |
196
|
|
|
* @param callable $callback 回调方法 |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
public function event(string $event, callable $callback): void |
200
|
|
|
{ |
201
|
|
|
$this->event->listen('db.' . $event, $callback); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
|
|
|
|
205
|
|
|
* 创建一个新的查询对象 |
206
|
|
|
* @access public |
207
|
|
|
* @param string $query 查询对象类名 |
|
|
|
|
208
|
|
|
* @param Connection $connection 连接对象 |
|
|
|
|
209
|
|
|
* @return mixed |
210
|
|
|
*/ |
211
|
|
|
protected function newQuery(string $class, $connection) |
212
|
|
|
{ |
213
|
|
|
$query = new $class($connection); |
214
|
|
|
|
215
|
|
|
$query->setEvent($this->event); |
216
|
|
|
$query->setConfig($this->config); |
217
|
|
|
$query->setDb($this); |
218
|
|
|
|
219
|
|
|
return $query; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function __call($method, $args) |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
$query = $this->newQuery($this->option['query'], $this->connection); |
225
|
|
|
|
226
|
|
|
return call_user_func_array([$query, $method], $args); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|