Skip to content

Commit

Permalink
Fixed pagination for SqlServer
Browse files Browse the repository at this point in the history
  • Loading branch information
predominant committed Jun 21, 2011
1 parent c1e674f commit f1108b2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/Cake/Model/Datasource/Database/Sqlserver.php
Expand Up @@ -517,7 +517,20 @@ public function renderStatement($type, $data) {
$offset = intval($offset[1]) + intval($limitVal[1]);
$rOrder = $this->__switchSort($order);
list($order2, $rOrder) = array($this->__mapFields($order), $this->__mapFields($rOrder));
return "SELECT * FROM (SELECT {$limit} * FROM (SELECT TOP {$offset} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order}) AS Set1 {$rOrder}) AS Set2 {$order2}";
$limit2 = str_replace('TOP', '', $limit);
if (!$order) {
$order = 'ORDER BY (SELECT NULL)';
}

$pagination = "
SELECT {$limit} * FROM (
SELECT {$fields}, ROW_NUMBER() OVER ({$order}) AS ssma\$rownum
FROM {$table} {$alias} {$joins} {$conditions} {$group}
) AS ssma\$sub1
WHERE ssma\$sub1.[ssma\$rownum] > {$limit2}
ORDER BY ssma\$sub1.[ssma\$rownum]
";
return $pagination;
} else {
return "SELECT {$limit} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order}";
}
Expand Down

0 comments on commit f1108b2

Please sign in to comment.