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
|
|
|
* @param mixed $config 连接配置 |
69
|
|
|
* @param bool|string $name 连接标识 true 强制重新连接 |
|
|
|
|
70
|
|
|
* @return $this|object |
71
|
|
|
* @throws Exception |
72
|
|
|
*/ |
73
|
|
|
public function connect($config = [], $name = false) |
74
|
|
|
{ |
75
|
|
|
$this->connection = Connection::instance($this->parseConfig($config), $name); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 设置从主库读取数据 |
82
|
|
|
* @access public |
83
|
|
|
* @param string $table 数据表 |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function readMaster(string $table = '*') |
87
|
|
|
{ |
88
|
|
|
$this->readMaster[$table] = true; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* 是否从主库读取数据 |
95
|
|
|
* @access public |
96
|
|
|
* @param string $table 数据表 |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function isReadMaster(string $table): bool |
100
|
|
|
{ |
101
|
|
|
return isset($this->readMaster['*']) || isset($this->readMaster[$table]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* 使用表达式设置数据 |
106
|
|
|
* @access public |
107
|
|
|
* @param string $value 表达式 |
108
|
|
|
* @return Raw |
109
|
|
|
*/ |
110
|
|
|
public function raw(string $value): Raw |
111
|
|
|
{ |
112
|
|
|
return new Raw($value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* 更新查询次数 |
117
|
|
|
* @access public |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
public function updateQueryTimes(): void |
121
|
|
|
{ |
122
|
|
|
$this->queryTimes++; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* 获得查询次数 |
127
|
|
|
* @access public |
128
|
|
|
* @return integer |
129
|
|
|
*/ |
130
|
|
|
public function getQueryTimes(): int |
131
|
|
|
{ |
132
|
|
|
return $this->queryTimes; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* 数据库连接参数解析 |
137
|
|
|
* @access private |
138
|
|
|
* @param mixed $config |
|
|
|
|
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
private function parseConfig($config): array |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
if (empty($config)) { |
144
|
|
|
$config = $this->config; |
145
|
|
|
} elseif (is_string($config)) { |
146
|
|
|
// 支持读取配置参数 |
147
|
|
|
$config = $this->config[$config] ?? null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $config; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* 获取数据库的配置参数 |
155
|
|
|
* @access public |
156
|
|
|
* @param string $name 参数名称 |
157
|
|
|
* @return mixed |
158
|
|
|
*/ |
159
|
|
|
public function getConfig(string $name = '') |
160
|
|
|
{ |
161
|
|
|
return $name ? ($this->config[$name] ?? null) : $this->config; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* 创建一个新的Connection对象 |
166
|
|
|
* @access public |
167
|
|
|
* @param mixed $connection 连接配置信息 |
|
|
|
|
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
public function buildConnection($connection) |
171
|
|
|
{ |
172
|
|
|
return Connection::instance($this->parseConfig($connection)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* 创建一个新的查询对象 |
177
|
|
|
* @access public |
178
|
|
|
* @param string $query 查询对象类名 |
|
|
|
|
179
|
|
|
* @param mixed $connection 连接配置信息 |
|
|
|
|
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
|
public function buildQuery(string $query, $connection) |
183
|
|
|
{ |
184
|
|
|
$connection = $this->buildConnection($connection); |
185
|
|
|
return new $query($connection); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function __call($method, $args) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$class = $this->config['query']; |
191
|
|
|
|
192
|
|
|
$query = new $class($this->connection); |
193
|
|
|
|
194
|
|
|
return call_user_func_array([$query, $method], $args); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|