Passed
Push — master ( 1dcf9b...c6a312 )
by y
01:25
created

AggregateTrait::sum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Helix\DB\SQL;
4
5
/**
6
 * Produces aggregate expressions for the instance.
7
 */
8
trait AggregateTrait {
9
10
    use AbstractTrait;
11
12
    /**
13
     * `AVG($this)`
14
     *
15
     * @return Num
16
     */
17
    public function avg () {
18
        return Num::factory($this->db, "AVG({$this})");
19
    }
20
21
    /**
22
     * `AVG(DISTINCT $this)`
23
     *
24
     * @return Num
25
     */
26
    public function avgDistinct () {
27
        return Num::factory($this->db, "AVG(DISTINCT {$this})");
28
    }
29
30
    /**
31
     * `COUNT($this)`
32
     *
33
     * @return Num
34
     */
35
    public function count () {
36
        return Num::factory($this->db, "COUNT({$this})");
37
    }
38
39
    /**
40
     * `COUNT(DISTINCT $this)`
41
     *
42
     * @return Num
43
     */
44
    public function countDistinct () {
45
        return Num::factory($this->db, "COUNT(DISTINCT {$this})");
46
    }
47
48
    /**
49
     * `GROUP_CONCAT($this)` using a delimiter.
50
     *
51
     * @param string $delimiter
52
     * @return Text
53
     */
54
    public function groupConcat (string $delimiter = ',') {
55
        $delimiter = $this->db->quote($delimiter);
56
        if ($this->db->isSQLite()) {
57
            return Text::factory($this->db, "GROUP_CONCAT({$this},{$delimiter})");
58
        }
59
        return Text::factory($this->db, "GROUP_CONCAT({$this} SEPARATOR {$delimiter})");
60
    }
61
62
    /**
63
     * `MAX($this)`
64
     *
65
     * @return Num
66
     */
67
    public function max () {
68
        return Num::factory($this->db, "MAX({$this})");
69
    }
70
71
    /**
72
     * `MIN($this)`
73
     *
74
     * @return Num
75
     */
76
    public function min () {
77
        return Num::factory($this->db, "MIN({$this})");
78
    }
79
80
    /**
81
     * `SUM($this)`
82
     *
83
     * @return Num
84
     */
85
    public function sum () {
86
        return Num::factory($this->db, "SUM({$this})");
87
    }
88
89
    /**
90
     * `SUM(DISTINCT $this)`
91
     *
92
     * @return Num
93
     */
94
    public function sumDistinct () {
95
        return Num::factory($this->db, "SUM(DISTINCT {$this})");
96
    }
97
}