Completed
Push — 6.0 ( 7f1370...dfc5cb )
by liu
03:40
created

Db::updateQueryTimes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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