Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Fix DELETE queries with alias on SQLite #29164

Merged
merged 2 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,31 @@ public function prepareBindingsForUpdate(array $bindings, array $values)
public function compileDelete(Builder $query)
{
if (isset($query->joins) || isset($query->limit)) {
$selectSql = parent::compileSelect($query->select("{$query->from}.rowid"));

return "delete from {$this->wrapTable($query->from)} where {$this->wrap('rowid')} in ({$selectSql})";
return $this->compileDeleteWithJoinsOrLimit($query);
}

$wheres = is_array($query->wheres) ? $this->compileWheres($query) : '';

return trim("delete from {$this->wrapTable($query->from)} $wheres");
}

/**
* Compile a delete statement with joins or limit into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
protected function compileDeleteWithJoinsOrLimit(Builder $query)
{
$segments = preg_split('/\s+as\s+/i', $query->from);

$alias = $segments[1] ?? $segments[0];

$selectSql = parent::compileSelect($query->select($alias.'.rowid'));

return "delete from {$this->wrapTable($query->from)} where {$this->wrap('rowid')} in ({$selectSql})";
}

/**
* 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 @@ -2104,6 +2104,11 @@ public function testDeleteWithJoinMethod()
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->where('users.email', '=', 'foo')->orderBy('users.id')->limit(1)->delete();
$this->assertEquals(1, $result);

$builder = $this->getSqliteBuilder();
$builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" as "u" where "rowid" in (select "u"."rowid" from "users" as "u" inner join "contacts" as "c" on "u"."id" = "c"."id")', [])->andReturn(1);
$result = $builder->from('users as u')->join('contacts as c', 'u.id', '=', 'c.id')->delete();
$this->assertEquals(1, $result);

$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('delete')->once()->with('delete `users` from `users` inner join `contacts` on `users`.`id` = `contacts`.`id` where `email` = ?', ['foo'])->andReturn(1);
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
Expand Down