Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 7c369a383a8e6a6828a74209d70cc88678d7f411 1 parent baf3d39
Sean Harvey halkyon authored
Showing with 14 additions and 2 deletions.
  1. +14 −2 code/MSSQLDatabase.php
16 code/MSSQLDatabase.php
View
@@ -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();
Please sign in to comment.
Something went wrong with that request. Please try again.