From 6a5662a7663e6332a87620b5fa6df21797030d23 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Tue, 28 May 2024 11:58:45 +0200 Subject: [PATCH] closes #52 --- src/framework/pdo/PreparedPDOStatement.php | 11 ++++++- test/test-integration.php | 34 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/framework/pdo/PreparedPDOStatement.php b/src/framework/pdo/PreparedPDOStatement.php index ce3f0ad..b62420f 100644 --- a/src/framework/pdo/PreparedPDOStatement.php +++ b/src/framework/pdo/PreparedPDOStatement.php @@ -8,6 +8,7 @@ use mindplay\sql\model\Driver; use mindplay\sql\model\TypeProvider; use PDO; +use PDOException; use PDOStatement; /** @@ -104,7 +105,15 @@ public function execute() { $microtime_begin = microtime(true); - if (@$this->handle->execute()) { + $success = false; + + try { + $success = @$this->handle->execute(); + } catch (PDOException $e) { + // error will be handled below + } + + if ($success) { $this->executed = true; $microtime_end = microtime(true); $time_msec = ($microtime_end - $microtime_begin) * 1000; diff --git a/test/test-integration.php b/test/test-integration.php index cd05483..6c36236 100644 --- a/test/test-integration.php +++ b/test/test-integration.php @@ -3,8 +3,9 @@ use mindplay\sql\framework\pdo\PDOProvider; use mindplay\sql\mysql\MySQLDatabase; use mindplay\sql\postgres\PostgresDatabase; +use mindplay\sql\exceptions\SQLException; -use function mindplay\testies\{ test, eq }; +use function mindplay\testies\{ test, eq, expect }; $config = file_exists(__DIR__ . '/config.json') ? json_decode(file_get_contents(__DIR__ . '/config.json'), true) @@ -42,6 +43,37 @@ function () use ($config) { } ); +test( + 'handles PDO::ERRMODE_EXCEPTION and produces an SQLException', + function () use ($config) { + $postgres_config = [ + ...$config["postgres"], + "options" => [ + ...$config["postgres"]["options"] ?? [], + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION + ] + ]; + + $provider = new PDOProvider( + PDOProvider::PROTOCOL_POSTGRES, + ...$postgres_config + ); + + $db = new PostgresDatabase(); + + $connection = $db->createConnection($provider->getPDO()); + + expect( + SQLException::class, + 'pdo exception mode is handled', + function () use ($connection, $db) { + $connection->fetch($db->sql('invalid syntax'))->firstCol(); + }, + ['/42601: ERROR: syntax error/'] + ); + } +); + // TODO tests for prepared statements // TODO test for PreparedStatement::getRowsAffected()