Skip to content

Commit

Permalink
closes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
mindplay-dk committed May 28, 2024
1 parent e0047f0 commit 6a5662a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/framework/pdo/PreparedPDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use mindplay\sql\model\Driver;
use mindplay\sql\model\TypeProvider;
use PDO;
use PDOException;
use PDOStatement;

/**
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 33 additions & 1 deletion test/test-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6a5662a

Please sign in to comment.