Skip to content

Commit ec0f372

Browse files
mmachatschekMarkus Machatschek
authored andcommitted
fix: whereNotLike misses the not in the sql query
1 parent ecc5753 commit ec0f372

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/Query/GrammarWhere.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function whereAny(Builder $query, $where): string
5050
*/
5151
public function whereLike(Builder $query, $where): string
5252
{
53-
return match ($where['caseSensitive']) {
54-
true => "{$this->wrap($where['column'])} like {$this->parameter($where['value'])}",
55-
false => "{$this->wrap($where['column'])} ilike {$this->parameter($where['value'])}",
56-
};
53+
$operator = $where['not'] ? 'not ' : '';
54+
$operator .= $where['caseSensitive'] ? 'like' : 'ilike';
55+
56+
return "{$this->wrap($where['column'])} {$operator} {$this->parameter($where['value'])}";
5757
}
5858

5959
/**

tests/Query/WhereTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ public function testOrWhereLike(): void
108108
);
109109
}
110110

111+
public function testOrWhereNotLike(): void
112+
{
113+
$this->getConnection()->unprepared('CREATE TABLE example (str text)');
114+
115+
$queries = $this->withQueryLog(function (): void {
116+
$this->getConnection()->table('example')->orWhereNotLike('str', 'ZsbBUJmR')->orWhereNotLike('str', '7Cc1Uf8t')->get();
117+
$this->getConnection()->table('example')->orWhereNotLike('str', 'OamekKIC', true)->orWhereNotLike('str', 'HmC3xURl', true)->get();
118+
});
119+
$this->assertEquals(
120+
['select * from "example" where "str" not ilike ? or "str" not ilike ?', 'select * from "example" where "str" not like ? or "str" not like ?'],
121+
array_column($queries, 'query'),
122+
);
123+
$this->assertEquals(
124+
[['ZsbBUJmR', '7Cc1Uf8t'], ['OamekKIC', 'HmC3xURl']],
125+
array_column($queries, 'bindings'),
126+
);
127+
}
128+
111129
public function testOrWhereNotAllValues(): void
112130
{
113131
$this->getConnection()->unprepared('CREATE TABLE example (val text)');
@@ -267,6 +285,24 @@ public function testWhereLike(): void
267285
);
268286
}
269287

288+
public function testWhereNotLike(): void
289+
{
290+
$this->getConnection()->unprepared('CREATE TABLE example (str text)');
291+
292+
$queries = $this->withQueryLog(function (): void {
293+
$this->getConnection()->table('example')->whereNotLike('str', 'UkAymQlg')->get();
294+
$this->getConnection()->table('example')->whereNotLike('str', 'IcuC5Cqz', true)->get();
295+
});
296+
$this->assertEquals(
297+
['select * from "example" where "str" not ilike ?', 'select * from "example" where "str" not like ?'],
298+
array_column($queries, 'query'),
299+
);
300+
$this->assertEquals(
301+
[['UkAymQlg'], ['IcuC5Cqz']],
302+
array_column($queries, 'bindings'),
303+
);
304+
}
305+
270306
public function testWhereNotAllValues(): void
271307
{
272308
$this->getConnection()->unprepared('CREATE TABLE example (val text)');

0 commit comments

Comments
 (0)