-
-
Notifications
You must be signed in to change notification settings - Fork 56
feat: add support for xmlagg()
#318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/XmlAgg.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use Doctrine\ORM\Query\Lexer; | ||
| use Doctrine\ORM\Query\Parser; | ||
| use Doctrine\ORM\Query\SqlWalker; | ||
| use Doctrine\ORM\Query\TokenType; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\OrderableTrait; | ||
| use MartinGeorgiev\Utils\DoctrineOrm; | ||
|
|
||
| /** | ||
| * Implementation of PostgreSQL XMLAGG(). | ||
| * | ||
| * @see https://www.postgresql.org/docs/17/functions-aggregate.html | ||
| * @since 3.0 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| class XmlAgg extends BaseFunction | ||
| { | ||
| use OrderableTrait; | ||
|
|
||
| protected function customizeFunction(): void | ||
| { | ||
| $this->setFunctionPrototype('xmlagg(%s%s)'); | ||
| $this->addNodeMapping('StringPrimary'); | ||
| } | ||
|
|
||
| public function parse(Parser $parser): void | ||
| { | ||
| $shouldUseLexer = DoctrineOrm::isPre219(); | ||
|
|
||
| $this->customizeFunction(); | ||
|
|
||
| $parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER); | ||
| $parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS); | ||
|
|
||
| $this->expression = $parser->StringPrimary(); | ||
| $this->parseOrderByClause($parser); | ||
|
|
||
| $parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS); | ||
| } | ||
|
|
||
| public function getSql(SqlWalker $sqlWalker): string | ||
| { | ||
| $dispatched = [ | ||
| $this->expression->dispatch($sqlWalker), | ||
| $this->getOptionalOrderByClause($sqlWalker), | ||
| ]; | ||
|
|
||
| return \vsprintf($this->functionPrototype, $dispatched); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/XmlAggTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsTexts; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\XmlAgg; | ||
|
|
||
| class XmlAggTest extends TestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'XML_AGG' => XmlAgg::class, | ||
| ]; | ||
| } | ||
|
|
||
| protected function getExpectedSqlStatements(): array | ||
| { | ||
| return [ | ||
| 'basic usage' => 'SELECT xmlagg(c0_.text1) AS sclr_0 FROM ContainsTexts c0_', | ||
| 'with concatenation' => 'SELECT xmlagg(c0_.text1 || c0_.text2) AS sclr_0 FROM ContainsTexts c0_', | ||
| 'with ORDER BY' => 'SELECT xmlagg(c0_.text1 ORDER BY c0_.text1 ASC) AS sclr_0 FROM ContainsTexts c0_', | ||
| 'with ORDER BY DESC' => 'SELECT xmlagg(c0_.text1 ORDER BY c0_.text1 DESC) AS sclr_0 FROM ContainsTexts c0_', | ||
| ]; | ||
| } | ||
|
|
||
| protected function getDqlStatements(): array | ||
| { | ||
| return [ | ||
| 'basic usage' => \sprintf('SELECT XML_AGG(e.text1) FROM %s e', ContainsTexts::class), | ||
| 'with concatenation' => \sprintf('SELECT XML_AGG(CONCAT(e.text1, e.text2)) FROM %s e', ContainsTexts::class), | ||
| 'with ORDER BY' => \sprintf('SELECT XML_AGG(e.text1 ORDER BY e.text1) FROM %s e', ContainsTexts::class), | ||
| 'with ORDER BY DESC' => \sprintf('SELECT XML_AGG(e.text1 ORDER BY e.text1 DESC) FROM %s e', ContainsTexts::class), | ||
| ]; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Missing
TestCaseimport.You extend
TestCasebut it's not explicitly imported here. Ensure you have the necessary import (e.g.,use PHPUnit\Framework\TestCase;) or are otherwise inheriting correctly.🏁 Script executed:
Length of output: 329
Attention: Missing
TestCaseImportThe file
tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/XmlAggTest.phpextendsTestCase, but it currently does not include an explicit import for it. To ensure clarity and avoid potential issues with autoloading, please add the following import at the top of the file (below the namespace declaration):tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/XmlAggTest.phpPHPUnit\Framework\TestCase.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you ignoring that all tests in this namespace rely on
Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\TestCase?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
@martin-georgiev You're absolutely right, and I apologize for the oversight! I see that tests in this namespace are extending the custom
Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\TestCaseclass rather than PHPUnit's TestCase. Since theTestCaseclass is in the same namespace, no import statement is needed. Thank you for the correction.🏁 Script executed:
Length of output: 968
✏️ Learnings added
Code Analysis Tools
Tool Analysis Results
CodeRabbit ran static analysis tools on your files and found the following:
No specific issues were found in your files. You can proceed with your changes.