BUG Fix SortColumn ORDER BY not working when using a limit

MSSQL doesn't support putting an alias into the OVER clause,
something which is required when limiting results with an offset.
The workaround is to just put the aggregate inline, taken from
the select list.
This commit is contained in:
Sean Harvey 2012-12-20 15:56:11 +13:00
parent baf3d39cc0
commit 7c369a383a

View File

@ -1230,8 +1230,20 @@ class MSSQLDatabase extends SS_Database {
// If there's a limit and an offset, then we need to do a subselect
} else if($limit && $offset) {
if($query->getOrderBy()) {
$orderByClause = $this->sqlOrderByToString($query->getOrderBy());
$orderby = $query->getOrderBy();
// workaround for subselect not working with alias functions
// just use the function directly in the order by instead of the alias
$selects = $query->getSelect();
foreach($orderby as $field => $dir) {
if(preg_match('/SortColumn/', $field)) {
unset($orderby[$field]);
$orderby[$selects[str_replace('"', '', $field)]] = $dir;
}
}
if($orderby) {
$orderByClause = $this->sqlOrderByToString($orderby);
$rowNumber = "ROW_NUMBER() OVER ($orderByClause) AS Number";
} else {
$selects = $query->getSelect();