Skip to content

Commit 4e64ea3

Browse files
mmachatschekMarkus Machatschek
authored andcommitted
chore: use laravels implementation when we are on laravel >= 11.17.0
1 parent ec0f372 commit 4e64ea3

File tree

3 files changed

+92
-41
lines changed

3 files changed

+92
-41
lines changed

src/Query/BuilderWhere.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tpetry\PostgresqlEnhanced\Query;
66

77
use Illuminate\Database\Query\Expression;
8+
use Illuminate\Support\Facades\App;
89

910
trait BuilderWhere
1011
{
@@ -67,6 +68,10 @@ public function orWhereIntegerArrayMatches($column, string $query): static
6768
*/
6869
public function orWhereLike($column, $value, $caseSensitive = false): static
6970
{
71+
if (version_compare(App::version(), '11.17.0', '>=')) {
72+
return parent::orWhereLike($column, $value, $caseSensitive);
73+
}
74+
7075
return $this->whereLike($column, $value, $caseSensitive, 'or', false);
7176
}
7277

@@ -188,6 +193,10 @@ public function whereIntegerArrayMatches($column, string $query): static
188193
*/
189194
public function whereLike($column, $value, $caseSensitive = false, $boolean = 'and', $not = false): static
190195
{
196+
if (version_compare(App::version(), '11.17.0', '>=')) {
197+
return parent::whereLike($column, $value, $caseSensitive, $boolean, $not);
198+
}
199+
191200
$type = 'like';
192201

193202
$this->wheres[] = compact('type', 'column', 'value', 'caseSensitive', 'boolean', 'not');

src/Query/GrammarWhere.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Exception;
88
use Illuminate\Database\Query\Builder;
9+
use Illuminate\Support\Facades\App;
910

1011
trait GrammarWhere
1112
{
@@ -50,9 +51,13 @@ public function whereAny(Builder $query, $where): string
5051
*/
5152
public function whereLike(Builder $query, $where): string
5253
{
54+
if (version_compare(App::version(), '11.17.0', '>=')) {
55+
return parent::whereLike($query, $where);
56+
}
57+
5358
$operator = $where['not'] ? 'not ' : '';
5459
$operator .= $where['caseSensitive'] ? 'like' : 'ilike';
55-
60+
5661
return "{$this->wrap($where['column'])} {$operator} {$this->parameter($where['value'])}";
5762
}
5863

tests/Query/WhereTest.php

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tpetry\PostgresqlEnhanced\Tests\Query;
66

7+
use Illuminate\Support\Facades\App;
78
use Tpetry\PostgresqlEnhanced\Tests\TestCase;
89

910
class WhereTest extends TestCase
@@ -98,28 +99,19 @@ public function testOrWhereLike(): void
9899
$this->getConnection()->table('example')->orWhereLike('str', 'ZsbBUJmR')->orWhereLike('str', '7Cc1Uf8t')->get();
99100
$this->getConnection()->table('example')->orWhereLike('str', 'OamekKIC', true)->orWhereLike('str', 'HmC3xURl', true)->get();
100101
});
101-
$this->assertEquals(
102-
['select * from "example" where "str" ilike ? or "str" ilike ?', 'select * from "example" where "str" like ? or "str" like ?'],
103-
array_column($queries, 'query'),
104-
);
105-
$this->assertEquals(
106-
[['ZsbBUJmR', '7Cc1Uf8t'], ['OamekKIC', 'HmC3xURl']],
107-
array_column($queries, 'bindings'),
108-
);
109-
}
110102

111-
public function testOrWhereNotLike(): void
112-
{
113-
$this->getConnection()->unprepared('CREATE TABLE example (str text)');
103+
if (version_compare(App::version(), '11.17.0', '>=')) {
104+
$this->assertEquals(
105+
['select * from "example" where "str"::text ilike ? or "str"::text ilike ?', 'select * from "example" where "str"::text like ? or "str"::text like ?'],
106+
array_column($queries, 'query'),
107+
);
108+
} else {
109+
$this->assertEquals(
110+
['select * from "example" where "str" ilike ? or "str" ilike ?', 'select * from "example" where "str" like ? or "str" like ?'],
111+
array_column($queries, 'query'),
112+
);
113+
}
114114

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-
);
123115
$this->assertEquals(
124116
[['ZsbBUJmR', '7Cc1Uf8t'], ['OamekKIC', 'HmC3xURl']],
125117
array_column($queries, 'bindings'),
@@ -190,6 +182,33 @@ public function testOrWhereNotBoolean(): void
190182
);
191183
}
192184

185+
public function testOrWhereNotLike(): void
186+
{
187+
$this->getConnection()->unprepared('CREATE TABLE example (str text)');
188+
189+
$queries = $this->withQueryLog(function (): void {
190+
$this->getConnection()->table('example')->orWhereNotLike('str', 'ZsbBUJmR')->orWhereNotLike('str', '7Cc1Uf8t')->get();
191+
$this->getConnection()->table('example')->orWhereNotLike('str', 'OamekKIC', true)->orWhereNotLike('str', 'HmC3xURl', true)->get();
192+
});
193+
194+
if (version_compare(App::version(), '11.17.0', '>=')) {
195+
$this->assertEquals(
196+
['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 ?'],
197+
array_column($queries, 'query'),
198+
);
199+
} else {
200+
$this->assertEquals(
201+
['select * from "example" where "str" not ilike ? or "str" not ilike ?', 'select * from "example" where "str" not like ? or "str" not like ?'],
202+
array_column($queries, 'query'),
203+
);
204+
}
205+
206+
$this->assertEquals(
207+
[['ZsbBUJmR', '7Cc1Uf8t'], ['OamekKIC', 'HmC3xURl']],
208+
array_column($queries, 'bindings'),
209+
);
210+
}
211+
193212
public function testWhereAllValues(): void
194213
{
195214
$this->getConnection()->unprepared('CREATE TABLE example (val text)');
@@ -275,28 +294,19 @@ public function testWhereLike(): void
275294
$this->getConnection()->table('example')->whereLike('str', 'UkAymQlg')->get();
276295
$this->getConnection()->table('example')->whereLike('str', 'IcuC5Cqz', true)->get();
277296
});
278-
$this->assertEquals(
279-
['select * from "example" where "str" ilike ?', 'select * from "example" where "str" like ?'],
280-
array_column($queries, 'query'),
281-
);
282-
$this->assertEquals(
283-
[['UkAymQlg'], ['IcuC5Cqz']],
284-
array_column($queries, 'bindings'),
285-
);
286-
}
287297

288-
public function testWhereNotLike(): void
289-
{
290-
$this->getConnection()->unprepared('CREATE TABLE example (str text)');
298+
if (version_compare(App::version(), '11.17.0', '>=')) {
299+
$this->assertEquals(
300+
['select * from "example" where "str"::text ilike ?', 'select * from "example" where "str"::text like ?'],
301+
array_column($queries, 'query'),
302+
);
303+
} else {
304+
$this->assertEquals(
305+
['select * from "example" where "str" ilike ?', 'select * from "example" where "str" like ?'],
306+
array_column($queries, 'query'),
307+
);
308+
}
291309

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-
);
300310
$this->assertEquals(
301311
[['UkAymQlg'], ['IcuC5Cqz']],
302312
array_column($queries, 'bindings'),
@@ -366,4 +376,31 @@ public function testWhereNotBoolean(): void
366376
array_column($queries, 'query'),
367377
);
368378
}
379+
380+
public function testWhereNotLike(): void
381+
{
382+
$this->getConnection()->unprepared('CREATE TABLE example (str text)');
383+
384+
$queries = $this->withQueryLog(function (): void {
385+
$this->getConnection()->table('example')->whereNotLike('str', 'UkAymQlg')->get();
386+
$this->getConnection()->table('example')->whereNotLike('str', 'IcuC5Cqz', true)->get();
387+
});
388+
389+
if (version_compare(App::version(), '11.17.0', '>=')) {
390+
$this->assertEquals(
391+
['select * from "example" where "str"::text not ilike ?', 'select * from "example" where "str"::text not like ?'],
392+
array_column($queries, 'query'),
393+
);
394+
} else {
395+
$this->assertEquals(
396+
['select * from "example" where "str" not ilike ?', 'select * from "example" where "str" not like ?'],
397+
array_column($queries, 'query'),
398+
);
399+
}
400+
401+
$this->assertEquals(
402+
[['UkAymQlg'], ['IcuC5Cqz']],
403+
array_column($queries, 'bindings'),
404+
);
405+
}
369406
}

0 commit comments

Comments
 (0)