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

Commit

Permalink
Fix like to be case-sensitive and support not ilike
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jul 10, 2023
1 parent 1370e57 commit 06d3624
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
12 changes: 5 additions & 7 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Builder extends BaseBuilder
'not like',
'between',
'ilike',
'not ilike',
'&',
'|',
'^',
Expand Down Expand Up @@ -1070,12 +1071,9 @@ protected function compileWhereBasic(array $where): array
extract($where);

// Replace like or not like with a Regex instance.
if (in_array($operator, ['like', 'not like'])) {
if ($operator === 'not like') {
$operator = 'not';
} else {
$operator = '=';
}
if (in_array($operator, ['like', 'not like', 'ilike', 'not ilike'])) {
$flags = str_ends_with($operator, 'ilike') ? 'i' : '';
$operator = str_starts_with($operator, 'not') ? 'not' : '=';

// Convert to regular expression.
$regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value));
Expand All @@ -1088,7 +1086,7 @@ protected function compileWhereBasic(array $where): array
$regex .= '$';
}

$value = new Regex($regex, 'i');
$value = new Regex($regex, $flags);
} // Manipulate regexp operations.
elseif (in_array($operator, ['regexp', 'not regexp', 'regex', 'not regex'])) {
// Automatically convert regular expression strings to Regex objects.
Expand Down
47 changes: 36 additions & 11 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1022,25 +1022,50 @@ public function testWheresWithArrayValue()
]], $builder->toMql());
}


/** @see DatabaseQueryBuilderTest::testDateBasedWheresAcceptsTwoArguments() */
public function testDateBasedWheresAcceptsTwoArguments()
/** @see DatabaseQueryBuilderTest::testWhereLikePostgres() */
public function testWhereLike()
{
$builder = $this->getBuilder();
$builder->whereDate('created_at', 1);
$this->assertSame('select * from `users` where date(`created_at`) = ?', $builder->toSql());
$builder->where('id', 'like', '1');
$this->assertEquals(['find' => [
['id' => new Regex('^1$')],
['typeMap' => ['root' => 'array', 'document' => 'array']]],
], $builder->toMql());

$builder = $this->getBuilder();
$builder->whereDay('created_at', 1);
$this->assertSame('select * from `users` where day(`created_at`) = ?', $builder->toSql());
$builder->where('id', 'like', '%{#}%');
$this->assertEquals(['find' => [
['id' => new Regex('.*\{\#\}.*')],
['typeMap' => ['root' => 'array', 'document' => 'array']]],
], $builder->toMql());

$builder = $this->getBuilder();
$builder->whereMonth('created_at', 1);
$this->assertSame('select * from `users` where month(`created_at`) = ?', $builder->toSql());
$builder->where('id', 'LIKE', '1');
$this->assertEquals(['find' => [
['id' => new Regex('^1$')],
['typeMap' => ['root' => 'array', 'document' => 'array']]],
], $builder->toMql());

$builder = $this->getBuilder();
$builder->whereYear('created_at', 1);
$this->assertSame('select * from `users` where year(`created_at`) = ?', $builder->toSql());
$builder->where('id', 'ilike', '1');
$this->assertEquals(['find' => [
['id' => new Regex('^1$', 'i')],
['typeMap' => ['root' => 'array', 'document' => 'array']]],
], $builder->toMql());

$builder = $this->getBuilder();
$builder->where('id', 'not like', '1');
$this->assertEquals(['find' => [
['id' => ['$not' => new Regex('^1$')]],
['typeMap' => ['root' => 'array', 'document' => 'array']]],
], $builder->toMql());

$builder = $this->getBuilder();
$builder->where('id', 'not ilike', '1');
$this->assertEquals(['find' => [
['id' => ['$not' => new Regex('^1$', 'i')]],
['typeMap' => ['root' => 'array', 'document' => 'array']]],
], $builder->toMql());
}

/** @see DatabaseQueryBuilderTest::testWhereBetweens() */
Expand Down

0 comments on commit 06d3624

Please sign in to comment.