Skip to content

Commit

Permalink
allow to use case sensitive regexp matching
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 23, 2024
1 parent b593f88 commit 235b4ea
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Persistence/Sql/Mysql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function _renderConditionLikeOperator(bool $negated, string $sqlLeft,
}

#[\Override]
protected function _renderConditionRegexpOperator(bool $negated, string $sqlLeft, string $sqlRight): string
protected function _renderConditionRegexpOperator(bool $negated, string $sqlLeft, string $sqlRight, ?bool $caseSensitive = null): string
{
$serverVersion = $this->connection->getConnection()->getWrappedConnection()->getServerVersion(); // @phpstan-ignore-line
$isMysql5x = str_starts_with($serverVersion, '5.') && !str_contains($serverVersion, 'MariaDB');
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Sql/Oracle/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class Query extends BaseQuery
protected string $expressionClass = Expression::class;

#[\Override]
protected function _renderConditionRegexpOperator(bool $negated, string $sqlLeft, string $sqlRight): string
protected function _renderConditionRegexpOperator(bool $negated, string $sqlLeft, string $sqlRight, ?bool $caseSensitive = null): string
{
return ($negated ? 'not ' : '') . 'regexp_like(' . $sqlLeft . ', ' . $sqlRight
. ', ' . $this->escapeStringLiteral('in') . ')';
. ', ' . $this->escapeStringLiteral(($caseSensitive ? '' : 'i') . 'n') . ')';
}

#[\Override]
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Sql/Postgresql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ protected function _renderConditionLikeOperator(bool $negated, string $sqlLeft,

// needed for PostgreSQL v14 and lower
#[\Override]
protected function _renderConditionRegexpOperator(bool $negated, string $sqlLeft, string $sqlRight): string
protected function _renderConditionRegexpOperator(bool $negated, string $sqlLeft, string $sqlRight, ?bool $caseSensitive = null): string
{
return $sqlLeft . ' ' . ($negated ? '!' : '') . '~* ' . $sqlRight;
return $sqlLeft . ' ' . ($negated ? '!' : '') . '~' . ($caseSensitive ? '' : '*') . ' ' . $sqlRight;
}

#[\Override]
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Sql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,10 @@ protected function _renderConditionLikeOperator(bool $negated, string $sqlLeft,
. ' escape ' . $this->escapeStringLiteral('\\');
}

protected function _renderConditionRegexpOperator(bool $negated, string $sqlLeft, string $sqlRight): string
protected function _renderConditionRegexpOperator(bool $negated, string $sqlLeft, string $sqlRight, ?bool $caseSensitive = null): string
{
return ($negated ? 'not ' : '') . 'regexp_like(' . $sqlLeft . ', ' . $sqlRight
. ', ' . $this->escapeStringLiteral('is') . ')';
. ', ' . $this->escapeStringLiteral(($caseSensitive ? '' : 'i') . 's') . ')';
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Persistence/Sql/Sqlite/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function ($sqlLeft, $sqlRight) {
$res = '('
. parent::_renderConditionLikeOperator(false, $sqlLeft, $sqlRight)
. ' and ((' . $sqlLeft . ' = lower(' . $sqlLeft . ') and ' . $sqlLeft . ' = upper(' . $sqlLeft . '))'
. ' or ' . preg_replace('~(?<=\')i(?=s\'\)$)~', '', $this->_renderConditionRegexpOperator(
. ' or ' . $this->_renderConditionRegexpOperator(
false,
$sqlLeft,
'concat(' . $this->escapeStringLiteral('^') . ',' . $regexReplaceSqlFx(
Expand All @@ -128,8 +128,9 @@ function ($sqlLeft, $sqlRight) {
),
'(?<!\\\)(\\\\\\\)*\K\\\(?=[_%])',
''
) . ', ' . $this->escapeStringLiteral('$') . ')'
)) . '))';
) . ', ' . $this->escapeStringLiteral('$') . ')',
true
) . '))';

return $res;
}
Expand Down

0 comments on commit 235b4ea

Please sign in to comment.