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