Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Query/Postgresql/RegexpReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ class RegexpReplace extends FunctionNode

private $replace;

private $flags;

public function getSql(SqlWalker $sqlWalker): string
{
return 'REGEXP_REPLACE('.$this->string->dispatch($sqlWalker).', '.$this->search->dispatch($sqlWalker).', '.$this->replace->dispatch($sqlWalker).')';
$sql = 'REGEXP_REPLACE('.$this->string->dispatch($sqlWalker).', '.$this->search->dispatch($sqlWalker).', '.$this->replace->dispatch($sqlWalker);
if ($this->flags) {
$sql .= ', '.$this->flags->dispatch($sqlWalker);
}
$sql .= ')';

return $sql;
}

public function parse(Parser $parser): void
Expand All @@ -35,6 +43,11 @@ public function parse(Parser $parser): void
$parser->match(Lexer::T_COMMA);
$this->replace = $parser->StringExpression();

if ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {
$parser->match(Lexer::T_COMMA);
$this->flags = $parser->StringExpression();
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
11 changes: 11 additions & 0 deletions tests/Query/Postgresql/RegexpReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ public function testRegexpReplace(): void
$q->getSql()
);
}

public function testRegexpReplaceWithFlags(): void
{
$dql = "SELECT p FROM DoctrineExtensions\Tests\Entities\Set p WHERE REGEXP_REPLACE(p.set, '\d', 'X', 'g') LIKE 'testXXX'";
$q = $this->entityManager->createQuery($dql);

$this->assertEquals(
"SELECT s0_.id AS id_0, s0_.set AS set_1 FROM Set s0_ WHERE REGEXP_REPLACE(s0_.set, '\d', 'X', 'g') LIKE 'testXXX'",
$q->getSql()
);
}
}