Permalink
Browse files

Fix LIMIT/OFFSET queries handling on MS SQL Server (#1488984) - requi…

…re version 2005+
  • Loading branch information...
1 parent 65de001 commit ec6a77bab27984ce05b003af07ac9f42ca410d94 @alecpl alecpl committed Mar 11, 2013
Showing with 33 additions and 30 deletions.
  1. +1 −0 CHANGELOG
  2. +1 −3 INSTALL
  3. +15 −15 program/lib/Roundcube/rcube_db_mssql.php
  4. +16 −12 program/lib/Roundcube/rcube_db_sqlsrv.php
View
1 CHANGELOG
@@ -1,6 +1,7 @@
CHANGELOG Roundcube Webmail
===========================
+- Fix LIMIT/OFFSET queries handling on MS SQL Server (#1488984)
- Fix so task name can really contain all from a-z0-9_- characters (#1488941)
- Support IMAP MOVE extension [RFC 6851]
- Fix javascript errors when working in a page opened with taget="_blank"
View
4 INSTALL
@@ -34,7 +34,7 @@ REQUIREMENTS
- magic_quotes_runtime disabled
- magic_quotes_sybase disabled
* PHP compiled with OpenSSL to connect to IMAPS and to use the spell checker
-* A MySQL (4.0.8 or newer), PostgreSQL, MSSQL database engine
+* A MySQL (4.0.8 or newer), PostgreSQL, MS SQL Server (2005 or newer) database engine
or SQLite support in PHP
* One of the above databases with permission to create tables
* An SMTP server (recommended) or PHP configured for mail delivery
@@ -232,5 +232,3 @@ $HTTP["host"] == "www.example.com" {
compress.filetype = ("text/plain", "text/html", "text/javascript", "text/css", "text/xml", "image/gif", "image/png")
}
-
-
View
30 program/lib/Roundcube/rcube_db_mssql.php
@@ -100,30 +100,30 @@ protected function set_limit($query, $limit = 0, $offset = 0)
{
$limit = intval($limit);
$offset = intval($offset);
+ $end = $offset + $limit;
- $orderby = stristr($query, 'ORDER BY');
- 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 = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . ($limit + $offset) . ' ', $query);
-
+ // query without OFFSET
if (!$offset) {
+ $query = preg_replace('/^SELECT\s/i', "SELECT TOP $limit ", $query);
return $query;
}
- $query = 'SELECT * FROM (SELECT TOP ' . $limit . ' * FROM (' . $query . ') AS inner_tbl';
+ $orderby = stristr($query, 'ORDER BY');
+ $offset += 1;
+
if ($orderby !== false) {
- $query .= ' ORDER BY ' . $order . ' ';
- $query .= (stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC';
+ $query = trim(substr($query, 0, -1 * strlen($orderby)));
}
- $query .= ') AS outer_tbl';
- if ($orderby !== false) {
- $query .= ' ORDER BY ' . $order . ' ' . $sort;
+ 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', '', $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;
}
View
28 program/lib/Roundcube/rcube_db_sqlsrv.php
@@ -100,26 +100,30 @@ protected function set_limit($query, $limit = 0, $offset = 0)
{
$limit = intval($limit);
$offset = intval($offset);
+ $end = $offset + $limit;
- $orderby = stristr($query, 'ORDER BY');
- 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 without OFFSET
+ if (!$offset) {
+ $query = preg_replace('/^SELECT\s/i', "SELECT TOP $limit ", $query);
+ return $query;
}
- $query = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . ($limit + $offset) . ' ', $query);
+ $orderby = stristr($query, 'ORDER BY');
+ $offset += 1;
- $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 = trim(substr($query, 0, -1 * strlen($orderby)));
}
- $query .= ') AS outer_tbl';
- if ($orderby !== false) {
- $query .= ' ORDER BY ' . $order . ' ' . $sort;
+ 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', '', $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;
}

0 comments on commit ec6a77b

Please sign in to comment.