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

[10.x] Possible fix for unexpected column in SQL Server offset #44377

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ protected function compileAnsiOffset(Builder $query, $components)
*/
protected function compileOver($orderings)
{
return ", row_number() over ({$orderings}) as row_num";
return ", row_number() over ({$orderings}) as temp_row_num";
}

/**
Expand Down Expand Up @@ -333,7 +333,7 @@ protected function compileTableExpression($sql, $query)
{
$constraint = $this->compileRowConstraint($query);

return "select * from ({$sql}) as temp_table where row_num {$constraint} order by row_num";
return "select * from ({$sql}) as temp_table where temp_row_num {$constraint} order by temp_row_num /* remove_temp_row_num */";
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Query/Processors/SqlServerProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@

class SqlServerProcessor extends Processor
{
/**
* Process the results of a "select" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $results
* @return array
*/
public function processSelect(Builder $query, $results)
{
if (str_ends_with($query->toSql(), '/* remove_temp_row_num */')) {
array_map(function ($row) {
unset($row->temp_row_num);
}, $results);
}

return $results;
}

/**
* Process an "insert get ID" query.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3683,11 +3683,11 @@ public function testSqlServerLimitsAndOffsets()

$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->skip(10);
$this->assertSame('select * from (select *, row_number() over (order by (select 0)) as row_num from [users]) as temp_table where row_num >= 11 order by row_num', $builder->toSql());
$this->assertSame('select * from (select *, row_number() over (order by (select 0)) as temp_row_num from [users]) as temp_table where temp_row_num >= 11 order by temp_row_num /* remove_temp_row_num */', $builder->toSql());

$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->skip(10)->take(10);
$this->assertSame('select * from (select *, row_number() over (order by (select 0)) as row_num from [users]) as temp_table where row_num between 11 and 20 order by row_num', $builder->toSql());
$this->assertSame('select * from (select *, row_number() over (order by (select 0)) as temp_row_num from [users]) as temp_table where temp_row_num between 11 and 20 order by temp_row_num /* remove_temp_row_num */', $builder->toSql());

$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->skip(11)->take(10)->orderBy('email', 'desc');
Expand Down