|
|
@@ -314,46 +314,50 @@ class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
|
|
|
throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
|
|
|
}
|
|
|
|
|
|
- $orderby = stristr($sql, 'ORDER BY');
|
|
|
-
|
|
|
- if ($orderby !== false) {
|
|
|
- $orderParts = explode(',', substr($orderby, 8));
|
|
|
- $pregReplaceCount = null;
|
|
|
- $orderbyInverseParts = array();
|
|
|
- foreach ($orderParts as $orderPart) {
|
|
|
- $inv = preg_replace('/\s+desc$/i', ' ASC', $orderPart, 1, $pregReplaceCount);
|
|
|
- if ($pregReplaceCount) {
|
|
|
- $orderbyInverseParts[] = $inv;
|
|
|
- continue;
|
|
|
- }
|
|
|
- $inv = preg_replace('/\s+asc$/i', ' DESC', $orderPart, 1, $pregReplaceCount);
|
|
|
- if ($pregReplaceCount) {
|
|
|
- $orderbyInverseParts[] = $inv;
|
|
|
- continue;
|
|
|
- } else {
|
|
|
- $orderbyInverseParts[] = $orderPart . ' DESC';
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- $orderbyInverse = 'ORDER BY ' . implode(', ', $orderbyInverseParts);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
$sql = preg_replace(
|
|
|
'/^SELECT\s+(DISTINCT\s)?/i',
|
|
|
'SELECT $1TOP ' . ($count+$offset) . ' ',
|
|
|
$sql
|
|
|
);
|
|
|
-
|
|
|
- $sql = 'SELECT * FROM (SELECT TOP ' . $count . ' * FROM (' . $sql . ') AS inner_tbl';
|
|
|
- if ($orderby !== false) {
|
|
|
- $sql .= ' ' . $orderbyInverse . ' ';
|
|
|
- }
|
|
|
- $sql .= ') AS outer_tbl';
|
|
|
- if ($orderby !== false) {
|
|
|
- $sql .= ' ' . $orderby;
|
|
|
+
|
|
|
+ if ($offset > 0) {
|
|
|
+ $orderby = stristr($sql, 'ORDER BY');
|
|
|
+
|
|
|
+ if ($orderby !== false) {
|
|
|
+ $orderParts = explode(',', substr($orderby, 8));
|
|
|
+ $pregReplaceCount = null;
|
|
|
+ $orderbyInverseParts = array();
|
|
|
+ foreach ($orderParts as $orderPart) {
|
|
|
+ $inv = preg_replace('/\s+desc$/i', ' ASC', $orderPart, 1, $pregReplaceCount);
|
|
|
+ if ($pregReplaceCount) {
|
|
|
+ $orderbyInverseParts[] = $inv;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $inv = preg_replace('/\s+asc$/i', ' DESC', $orderPart, 1, $pregReplaceCount);
|
|
|
+ if ($pregReplaceCount) {
|
|
|
+ $orderbyInverseParts[] = $inv;
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ $orderbyInverseParts[] = $orderPart . ' DESC';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $orderbyInverse = 'ORDER BY ' . implode(', ', $orderbyInverseParts);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $sql = 'SELECT * FROM (SELECT TOP ' . $count . ' * FROM (' . $sql . ') AS inner_tbl';
|
|
|
+ if ($orderby !== false) {
|
|
|
+ $sql .= ' ' . $orderbyInverse . ' ';
|
|
|
+ }
|
|
|
+ $sql .= ') AS outer_tbl';
|
|
|
+ if ($orderby !== false) {
|
|
|
+ $sql .= ' ' . $orderby;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return $sql;
|
|
|
}
|
|
|
|