From 92cd7f34b07e86062f2c024039e3309768b48ce6 Mon Sep 17 00:00:00 2001
From: Andy Wermke <andy@dev.next-step-software.com>
Date: Thu, 04 Apr 2013 10:10:23 -0400
Subject: [PATCH] Merge branch 'master' of https://github.com/roundcube/roundcubemail

---
 program/lib/Roundcube/rcube_db_mssql.php |   36 ++++++++++++++++++------------------
 1 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/program/lib/Roundcube/rcube_db_mssql.php b/program/lib/Roundcube/rcube_db_mssql.php
index c95663c..37a4267 100644
--- a/program/lib/Roundcube/rcube_db_mssql.php
+++ b/program/lib/Roundcube/rcube_db_mssql.php
@@ -2,8 +2,6 @@
 
 /**
  +-----------------------------------------------------------------------+
- | program/include/rcube_db_mssql.php                                    |
- |                                                                       |
  | This file is part of the Roundcube Webmail client                     |
  | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
  |                                                                       |
@@ -14,12 +12,10 @@
  | PURPOSE:                                                              |
  |   Database wrapper class that implements PHP PDO functions            |
  |   for MS SQL Server database                                          |
- |                                                                       |
  +-----------------------------------------------------------------------+
  | Author: Aleksander Machniak <alec@alec.pl>                            |
  +-----------------------------------------------------------------------+
 */
-
 
 /**
  * Database independent query interface
@@ -104,25 +100,29 @@
     {
         $limit  = intval($limit);
         $offset = intval($offset);
+        $end    = $offset + $limit;
+
+        // query without OFFSET
+        if (!$offset) {
+            $query = preg_replace('/^SELECT\s/i', "SELECT TOP $limit ", $query);
+            return $query;
+        }
 
         $orderby = stristr($query, 'ORDER BY');
+        $offset += 1;
+
         if ($orderby !== false) {
-            $sort  = (stripos($orderby, ' desc') !== false) ? 'desc' : 'asc';
-            $order = str_ireplace('ORDER BY', '', $orderby);
-            $order = trim(preg_replace('/\bASC\b|\bDESC\b/i', '', $order));
+            $query = trim(substr($query, 0, -1 * strlen($orderby)));
+        }
+        else {
+            // it shouldn't happen, paging without sorting has not much sense
+            // @FIXME: I don't know how to build paging query without ORDER BY
+            $orderby = "ORDER BY 1";
         }
 
-        $query = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . ($limit + $offset) . ' ', $query);
-
-        $query = 'SELECT * FROM (SELECT TOP ' . $limit . ' * FROM (' . $query . ') AS inner_tbl';
-        if ($orderby !== false) {
-            $query .= ' ORDER BY ' . $order . ' ';
-            $query .= (stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC';
-        }
-        $query .= ') AS outer_tbl';
-        if ($orderby !== false) {
-            $query .= ' ORDER BY ' . $order . ' ' . $sort;
-        }
+        $query = preg_replace('/^SELECT\s/i', '', $query);
+        $query = "WITH paging AS (SELECT ROW_NUMBER() OVER ($orderby) AS [RowNumber], $query)"
+            . " SELECT * FROM paging WHERE [RowNumber] BETWEEN $offset AND $end ORDER BY [RowNumber]";
 
         return $query;
     }

--
Gitblit v1.9.1