Ver código fonte

ZF-7629
- added support for supplying schemaname to describeTable() in Pdo_Mssql

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

ralph 16 anos atrás
pai
commit
09f30e1bf6

+ 14 - 0
library/Zend/Db/Adapter/Pdo/Mssql.php

@@ -220,10 +220,20 @@ class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
      */
     public function describeTable($tableName, $schemaName = null)
     {
+        if ($schemaName != null) {
+            if (strpos($schemaName, '.') !== false) {
+                $result = explode('.', $schemaName);
+                $schemaName = $result[1];
+            }
+        }
         /**
          * Discover metadata information about this table.
          */
         $sql = "exec sp_columns @table_name = " . $this->quoteIdentifier($tableName, true);
+        if ($schemaName != null) {
+            $sql .= ", @table_owner = " . $this->quoteIdentifier($schemaName, true);
+        }
+
         $stmt = $this->query($sql);
         $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
 
@@ -241,6 +251,10 @@ class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
          * Discover primary key column(s) for this table.
          */
         $sql = "exec sp_pkeys @table_name = " . $this->quoteIdentifier($tableName, true);
+        if ($schemaName != null) {
+            $sql .= ", @table_owner = " . $this->quoteIdentifier($schemaName, true);
+        }
+        
         $stmt = $this->query($sql);
         $primaryKeysResult = $stmt->fetchAll(Zend_Db::FETCH_NUM);
         $primaryKeyColumn = array();

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

@@ -379,6 +379,16 @@ class Zend_Db_Adapter_Pdo_MssqlTest extends Zend_Db_Adapter_Pdo_TestCommon
         $this->assertEquals('SELECT DISTINCT TOP 3 * FROM foo ORDER BY bar DESC', $this->_db->limit($sql, 3));
     }
     
+    /**
+     * @group ZF-7629
+     */
+    public function testAdapterDescribeTableWithSchemaName()
+    {
+    	$productsTableInfo = $this->_db->describeTable('zfproducts', 'dbo');
+    	$this->assertArrayHasKey('product_id', $productsTableInfo);
+    	$this->assertArrayHasKey('product_name', $productsTableInfo);
+    }
+    
     public function getDriver()
     {
         return 'Pdo_Mssql';