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