-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonbAgg.php
45 lines (36 loc) · 1.25 KB
/
JsonbAgg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
declare(strict_types=1);
namespace Pfilsx\PostgreSQLDoctrine\ORM\Query\AST\Functions;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* Implementation of PostgreSql JSONB_AGG() function.
*
* @see https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
*
* @example JSONB_AGG(entity.field)
* @example JSONB_AGG(entity.field) FILTER (WHERE entity.field IS NOT NULL)
*/
final class JsonbAgg extends AbstractAggregateWithFilterFunction
{
private bool $distinct = false;
private Node $expr;
public function parseFunction(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$lexer = $parser->getLexer();
if ($lexer->isNextToken(Lexer::T_DISTINCT)) {
$parser->match(Lexer::T_DISTINCT);
$this->distinct = true;
}
$this->expr = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getFunctionSql(SqlWalker $sqlWalker): string
{
return sprintf('JSONB_AGG(%s%s)', $this->distinct ? 'DISTINCT ' : '', $this->expr->dispatch($sqlWalker));
}
}