Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Fix support for % and _ in like expression
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jul 20, 2023
1 parent 34a6085 commit 654b04e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
- Throw an exception when `Query\Builder::orderBy()` is used with invalid direction [#7](https://github.com/GromNaN/laravel-mongodb-private/pull/7) by [@GromNaN](https://github.com/GromNaN).
- Throw an exception when `Query\Builder::push()` is used incorrectly [#5](https://github.com/GromNaN/laravel-mongodb-private/pull/5) by [@GromNaN](https://github.com/GromNaN).
- Remove public property `Query\Builder::$paginating` [#15](https://github.com/GromNaN/laravel-mongodb-private/pull/15) by [@GromNaN](https://github.com/GromNaN).
- Fix support for `%` and `_` in `like` expression [#17](https://github.com/GromNaN/laravel-mongodb-private/pull/17) by [@GromNaN](https://github.com/GromNaN).
- Remove `ilike` and `regexp` operators, use `like` and `regex` instead [#17](https://github.com/GromNaN/laravel-mongodb-private/pull/17) by [@GromNaN](https://github.com/GromNaN).

## [3.9.2] - 2022-09-01
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ protected function compileWhereBasic(array $where): array
}

// Convert to regular expression.
$regex = preg_replace(['#(^|[^\\\])%#', '#(^|[^\\\])_#'], ['$1.*', '$1.'], preg_quote($value));
$regex = preg_replace(['#(^|[^\\\])%#', '#(^|[^\\\])_#', '#\\\(\\\)(%|_)#'], ['$1.*', '$1.', '$1$2'], preg_quote($value));
$value = new Regex('^'.$regex.'$', 'i');
} // Manipulate regex operations.
elseif (in_array($operator, ['regex', 'not regex'])) {
Expand Down
5 changes: 5 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ function (Builder $builder) {
fn (Builder $builder) => $builder->where('name', 'like', '^ac.me$'),
];

yield 'where like escaped \% \_' => [
['find' => [['name' => new Regex('^a\\%cm\\_e$', 'i')], []]],
fn (Builder $builder) => $builder->where('name', 'like', 'a\%cm\_e'),
];

yield 'where like %' => [
['find' => [['name' => new Regex('^.*ac.*me.*$', 'i')], []]],
fn (Builder $builder) => $builder->where('name', 'like', '%ac%me%'),
Expand Down

0 comments on commit 654b04e

Please sign in to comment.