Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 1 changed file with 14 additions and 2 deletions. Show diff stats Hide diff stats

  1. +14 2 code/MSSQLDatabase.php
16 code/MSSQLDatabase.php
@@ -1230,8 +1230,20 @@ public function sqlQueryToString(SQLQuery $query) {
1230 1230
1231 1231 // If there's a limit and an offset, then we need to do a subselect
1232 1232 } else if($limit && $offset) {
1233   - if($query->getOrderBy()) {
1234   - $orderByClause = $this->sqlOrderByToString($query->getOrderBy());
  1233 + $orderby = $query->getOrderBy();
  1234 +
  1235 + // workaround for subselect not working with alias functions
  1236 + // just use the function directly in the order by instead of the alias
  1237 + $selects = $query->getSelect();
  1238 + foreach($orderby as $field => $dir) {
  1239 + if(preg_match('/SortColumn/', $field)) {
  1240 + unset($orderby[$field]);
  1241 + $orderby[$selects[str_replace('"', '', $field)]] = $dir;
  1242 + }
  1243 + }
  1244 +
  1245 + if($orderby) {
  1246 + $orderByClause = $this->sqlOrderByToString($orderby);
1235 1247 $rowNumber = "ROW_NUMBER() OVER ($orderByClause) AS Number";
1236 1248 } else {
1237 1249 $selects = $query->getSelect();

0 comments on commit 7c369a3

Please sign in to comment.
Something went wrong with that request. Please try again.