Browse Source

ZF-3216
Zend_Db_Statement_Mysqli now includes the native MySQL error codes as part of any exceptions it throws. Includes relevant unit test.

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

jimbojsb 16 năm trước cách đây
mục cha
commit
6de0c73ef0

+ 3 - 3
library/Zend/Db/Statement/Mysqli.php

@@ -81,7 +81,7 @@ class Zend_Db_Statement_Mysqli extends Zend_Db_Statement
              * @see Zend_Db_Statement_Mysqli_Exception
              */
             require_once 'Zend/Db/Statement/Mysqli/Exception.php';
-            throw new Zend_Db_Statement_Mysqli_Exception("Mysqli prepare error: " . $mysqli->error);
+            throw new Zend_Db_Statement_Mysqli_Exception("Mysqli prepare error: " . $mysqli->error, $mysqli->errno);
         }
     }
 
@@ -218,7 +218,7 @@ class Zend_Db_Statement_Mysqli extends Zend_Db_Statement
              * @see Zend_Db_Statement_Mysqli_Exception
              */
             require_once 'Zend/Db/Statement/Mysqli/Exception.php';
-            throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement execute error : " . $this->_stmt->error);
+            throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement execute error : " . $this->_stmt->error, $this->_stmt->errno);
         }
 
 
@@ -230,7 +230,7 @@ class Zend_Db_Statement_Mysqli extends Zend_Db_Statement
                  * @see Zend_Db_Statement_Mysqli_Exception
                  */
                 require_once 'Zend/Db/Statement/Mysqli/Exception.php';
-                throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement metadata error: " . $this->_stmt->error);
+                throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement metadata error: " . $this->_stmt->error, $this->_stmt->errno);
             }
         }
 

+ 16 - 0
tests/Zend/Db/Statement/MysqliTest.php

@@ -106,6 +106,22 @@ class Zend_Db_Statement_MysqliTest extends Zend_Db_Statement_TestCommon
     {
         $this->markTestIncomplete($this->getDriver() . ' has not implemented getColumnMeta() yet [ZF-1424]');
     }
+    
+    /**
+     * Tests ZF-3216, that the statement object throws exceptions that
+     * contain the numerica MySQL SQLSTATE error code
+     * @group ZF-3216
+     */
+    public function testStatementExceptionShouldContainErrorCode()
+    {
+        $sql = "SELECT * FROM *";
+        try {
+            $stmt = $this->_db->query($sql);
+            $this->fail('Expected to catch Zend_Db_Statement_Exception');
+        } catch (Zend_Exception $e) {
+            $this->assertType('int', $e->getCode());
+        }
+    }
 
     public function getDriver()
     {