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