Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Implement our own error handler for db2_prepare #273

Merged
merged 10 commits into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/Adapter/Driver/IbmDb2/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\Db\Adapter\Driver\IbmDb2;

use ErrorException;
use Throwable;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\ParameterContainer;
Expand Down Expand Up @@ -172,7 +174,14 @@ public function prepare($sql = null)
$sql = $this->sql;
}

$this->resource = db2_prepare($this->db2, $sql);
try {
set_error_handler($this->createErrorHandler());
$this->resource = db2_prepare($this->db2, $sql);
} catch (Throwable $e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should catch Exception for php 5.5/5.6 fallback as well ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - in 5.6, nothing is thrown.

throw new Exception\RuntimeException($e->getMessage() . '. ' . db2_stmt_errormsg(), db2_stmt_error(), $e);
} finally {
restore_error_handler();
}

if ($this->resource === false) {
throw new Exception\RuntimeException(db2_stmt_errormsg(), db2_stmt_error());
Expand Down Expand Up @@ -238,4 +247,31 @@ public function execute($parameters = null)
$result = $this->driver->createResult($this->resource);
return $result;
}

/**
* Creates and returns a callable error handler that raises exceptions.
*
* Only raises exceptions for errors that are within the error_reporting mask.
*
* @return callable
*/
private function createErrorHandler()
{
/**
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
* @throws ErrorException if error is not within the error_reporting mask.
*/
return function ($errno, $errstr, $errfile, $errline) {
if (! (error_reporting() & $errno)) {
// error_reporting does not include this error
return;
}

throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
};
}
}
20 changes: 19 additions & 1 deletion test/Adapter/Driver/IbmDb2/StatementIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Zend\Db\Adapter\Driver\IbmDb2\IbmDb2;
use Zend\Db\Adapter\Driver\IbmDb2\Statement;
use Zend\Db\Adapter\Exception\RuntimeException;

/**
* @group integration
Expand Down Expand Up @@ -63,7 +64,7 @@ public function testGetResource()

$statement = new Statement;
$statement->initialize($db2Resource);
$statement->prepare("SELECT 'foo'");
$statement->prepare("SELECT 'foo' FROM sysibm.sysdummy1");
$resource = $statement->getResource();
$this->assertEquals('DB2 Statement', get_resource_type($resource));
unset($resource, $db2Resource);
Expand All @@ -85,6 +86,23 @@ public function testPrepare()
unset($resource, $db2Resource);
}

/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::prepare
* @expectedException RuntimeException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move it to function use, because right now it's not clear, which line should thrown that exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webimpress I can't - it doesn't work on the AS/400 when I do that. I have no idea why.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... so maybe exception is thrown not where you expecting it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a PHP error. I'll have another look when I'm next connected to that computer.

*/
public function testPrepareThrowsAnExceptionOnFailure()
{
$db2Resource = db2_connect(
$this->variables['database'],
$this->variables['username'],
$this->variables['password']
);
$statement = new Statement;
$statement->initialize($db2Resource);
$statement->prepare("SELECT");
unset($resource, $db2Resource);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line will be not even hit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will if the test fails :)

}

/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::execute
*/
Expand Down