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