From 0585ef390860bf0c4d678aec5067d5661511b54c Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 16 Jun 2009 04:04:01 +0000 Subject: [PATCH] BUGFIX: Fixed limit/offset code in MSSQL --- code/MSSQLDatabase.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/code/MSSQLDatabase.php b/code/MSSQLDatabase.php index 2d912e1..86ab7a9 100644 --- a/code/MSSQLDatabase.php +++ b/code/MSSQLDatabase.php @@ -1006,9 +1006,11 @@ class MSSQLDatabase extends Database { } else { //could be a comma delimited string $bits=explode(',', $sqlQuery->limit); - $limit=trim($bits[0]); - if(isset($bits[1])) - $offset=trim($bits[1]); + if(sizeof($bits) > 1) { + list($offset, $limit) = $bits; + } else { + $limit = $bits[0]; + } } $text = ''; @@ -1030,9 +1032,14 @@ class MSSQLDatabase extends Database { // If there's a limit and an offset, then we need to do a subselect } else if($limit && $offset) { - $text = "SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY $sqlQuery->orderby)" - . " AS Number, "; - $suffixText .= ") AS Numbered WHERE Number BETWEEN $offset AND " . ($offset+$limit) + if($sqlQuery->orderby) { + $rowNumber = "ROW_NUMBER() OVER (ORDER BY $sqlQuery->orderby) AS Number"; + } else { + $firstCol = reset($sqlQuery->select); + $rowNumber = "ROW_NUMBER() OVER (ORDER BY $firstCol) AS Number"; + } + $text = "SELECT * FROM ( SELECT $rowNumber, "; + $suffixText .= ") AS Numbered WHERE Number BETWEEN " . ($offset+1) ." AND " . ($offset+$limit) . " ORDER BY Number"; $nestedQuery = true;