Skip to content

Commit

Permalink
fix LIKE query issue. (#44)
Browse files Browse the repository at this point in the history
* fix LIKE issue

* code review comments

Co-authored-by: Yasmine Mortazavi <[email protected]>
  • Loading branch information
YazMortaz and Yasmine Mortazavi authored Mar 24, 2020
1 parent ea3710a commit 30fbdd2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Expressions/BinaryOperatorExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ public function evaluate(row $row, AsyncMysqlConnection $conn): mixed {
$pattern = Str\strip_suffix($pattern, '%');
}

// escape all + characters
$pattern = \preg_quote($pattern, '/');

// replace only unescaped % and _ characters to make regex
$pattern = Regex\replace($pattern, re"/(?<!\\\)%/", '.*?');
$pattern = Regex\replace($pattern, re"/(?<!\\\)_/", '.');
Expand Down
4 changes: 2 additions & 2 deletions tests/SelectExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ final class SelectExpressionTest extends HackTest {
public async function testLike(): Awaitable<void> {
$conn = static::$conn as nonnull;
$results = await $conn->query(
"SELECT 'foo' LIKE 'foo' as test1, 'foobarbaz' like '%bar%' test2, 'foobaz' LIKE 'foo_az' test3, 'foobarqux' like 'foo%' test4, 'foobarqux' LIKE '%qux' test5, 'blahfoobarbazqux blah' LIKE '%foo%qux%' test6, 'blegh' LIKE '%foo%' test7",
"SELECT 'foo' LIKE 'foo' as test1, 'foobarbaz' like '%bar%' test2, 'foobaz' LIKE 'foo_az' test3, 'foobarqux' like 'foo%' test4, 'foobarqux' LIKE '%qux' test5, 'blahfoobarbazqux blah' LIKE '%foo%qux%' test6, 'blegh' LIKE '%foo%' test7, 'foo+bar' LIKE '%foo+bar%' test8, 'foo/bar' LIKE '%foo/bar%' test9",
);
expect($results->rows())->toBeSame(
vec[
dict['test1' => 1, 'test2' => 1, 'test3' => 1, 'test4' => 1, 'test5' => 1, 'test6' => 1, 'test7' => 0],
dict['test1' => 1, 'test2' => 1, 'test3' => 1, 'test4' => 1, 'test5' => 1, 'test6' => 1, 'test7' => 0, 'test8' => 1, 'test9' => 1],
],
);
}
Expand Down

0 comments on commit 30fbdd2

Please sign in to comment.