From c3e1766528434984497b0e7fa970afe3592389b2 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 18 Aug 2024 00:27:52 +0400 Subject: [PATCH] Add support for string concatenation (using `||` operator) (#226) Co-authored-by: Martin Georgiev --- docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md | 1 + docs/INTEGRATING-WITH-LARAVEL.md | 1 + docs/INTEGRATING-WITH-SYMFONY.md | 1 + .../ORM/Query/AST/Functions/StrConcat.php | 23 +++++++++++++ .../ORM/Query/AST/Functions/StrConcatTest.php | 32 +++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/StrConcat.php create mode 100644 tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/StrConcatTest.php diff --git a/docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md b/docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md index 57d3d862..c0bfe8b2 100644 --- a/docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md +++ b/docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md @@ -22,6 +22,7 @@ | !~ | NOT_REGEXP | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\NotRegexp` | | !~* | NOT_IREGEXP | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\NotIRegexp` | | @@ | TSMATCH | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Tsmatch` | +| \|\| | STRCONCAT | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\StrConcat` | # Available functions diff --git a/docs/INTEGRATING-WITH-LARAVEL.md b/docs/INTEGRATING-WITH-LARAVEL.md index 3b5c63de..6124cb18 100644 --- a/docs/INTEGRATING-WITH-LARAVEL.md +++ b/docs/INTEGRATING-WITH-LARAVEL.md @@ -158,6 +158,7 @@ return [ 'REGEXP_LIKE' => MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\RegexpLike::class, 'FLAGGED_REGEXP_MATCH' => MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\FlaggedRegexpMatch::class, 'REGEXP_MATCH' => MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\RegexpMatch::class, + 'STRCONCAT' => MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\StrConcat::class, // the `||` operator ], ... diff --git a/docs/INTEGRATING-WITH-SYMFONY.md b/docs/INTEGRATING-WITH-SYMFONY.md index f4e17b3a..40fc7a60 100644 --- a/docs/INTEGRATING-WITH-SYMFONY.md +++ b/docs/INTEGRATING-WITH-SYMFONY.md @@ -154,4 +154,5 @@ doctrine: REGEXP_LIKE: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\FlaggedRegexpLike FLAGGED_REGEXP_MATCH: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\FlaggedRegexpMatch REGEXP_MATCH: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\RegexpMatch + STRCONCAT: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\StrConcat ``` diff --git a/src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/StrConcat.php b/src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/StrConcat.php new file mode 100644 index 00000000..6071ac97 --- /dev/null +++ b/src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/StrConcat.php @@ -0,0 +1,23 @@ + + */ +class StrConcat extends BaseFunction +{ + protected function customiseFunction(): void + { + $this->setFunctionPrototype('(%s || %s)'); + $this->addNodeMapping('StringPrimary'); + $this->addNodeMapping('StringPrimary'); + } +} diff --git a/tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/StrConcatTest.php b/tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/StrConcatTest.php new file mode 100644 index 00000000..c75939df --- /dev/null +++ b/tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/StrConcatTest.php @@ -0,0 +1,32 @@ + StrConcat::class, + ]; + } + + protected function getExpectedSqlStatements(): array + { + return [ + "SELECT (c0_.text1 || 'text2') AS sclr_0 FROM ContainsTexts c0_", + ]; + } + + protected function getDqlStatements(): array + { + return [ + \sprintf("SELECT STRCONCAT(e.text1, 'text2') FROM %s e", ContainsTexts::class), + ]; + } +}