1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Query\Lexer; |
8
|
|
|
use Doctrine\ORM\Query\Parser; |
9
|
|
|
use Doctrine\ORM\Query\SqlWalker; |
10
|
|
|
use Doctrine\ORM\Query\TokenType; |
11
|
|
|
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\DistinctableTrait; |
12
|
|
|
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\OrderableTrait; |
13
|
|
|
use MartinGeorgiev\Utils\DoctrineOrm; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Implementation of PostgreSQL ARRAY_AGG(). |
17
|
|
|
* |
18
|
|
|
* @see https://www.postgresql.org/docs/17/functions-aggregate.html |
19
|
|
|
* @since 1.4 |
20
|
|
|
* |
21
|
|
|
* @author Martin Georgiev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ArrayAgg extends BaseFunction |
24
|
|
|
{ |
25
|
|
|
use OrderableTrait; |
26
|
|
|
use DistinctableTrait; |
27
|
|
|
|
28
|
1 |
|
protected function customizeFunction(): void |
29
|
|
|
{ |
30
|
1 |
|
$this->setFunctionPrototype('array_agg(%s%s%s)'); |
31
|
1 |
|
$this->addNodeMapping('StringPrimary'); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
public function parse(Parser $parser): void |
35
|
|
|
{ |
36
|
1 |
|
$shouldUseLexer = DoctrineOrm::isPre219(); |
37
|
|
|
|
38
|
1 |
|
$this->customizeFunction(); |
39
|
|
|
|
40
|
1 |
|
$parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER); |
|
|
|
|
41
|
1 |
|
$parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS); |
|
|
|
|
42
|
|
|
|
43
|
1 |
|
$this->parseDistinctClause($parser); |
44
|
1 |
|
$this->expression = $parser->StringPrimary(); |
45
|
|
|
|
46
|
1 |
|
$this->parseOrderByClause($parser); |
47
|
|
|
|
48
|
1 |
|
$parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public function getSql(SqlWalker $sqlWalker): string |
52
|
|
|
{ |
53
|
1 |
|
$dispatched = [ |
54
|
1 |
|
$this->getOptionalDistinctClause(), |
55
|
1 |
|
$this->expression->dispatch($sqlWalker), |
56
|
1 |
|
$this->getOptionalOrderByClause($sqlWalker), |
57
|
1 |
|
]; |
58
|
|
|
|
59
|
1 |
|
return \vsprintf($this->functionPrototype, $dispatched); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|