Skip to content

Commit db0c873

Browse files
mmachatschektpetry
andauthored
fix: whereNotLike misses the not in the sql query (#105)
Co-authored-by: tpetry <[email protected]>
1 parent ecc5753 commit db0c873

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

src/Query/BuilderWhere.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ public function orWhereNotBoolean($column, bool $value): static
110110
return $this->orWhere($column, '!=', new Expression(var_export($value, true)));
111111
}
112112

113+
/**
114+
* Add an "or where not like" clause to the query.
115+
*
116+
* @param Expression|string $column
117+
* @param Expression|string $value
118+
* @param bool $caseSensitive
119+
*/
120+
public function orWhereNotLike($column, $value, $caseSensitive = false): static
121+
{
122+
return $this->whereNotLike($column, $value, $caseSensitive, 'or');
123+
}
124+
113125
/**
114126
* Add a where all statement to the query.
115127
*
@@ -178,7 +190,7 @@ public function whereIntegerArrayMatches($column, string $query): static
178190
}
179191

180192
/**
181-
* Add a "where month" statement to the query.
193+
* Add a "where like" statement to the query.
182194
*
183195
* @param Expression|string $column
184196
* @param Expression|string $value
@@ -235,4 +247,17 @@ public function whereNotBoolean($column, bool $value): static
235247
{
236248
return $this->where($column, '!=', new Expression(var_export($value, true)));
237249
}
250+
251+
/**
252+
* Add a "where not like" statement to the query.
253+
*
254+
* @param Expression|string $column
255+
* @param Expression|string $value
256+
* @param bool $caseSensitive
257+
* @param string $boolean
258+
*/
259+
public function whereNotLike($column, $value, $caseSensitive = false, $boolean = 'and'): static
260+
{
261+
return $this->whereLike($column, $value, $caseSensitive, $boolean, true);
262+
}
238263
}

src/Query/GrammarWhere.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ public function whereAny(Builder $query, $where): string
4646
/**
4747
* Compile a "like" clause.
4848
*
49-
* @param array{caseSensitive: bool, column: string, value: mixed} $where
49+
* @param array{caseSensitive: bool, column: string, not: bool, value: mixed} $where
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'])}::text {$operator} {$this->parameter($where['value'])}";
5757
}
5858

5959
/**

tests/Query/WhereTest.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function testOrWhereLike(): void
9999
$this->getConnection()->table('example')->orWhereLike('str', 'OamekKIC', true)->orWhereLike('str', 'HmC3xURl', true)->get();
100100
});
101101
$this->assertEquals(
102-
['select * from "example" where "str" ilike ? or "str" ilike ?', 'select * from "example" where "str" like ? or "str" like ?'],
102+
['select * from "example" where "str"::text ilike ? or "str"::text ilike ?', 'select * from "example" where "str"::text like ? or "str"::text like ?'],
103103
array_column($queries, 'query'),
104104
);
105105
$this->assertEquals(
@@ -172,6 +172,24 @@ public function testOrWhereNotBoolean(): void
172172
);
173173
}
174174

175+
public function testOrWhereNotLike(): void
176+
{
177+
$this->getConnection()->unprepared('CREATE TABLE example (str text)');
178+
179+
$queries = $this->withQueryLog(function (): void {
180+
$this->getConnection()->table('example')->orWhereNotLike('str', 'AERLFW4s')->orWhereNotLike('str', 'sPtXxruW')->get();
181+
$this->getConnection()->table('example')->orWhereNotLike('str', 'EWnGRRc5', true)->orWhereNotLike('str', 'wihuWcph', true)->get();
182+
});
183+
$this->assertEquals(
184+
['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 ?'],
185+
array_column($queries, 'query'),
186+
);
187+
$this->assertEquals(
188+
[['AERLFW4s', 'sPtXxruW'], ['EWnGRRc5', 'wihuWcph']],
189+
array_column($queries, 'bindings'),
190+
);
191+
}
192+
175193
public function testWhereAllValues(): void
176194
{
177195
$this->getConnection()->unprepared('CREATE TABLE example (val text)');
@@ -258,7 +276,7 @@ public function testWhereLike(): void
258276
$this->getConnection()->table('example')->whereLike('str', 'IcuC5Cqz', true)->get();
259277
});
260278
$this->assertEquals(
261-
['select * from "example" where "str" ilike ?', 'select * from "example" where "str" like ?'],
279+
['select * from "example" where "str"::text ilike ?', 'select * from "example" where "str"::text like ?'],
262280
array_column($queries, 'query'),
263281
);
264282
$this->assertEquals(
@@ -330,4 +348,22 @@ public function testWhereNotBoolean(): void
330348
array_column($queries, 'query'),
331349
);
332350
}
351+
352+
public function testWhereNotLike(): void
353+
{
354+
$this->getConnection()->unprepared('CREATE TABLE example (str text)');
355+
356+
$queries = $this->withQueryLog(function (): void {
357+
$this->getConnection()->table('example')->whereNotLike('str', 'mgb7DtKz')->get();
358+
$this->getConnection()->table('example')->whereNotLike('str', 'tpx6Bxpm', true)->get();
359+
});
360+
$this->assertEquals(
361+
['select * from "example" where "str"::text not ilike ?', 'select * from "example" where "str"::text not like ?'],
362+
array_column($queries, 'query'),
363+
);
364+
$this->assertEquals(
365+
[['mgb7DtKz'], ['tpx6Bxpm']],
366+
array_column($queries, 'bindings'),
367+
);
368+
}
333369
}

0 commit comments

Comments
 (0)