Skip to content

Commit

Permalink
support delete with limit on sqlsrv
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 4, 2020
1 parent dbcc98d commit f16d325
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

class SqlServerGrammar extends Grammar
{
Expand Down Expand Up @@ -232,6 +233,23 @@ protected function compileRowConstraint($query)
return ">= {$start}";
}

/**
* Compile a delete statement without joins into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $table
* @param string $where
* @return string
*/
protected function compileDeleteWithoutJoins(Builder $query, $table, $where)
{
$sql = parent::compileDeleteWithoutJoins($query, $table, $where);

return ! is_null($query->limit) && $query->limit > 0 && $query->offset <= 0
? Str::replaceFirst('delete', 'delete top ('.$query->limit.')', $sql)
: $sql;
}

/**
* Compile the random 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 @@ -2355,6 +2355,11 @@ public function testDeleteMethod()
$builder->getConnection()->shouldReceive('delete')->once()->with('delete from [users] where [email] = ?', ['foo'])->andReturn(1);
$result = $builder->from('users')->where('email', '=', 'foo')->delete();
$this->assertEquals(1, $result);

$builder = $this->getSqlServerBuilder();
$builder->getConnection()->shouldReceive('delete')->once()->with('delete top (1) from [users] where [email] = ?', ['foo'])->andReturn(1);
$result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();
$this->assertEquals(1, $result);
}

public function testDeleteWithJoinMethod()
Expand Down

0 comments on commit f16d325

Please sign in to comment.