Permalink
Browse files

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.
  • Loading branch information...
1 parent baf3d39 commit 7c369a383a8e6a6828a74209d70cc88678d7f411 @halkyon halkyon committed Dec 20, 2012
Showing with 14 additions and 2 deletions.
  1. +14 −2 code/MSSQLDatabase.php
View
16 code/MSSQLDatabase.php
@@ -1230,8 +1230,20 @@ public function sqlQueryToString(SQLQuery $query) {
// 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();

0 comments on commit 7c369a3

Please sign in to comment.