Completed
Push — 6.0 ( be0b6e...c47dd5 )
by liu
06:54 queued 10s
created

Db::raw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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\Connection;
17
use think\db\Query;
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 (!isset($this->config['connections'][$name])) {
103
            throw new InvalidArgumentException('Undefined db config:' . $name);
104
        }
105
106
        $config = $this->config['connections'][$name];
107
108
        if ($force || !isset($this->instance[$name])) {
109
            if (empty($config['type'])) {
110
                throw new InvalidArgumentException('Undefined db type');
111
            }
112
113
            $this->instance[$name] = App::factory($config['type'], '\\think\\db\\connector\\', $config);
114
        }
115
116
        $this->connection = $this->instance[$name];
117
        return $this;
118
    }
119
120
    /**
121
     * 使用表达式设置数据
122
     * @access public
123
     * @param string $value 表达式
124
     * @return Raw
125
     */
126
    public function raw(string $value): Raw
127
    {
128
        return new Raw($value);
129
    }
130
131
    /**
132
     * 更新查询次数
133
     * @access public
134
     * @return void
135
     */
136
    public function updateQueryTimes(): void
137
    {
138
        $this->queryTimes++;
139
    }
140
141
    /**
142
     * 重置查询次数
143
     * @access public
144
     * @return void
145
     */
146
    public function clearQueryTimes(): void
147
    {
148
        $this->queryTimes = 0;
149
    }
150
151
    /**
152
     * 获得查询次数
153
     * @access public
154
     * @return integer
155
     */
156
    public function getQueryTimes(): int
157
    {
158
        return $this->queryTimes;
159
    }
160
161
    /**
162
     * 创建一个新的查询对象
163
     * @access public
164
     * @param string|null $connection 连接配置标识
165
     * @return Query
166
     */
167
    public function buildQuery(string $connection = null): Query
168
    {
169
        $this->connect($connection);
170
        return $this->newQuery($this->connection);
171
    }
172
173
    /**
174
     * 监听SQL执行
175
     * @access public
176
     * @param callable $callback 回调方法
177
     * @return void
178
     */
179
    public function listen(callable $callback): void
180
    {
181
        $this->listen[] = $callback;
182
    }
183
184
    /**
185
     * 获取监听SQL执行
186
     * @access public
187
     * @return array
188
     */
189
    public function getListen(): array
190
    {
191
        return $this->listen;
192
    }
193
194
    /**
195
     * 设置Event对象
196
     * @param Event $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
197
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
198
    public function setEvent(Event $event)
199
    {
200
        $this->event = $event;
201
    }
202
203
    /**
204
     * 注册回调方法
205
     * @access public
206
     * @param string   $event    事件名
207
     * @param callable $callback 回调方法
208
     * @return void
209
     */
210
    public function event(string $event, callable $callback): void
211
    {
212
        if ($this->event) {
213
            $this->event->listen('db.' . $event, $callback);
214
        }
215
    }
216
217
    /**
218
     * 触发事件
219
     * @access public
220
     * @param string $event  事件名
221
     * @param mixed  $params 传入参数
222
     * @param bool   $once
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
223
     * @return mixed
224
     */
225
    public function trigger(string $event, $params = null, bool $once = false)
226
    {
227
        if ($this->event) {
228
            return $this->event->trigger('db.' . $event, $params, $once);
229
        }
230
    }
231
232
    /**
233
     * 创建一个新的查询对象
234
     * @access protected
235
     * @param Connection $connection 连接对象
236
     * @return Query
237
     */
238
    protected function newQuery(Connection $connection = null): Query
239
    {
240
        /** @var Query $query */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
241
        if (is_null($connection) && !$this->connection) {
242
            $this->connect();
243
        }
244
245
        $connection = $connection ?: $this->connection;
246
        $connection->setDb($this);
247
248
        $class = $connection->getQueryClass();
249
        return new $class($connection);
250
    }
251
252
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
253
    {
254
        $query = $this->newQuery($this->connection);
255
256
        return call_user_func_array([$query, $method], $args);
257
    }
258
}
259