|
9 | 9 |
|
10 | 10 | namespace Zend\Db\Adapter\Driver\IbmDb2;
|
11 | 11 |
|
| 12 | +use ErrorException; |
12 | 13 | use Zend\Db\Adapter\Driver\StatementInterface;
|
13 | 14 | use Zend\Db\Adapter\Exception;
|
14 | 15 | use Zend\Db\Adapter\ParameterContainer;
|
@@ -172,7 +173,14 @@ public function prepare($sql = null)
|
172 | 173 | $sql = $this->sql;
|
173 | 174 | }
|
174 | 175 |
|
175 |
| - $this->resource = db2_prepare($this->db2, $sql); |
| 176 | + try { |
| 177 | + set_error_handler($this->createErrorHandler()); |
| 178 | + $this->resource = db2_prepare($this->db2, $sql); |
| 179 | + } catch (ErrorException $e) { |
| 180 | + throw new Exception\RuntimeException($e->getMessage() . '. ' . db2_stmt_errormsg(), db2_stmt_error(), $e); |
| 181 | + } finally { |
| 182 | + restore_error_handler(); |
| 183 | + } |
176 | 184 |
|
177 | 185 | if ($this->resource === false) {
|
178 | 186 | throw new Exception\RuntimeException(db2_stmt_errormsg(), db2_stmt_error());
|
@@ -239,4 +247,31 @@ public function execute($parameters = null)
|
239 | 247 | $result = $this->driver->createResult($this->resource);
|
240 | 248 | return $result;
|
241 | 249 | }
|
| 250 | + |
| 251 | + /** |
| 252 | + * Creates and returns a callable error handler that raises exceptions. |
| 253 | + * |
| 254 | + * Only raises exceptions for errors that are within the error_reporting mask. |
| 255 | + * |
| 256 | + * @return callable |
| 257 | + */ |
| 258 | + private function createErrorHandler() |
| 259 | + { |
| 260 | + /** |
| 261 | + * @param int $errno |
| 262 | + * @param string $errstr |
| 263 | + * @param string $errfile |
| 264 | + * @param int $errline |
| 265 | + * @return void |
| 266 | + * @throws ErrorException if error is not within the error_reporting mask. |
| 267 | + */ |
| 268 | + return function ($errno, $errstr, $errfile, $errline) { |
| 269 | + if (! (error_reporting() & $errno)) { |
| 270 | + // error_reporting does not include this error |
| 271 | + return; |
| 272 | + } |
| 273 | + |
| 274 | + throw new ErrorException($errstr, 0, $errno, $errfile, $errline); |
| 275 | + }; |
| 276 | + } |
242 | 277 | }
|
0 commit comments