Skip to content

Commit f16d325

Browse files
committed
support delete with limit on sqlsrv
1 parent dbcc98d commit f16d325

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Query\Builder;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Str;
78

89
class SqlServerGrammar extends Grammar
910
{
@@ -232,6 +233,23 @@ protected function compileRowConstraint($query)
232233
return ">= {$start}";
233234
}
234235

236+
/**
237+
* Compile a delete statement without joins into SQL.
238+
*
239+
* @param \Illuminate\Database\Query\Builder $query
240+
* @param string $table
241+
* @param string $where
242+
* @return string
243+
*/
244+
protected function compileDeleteWithoutJoins(Builder $query, $table, $where)
245+
{
246+
$sql = parent::compileDeleteWithoutJoins($query, $table, $where);
247+
248+
return ! is_null($query->limit) && $query->limit > 0 && $query->offset <= 0
249+
? Str::replaceFirst('delete', 'delete top ('.$query->limit.')', $sql)
250+
: $sql;
251+
}
252+
235253
/**
236254
* Compile the random statement into SQL.
237255
*

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,6 +2355,11 @@ public function testDeleteMethod()
23552355
$builder->getConnection()->shouldReceive('delete')->once()->with('delete from [users] where [email] = ?', ['foo'])->andReturn(1);
23562356
$result = $builder->from('users')->where('email', '=', 'foo')->delete();
23572357
$this->assertEquals(1, $result);
2358+
2359+
$builder = $this->getSqlServerBuilder();
2360+
$builder->getConnection()->shouldReceive('delete')->once()->with('delete top (1) from [users] where [email] = ?', ['foo'])->andReturn(1);
2361+
$result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();
2362+
$this->assertEquals(1, $result);
23582363
}
23592364

23602365
public function testDeleteWithJoinMethod()

0 commit comments

Comments
 (0)