1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB\SQL; |
4
|
|
|
|
5
|
|
|
use Helix\DB; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Produces aggregate expressions for the instance. |
9
|
|
|
*/ |
10
|
|
|
trait AggregateTrait { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var DB |
14
|
|
|
*/ |
15
|
|
|
protected $db; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* `AVG(ALL|DISTINCT $this)` |
19
|
|
|
* |
20
|
|
|
* @param string $aggregate `ALL|DISTINCT` |
21
|
|
|
* @return Numeric |
22
|
|
|
*/ |
23
|
|
|
public function getAvg ($aggregate = 'ALL') { |
24
|
|
|
return new Numeric($this->db, "AVG({$aggregate} {$this})"); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* `GROUP_CONCAT($this)` using a delimiter. |
29
|
|
|
* |
30
|
|
|
* @param string $delimiter |
31
|
|
|
* @return Text |
32
|
|
|
*/ |
33
|
|
|
public function getConcat (string $delimiter = ',') { |
34
|
|
|
$delimiter = $this->db->quote($delimiter); |
35
|
|
|
switch ($this->db->getDriver()) { |
36
|
|
|
case 'sqlite': |
37
|
|
|
return new Text($this->db, "GROUP_CONCAT({$this},{$delimiter})"); |
38
|
|
|
default: |
39
|
|
|
return new Text($this->db, "GROUP_CONCAT({$this} SEPARATOR {$delimiter})"); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* `COUNT(ALL|DISTINCT $this)` |
45
|
|
|
* |
46
|
|
|
* @param string $aggregate `ALL|DISTINCT` |
47
|
|
|
* @return Numeric |
48
|
|
|
*/ |
49
|
|
|
public function getCount ($aggregate = 'ALL') { |
50
|
|
|
return new Numeric($this->db, "COUNT({$aggregate} {$this})"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* `MAX($this)` |
55
|
|
|
* |
56
|
|
|
* @return Numeric |
57
|
|
|
*/ |
58
|
|
|
public function getMax () { |
59
|
|
|
return new Numeric($this->db, "MAX({$this})"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* `MIN($this)` |
64
|
|
|
* |
65
|
|
|
* @return Numeric |
66
|
|
|
*/ |
67
|
|
|
public function getMin () { |
68
|
|
|
return new Numeric($this->db, "MIN({$this})"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* `SUM(ALL|DISTINCT $this)` |
73
|
|
|
* |
74
|
|
|
* @param string $aggregate `ALL|DISTINCT` |
75
|
|
|
* @return Numeric |
76
|
|
|
*/ |
77
|
|
|
public function getSum ($aggregate = 'ALL') { |
78
|
|
|
return new Numeric($this->db, "SUM({$aggregate} {$this})"); |
79
|
|
|
} |
80
|
|
|
} |