Passed
Push — 6.0 ( 56ba62...b1927d )
by liu
05:06 queued 10s
created

Connection::getTableInfo()   C

Complexity

Conditions 14
Paths 60

Size

Total Lines 58
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
cc 14
eloc 30
nc 60
nop 2
dl 0
loc 58
ccs 0
cts 28
cp 0
crap 210
rs 6.2666
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Connection::cacheData() 0 7 2
A Connection::parseCache() 0 21 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\db;
14
15
use think\Cache;
16
use think\cache\CacheItem;
17
use think\Db;
18
use think\Exception;
19
use think\exception\PDOException;
20
use think\Log;
21
22
/**
23
 * 数据库连接基础类
24
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
25
abstract class Connection
26
{
27
28
    /**
29
     * 当前SQL指令
30
     * @var string
31
     */
32
    protected $queryStr = '';
33
34
    /**
35
     * 返回或者影响记录数
36
     * @var int
37
     */
38
    protected $numRows = 0;
39
40
    /**
41
     * 事务指令数
42
     * @var int
43
     */
44
    protected $transTimes = 0;
45
46
    /**
47
     * 错误信息
48
     * @var string
49
     */
50
    protected $error = '';
51
52
    /**
53
     * 数据库连接ID 支持多个连接
54
     * @var array
55
     */
56
    protected $links = [];
57
58
    /**
59
     * 当前连接ID
60
     * @var object
61
     */
62
    protected $linkID;
63
64
    /**
65
     * 当前读连接ID
66
     * @var object
67
     */
68
    protected $linkRead;
69
70
    /**
71
     * 当前写连接ID
72
     * @var object
73
     */
74
    protected $linkWrite;
75
76
    /**
77
     * 数据表信息
78
     * @var array
79
     */
80
    protected $info = [];
81
82
    /**
83
     * 查询开始时间
84
     * @var float
85
     */
86
    protected $queryStartTime;
87
88
    /**
89
     * Builder对象
90
     * @var Builder
91
     */
92
    protected $builder;
93
94
    /**
95
     * Db对象
96
     * @var Db
97
     */
98
    protected $db;
99
100
    /**
101
     * 是否读取主库
102
     * @var bool
103
     */
104
    protected $readMaster = false;
105
106
    /**
107
     * 数据库连接参数配置
108
     * @var array
109
     */
110
    protected $config = [];
111
112
    /**
113
     * 缓存对象
114
     * @var Cache
115
     */
116
    protected $cache;
117
118
    /**
119
     * 日志对象
120
     * @var Log
121
     */
122
    protected $log;
123
124
    /**
125
     * 架构函数 读取数据库配置信息
126
     * @access public
127
     * @param Cache $cache 缓存对象
1 ignored issue
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
128
     * @param Log   $log 日志对象
1 ignored issue
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
129
     * @param array $config 数据库配置数组
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
130
     */
131
    public function __construct(Cache $cache, Log $log, array $config = [])
132
    {
133
        if (!empty($config)) {
134
            $this->config = array_merge($this->config, $config);
135
        }
136
137
        // 创建Builder对象
138
        $class = $this->getBuilderClass();
139
140
        $this->builder = new $class($this);
141
        $this->cache   = $cache;
142
        $this->log     = $log;
143
144
        // 执行初始化操作
145
        $this->initialize();
146
    }
147
148
    /**
149
     * 初始化
150
     * @access protected
151
     * @return void
152
     */
153
    protected function initialize()
154
    {
155
    }
156
157
    /**
158
     * 获取当前连接器类对应的Query类
159
     * @access public
160
     * @return string
161
     */
162
    abstract public function getQueryClass(): string;
163
164
    /**
165
     * 获取当前连接器类对应的Builder类
166
     * @access public
167
     * @return string
168
     */
169
    abstract public function getBuilderClass(): string;
170
171
    /**
172
     * 设置当前的数据库Builder对象
173
     * @access protected
174
     * @param Builder $builder
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
175
     * @return void
176
     */
177
    protected function setBuilder($builder)
178
    {
179
        $this->builder = $builder;
180
    }
181
182
    /**
183
     * 获取当前的builder实例对象
184
     * @access public
185
     * @return Builder
186
     */
187
    public function getBuilder()
188
    {
189
        return $this->builder;
190
    }
191
192
    /**
193
     * 设置当前的数据库Db对象
194
     * @access public
195
     * @param Db $db
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
196
     * @return void
197
     */
198
    public function setDb(Db $db)
199
    {
200
        $this->db = $db;
201
    }
202
203
    /**
204
     * 获取数据库的配置参数
205
     * @access public
206
     * @param string $config 配置名称
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
207
     * @return mixed
208
     */
209
    public function getConfig(string $config = '')
210
    {
211
        if ('' === $config) {
212
            return $this->config;
213
        }
214
        return $this->config[$config] ?? null;
215
    }
216
217
    /**
218
     * 设置数据库的配置参数
219
     * @access public
220
     * @param array $config 配置
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
221
     * @return void
222
     */
223
    public function setConfig(array $config)
224
    {
225
        $this->config = array_merge($this->config, $config);
226
    }
227
228
    /**
229
     * 连接数据库方法
230
     * @access public
231
     * @param array   $config  接参数
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
232
     * @param integer $linkNum 连接序号
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
233
     * @return mixed
234
     * @throws Exception
235
     */
236
    abstract public function connect(array $config = [], $linkNum = 0);
237
238
    /**
239
     * 释放查询结果
240
     * @access public
241
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
242
    abstract public function free();
243
244
    /**
245
     * 查找单条记录
246
     * @access public
247
     * @param Query $query 查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
248
     * @return array
249
     * @throws DbException
250
     * @throws ModelNotFoundException
251
     * @throws DataNotFoundException
252
     */
253
    abstract public function find(Query $query): array;
254
255
    /**
256
     * 使用游标查询记录
257
     * @access public
258
     * @param Query $query 查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
259
     * @return \Generator
260
     */
261
    abstract public function cursor(Query $query);
262
263
    /**
264
     * 查找记录
265
     * @access public
266
     * @param Query $query 查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
267
     * @return array
268
     * @throws DbException
269
     * @throws ModelNotFoundException
270
     * @throws DataNotFoundException
271
     */
272
    abstract public function select(Query $query): array;
273
274
    /**
275
     * 插入记录
276
     * @access public
277
     * @param Query   $query        查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
278
     * @param boolean $getLastInsID 返回自增主键
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
279
     * @return mixed
280
     */
281
    abstract public function insert(Query $query, bool $getLastInsID = false);
282
283
    /**
284
     * 批量插入记录
285
     * @access public
286
     * @param Query   $query   查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
287
     * @param mixed   $dataSet 数据集
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
288
     * @return integer
289
     * @throws \Exception
290
     * @throws \Throwable
291
     */
292
    abstract public function insertAll(Query $query, array $dataSet = []): int;
293
294
    /**
295
     * 更新记录
296
     * @access public
297
     * @param Query $query 查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
298
     * @return integer
299
     * @throws Exception
300
     * @throws PDOException
301
     */
302
    abstract public function update(Query $query): int;
303
304
    /**
305
     * 删除记录
306
     * @access public
307
     * @param Query $query 查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
308
     * @return int
309
     * @throws Exception
310
     * @throws PDOException
311
     */
312
    abstract public function delete(Query $query): int;
313
314
    /**
315
     * 得到某个字段的值
316
     * @access public
317
     * @param Query  $query   查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
318
     * @param string $field   字段名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
319
     * @param mixed  $default 默认值
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
320
     * @param bool   $one     返回一个值
1 ignored issue
show
Coding Style introduced by
Superfluous parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
321
     * @return mixed
322
     */
323
    abstract public function value(Query $query, string $field, $default = null);
324
325
    /**
326
     * 得到某个列的数组
327
     * @access public
328
     * @param Query  $query  查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
329
     * @param string $column 字段名 多个字段用逗号分隔
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
330
     * @param string $key    索引
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
331
     * @return array
332
     */
333
    abstract public function column(Query $query, string $column, string $key = ''): array;
334
335
    /**
336
     * 执行数据库事务
337
     * @access public
338
     * @param callable $callback 数据操作方法回调
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
339
     * @return mixed
340
     * @throws PDOException
341
     * @throws \Exception
342
     * @throws \Throwable
343
     */
344
    abstract public function transaction(callable $callback);
345
346
    /**
347
     * 启动事务
348
     * @access public
349
     * @return void
350
     * @throws \PDOException
351
     * @throws \Exception
352
     */
353
    abstract public function startTrans();
354
355
    /**
356
     * 用于非自动提交状态下面的查询提交
357
     * @access public
358
     * @return void
359
     * @throws PDOException
360
     */
361
    abstract public function commit();
362
363
    /**
364
     * 事务回滚
365
     * @access public
366
     * @return void
367
     * @throws PDOException
368
     */
369
    abstract public function rollback();
370
371
    /**
372
     * 关闭数据库(或者重新连接)
373
     * @access public
374
     * @return $this
375
     */
376
    abstract public function close();
377
378
    /**
379
     * 获取最近一次查询的sql语句
380
     * @access public
381
     * @return string
382
     */
383
    abstract public function getLastSql(): string;
384
385
    /**
386
     * 获取最近插入的ID
387
     * @access public
388
     * @param string $sequence 自增序列名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
389
     * @return string
390
     */
391
    abstract public function getLastInsID(string $sequence = null): string;
392
393
    /**
394
     * 获取最近的错误信息
395
     * @access public
396
     * @return string
397
     */
398
    abstract public function getError(): string;
399
400
    /**
401
     * 初始化数据库连接
402
     * @access protected
403
     * @param boolean $master 是否主服务器
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
404
     * @return void
405
     */
406
    abstract protected function initConnect(bool $master = true);
407
408
    /**
409
     * 记录SQL日志
410
     * @access protected
411
     * @param string $log  SQL日志信息
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
412
     * @param string $type 日志类型
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
413
     * @return void
414
     */
415
    protected function log($log, $type = 'sql')
416
    {
417
        if ($this->config['debug']) {
418
            $this->log->record($log, $type);
419
        }
420
    }
421
422
    /**
423
     * 缓存数据
424
     * @access protected
425
     * @param CacheItem $cacheItem 缓存Item
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
426
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
427
    protected function cacheData(CacheItem $cacheItem)
428
    {
429
        if ($cacheItem->getTag()) {
430
            $this->cache->tag($cacheItem->getTag());
431
        }
432
433
        $this->cache->set($cacheItem->getKey(), $cacheItem->get(), $cacheItem->getExpire());
434
    }
435
436
    /**
437
     * 分析缓存
438
     * @access protected
439
     * @param Query $query 查询对象
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
440
     * @param array $cache 缓存信息
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
441
     * @return CacheItem
442
     */
443
    protected function parseCache(Query $query, array $cache): CacheItem
444
    {
445
        list($key, $expire, $tag) = $cache;
446
447
        if ($key instanceof CacheItem) {
448
            $cacheItem = $key;
449
        } else {
450
            if (true === $key) {
451
                if (!empty($query->getOptions('key'))) {
452
                    $key = 'think:' . $this->getConfig('database') . '.' . $query->getTable() . '|' . $query->getOptions('key');
453
                } else {
454
                    $key = md5($this->getConfig('database') . serialize($query->getOptions()));
455
                }
456
            }
457
458
            $cacheItem = new CacheItem($key);
459
            $cacheItem->expire($expire);
460
            $cacheItem->tag($tag);
461
        }
462
463
        return $cacheItem;
464
    }
465
466
    /**
467
     * 延时更新检查 返回false表示需要延时
468
     * 否则返回实际写入的数值
469
     * @access public
470
     * @param string  $type     自增或者自减
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
471
     * @param string  $guid     写入标识
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
472
     * @param float   $step     写入步进值
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
473
     * @param integer $lazyTime 延时时间(s)
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
474
     * @return false|integer
475
     */
476
    public function lazyWrite(string $type, string $guid, float $step, int $lazyTime)
477
    {
478
        if (!$this->cache->has($guid . '_time')) {
479
            // 计时开始
480
            $this->cache->set($guid . '_time', time(), 0);
481
            $this->cache->$type($guid, $step);
482
        } elseif (time() > $this->cache->get($guid . '_time') + $lazyTime) {
483
            // 删除缓存
484
            $value = $this->cache->$type($guid, $step);
485
            $this->cache->delete($guid);
486
            $this->cache->delete($guid . '_time');
487
            return 0 === $value ? false : $value;
488
        } else {
489
            // 更新缓存
490
            $this->cache->$type($guid, $step);
491
        }
492
493
        return false;
494
    }
495
496
    /**
497
     * 析构方法
498
     * @access public
499
     */
500
    public function __destruct()
501
    {
502
        // 释放查询
503
        $this->free();
504
505
        // 关闭连接
506
        $this->close();
507
    }
508
}
509