1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Query\AST\Node; |
8
|
|
|
use Doctrine\ORM\Query\AST\OrderByClause; |
9
|
|
|
use Doctrine\ORM\Query\Lexer; |
10
|
|
|
use Doctrine\ORM\Query\Parser; |
11
|
|
|
use Doctrine\ORM\Query\SqlWalker; |
12
|
|
|
use Doctrine\ORM\Query\TokenType; |
13
|
|
|
use MartinGeorgiev\Utils\DoctrineOrm; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Implementation of PostgreSql STRING_AGG(). |
17
|
|
|
* |
18
|
|
|
* @see https://www.postgresql.org/docs/9.5/functions-aggregate.html |
19
|
|
|
* @since 1.4 |
20
|
|
|
* |
21
|
|
|
* @author Martin Georgiev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class StringAgg extends BaseFunction |
24
|
|
|
{ |
25
|
|
|
private bool $isDistinct = false; |
26
|
|
|
|
27
|
|
|
private Node $expression; |
28
|
|
|
|
29
|
|
|
private Node $delimiter; |
30
|
|
|
|
31
|
|
|
private OrderByClause $orderByClause; |
32
|
|
|
|
33
|
|
|
protected function customiseFunction(): void |
34
|
|
|
{ |
35
|
|
|
$this->setFunctionPrototype('string_agg(%s%s, %s%s)'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function parse(Parser $parser): void |
39
|
|
|
{ |
40
|
|
|
$shouldUseLexer = DoctrineOrm::isPre219(); |
41
|
|
|
|
42
|
|
|
$this->customiseFunction(); |
43
|
|
|
|
44
|
|
|
$parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER); |
|
|
|
|
45
|
|
|
$parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$lexer = $parser->getLexer(); |
48
|
|
|
if ($lexer->isNextToken($shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT)) { |
|
|
|
|
49
|
|
|
$parser->match($shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT); |
50
|
|
|
$this->isDistinct = true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->expression = $parser->StringPrimary(); |
54
|
|
|
$parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA); |
|
|
|
|
55
|
|
|
$this->delimiter = $parser->StringPrimary(); |
56
|
|
|
|
57
|
|
|
if ($lexer->isNextToken($shouldUseLexer ? Lexer::T_ORDER : TokenType::T_ORDER)) { |
|
|
|
|
58
|
|
|
$this->orderByClause = $parser->OrderByClause(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getSql(SqlWalker $sqlWalker): string |
65
|
|
|
{ |
66
|
|
|
$dispatched = [ |
67
|
|
|
$this->isDistinct ? 'distinct ' : '', |
68
|
|
|
$this->expression->dispatch($sqlWalker), |
69
|
|
|
$this->delimiter->dispatch($sqlWalker), |
70
|
|
|
isset($this->orderByClause) ? $this->orderByClause->dispatch($sqlWalker) : '', |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
return \vsprintf($this->functionPrototype, $dispatched); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|