File tree Expand file tree Collapse file tree 3 files changed +37
-4
lines changed Expand file tree Collapse file tree 3 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -202,7 +202,7 @@ use Tpetry\QueryExpressions\Function\Aggregate\{
202
202
use Tpetry\QueryExpressions\Operator\Value\Value;
203
203
204
204
new Avg(string|Expression $value);
205
- new Count(string|Expression $value);
205
+ new Count(string|Expression $value, bool $distinct = false );
206
206
new CountFilter(string|Expression $filter);
207
207
new Max(string|Expression $value);
208
208
new Min(string|Expression $value);
Original file line number Diff line number Diff line change 4
4
5
5
namespace Tpetry \QueryExpressions \Function \Aggregate ;
6
6
7
- class Count extends AggregateExpression
7
+ use Illuminate \Contracts \Database \Query \Expression ;
8
+ use Illuminate \Database \Grammar ;
9
+ use Tpetry \QueryExpressions \Concerns \StringizeExpression ;
10
+
11
+ class Count implements Expression
8
12
{
9
- protected function aggregate (): string
13
+ use StringizeExpression;
14
+
15
+ public function __construct (
16
+ private readonly string |Expression $ value ,
17
+ private readonly bool $ distinct = false ,
18
+ ) {
19
+ }
20
+
21
+ public function getValue (Grammar $ grammar ): string
10
22
{
11
- return 'count ' ;
23
+ $ value = $ this ->stringize ($ grammar , $ this ->value );
24
+
25
+ return match ($ this ->distinct ) {
26
+ true => "count(distinct {$ value }) " ,
27
+ false => "count( {$ value }) " ,
28
+ };
12
29
}
13
30
}
Original file line number Diff line number Diff line change 13
13
->toBeSqlite ('count("val") ' )
14
14
->toBeSqlsrv ('count([val]) ' );
15
15
16
+ it ('can aggregate a column by COUNT with DISTINCT ' )
17
+ ->expect (new Count ('val ' , true ))
18
+ ->toBeExecutable (['val int ' ])
19
+ ->toBeMysql ('count(distinct `val`) ' )
20
+ ->toBePgsql ('count(distinct "val") ' )
21
+ ->toBeSqlite ('count(distinct "val") ' )
22
+ ->toBeSqlsrv ('count(distinct [val]) ' );
23
+
16
24
it ('can aggregate an expression by COUNT ' )
17
25
->expect (new Count (new Expression (1 )))
18
26
->toBeExecutable ()
19
27
->toBeMysql ('count(1) ' )
20
28
->toBePgsql ('count(1) ' )
21
29
->toBeSqlite ('count(1) ' )
22
30
->toBeSqlsrv ('count(1) ' );
31
+
32
+ it ('can aggregate an expression by COUNT with DISTINCT ' )
33
+ ->expect (new Count (new Expression (1 ), true ))
34
+ ->toBeExecutable ()
35
+ ->toBeMysql ('count(distinct 1) ' )
36
+ ->toBePgsql ('count(distinct 1) ' )
37
+ ->toBeSqlite ('count(distinct 1) ' )
38
+ ->toBeSqlsrv ('count(distinct 1) ' );
You can’t perform that action at this time.
0 commit comments