Passed
Push — 6.0 ( 8989f5...8df141 )
by liu
04:09
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 class doc comment
Loading history...
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)
1 ignored issue
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
Missing function doc comment
Loading history...
61
    {
62
        return 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

62
        return new static(/** @scrutinizer ignore-type */ $config->get('database'));
Loading history...
63
    }
64
65
    /**
66
     * 切换数据库连接
67
     * @access public
68
     * @param  mixed         $config 连接配置
69
     * @param  bool|string   $name 连接标识 true 强制重新连接
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
139
     * @return array
140
     */
141
    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...
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   连接配置信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
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        查询对象类名
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 8 found
Loading history...
179
     * @param  mixed  $connection   连接配置信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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