Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 21, 2021
1 parent 1cd92f7 commit 401928b
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,8 @@ protected function compileAnsiOffset(Builder $query, $components)

unset($components['orders']);

// As this moves the order statement to be part of the "select" statement, we might need to
// move the bindings to the right position: right after "select".
if ($this->queryOrderContainsSubquery($query)) {
$preferredBindingOrder = ['select', 'order', 'from', 'join', 'where', 'groupBy', 'having', 'union', 'unionOrder'];
$sortedBindings = Arr::sort($query->bindings, function ($bindings, $key) use ($preferredBindingOrder) {
return array_search($key, $preferredBindingOrder);
});
$query->bindings = $sortedBindings;
$query->bindings = $this->sortBindingsForSubqueryOrderBy($query);
}

// Next we need to calculate the constraints that should be placed on the query
Expand All @@ -200,7 +194,18 @@ protected function compileAnsiOffset(Builder $query, $components)
}

/**
* Check if the query order by clauses contain a subquery.
* Compile the over statement for a table expression.
*
* @param string $orderings
* @return string
*/
protected function compileOver($orderings)
{
return ", row_number() over ({$orderings}) as row_num";
}

/**
* Determine if the query's order by clauses contain a subquery.
*
* @param \Illuminate\Database\Query\Builder $query
* @return bool
Expand All @@ -217,14 +222,16 @@ protected function queryOrderContainsSubquery($query)
}

/**
* Compile the over statement for a table expression.
* Move the order bindings to be after the "select" statement to account for a order by subquery.
*
* @param string $orderings
* @return string
* @param \Illuminate\Database\Query\Builder $query
* @return array
*/
protected function compileOver($orderings)
protected function sortBindingsForSubqueryOrderBy($query)
{
return ", row_number() over ({$orderings}) as row_num";
return Arr::sort($query->bindings, function ($bindings, $key) {
return array_search($key, ['select', 'order', 'from', 'join', 'where', 'groupBy', 'having', 'union', 'unionOrder']);
});
}

/**
Expand Down

0 comments on commit 401928b

Please sign in to comment.