Expect driver-layer classes to throw only Driver\Exception

parent 7f5506d7
...@@ -467,7 +467,7 @@ class Connection implements DriverConnection ...@@ -467,7 +467,7 @@ class Connection implements DriverConnection
{ {
try { try {
return $this->executeQuery($query, $params, $types)->fetchAssociative(); return $this->executeQuery($query, $params, $types)->fetchAssociative();
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} }
} }
...@@ -488,7 +488,7 @@ class Connection implements DriverConnection ...@@ -488,7 +488,7 @@ class Connection implements DriverConnection
{ {
try { try {
return $this->executeQuery($query, $params, $types)->fetchNumeric(); return $this->executeQuery($query, $params, $types)->fetchNumeric();
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} }
} }
...@@ -509,7 +509,7 @@ class Connection implements DriverConnection ...@@ -509,7 +509,7 @@ class Connection implements DriverConnection
{ {
try { try {
return $this->executeQuery($query, $params, $types)->fetchOne(); return $this->executeQuery($query, $params, $types)->fetchOne();
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} }
} }
...@@ -771,7 +771,7 @@ class Connection implements DriverConnection ...@@ -771,7 +771,7 @@ class Connection implements DriverConnection
{ {
try { try {
return $this->executeQuery($query, $params, $types)->fetchAllNumeric(); return $this->executeQuery($query, $params, $types)->fetchAllNumeric();
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} }
} }
...@@ -791,7 +791,7 @@ class Connection implements DriverConnection ...@@ -791,7 +791,7 @@ class Connection implements DriverConnection
{ {
try { try {
return $this->executeQuery($query, $params, $types)->fetchAllAssociative(); return $this->executeQuery($query, $params, $types)->fetchAllAssociative();
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} }
} }
...@@ -811,7 +811,7 @@ class Connection implements DriverConnection ...@@ -811,7 +811,7 @@ class Connection implements DriverConnection
{ {
try { try {
return $this->executeQuery($query, $params, $types)->fetchFirstColumn(); return $this->executeQuery($query, $params, $types)->fetchFirstColumn();
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} }
} }
...@@ -835,7 +835,7 @@ class Connection implements DriverConnection ...@@ -835,7 +835,7 @@ class Connection implements DriverConnection
while (($row = $result->fetchNumeric()) !== false) { while (($row = $result->fetchNumeric()) !== false) {
yield $row; yield $row;
} }
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} }
} }
...@@ -859,7 +859,7 @@ class Connection implements DriverConnection ...@@ -859,7 +859,7 @@ class Connection implements DriverConnection
while (($row = $result->fetchAssociative()) !== false) { while (($row = $result->fetchAssociative()) !== false) {
yield $row; yield $row;
} }
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} }
} }
...@@ -883,7 +883,7 @@ class Connection implements DriverConnection ...@@ -883,7 +883,7 @@ class Connection implements DriverConnection
while (($value = $result->fetchOne()) !== false) { while (($value = $result->fetchOne()) !== false) {
yield $value; yield $value;
} }
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} }
} }
...@@ -899,11 +899,7 @@ class Connection implements DriverConnection ...@@ -899,11 +899,7 @@ class Connection implements DriverConnection
*/ */
public function prepare(string $sql): DriverStatement public function prepare(string $sql): DriverStatement
{ {
try { return new Statement($sql, $this);
return new Statement($sql, $this);
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $sql);
}
} }
/** /**
...@@ -952,7 +948,7 @@ class Connection implements DriverConnection ...@@ -952,7 +948,7 @@ class Connection implements DriverConnection
} }
return new Result($result, $this); return new Result($result, $this);
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} finally { } finally {
if ($logger !== null) { if ($logger !== null) {
...@@ -1024,7 +1020,7 @@ class Connection implements DriverConnection ...@@ -1024,7 +1020,7 @@ class Connection implements DriverConnection
try { try {
return $connection->query($sql); return $connection->query($sql);
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $sql); $this->handleExceptionDuringQuery($e, $sql);
} finally { } finally {
if ($logger !== null) { if ($logger !== null) {
...@@ -1072,7 +1068,7 @@ class Connection implements DriverConnection ...@@ -1072,7 +1068,7 @@ class Connection implements DriverConnection
} }
return $connection->exec($query); return $connection->exec($query);
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types); $this->handleExceptionDuringQuery($e, $query, $params, $types);
} finally { } finally {
if ($logger !== null) { if ($logger !== null) {
...@@ -1095,7 +1091,7 @@ class Connection implements DriverConnection ...@@ -1095,7 +1091,7 @@ class Connection implements DriverConnection
try { try {
return $connection->exec($statement); return $connection->exec($statement);
} catch (Throwable $e) { } catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $statement); $this->handleExceptionDuringQuery($e, $statement);
} finally { } finally {
if ($logger !== null) { if ($logger !== null) {
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
namespace Doctrine\DBAL; namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\Result as DriverResult; use Doctrine\DBAL\Driver\Result as DriverResult;
use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Throwable;
use function is_string; use function is_string;
...@@ -70,8 +70,16 @@ class Statement implements DriverStatement ...@@ -70,8 +70,16 @@ class Statement implements DriverStatement
*/ */
public function __construct($sql, Connection $conn) public function __construct($sql, Connection $conn)
{ {
$driverConnection = $conn->getWrappedConnection();
try {
$stmt = $driverConnection->prepare($sql);
} catch (Exception $ex) {
$conn->handleExceptionDuringQuery($ex, $sql);
}
$this->sql = $sql; $this->sql = $sql;
$this->stmt = $conn->getWrappedConnection()->prepare($sql); $this->stmt = $stmt;
$this->conn = $conn; $this->conn = $conn;
$this->platform = $conn->getDatabasePlatform(); $this->platform = $conn->getDatabasePlatform();
} }
...@@ -156,7 +164,7 @@ class Statement implements DriverStatement ...@@ -156,7 +164,7 @@ class Statement implements DriverStatement
$this->stmt->execute($params), $this->stmt->execute($params),
$this->conn $this->conn
); );
} catch (Throwable $ex) { } catch (Exception $ex) {
$this->conn->handleExceptionDuringQuery($ex, $this->sql, $this->params, $this->types); $this->conn->handleExceptionDuringQuery($ex, $this->sql, $this->params, $this->types);
} finally { } finally {
if ($logger !== null) { if ($logger !== null) {
......
...@@ -190,66 +190,6 @@ class DataAccessTest extends FunctionalTestCase ...@@ -190,66 +190,6 @@ class DataAccessTest extends FunctionalTestCase
self::assertStringStartsWith($datetimeString, $row['test_datetime']); self::assertStringStartsWith($datetimeString, $row['test_datetime']);
} }
/**
* @group DBAL-209
* @dataProvider fetchProvider
*/
public function testFetchAllWithMissingTypes(callable $fetch): void
{
if (
$this->connection->getDriver() instanceof MySQLiDriver ||
$this->connection->getDriver() instanceof SQLSrvDriver
) {
self::markTestSkipped('mysqli and sqlsrv actually supports this');
}
if (
$this->connection->getDriver() instanceof IBMDB2Driver
) {
$this->markTestSkipped(
'ibm_ibm2 may or may not report the error depending on the PHP version and the connection state'
);
}
$datetimeString = '2010-01-01 10:10:10';
$datetime = new DateTime($datetimeString);
$sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?';
$this->expectException(DBALException::class);
$fetch($this->connection, $sql, [1, $datetime]);
}
/**
* @return iterable<string,array{0:callable}>
*/
public static function fetchProvider(): iterable
{
yield 'fetch-all-associative' => [
static function (Connection $connection, string $query, array $params): void {
$connection->fetchAllAssociative($query, $params);
},
];
yield 'fetch-numeric' => [
static function (Connection $connection, string $query, array $params): void {
$connection->fetchNumeric($query, $params);
},
];
yield 'fetch-associative' => [
static function (Connection $connection, string $query, array $params): void {
$connection->fetchAssociative($query, $params);
},
];
yield 'fetch-one' => [
static function (Connection $connection, string $query, array $params): void {
$connection->fetchOne($query, $params);
},
];
}
public function testFetchNoResult(): void public function testFetchNoResult(): void
{ {
self::assertFalse( self::assertFalse(
......
...@@ -7,11 +7,11 @@ use Doctrine\DBAL\Connection; ...@@ -7,11 +7,11 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\Exception as DriverException;
use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Logging\SQLLogger; use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Statement; use Doctrine\DBAL\Statement;
use Exception;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
...@@ -139,7 +139,9 @@ class StatementTest extends TestCase ...@@ -139,7 +139,9 @@ class StatementTest extends TestCase
$this->driverStatement->expects(self::once()) $this->driverStatement->expects(self::once())
->method('execute') ->method('execute')
->will(self::throwException(new Exception('Mock test exception'))); ->will(self::throwException(
$this->createMock(DriverException::class)
));
$statement = new Statement('', $this->conn); $statement = new Statement('', $this->conn);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment