ZF-1710: DB2: Improved error reporting from errorCode() and errorInfo() methods in Statement class


Calling db2_stmt_error() or db2_stmt_errormsg() without a statement resource parameter will also return the error message from the last operation, if there is one. This message is cached so no matter how many times it is called and as long as another SQL operation doesn't occur, it will return the same message

If either function is called with a statement resource, it will return the message associated with that particular resource. A subsequent call with the same resource parameter attempts to retrieve further diagnostic data. If there is no more diagnostic data, nothing is returned.

In the ZF, a particular method, such as execute(), may throw an exception. The exception object is populated with specific diagnostic information using db2_stmt_error() and db2_stmt_erromsg(). For example, this is what _execute() looks like for the DB2 adapter:

if ($retval === false) {
    * @see Zend_Db_Statement_Db2_Exception
   require_once 'Zend/Db/Statement/Db2/Exception.php';
         throw new Zend_Db_Statement_Db2_Exception(

When an application detects an exception it may try to call errorCode() or errorInfo(). The problem is that both these methods call db2_stmt_error() with the $this->_stmt instance variable again.
For example:

   public function errorCode()
        if (!$this->_stmt) {
            return '0000';
        return db2_stmt_error($this->_stmt);

Since db2_stmt_error($this->_stmt) has been previously called when the exception was generated, the driver attempts to obtain further diagnostic data. If there is none, nothing is returned giving the impression that there is no error message.

The patch removes the $this->_stmt parameter from the errorCode() and errorInfo() methods so that repeated calls to db_stmt_error() and db2_stmt_errormsg() return the same message previously cached.

I will upload the patch as well as the modified DB2 unit test.


Contains modified errorCode() and errorInfo() methods

removed skipped error tests

Assigning to [~ledezma]

Hy Salvador,

if you are not able to commit this patch, you should un-assign this issue so anyone with commit-rights will do this.

Otherwise please commit your changes and close this issue. ;-)

I had submitted these patches for review by someone with commit authority.

Please use as you see fit.

Resetting 'fix version priority' and 'fix version' to be re-evaluated for next release.

Solved in SVN r 13091. If no error occurs, errorCode() and errorInfo() method returns false like Oracle and MySqli.

Unit test not updated to reflect changes.

fixed in r13279, still needs merging - will do so close to next mini release.

I checked to be merged from trunk to branch/release-1.7.