Przeglądaj źródła

ZF-4251
- Fix for usage of DISTINCT in limit query for Pdo_Mssql

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17713 44c647ce-9c0f-0410-b52a-842ac1e357ba

ralph 16 lat temu
rodzic
commit
b2cd4107ea

+ 6 - 1
library/Zend/Db/Adapter/Pdo/Mssql.php

@@ -338,7 +338,12 @@ class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
             $orderbyInverse = 'ORDER BY ' . implode(', ', $orderbyInverseParts);
         }
 
-        $sql = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . ($count+$offset) . ' ', $sql);
+        
+        $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) {

+ 19 - 0
tests/Zend/Db/Adapter/Pdo/MssqlTest.php

@@ -345,6 +345,25 @@ class Zend_Db_Adapter_Pdo_MssqlTest extends Zend_Db_Adapter_Pdo_TestCommon
         $this->assertEquals($expectedProducts, $products);
     }
     
+    public function testAdapterLimitWorksWithDistinctClause()
+    {
+        $this->_db->insert('zfproducts', array('product_name' => 'Unix'));
+        $this->_db->insert('zfproducts', array('product_name' => 'Windows'));
+        $this->_db->insert('zfproducts', array('product_name' => 'AIX'));
+        $this->_db->insert('zfproducts', array('product_name' => 'I5'));
+        $this->_db->insert('zfproducts', array('product_name' => 'Linux'));
+    	
+    	$sql = 'SELECT DISTINCT product_name FROM zfproducts ORDER BY product_name DESC';
+    	$sql = $this->_db->limit($sql, 3, 3);
+    	$products = $this->_db->fetchAll($sql);
+    	$expectedProducts = array(
+    	   0 => array('product_name' => 'Linux'),
+    	   1 => array('product_name' => 'I5'),
+    	   2 => array('product_name' => 'AIX')
+    	   );
+        $this->assertEquals($expectedProducts, $products);
+    }
+    
     
     public function getDriver()
     {