From f745d6c8052896adbde88e95c0183784d8d8cbde Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Sat, 10 Sep 2022 21:18:28 +0200 Subject: [PATCH 1/5] bugfix --- src/Illuminate/Database/Query/Builder.php | 4 +++- tests/Database/DatabaseQueryBuilderTest.php | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 799463d6099b..d43fbdd7622d 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -894,7 +894,9 @@ public function orWhere($column, $operator = null, $value = null) */ public function whereNot($column, $operator = null, $value = null, $boolean = 'and') { - return $this->where($column, $operator, $value, $boolean.' not'); + return $this->whereNested(function($query) use ($column, $operator, $value, $boolean) { + $query->where($column, $operator, $value, $boolean); + }, $boolean.' not'); } /** diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index ee892d851170..47bd40ffa6ae 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -1746,6 +1746,24 @@ public function testWhereNot() $this->assertEquals([0 => 'bar', 1 => 'foo'], $builder->getBindings()); } + public function testWhereNotWithArrayConditions() + { + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', 2]]); + $this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql()); + $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings()); + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->whereNot(['foo' => 1, 'bar' => 2]); + $this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql()); + $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings()); + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', '<', 2]]); + $this->assertSame('select * from "users" where not (("foo" = ? and "bar" < ?))', $builder->toSql()); + $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings()); + } + public function testFullSubSelects() { $builder = $this->getBuilder(); From f161b4cd29dda24d2b94145f71ca664c0ede54c2 Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Sat, 10 Sep 2022 21:19:03 +0200 Subject: [PATCH 2/5] consequences --- tests/Database/DatabaseQueryBuilderTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 47bd40ffa6ae..3294fc63407f 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -306,7 +306,7 @@ public function testBasicWhereNot() { $builder = $this->getBuilder(); $builder->select('*')->from('users')->whereNot('name', 'foo')->whereNot('name', '<>', 'bar'); - $this->assertSame('select * from "users" where not "name" = ? and not "name" <> ?', $builder->toSql()); + $this->assertSame('select * from "users" where not ("name" = ?) and not ("name" <> ?)', $builder->toSql()); $this->assertEquals(['foo', 'bar'], $builder->getBindings()); } @@ -728,7 +728,7 @@ public function testBasicOrWhereNot() { $builder = $this->getBuilder(); $builder->select('*')->from('users')->orWhereNot('name', 'foo')->orWhereNot('name', '<>', 'bar'); - $this->assertSame('select * from "users" where not "name" = ? or not "name" <> ?', $builder->toSql()); + $this->assertSame('select * from "users" where not ("name" = ?) or not ("name" <> ?)', $builder->toSql()); $this->assertEquals(['foo', 'bar'], $builder->getBindings()); } @@ -1728,21 +1728,21 @@ public function testWhereNot() $builder->select('*')->from('users')->whereNot(function ($q) { $q->where('email', '=', 'foo'); }); - $this->assertSame('select * from "users" where not ("email" = ?)', $builder->toSql()); + $this->assertSame('select * from "users" where not (("email" = ?))', $builder->toSql()); $this->assertEquals([0 => 'foo'], $builder->getBindings()); $builder = $this->getBuilder(); $builder->select('*')->from('users')->where('name', '=', 'bar')->whereNot(function ($q) { $q->where('email', '=', 'foo'); }); - $this->assertSame('select * from "users" where "name" = ? and not ("email" = ?)', $builder->toSql()); + $this->assertSame('select * from "users" where "name" = ? and not (("email" = ?))', $builder->toSql()); $this->assertEquals([0 => 'bar', 1 => 'foo'], $builder->getBindings()); $builder = $this->getBuilder(); $builder->select('*')->from('users')->where('name', '=', 'bar')->orWhereNot(function ($q) { $q->where('email', '=', 'foo'); }); - $this->assertSame('select * from "users" where "name" = ? or not ("email" = ?)', $builder->toSql()); + $this->assertSame('select * from "users" where "name" = ? or not (("email" = ?))', $builder->toSql()); $this->assertEquals([0 => 'bar', 1 => 'foo'], $builder->getBindings()); } From 8a880f519e34d819e04ff2b35a93674986e888e6 Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Sat, 10 Sep 2022 21:19:38 +0200 Subject: [PATCH 3/5] boyscout rule --- src/Illuminate/Database/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index d43fbdd7622d..a534080210c2 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -800,7 +800,7 @@ protected function addArrayOfWheres($column, $boolean, $method = 'where') if (is_numeric($key) && is_array($value)) { $query->{$method}(...array_values($value)); } else { - $query->$method($key, '=', $value, $boolean); + $query->{$method}($key, '=', $value, $boolean); } } }, $boolean); From faf5dfaa8cbd4507ec0bb7c69792e020de3f3511 Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Sat, 10 Sep 2022 21:30:15 +0200 Subject: [PATCH 4/5] styleci --- src/Illuminate/Database/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index a534080210c2..72bfa3ea1ee5 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -894,7 +894,7 @@ public function orWhere($column, $operator = null, $value = null) */ public function whereNot($column, $operator = null, $value = null, $boolean = 'and') { - return $this->whereNested(function($query) use ($column, $operator, $value, $boolean) { + return $this->whereNested(function ($query) use ($column, $operator, $value, $boolean) { $query->where($column, $operator, $value, $boolean); }, $boolean.' not'); } From 529ae9793be6711eb4126a238ed669ffcaab6e4d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 12 Sep 2022 09:48:08 -0500 Subject: [PATCH 5/5] narrow surface area --- src/Illuminate/Database/Query/Builder.php | 10 +++++++--- tests/Database/DatabaseQueryBuilderTest.php | 10 +++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 72bfa3ea1ee5..59f45d27acd9 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -894,9 +894,13 @@ public function orWhere($column, $operator = null, $value = null) */ public function whereNot($column, $operator = null, $value = null, $boolean = 'and') { - return $this->whereNested(function ($query) use ($column, $operator, $value, $boolean) { - $query->where($column, $operator, $value, $boolean); - }, $boolean.' not'); + if (is_array($column)) { + return $this->whereNested(function ($query) use ($column, $operator, $value, $boolean) { + $query->where($column, $operator, $value, $boolean); + }, $boolean.' not'); + } + + return $this->where($column, $operator, $value, $boolean.' not'); } /** diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 3294fc63407f..47bd40ffa6ae 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -306,7 +306,7 @@ public function testBasicWhereNot() { $builder = $this->getBuilder(); $builder->select('*')->from('users')->whereNot('name', 'foo')->whereNot('name', '<>', 'bar'); - $this->assertSame('select * from "users" where not ("name" = ?) and not ("name" <> ?)', $builder->toSql()); + $this->assertSame('select * from "users" where not "name" = ? and not "name" <> ?', $builder->toSql()); $this->assertEquals(['foo', 'bar'], $builder->getBindings()); } @@ -728,7 +728,7 @@ public function testBasicOrWhereNot() { $builder = $this->getBuilder(); $builder->select('*')->from('users')->orWhereNot('name', 'foo')->orWhereNot('name', '<>', 'bar'); - $this->assertSame('select * from "users" where not ("name" = ?) or not ("name" <> ?)', $builder->toSql()); + $this->assertSame('select * from "users" where not "name" = ? or not "name" <> ?', $builder->toSql()); $this->assertEquals(['foo', 'bar'], $builder->getBindings()); } @@ -1728,21 +1728,21 @@ public function testWhereNot() $builder->select('*')->from('users')->whereNot(function ($q) { $q->where('email', '=', 'foo'); }); - $this->assertSame('select * from "users" where not (("email" = ?))', $builder->toSql()); + $this->assertSame('select * from "users" where not ("email" = ?)', $builder->toSql()); $this->assertEquals([0 => 'foo'], $builder->getBindings()); $builder = $this->getBuilder(); $builder->select('*')->from('users')->where('name', '=', 'bar')->whereNot(function ($q) { $q->where('email', '=', 'foo'); }); - $this->assertSame('select * from "users" where "name" = ? and not (("email" = ?))', $builder->toSql()); + $this->assertSame('select * from "users" where "name" = ? and not ("email" = ?)', $builder->toSql()); $this->assertEquals([0 => 'bar', 1 => 'foo'], $builder->getBindings()); $builder = $this->getBuilder(); $builder->select('*')->from('users')->where('name', '=', 'bar')->orWhereNot(function ($q) { $q->where('email', '=', 'foo'); }); - $this->assertSame('select * from "users" where "name" = ? or not (("email" = ?))', $builder->toSql()); + $this->assertSame('select * from "users" where "name" = ? or not ("email" = ?)', $builder->toSql()); $this->assertEquals([0 => 'bar', 1 => 'foo'], $builder->getBindings()); }