File tree Expand file tree Collapse file tree
src/Illuminate/Database/Query/Grammars Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55use Illuminate \Database \Query \Builder ;
66use Illuminate \Support \Arr ;
7+ use Illuminate \Support \Str ;
78
89class 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 *
Original file line number Diff line number Diff 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 ()
You can’t perform that action at this time.
0 commit comments