Skip to content

Commit

Permalink
fix PostgreSQL LIKE binary
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 29, 2024
1 parent 9d5206a commit bffbc5d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Persistence/Sql/Postgresql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function ($sqlLeft, $sqlRight) use ($makeSqlFx) {
// replace backslash in "\xddd" for LIKE/REGEXP
$res = 'regexp_replace(' . $res . ', '
. $this->escapeStringLiteral('(?<!\\\)((\\\\\\\)*)\\\(\d\d\d)') . ', '
. $this->escapeStringLiteral('\1~~bytea~\3~~') . ', '
. $this->escapeStringLiteral("\\1\u{00a9}\\3\u{00a9}") . ', '
. $this->escapeStringLiteral('g') . ')';

// revert double backslashes
Expand Down
5 changes: 0 additions & 5 deletions tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,6 @@ public function testRegexpCondition(string $type, bool $isBinary): void

self::assertSame([1], $findIdsRegexFx('name', 'John'));
self::assertSame($isBinary ? [] : [1], $findIdsRegexFx('name', 'john'));

if ($this->getDatabasePlatform() instanceof PostgreSQLPlatform && $isBinary) {
return; // TODO
}

self::assertSame($isBinaryMysql8x ? [] : [13], $findIdsRegexFx('name', 'heiß'));
self::assertSame($isBinary ? [] : [13], $findIdsRegexFx('name', 'Heiß'));
self::assertSame([1], $findIdsRegexFx('name', 'Joh'));
Expand Down
4 changes: 2 additions & 2 deletions tests/Persistence/Sql/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ public function testWhereSpecialValues(): void
}
self::assertSame(
<<<'EOF'
where case when pg_typeof("name") = 'bytea'::regtype then replace(regexp_replace(encode(case when pg_typeof("name") = 'bytea'::regtype then decode(case when pg_typeof("name") = 'bytea'::regtype then replace(substring(cast("name" as text) from 3), chr(92), repeat(chr(92), 2)) else '' end, 'hex') else cast(replace(cast("name" as text), chr(92), repeat(chr(92), 2)) as bytea) end, 'escape'), '(?<!\\)((\\\\)*)\\(\d\d\d)', '\1~~bytea~\3~~', 'g'), repeat(chr(92), 2), chr(92)) like regexp_replace(replace(regexp_replace(encode(cast(replace(cast(:a as text), chr(92), repeat(chr(92), 2)) as bytea), 'escape'), '(?<!\\)((\\\\)*)\\(\d\d\d)', '\1~~bytea~\3~~', 'g'), repeat(chr(92), 2), chr(92)), '(\\[\\_%])|(\\)', '\1\2\2', 'g') escape chr(92) else cast("name" as citext) like regexp_replace(:a, '(\\[\\_%])|(\\)', '\1\2\2', 'g') escape chr(92) end
where case when pg_typeof("name") = 'bytea'::regtype then replace(regexp_replace(encode(case when pg_typeof("name") = 'bytea'::regtype then decode(case when pg_typeof("name") = 'bytea'::regtype then replace(substring(cast("name" as text) from 3), chr(92), repeat(chr(92), 2)) else '' end, 'hex') else cast(replace(cast("name" as text), chr(92), repeat(chr(92), 2)) as bytea) end, 'escape'), '(?<!\\)((\\\\)*)\\(\d\d\d)', '\1©\3©', 'g'), repeat(chr(92), 2), chr(92)) like regexp_replace(replace(regexp_replace(encode(cast(replace(cast(:a as text), chr(92), repeat(chr(92), 2)) as bytea), 'escape'), '(?<!\\)((\\\\)*)\\(\d\d\d)', '\1©\3©', 'g'), repeat(chr(92), 2), chr(92)), '(\\[\\_%])|(\\)', '\1\2\2', 'g') escape chr(92) else cast("name" as citext) like regexp_replace(:a, '(\\[\\_%])|(\\)', '\1\2\2', 'g') escape chr(92) end
EOF,
(new PostgresqlQuery('[where]'))->where('name', 'like', 'foo')->render()[0]
);
Expand Down Expand Up @@ -925,7 +925,7 @@ public function testWhereSpecialValues(): void
}
self::assertSame(
<<<'EOF'
where case when pg_typeof("name") = 'bytea'::regtype then replace(regexp_replace(encode(case when pg_typeof("name") = 'bytea'::regtype then decode(case when pg_typeof("name") = 'bytea'::regtype then replace(substring(cast("name" as text) from 3), chr(92), repeat(chr(92), 2)) else '' end, 'hex') else cast(replace(cast("name" as text), chr(92), repeat(chr(92), 2)) as bytea) end, 'escape'), '(?<!\\)((\\\\)*)\\(\d\d\d)', '\1~~bytea~\3~~', 'g'), repeat(chr(92), 2), chr(92)) ~ replace(regexp_replace(encode(cast(replace(cast(:a as text), chr(92), repeat(chr(92), 2)) as bytea), 'escape'), '(?<!\\)((\\\\)*)\\(\d\d\d)', '\1~~bytea~\3~~', 'g'), repeat(chr(92), 2), chr(92)) else cast("name" as citext) ~ :a end
where case when pg_typeof("name") = 'bytea'::regtype then replace(regexp_replace(encode(case when pg_typeof("name") = 'bytea'::regtype then decode(case when pg_typeof("name") = 'bytea'::regtype then replace(substring(cast("name" as text) from 3), chr(92), repeat(chr(92), 2)) else '' end, 'hex') else cast(replace(cast("name" as text), chr(92), repeat(chr(92), 2)) as bytea) end, 'escape'), '(?<!\\)((\\\\)*)\\(\d\d\d)', '\1©\3©', 'g'), repeat(chr(92), 2), chr(92)) ~ replace(regexp_replace(encode(cast(replace(cast(:a as text), chr(92), repeat(chr(92), 2)) as bytea), 'escape'), '(?<!\\)((\\\\)*)\\(\d\d\d)', '\1©\3©', 'g'), repeat(chr(92), 2), chr(92)) else cast("name" as citext) ~ :a end
EOF,
(new PostgresqlQuery('[where]'))->where('name', 'regexp', 'foo')->render()[0]
);
Expand Down

0 comments on commit bffbc5d

Please sign in to comment.