Skip to content

Commit

Permalink
Merge pull request #29165 from staudenmeir/delete-bindings
Browse files Browse the repository at this point in the history
[5.8] Refactor DELETE query bindings
  • Loading branch information
taylorotwell authored Jul 15, 2019
2 parents 190521f + a7f986a commit ff38578
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 32 deletions.
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

/**
Expand Down
16 changes: 0 additions & 16 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Illuminate\Database\Query\Grammars;

use Illuminate\Support\Arr;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\JsonExpression;

Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 0 additions & 15 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 5 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ff38578

Please sign in to comment.