From a7f986a5a327a32ac182f8281e347270d72e0bb0 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Sun, 14 Jul 2019 20:11:28 +0200 Subject: [PATCH] Refactor DELETE query bindings --- .../Database/Query/Grammars/Grammar.php | 4 +++- .../Database/Query/Grammars/MySqlGrammar.php | 16 ---------------- .../Database/Query/Grammars/SQLiteGrammar.php | 15 --------------- tests/Database/DatabaseQueryBuilderTest.php | 5 +++++ 4 files changed, 8 insertions(+), 32 deletions(-) diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 2bccbaa63472..7c30c67a14f2 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -970,7 +970,9 @@ public function compileDelete(Builder $query) */ public function prepareBindingsForDelete(array $bindings) { - return Arr::flatten($bindings); + return Arr::flatten( + Arr::except($bindings, 'select') + ); } /** diff --git a/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php index b93226b47684..5a9cea6e5663 100755 --- a/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php @@ -2,7 +2,6 @@ namespace Illuminate\Database\Query\Grammars; -use Illuminate\Support\Arr; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JsonExpression; @@ -239,21 +238,6 @@ public function compileDelete(Builder $query) : $this->compileDeleteWithoutJoins($query, $table, $where); } - /** - * Prepare the bindings for a delete statement. - * - * @param array $bindings - * @return array - */ - public function prepareBindingsForDelete(array $bindings) - { - $cleanBindings = Arr::except($bindings, ['join', 'select']); - - return array_values( - array_merge($bindings['join'], Arr::flatten($cleanBindings)) - ); - } - /** * Compile a delete query that does not use joins. * diff --git a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php index 2d1b9e128657..9c6df753b663 100755 --- a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php @@ -237,21 +237,6 @@ public function compileDelete(Builder $query) return trim("delete from {$this->wrapTable($query->from)} $wheres"); } - /** - * Prepare the bindings for a delete statement. - * - * @param array $bindings - * @return array - */ - public function prepareBindingsForDelete(array $bindings) - { - $cleanBindings = Arr::except($bindings, ['select', 'join']); - - return array_values( - array_merge($bindings['join'], Arr::flatten($cleanBindings)) - ); - } - /** * Compile a truncate table statement into SQL. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 2998d5821e58..0e3cb0798767 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -2076,6 +2076,11 @@ public function testDeleteMethod() $result = $builder->from('users')->delete(1); $this->assertEquals(1, $result); + $builder = $this->getBuilder(); + $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "users"."id" = ?', [1])->andReturn(1); + $result = $builder->from('users')->selectRaw('?', ['ignore'])->delete(1); + $this->assertEquals(1, $result); + $builder = $this->getSqliteBuilder(); $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "rowid" in (select "users"."rowid" from "users" where "email" = ? order by "id" asc limit 1)', ['foo'])->andReturn(1); $result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();