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