Skip to content

Commit e5347b2

Browse files
committed
StringAgg now allow sub-request/function as first parameter
it's especially useful when combined with CAST() method
1 parent 67f32c1 commit e5347b2

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Query/Postgresql/StringAgg.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
/**
1212
* @author Roberto Júnior <[email protected]>
1313
* @author Vaskevich Eugeniy <[email protected]>
14+
* @author Allan Simon <[email protected]>
1415
*/
1516
class StringAgg extends FunctionNode
1617
{
@@ -34,7 +35,7 @@ public function parse(Parser $parser): void
3435
$this->isDistinct = true;
3536
}
3637

37-
$this->expression = $parser->PathExpression(PathExpression::TYPE_STATE_FIELD);
38+
$this->expression = $parser->ArithmeticPrimary();
3839
$parser->match(Lexer::T_COMMA);
3940
$this->delimiter = $parser->StringPrimary();
4041

@@ -50,7 +51,7 @@ public function getSql(SqlWalker $sqlWalker): string
5051
return \sprintf(
5152
'string_agg(%s%s, %s%s)',
5253
($this->isDistinct ? 'DISTINCT ' : ''),
53-
$sqlWalker->walkPathExpression($this->expression),
54+
$this->expression->dispatch($sqlWalker),
5455
$sqlWalker->walkStringPrimary($this->delimiter),
5556
($this->orderBy ? $sqlWalker->walkOrderByClause($this->orderBy) : '')
5657
);

tests/Query/Postgresql/StringAggTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,17 @@ public function testStringAggFullDeclaration()
7171

7272
$this->assertEquals($expected, $queryBuilder->getQuery()->getSQL());
7373
}
74+
75+
public function testStringAggWithInnerFunction()
76+
{
77+
$queryBuilder = new QueryBuilder($this->entityManager);
78+
$queryBuilder
79+
->select("string_agg(DISTINCT LOWER(bpt.latitude), '-' ORDER BY bpt.latitude DESC)")
80+
->from('DoctrineExtensions\Tests\Entities\BlogPost', 'bpt')
81+
->groupBy('bpt.created');
82+
83+
$expected = 'SELECT string_agg(DISTINCT LOWER(b0_.latitude), \'-\' ORDER BY b0_.latitude DESC) AS sclr_0 FROM BlogPost b0_ GROUP BY b0_.created';
84+
85+
$this->assertEquals($expected, $queryBuilder->getQuery()->getSQL());
86+
}
7487
}

0 commit comments

Comments
 (0)