Skip to content

Commit 53b02b3

Browse files
authored
Fix sql server paging problems (#47763)
1 parent 28b0d9e commit 53b02b3

File tree

2 files changed

+4
-36
lines changed

2 files changed

+4
-36
lines changed

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

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SqlServerGrammar extends Grammar
4848
public function compileSelect(Builder $query)
4949
{
5050
// An order by clause is required for SQL Server offset to function...
51-
if ($query->offset && empty($query->orders)) {
51+
if (($query->offset || $query->limit) && empty($query->orders)) {
5252
$query->orders[] = ['sql' => '(SELECT 0)'];
5353
}
5454

@@ -266,38 +266,6 @@ protected function compileHavingBitwise($having)
266266
return '('.$column.' '.$having['operator'].' '.$parameter.') != 0';
267267
}
268268

269-
/**
270-
* Move the order bindings to be after the "select" statement to account for an order by subquery.
271-
*
272-
* @param \Illuminate\Database\Query\Builder $query
273-
* @return array
274-
*/
275-
protected function sortBindingsForSubqueryOrderBy($query)
276-
{
277-
return Arr::sort($query->bindings, function ($bindings, $key) {
278-
return array_search($key, ['select', 'order', 'from', 'join', 'where', 'groupBy', 'having', 'union', 'unionOrder']);
279-
});
280-
}
281-
282-
/**
283-
* Compile the limit / offset row constraint for a query.
284-
*
285-
* @param \Illuminate\Database\Query\Builder $query
286-
* @return string
287-
*/
288-
protected function compileRowConstraint($query)
289-
{
290-
$start = (int) $query->offset + 1;
291-
292-
if ($query->limit > 0) {
293-
$finish = (int) $query->offset + (int) $query->limit;
294-
295-
return "between {$start} and {$finish}";
296-
}
297-
298-
return ">= {$start}";
299-
}
300-
301269
/**
302270
* Compile a delete statement without joins into SQL.
303271
*

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,7 +2690,7 @@ public function testAggregateFunctions()
26902690
public function testSqlServerExists()
26912691
{
26922692
$builder = $this->getSqlServerBuilder();
2693-
$builder->getConnection()->shouldReceive('select')->once()->with('select top 1 1 [exists] from [users]', [], true)->andReturn([['exists' => 1]]);
2693+
$builder->getConnection()->shouldReceive('select')->once()->with('select top 1 1 [exists] from [users] order by (SELECT 0)', [], true)->andReturn([['exists' => 1]]);
26942694
$results = $builder->from('users')->exists();
26952695
$this->assertTrue($results);
26962696
}
@@ -3856,7 +3856,7 @@ public function testSqlServerLimitsAndOffsets()
38563856
{
38573857
$builder = $this->getSqlServerBuilder();
38583858
$builder->select('*')->from('users')->take(10);
3859-
$this->assertSame('select top 10 * from [users]', $builder->toSql());
3859+
$this->assertSame('select top 10 * from [users] order by (SELECT 0)', $builder->toSql());
38603860

38613861
$builder = $this->getSqlServerBuilder();
38623862
$builder->select('*')->from('users')->skip(10)->orderBy('email', 'desc');
@@ -3875,7 +3875,7 @@ public function testSqlServerLimitsAndOffsets()
38753875
return $query->select('created_at')->from('logins')->where('users.name', 'nameBinding')->whereColumn('user_id', 'users.id')->limit(1);
38763876
};
38773877
$builder->select('*')->from('users')->where('email', 'emailBinding')->orderBy($subQuery)->skip(10)->take(10);
3878-
$this->assertSame('select * from [users] where [email] = ? order by (select top 1 [created_at] from [logins] where [users].[name] = ? and [user_id] = [users].[id]) asc offset 10 rows fetch next 10 rows only', $builder->toSql());
3878+
$this->assertSame('select * from [users] where [email] = ? order by (select top 1 [created_at] from [logins] where [users].[name] = ? and [user_id] = [users].[id] order by (SELECT 0)) asc offset 10 rows fetch next 10 rows only', $builder->toSql());
38793879
$this->assertEquals(['emailBinding', 'nameBinding'], $builder->getBindings());
38803880

38813881
$builder = $this->getSqlServerBuilder();

0 commit comments

Comments
 (0)