Completed
Push — 6.0 ( 660e56...f0eade )
by liu
03:54
created

Db::buildQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
ccs 0
cts 9
cp 0
crap 6
rs 10
c 0
b 0
f 0
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\BaseQuery;
17
use think\db\Connection;
18
use think\db\Raw;
19
20
/**
21
 * Class Db
22
 * @package think
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
23
 * @mixin Query
24
 */
25
class Db
26
{
27
    /**
28
     * 数据库连接实例
29
     * @var array
30
     */
31
    protected $instance = [];
32
33
    /**
34
     * 当前连接实例
35
     * @var Connection
36
     */
37
    protected $connection;
38
39
    /**
40
     * Event对象
41
     * @var Event
42
     */
43
    protected $event;
44
45
    /**
46
     * 数据库配置
47
     * @var array
48
     */
49
    protected $config = [];
50
51
    /**
52
     * SQL监听
53
     * @var array
54
     */
55
    protected $listen = [];
56
57
    /**
58
     * 查询次数
59
     * @var int
60
     */
61
    protected $queryTimes = 0;
62
63
    /**
64
     * 架构函数
65
     * @param array $config 连接配置
66
     * @access public
67
     */
68 1
    public function __construct(array $config = [])
69
    {
70 1
        $this->config = $config;
71
72 1
    }
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @param Event  $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
76
     * @param Config $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
77
     * @return Db
78
     * @codeCoverageIgnore
79
     */
80
    public static function __make(Event $event, Config $config)
2 ignored issues
show
Coding Style introduced by
Method name "Db::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Db::__make" must not be prefixed with an underscore
Loading history...
81
    {
82
        $db = new static($config->get('database'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('database') can also be of type null; however, parameter $config of think\Db::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $db = new static(/** @scrutinizer ignore-type */ $config->get('database'));
Loading history...
83
84
        $db->setEvent($event);
85
86
        return $db;
87
    }
88
89
    /**
90
     * 连接/切换数据库连接
91
     * @access public
92
     * @param string|null $name  连接标识
93
     * @param bool        $force 强制重新连接
94
     * @return $this
95
     */
96
    public function connect(string $name = null, bool $force = false)
97
    {
98
        if (empty($name)) {
99
            $name = $this->config['default'] ?? 'mysql';
100
        }
101
102
        if ($force || !isset($this->instance[$name])) {
103
            if (!isset($this->config['connections'][$name])) {
104
                throw new InvalidArgumentException('Undefined db config:' . $name);
105
            }
106
107
            $config = $this->config['connections'][$name];
108
            $type   = !empty($config['type']) ? $config['type'] : 'mysql';
109
110
            $this->instance[$name] = App::factory($type, '\\think\\db\\connector\\', $config);
111
        }
112
113
        $this->connection = $this->instance[$name];
114
        return $this;
115
    }
116
117
    /**
118
     * 使用表达式设置数据
119
     * @access public
120
     * @param string $value 表达式
121
     * @return Raw
122
     */
123
    public function raw(string $value): Raw
124
    {
125
        return new Raw($value);
126
    }
127
128
    /**
129
     * 更新查询次数
130
     * @access public
131
     * @return void
132
     */
133
    public function updateQueryTimes(): void
134
    {
135
        $this->queryTimes++;
136
    }
137
138
    /**
139
     * 重置查询次数
140
     * @access public
141
     * @return void
142
     */
143
    public function clearQueryTimes(): void
144
    {
145
        $this->queryTimes = 0;
146
    }
147
148
    /**
149
     * 获得查询次数
150
     * @access public
151
     * @return integer
152
     */
153
    public function getQueryTimes(): int
154
    {
155
        return $this->queryTimes;
156
    }
157
158
    /**
159
     * 创建一个新的查询对象
160
     * @access public
161
     * @param string|null $name 连接配置标识
162
     * @return BaseQuery
163
     */
164
    public function buildQuery(string $name = null): BaseQuery
165
    {
166
        $this->connect($name);
167
168
        $connection = $this->connection;
169
        $connection->setDb($this);
170
171
        $class = $connection->getQueryClass();
172
        $query = new $class($connection);
173
174
        if (!empty($this->config['time_query_rule'])) {
175
            $query->timeRule($this->config['time_query_rule']);
176
        }
177
178
        return $query;
179
    }
180
181
    /**
182
     * 监听SQL执行
183
     * @access public
184
     * @param callable $callback 回调方法
185
     * @return void
186
     */
187
    public function listen(callable $callback): void
188
    {
189
        $this->listen[] = $callback;
190
    }
191
192
    /**
193
     * 获取监听SQL执行
194
     * @access public
195
     * @return array
196
     */
197
    public function getListen(): array
198
    {
199
        return $this->listen;
200
    }
201
202
    /**
203
     * 设置Event对象
204
     * @param Event $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
205
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
206
    public function setEvent(Event $event)
207
    {
208
        $this->event = $event;
209
    }
210
211
    /**
212
     * 注册回调方法
213
     * @access public
214
     * @param string   $event    事件名
215
     * @param callable $callback 回调方法
216
     * @return void
217
     */
218
    public function event(string $event, callable $callback): void
219
    {
220
        if ($this->event) {
221
            $this->event->listen('db.' . $event, $callback);
222
        }
223
    }
224
225
    /**
226
     * 触发事件
227
     * @access public
228
     * @param string $event  事件名
229
     * @param mixed  $params 传入参数
230
     * @param bool   $once
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
231
     * @return mixed
232
     */
233
    public function trigger(string $event, $params = null, bool $once = false)
234
    {
235
        if ($this->event) {
236
            return $this->event->trigger('db.' . $event, $params, $once);
237
        }
238
    }
239
240
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
241
    {
242
        return call_user_func_array([$this->buildQuery(), $method], $args);
243
    }
244
}
245