Skip to content

Commit

Permalink
Add support for string concatenation (using || operator) (#226)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Georgiev <martin-georgiev@users.noreply.github.com>
  • Loading branch information
samdark and martin-georgiev authored Aug 17, 2024
1 parent 75b1fb5 commit c3e1766
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docs/INTEGRATING-WITH-LARAVEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
],

...
Expand Down
1 change: 1 addition & 0 deletions docs/INTEGRATING-WITH-SYMFONY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
23 changes: 23 additions & 0 deletions src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/StrConcat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

/**
* Implementation of PostgreSQL's string concatenation operator (using `||`).
*
* @see https://www.postgresql.org/docs/15/functions-string.html
* @since 2.6.0
*
* @author Alexander Makarov <sam@rmcreative.ru>
*/
class StrConcat extends BaseFunction
{
protected function customiseFunction(): void
{
$this->setFunctionPrototype('(%s || %s)');
$this->addNodeMapping('StringPrimary');
$this->addNodeMapping('StringPrimary');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\StrConcat;

class StrConcatTest extends TestCase
{
protected function getStringFunctions(): array
{
return [
'STRCONCAT' => 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),
];
}
}

0 comments on commit c3e1766

Please sign in to comment.