Skip to content

fix: whereNotLike misses the not in the sql query #105

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

Merged
merged 7 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 4 additions & 4 deletions src/Query/GrammarWhere.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function whereAny(Builder $query, $where): string
*/
public function whereLike(Builder $query, $where): string
{
return match ($where['caseSensitive']) {
true => "{$this->wrap($where['column'])} like {$this->parameter($where['value'])}",
false => "{$this->wrap($where['column'])} ilike {$this->parameter($where['value'])}",
};
$operator = $where['not'] ? 'not ' : '';
$operator .= $where['caseSensitive'] ? 'like' : 'ilike';

return "{$this->wrap($where['column'])}::text {$operator} {$this->parameter($where['value'])}";
}

/**
Expand Down
40 changes: 38 additions & 2 deletions tests/Query/WhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testOrWhereLike(): void
$this->getConnection()->table('example')->orWhereLike('str', 'OamekKIC', true)->orWhereLike('str', 'HmC3xURl', true)->get();
});
$this->assertEquals(
['select * from "example" where "str" ilike ? or "str" ilike ?', 'select * from "example" where "str" like ? or "str" like ?'],
['select * from "example" where "str"::text ilike ? or "str"::text ilike ?', 'select * from "example" where "str"::text like ? or "str"::text like ?'],
array_column($queries, 'query'),
);
$this->assertEquals(
Expand Down Expand Up @@ -172,6 +172,24 @@ public function testOrWhereNotBoolean(): void
);
}

public function testOrWhereNotLike(): void
{
$this->getConnection()->unprepared('CREATE TABLE example (str text)');

$queries = $this->withQueryLog(function (): void {
$this->getConnection()->table('example')->orWhereNotLike('str', 'AERLFW4s')->orWhereNotLike('str', 'sPtXxruW')->get();
$this->getConnection()->table('example')->orWhereNotLike('str', 'EWnGRRc5', true)->orWhereNotLike('str', 'wihuWcph', true)->get();
});
$this->assertEquals(
['select * from "example" where "str"::text not ilike ? or "str"::text not ilike ?', 'select * from "example" where "str"::text not like ? or "str"::text not like ?'],
array_column($queries, 'query'),
);
$this->assertEquals(
[['AERLFW4s', 'sPtXxruW'], ['EWnGRRc5', 'wihuWcph']],
array_column($queries, 'bindings'),
);
}

public function testWhereAllValues(): void
{
$this->getConnection()->unprepared('CREATE TABLE example (val text)');
Expand Down Expand Up @@ -258,7 +276,7 @@ public function testWhereLike(): void
$this->getConnection()->table('example')->whereLike('str', 'IcuC5Cqz', true)->get();
});
$this->assertEquals(
['select * from "example" where "str" ilike ?', 'select * from "example" where "str" like ?'],
['select * from "example" where "str"::text ilike ?', 'select * from "example" where "str"::text like ?'],
array_column($queries, 'query'),
);
$this->assertEquals(
Expand Down Expand Up @@ -330,4 +348,22 @@ public function testWhereNotBoolean(): void
array_column($queries, 'query'),
);
}

public function testWhereNotLike(): void
{
$this->getConnection()->unprepared('CREATE TABLE example (str text)');

$queries = $this->withQueryLog(function (): void {
$this->getConnection()->table('example')->whereNotLike('str', 'mgb7DtKz')->get();
$this->getConnection()->table('example')->whereNotLike('str', 'tpx6Bxpm', true)->get();
});
$this->assertEquals(
['select * from "example" where "str"::text not ilike ?', 'select * from "example" where "str"::text not like ?'],
array_column($queries, 'query'),
);
$this->assertEquals(
[['mgb7DtKz'], ['tpx6Bxpm']],
array_column($queries, 'bindings'),
);
}
}
Loading