AggregateTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 14
c 2
b 0
f 1
dl 0
loc 98
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A avgDistinct() 0 3 1
A min() 0 3 1
A sumDistinct() 0 3 1
A countDistinct() 0 3 1
A avg() 0 3 1
A sum() 0 3 1
A max() 0 3 1
A count() 0 3 1
A groupConcat() 0 7 2
1
<?php
2
3
namespace Helix\DB\Fluent\Value;
4
5
use Helix\DB\Fluent\AbstractTrait;
6
use Helix\DB\Fluent\Num;
7
use Helix\DB\Fluent\Str;
8
9
/**
10
 * Aggregation functions.
11
 */
12
trait AggregateTrait
13
{
14
15
    use AbstractTrait;
16
17
    /**
18
     * `AVG($this)`
19
     *
20
     * @return Num
21
     */
22
    public function avg()
23
    {
24
        return Num::factory($this->db, "AVG({$this})");
25
    }
26
27
    /**
28
     * `AVG(DISTINCT $this)`
29
     *
30
     * @return Num
31
     */
32
    public function avgDistinct()
33
    {
34
        return Num::factory($this->db, "AVG(DISTINCT {$this})");
35
    }
36
37
    /**
38
     * `COUNT($this)`
39
     *
40
     * @return Num
41
     */
42
    public function count()
43
    {
44
        return Num::factory($this->db, "COUNT({$this})");
45
    }
46
47
    /**
48
     * `COUNT(DISTINCT $this)`
49
     *
50
     * @return Num
51
     */
52
    public function countDistinct()
53
    {
54
        return Num::factory($this->db, "COUNT(DISTINCT {$this})");
55
    }
56
57
    /**
58
     * `GROUP_CONCAT($this)` using a delimiter.
59
     *
60
     * @param string $delimiter
61
     * @return Str
62
     */
63
    public function groupConcat(string $delimiter = ',')
64
    {
65
        $delimiter = $this->db->quote($delimiter);
66
        if ($this->db->isSQLite()) {
67
            return Str::factory($this->db, "GROUP_CONCAT({$this},{$delimiter})");
68
        }
69
        return Str::factory($this->db, "GROUP_CONCAT({$this} SEPARATOR {$delimiter})");
70
    }
71
72
    /**
73
     * `MAX($this)`
74
     *
75
     * @return Num
76
     */
77
    public function max()
78
    {
79
        return Num::factory($this->db, "MAX({$this})");
80
    }
81
82
    /**
83
     * `MIN($this)`
84
     *
85
     * @return Num
86
     */
87
    public function min()
88
    {
89
        return Num::factory($this->db, "MIN({$this})");
90
    }
91
92
    /**
93
     * `SUM($this)`
94
     *
95
     * @return Num
96
     */
97
    public function sum()
98
    {
99
        return Num::factory($this->db, "SUM({$this})");
100
    }
101
102
    /**
103
     * `SUM(DISTINCT $this)`
104
     *
105
     * @return Num
106
     */
107
    public function sumDistinct()
108
    {
109
        return Num::factory($this->db, "SUM(DISTINCT {$this})");
110
    }
111
}
112