Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround empty REGEXP pattern support for MySQL 5.x #1211

Merged
merged 2 commits into from
May 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/Persistence/Sql/Mysql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function _renderConditionRegexpOperator(bool $negated, string $sqlLeft

return $sqlLeft . ($negated ? ' not' : '') . ' regexp ' . (
$isMysql5x
? $sqlRight
? 'concat(' . $this->escapeStringLiteral('@?') . ', ' . $sqlRight . ')' // https://dbfiddle.uk/diAepf8V
: 'concat(' . $this->escapeStringLiteral('(?s)') . ', ' . $sqlRight . ')'
);
}
Expand Down
9 changes: 0 additions & 9 deletions tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,15 +833,6 @@ public function testRegexpCondition(string $type, bool $isBinary): void
#[DataProvider('provideNullLikeRegexpConditionCases')]
public function testNullLikeRegexpCondition(string $operator, ?bool $expectedResult, ?string $value, ?string $pattern, bool $negated): void
{
if ($this->getDatabasePlatform() instanceof MySQLPlatform
&& !MysqlConnection::isServerMariaDb($this->getConnection())
&& MysqlConnection::getServerMinorVersion($this->getConnection()) < 600
&& $operator === 'regexp' && $pattern === ''
) {
// https://dbfiddle.uk/diAepf8V
self::markTestIncomplete('MySQL 5.x does not support REGEXP with empty pattern');
}

if ($this->getDatabasePlatform() instanceof SQLServerPlatform && $operator === 'regexp') {
// https://devblogs.microsoft.com/azure-sql/introducing-regular-expression-regex-support-in-azure-sql-db/
self::markTestIncomplete('MSSQL has no REGEXP support yet');
Expand Down
20 changes: 13 additions & 7 deletions tests/Persistence/Sql/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,11 +878,15 @@ public function testWhereSpecialValues(): void
EOF,
(new SqliteQuery('[where]'))->where($this->e('sum({})', ['a']), 'like', $this->e('sum({})', ['b']))->render()[0]
);
foreach (['8.0.0', 'MariaDB-11.0.0'] as $serverVersion) {
foreach (['5.7.0', '8.0.0', 'MariaDB-11.0.0'] as $serverVersion) {
self::assertSame(
<<<'EOF'
where `name` like regexp_replace(:a, '\\\\\\\\|\\\\(?![_%])', '\\\\\\\\') escape '\\'
EOF,
$serverVersion === '5.7.0'
? <<<'EOF'
where `name` like replace(replace(replace(replace(replace(replace(replace(replace(:a, '\\\\', '\\\\*'), '\\_', '\\_*'), '\\%', '\\%*'), '\\', '\\\\'), '\\\\_*', '\\_'), '\\\\%*', '\\%'), '\\\\\\\\*', '\\\\'), '%\\', '%\\\\') escape '\\'
EOF
: <<<'EOF'
where `name` like regexp_replace(:a, '\\\\\\\\|\\\\(?![_%])', '\\\\\\\\') escape '\\'
EOF,
$this->createMysqlQuery($serverVersion, '[where]')->where('name', 'like', 'foo')->render()[0]
);
}
Expand Down Expand Up @@ -942,9 +946,11 @@ public function testWhereSpecialValues(): void
EOF,
(new SqliteQuery('[where]'))->where($this->e('sum({})', ['a']), 'regexp', $this->e('sum({})', ['b']))->render()[0]
);
foreach (['8.0.0', 'MariaDB-11.0.0'] as $serverVersion) {
foreach (['5.7.0', '8.0.0', 'MariaDB-11.0.0'] as $serverVersion) {
self::assertSame(
'where `name` regexp concat(\'(?s)\', :a)',
$serverVersion === '5.7.0'
? 'where `name` regexp concat(\'@?\', :a)'
: 'where `name` regexp concat(\'(?s)\', :a)',
$this->createMysqlQuery($serverVersion, '[where]')->where('name', 'regexp', 'foo')->render()[0]
);
}
Expand Down Expand Up @@ -1646,7 +1652,7 @@ public function testWith(): void
self::assertSame('with recursive "q11" ("foo", "qwe""ry") as (select "salary" from "salaries"),' . "\n"
. '"q12" ("bar", "baz") as (select "salary" from "salaries")' . "\n" . 'select * from "q11", "q12"', $q2->render()[0]);

// now test some more useful reql life query
// now test some more useful real life query
$quotes = $this->q()
->table('quotes')
->field('emp_id')
Expand Down
Loading