Completed
Push — 6.0 ( e7c66b...3b6853 )
by liu
02:54
created

Db::buildQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Db
Loading history...
19
{
20
    /**
21
     * 当前数据库连接对象
22
     * @var Connection
23
     */
24
    protected $connection;
25
26
    /**
27
     * 配置对象
28
     * @var Config
29
     */
30
    protected $config;
31
32
    /**
33
     * Event对象
34
     * @var Event
35
     */
36
    protected $event;
37
38
    /**
39
     * 数据库配置
40
     * @var array
41
     */
42
    protected $option = [];
43
44
    /**
45
     * 读取主库
46
     * @var array
47
     */
48
    protected $readMaster = [];
49
50
    /**
51
     * 查询次数
52
     * @var int
53
     */
54
    protected $queryTimes = 0;
55
56
    /**
57
     * 架构函数
58
     * @param  array         $config 连接配置
59
     * @access public
60
     */
61
    public function __construct(array $config = [])
62
    {
63
        if (empty($config['query'])) {
64
            $config['query'] = '\\think\\db\\Query';
65
        }
66
67
        $this->option = $config;
68
69
        $this->connection = $this->connect($config);
70
    }
71
72
    public static function __make(Event $event, Config $config)
2 ignored issues
show
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
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...
73
    {
74
        $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

74
        $db = new static(/** @scrutinizer ignore-type */ $config->get('database'));
Loading history...
75
76
        $db->event  = $event;
77
        $db->config = $config;
78
79
        return $db;
80
    }
81
82
    /**
83
     * 切换数据库连接
84
     * @access public
85
     * @param  mixed       $config 连接配置
86
     * @param  bool|string $name 连接标识 true 强制重新连接
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
87
     * @return object
88
     * @throws Exception
89
     */
90
    public function connect($config = [], $name = false)
91
    {
92
        return Connection::instance($this->parseConfig($config), $name);
93
    }
94
95
    /**
96
     * 设置从主库读取数据
97
     * @access public
98
     * @param  string $table 数据表
99
     * @return $this
100
     */
101
    public function readMaster(string $table = '*')
102
    {
103
        $this->readMaster[$table] = true;
104
105
        return $this;
106
    }
107
108
    /**
109
     * 是否从主库读取数据
110
     * @access public
111
     * @param  string $table 数据表
112
     * @return bool
113
     */
114
    public function isReadMaster(string $table): bool
115
    {
116
        return isset($this->readMaster['*']) || isset($this->readMaster[$table]);
117
    }
118
119
    /**
120
     * 使用表达式设置数据
121
     * @access public
122
     * @param  string $value 表达式
123
     * @return Raw
124
     */
125
    public function raw(string $value): Raw
126
    {
127
        return new Raw($value);
128
    }
129
130
    /**
131
     * 更新查询次数
132
     * @access public
133
     * @return void
134
     */
135
    public function updateQueryTimes(): void
136
    {
137
        $this->queryTimes++;
138
    }
139
140
    /**
141
     * 获得查询次数
142
     * @access public
143
     * @return integer
144
     */
145
    public function getQueryTimes(): int
146
    {
147
        return $this->queryTimes;
148
    }
149
150
    /**
151
     * 数据库连接参数解析
152
     * @access private
153
     * @param  mixed $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
154
     * @return array
155
     */
156
    private function parseConfig($config): array
0 ignored issues
show
Coding Style introduced by
Private method name "Db::parseConfig" must be prefixed with an underscore
Loading history...
157
    {
158
        if (empty($config)) {
159
            $config = $this->option;
160
        } elseif (is_string($config)) {
161
            // 支持读取配置参数
162
            $config = $this->option[$config] ?? null;
163
        }
164
165
        return $config;
166
    }
167
168
    /**
169
     * 获取数据库的配置参数
170
     * @access public
171
     * @param  string $name 参数名称
172
     * @return mixed
173
     */
174
    public function getConfig(string $name = '')
175
    {
176
        return $name ? ($this->option[$name] ?? null) : $this->option;
177
    }
178
179
    /**
180
     * 创建一个新的查询对象
181
     * @access public
182
     * @param  string $query        查询对象类名
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 8 found
Loading history...
183
     * @param  mixed  $connection   连接配置信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
184
     * @return mixed
185
     */
186
    public function buildQuery(string $query, $connection)
187
    {
188
        $connection = $this->connect($connection);
189
        return $this->newQuery($query, $connection);
190
    }
191
192
    /**
193
     * 注册回调方法
194
     * @access public
195
     * @param  string   $event    事件名
196
     * @param  callable $callback 回调方法
197
     * @return void
198
     */
199
    public function event(string $event, callable $callback): void
200
    {
201
        $this->event->listen('db.' . $event, $callback);
202
    }
203
204
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $class should have a doc-comment as per coding-style.
Loading history...
205
     * 创建一个新的查询对象
206
     * @access public
207
     * @param  string     $query        查询对象类名
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $query does not match actual variable name $class
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 8 found
Loading history...
208
     * @param  Connection $connection   连接对象
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
209
     * @return mixed
210
     */
211
    protected function newQuery(string $class, $connection)
212
    {
213
        $query = new $class($connection);
214
215
        $query->setEvent($this->event);
216
        $query->setConfig($this->config);
217
        $query->setDb($this);
218
219
        return $query;
220
    }
221
222
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
223
    {
224
        $query = $this->newQuery($this->option['query'], $this->connection);
225
226
        return call_user_func_array([$query, $method], $args);
227
    }
228
}
229