1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Expression; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Aggregation expressions |
9
|
|
|
* Can be use on Query::select() method |
|
|
|
|
10
|
|
|
* |
11
|
|
|
* Usage: |
12
|
|
|
* <code> |
13
|
|
|
* User::select([ |
14
|
|
|
* 'firstId' => Aggregate::min(id), |
15
|
|
|
* 'lastId' => Aggregate::max(id), |
16
|
|
|
* 'count' => Aggregate::count(id), |
17
|
|
|
* ]); |
18
|
|
|
* </code> |
19
|
|
|
*/ |
20
|
|
|
abstract class Aggregate implements ExpressionInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $attribute; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Max constructor. |
29
|
|
|
* |
30
|
|
|
* @param string $attribute The attribute name |
31
|
|
|
*/ |
32
|
5 |
|
public function __construct(string $attribute) |
33
|
|
|
{ |
34
|
5 |
|
$this->attribute = $attribute; |
35
|
5 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
5 |
|
final public function build($query, $compiler) |
41
|
|
|
{ |
42
|
5 |
|
return $this->expression( |
43
|
5 |
|
$compiler->platform()->grammar(), |
44
|
5 |
|
$compiler->quoteIdentifier($query, $query->preprocessor()->field($this->attribute)) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the aggregate expression |
50
|
|
|
* |
51
|
|
|
* @param AbstractPlatform $platform The database platform |
52
|
|
|
* @param string $attribute The attribute to aggregate |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
abstract protected function expression(AbstractPlatform $platform, string $attribute): string; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Perform MIN() aggregation function |
60
|
|
|
* |
61
|
|
|
* @param string $attribute The attribute to aggregate |
62
|
|
|
* |
63
|
|
|
* @return self |
|
|
|
|
64
|
|
|
*/ |
65
|
1 |
|
public static function min(string $attribute): self |
66
|
|
|
{ |
67
|
|
|
return new class($attribute) extends Aggregate { |
68
|
|
|
protected function expression(AbstractPlatform $platform, string $attribute): string |
|
|
|
|
69
|
|
|
{ |
70
|
1 |
|
return $platform->getMinExpression($attribute); |
71
|
|
|
} |
72
|
|
|
}; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Perform MAX() aggregation function |
77
|
|
|
* |
78
|
|
|
* @param string $attribute The attribute to aggregate |
79
|
|
|
* |
80
|
|
|
* @return self |
|
|
|
|
81
|
|
|
*/ |
82
|
1 |
|
public static function max(string $attribute): self |
83
|
|
|
{ |
84
|
|
|
return new class($attribute) extends Aggregate { |
85
|
|
|
protected function expression(AbstractPlatform $platform, string $attribute): string |
|
|
|
|
86
|
|
|
{ |
87
|
1 |
|
return $platform->getMaxExpression($attribute); |
88
|
|
|
} |
89
|
|
|
}; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Perform AVG() aggregation function |
94
|
|
|
* |
95
|
|
|
* @param string $attribute The attribute to aggregate |
96
|
|
|
* |
97
|
|
|
* @return self |
|
|
|
|
98
|
|
|
*/ |
99
|
1 |
|
public static function avg(string $attribute): self |
100
|
|
|
{ |
101
|
|
|
return new class($attribute) extends Aggregate { |
102
|
|
|
protected function expression(AbstractPlatform $platform, string $attribute): string |
|
|
|
|
103
|
|
|
{ |
104
|
1 |
|
return $platform->getAvgExpression($attribute); |
105
|
|
|
} |
106
|
|
|
}; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Perform COUNT() aggregation function |
111
|
|
|
* |
112
|
|
|
* @param string $attribute The attribute to aggregate |
113
|
|
|
* |
114
|
|
|
* @return self |
|
|
|
|
115
|
|
|
*/ |
116
|
1 |
|
public static function count(string $attribute): self |
117
|
|
|
{ |
118
|
|
|
return new class($attribute) extends Aggregate { |
119
|
|
|
protected function expression(AbstractPlatform $platform, string $attribute): string |
|
|
|
|
120
|
|
|
{ |
121
|
1 |
|
return $platform->getCountExpression($attribute); |
122
|
|
|
} |
123
|
|
|
}; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Perform SUM() aggregation function |
128
|
|
|
* |
129
|
|
|
* @param string $attribute The attribute to aggregate |
130
|
|
|
* |
131
|
|
|
* @return self |
|
|
|
|
132
|
|
|
*/ |
133
|
1 |
|
public static function sum(string $attribute): self |
134
|
|
|
{ |
135
|
|
|
return new class($attribute) extends Aggregate { |
136
|
|
|
protected function expression(AbstractPlatform $platform, string $attribute): string |
|
|
|
|
137
|
|
|
{ |
138
|
1 |
|
return $platform->getSumExpression($attribute); |
139
|
|
|
} |
140
|
|
|
}; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|