Skip to content

Commit

Permalink
add support for split_part function (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
syl20b authored Oct 10, 2024
1 parent b9fe693 commit 3bc985b
Show file tree
Hide file tree
Showing 3 changed files with 56 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 @@ -76,6 +76,7 @@
| regexp_replace (with flags) | FLAGGED_REGEXP_REPLACE | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\FlaggedRegexpReplace` |
| regexp_replace (with no flags) | REGEXP_REPLACE | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\RegexpReplace` |
| row_to_json | ROW_TO_JSON | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\RowToJson` |
| split_part | SPLIT_PART | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\SplitPart` |
| starts_with | STARTS_WITH | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\StartsWith` |
| string_agg | STRING_AGG | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\StringAgg` |
| string_to_array | STRING_TO_ARRAY | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\StringToArray` |
Expand Down
21 changes: 21 additions & 0 deletions src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/SplitPart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

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

/**
* Implementation of PostgreSql SPLIT_PART().
*
* @see https://www.postgresql.org/docs/15/functions-string.html
*/
class SplitPart extends BaseFunction
{
protected function customiseFunction(): void
{
$this->setFunctionPrototype('split_part(%s, %s, %s)');
$this->addNodeMapping('StringPrimary');
$this->addNodeMapping('StringPrimary');
$this->addNodeMapping('SimpleArithmeticExpression');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\SplitPart;

class SplitPartTest extends TestCase
{
protected function getStringFunctions(): array
{
return [
'SPLIT_PART' => SplitPart::class,
];
}

protected function getExpectedSqlStatements(): array
{
return [
"SELECT split_part(c0_.text1, ',', 1) AS sclr_0 FROM ContainsTexts c0_",
"SELECT split_part(c0_.text2, '-', -2) AS sclr_0 FROM ContainsTexts c0_",
];
}

protected function getDqlStatements(): array
{
return [
\sprintf("SELECT SPLIT_PART(e.text1, ',', 1) FROM %s e", ContainsTexts::class),
\sprintf("SELECT SPLIT_PART(e.text2, '-', -2) FROM %s e", ContainsTexts::class),
];
}
}

0 comments on commit 3bc985b

Please sign in to comment.