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\Lexer; |
9
|
|
|
use Doctrine\ORM\Query\Parser; |
10
|
|
|
use Doctrine\ORM\Query\SqlWalker; |
11
|
|
|
use Doctrine\ORM\Query\TokenType; |
12
|
|
|
use MartinGeorgiev\Utils\DoctrineOrm; |
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 BaseOrderableFunction |
23
|
|
|
{ |
24
|
|
|
private bool $isDistinct = false; |
25
|
|
|
private Node $delimiter; |
26
|
|
|
|
27
|
|
|
protected function customiseFunction(): void |
28
|
|
|
{ |
29
|
|
|
$this->setFunctionPrototype('string_agg(%s%s, %s%s)'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function parseFunction(Parser $parser): void |
33
|
|
|
{ |
34
|
|
|
$shouldUseLexer = DoctrineOrm::isPre219(); |
35
|
|
|
$lexer = $parser->getLexer(); |
36
|
|
|
|
37
|
|
|
if ($lexer->isNextToken($shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT)) { |
|
|
|
|
38
|
|
|
$parser->match($shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT); |
39
|
|
|
$this->isDistinct = true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->expression = $parser->StringPrimary(); |
43
|
|
|
|
44
|
|
|
$parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->delimiter = $parser->StringPrimary(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getSql(SqlWalker $sqlWalker): string |
50
|
|
|
{ |
51
|
|
|
$dispatched = [ |
52
|
|
|
$this->isDistinct ? 'distinct ' : '', |
53
|
|
|
$this->expression->dispatch($sqlWalker), |
54
|
|
|
$this->delimiter->dispatch($sqlWalker), |
55
|
|
|
$this->getOptionalOrderByClause($sqlWalker), |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
return \vsprintf($this->functionPrototype, $dispatched); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|