From 9caac29f56fd1c9a2aeca757a359ec447c0fc685 Mon Sep 17 00:00:00 2001 From: 02 Date: Sat, 17 Oct 2020 00:56:13 +0900 Subject: [PATCH 1/2] add SQL Parentheses for whereRaw --- src/Illuminate/Database/Query/Builder.php | 2 ++ .../DatabaseEloquentSoftDeletesIntegrationTest.php | 13 +++++++++++++ tests/Database/DatabaseQueryBuilderTest.php | 12 ++++++------ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index d2d289f7a272..cf46f11cb772 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -903,6 +903,8 @@ public function orWhereColumn($first, $operator = null, $second = null) */ public function whereRaw($sql, $bindings = [], $boolean = 'and') { + $sql = "(" . $sql . ")"; + $this->wheres[] = ['type' => 'raw', 'sql' => $sql, 'boolean' => $boolean]; $this->addBinding((array) $bindings, 'where'); diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index da1bf880859e..d0cc4991bbee 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -714,6 +714,19 @@ public function testMorphToNonSoftDeletingModel() $this->assertNull($comment->owner); } + public function testSoftDeletesCanUseWhereRaw() + { + $this->createUsers(); + + $builder = SoftDeletesTestUser::whereRaw('id = 1 OR email = "abigailotwell@gmail.com"'); + $users = $builder->get(); + + $this->assertSame('select * from "users" where (id = 1 OR email = "abigailotwell@gmail.com") and "users"."deleted_at" is null', $builder->toSql()); + $this->assertCount(1, $users); + $this->assertSame(2, $users->first()->id); + $this->assertNull(SoftDeletesTestUser::find(1)); + } + /** * Helpers... */ diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 8e00974dcda8..bdb0f6b0a0af 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -690,7 +690,7 @@ public function testRawWheres() { $builder = $this->getBuilder(); $builder->select('*')->from('users')->whereRaw('id = ? or email = ?', [1, 'foo']); - $this->assertSame('select * from "users" where id = ? or email = ?', $builder->toSql()); + $this->assertSame('select * from "users" where (id = ? or email = ?)', $builder->toSql()); $this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings()); } @@ -698,7 +698,7 @@ public function testRawOrWheres() { $builder = $this->getBuilder(); $builder->select('*')->from('users')->where('id', '=', 1)->orWhereRaw('email = ?', ['foo']); - $this->assertSame('select * from "users" where "id" = ? or email = ?', $builder->toSql()); + $this->assertSame('select * from "users" where "id" = ? or (email = ?)', $builder->toSql()); $this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings()); } @@ -1649,7 +1649,7 @@ public function testJoinsWithAdvancedConditions() ->orWhereRaw('year(contacts.created_at) = 2016'); }); }); - $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and ("role" = ? or "contacts"."disabled" is null or year(contacts.created_at) = 2016)', $builder->toSql()); + $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and ("role" = ? or "contacts"."disabled" is null or (year(contacts.created_at) = 2016))', $builder->toSql()); $this->assertEquals(['admin'], $builder->getBindings()); } @@ -1675,7 +1675,7 @@ public function testJoinsWithSubqueryCondition() ->whereNull('deleted_at'); }); }); - $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where contact_types.id = contacts.contact_type_id and "category_id" = ? and "deleted_at" is null)', $builder->toSql()); + $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where (contact_types.id = contacts.contact_type_id) and "category_id" = ? and "deleted_at" is null)', $builder->toSql()); $this->assertEquals(['1'], $builder->getBindings()); } @@ -1694,7 +1694,7 @@ public function testJoinsWithAdvancedSubqueryCondition() }); }); }); - $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where contact_types.id = contacts.contact_type_id and "category_id" = ? and "deleted_at" is null and "level_id" in (select "id" from "levels" where "is_active" = ?))', $builder->toSql()); + $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where (contact_types.id = contacts.contact_type_id) and "category_id" = ? and "deleted_at" is null and "level_id" in (select "id" from "levels" where "is_active" = ?))', $builder->toSql()); $this->assertEquals(['1', true], $builder->getBindings()); } @@ -2782,7 +2782,7 @@ public function testMySqlWrappingJson() { $builder = $this->getMySqlBuilder(); $builder->select('*')->from('users')->whereRaw('items->\'$."price"\' = 1'); - $this->assertSame('select * from `users` where items->\'$."price"\' = 1', $builder->toSql()); + $this->assertSame('select * from `users` where (items->\'$."price"\' = 1)', $builder->toSql()); $builder = $this->getMySqlBuilder(); $builder->select('items->price')->from('users')->where('users.items->price', '=', 1)->orderBy('items->price'); From 53901ed8c3184cfc4791487c390c62d5521d245a Mon Sep 17 00:00:00 2001 From: 02 Date: Sat, 17 Oct 2020 13:58:10 +0900 Subject: [PATCH 2/2] remove space for ci --- 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 cf46f11cb772..0bdc0c2bdc9c 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -903,7 +903,7 @@ public function orWhereColumn($first, $operator = null, $second = null) */ public function whereRaw($sql, $bindings = [], $boolean = 'and') { - $sql = "(" . $sql . ")"; + $sql = '('.$sql.')'; $this->wheres[] = ['type' => 'raw', 'sql' => $sql, 'boolean' => $boolean];