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

AggregateQuery   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 85
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A avg() 0 3 1
A aggregate() 0 3 1
A min() 0 3 1
A count() 0 18 2
A max() 0 3 1
A sum() 0 3 1
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\db\concern;
14
15
/**
16
 * 聚合查询
17
 */
18
trait AggregateQuery
19
{
20
    /**
21
     * 聚合查询
22
     * @access protected
23
     * @param string     $aggregate 聚合方法
24
     * @param string|Raw $field     字段名
0 ignored issues
show
Bug introduced by
The type think\db\concern\Raw was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
     * @param bool       $force     强制转为数字类型
26
     * @return mixed
27
     */
28
    protected function aggregate(string $aggregate, $field, bool $force = false)
29
    {
30
        return $this->connection->aggregate($this, $aggregate, $field, $force);
31
    }
32
33
    /**
34
     * COUNT查询
35
     * @access public
36
     * @param string|Raw $field 字段名
37
     * @return int
38
     */
39
    public function count(string $field = '*'): int
40
    {
41
        if (!empty($this->options['group'])) {
42
            // 支持GROUP
43
            $options = $this->getOptions();
0 ignored issues
show
Bug introduced by
It seems like getOptions() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            /** @scrutinizer ignore-call */ 
44
            $options = $this->getOptions();
Loading history...
44
            $subSql  = $this->options($options)
0 ignored issues
show
Bug introduced by
It seems like options() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            $subSql  = $this->/** @scrutinizer ignore-call */ options($options)
Loading history...
45
                ->field('count(' . $field . ') AS think_count')
46
                ->bind($this->bind)
47
                ->buildSql();
48
49
            $query = $this->newQuery()->table([$subSql => '_group_count_']);
0 ignored issues
show
Bug introduced by
It seems like newQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            $query = $this->/** @scrutinizer ignore-call */ newQuery()->table([$subSql => '_group_count_']);
Loading history...
50
51
            $count = $query->aggregate('COUNT', '*');
52
        } else {
53
            $count = $this->aggregate('COUNT', $field);
54
        }
55
56
        return (int) $count;
57
    }
58
59
    /**
60
     * SUM查询
61
     * @access public
62
     * @param string|Raw $field 字段名
63
     * @return float
64
     */
65
    public function sum($field): float
66
    {
67
        return $this->aggregate('SUM', $field, true);
68
    }
69
70
    /**
71
     * MIN查询
72
     * @access public
73
     * @param string|Raw $field 字段名
74
     * @param bool       $force 强制转为数字类型
75
     * @return mixed
76
     */
77
    public function min($field, bool $force = true)
78
    {
79
        return $this->aggregate('MIN', $field, $force);
80
    }
81
82
    /**
83
     * MAX查询
84
     * @access public
85
     * @param string|Raw $field 字段名
86
     * @param bool       $force 强制转为数字类型
87
     * @return mixed
88
     */
89
    public function max($field, bool $force = true)
90
    {
91
        return $this->aggregate('MAX', $field, $force);
92
    }
93
94
    /**
95
     * AVG查询
96
     * @access public
97
     * @param string|Raw $field 字段名
98
     * @return float
99
     */
100
    public function avg($field): float
101
    {
102
        return $this->aggregate('AVG', $field, true);
103
    }
104
105
}
106