Expect driver-layer classes to throw only Driver\Exception

parent 7f5506d7
......@@ -467,7 +467,7 @@ class Connection implements DriverConnection
{
try {
return $this->executeQuery($query, $params, $types)->fetchAssociative();
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
}
......@@ -488,7 +488,7 @@ class Connection implements DriverConnection
{
try {
return $this->executeQuery($query, $params, $types)->fetchNumeric();
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
}
......@@ -509,7 +509,7 @@ class Connection implements DriverConnection
{
try {
return $this->executeQuery($query, $params, $types)->fetchOne();
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
}
......@@ -771,7 +771,7 @@ class Connection implements DriverConnection
{
try {
return $this->executeQuery($query, $params, $types)->fetchAllNumeric();
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
}
......@@ -791,7 +791,7 @@ class Connection implements DriverConnection
{
try {
return $this->executeQuery($query, $params, $types)->fetchAllAssociative();
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
}
......@@ -811,7 +811,7 @@ class Connection implements DriverConnection
{
try {
return $this->executeQuery($query, $params, $types)->fetchFirstColumn();
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
}
......@@ -835,7 +835,7 @@ class Connection implements DriverConnection
while (($row = $result->fetchNumeric()) !== false) {
yield $row;
}
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
}
......@@ -859,7 +859,7 @@ class Connection implements DriverConnection
while (($row = $result->fetchAssociative()) !== false) {
yield $row;
}
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
}
......@@ -883,7 +883,7 @@ class Connection implements DriverConnection
while (($value = $result->fetchOne()) !== false) {
yield $value;
}
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
}
}
......@@ -899,11 +899,7 @@ class Connection implements DriverConnection
*/
public function prepare(string $sql): DriverStatement
{
try {
return new Statement($sql, $this);
} catch (Throwable $e) {
$this->handleExceptionDuringQuery($e, $sql);
}
return new Statement($sql, $this);
}
/**
......@@ -952,7 +948,7 @@ class Connection implements DriverConnection
}
return new Result($result, $this);
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
} finally {
if ($logger !== null) {
......@@ -1024,7 +1020,7 @@ class Connection implements DriverConnection
try {
return $connection->query($sql);
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $sql);
} finally {
if ($logger !== null) {
......@@ -1072,7 +1068,7 @@ class Connection implements DriverConnection
}
return $connection->exec($query);
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $query, $params, $types);
} finally {
if ($logger !== null) {
......@@ -1095,7 +1091,7 @@ class Connection implements DriverConnection
try {
return $connection->exec($statement);
} catch (Throwable $e) {
} catch (DriverException $e) {
$this->handleExceptionDuringQuery($e, $statement);
} finally {
if ($logger !== null) {
......
......@@ -2,11 +2,11 @@
namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\Result as DriverResult;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Throwable;
use function is_string;
......@@ -70,8 +70,16 @@ class Statement implements DriverStatement
*/
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->stmt = $conn->getWrappedConnection()->prepare($sql);
$this->stmt = $stmt;
$this->conn = $conn;
$this->platform = $conn->getDatabasePlatform();
}
......@@ -156,7 +164,7 @@ class Statement implements DriverStatement
$this->stmt->execute($params),
$this->conn
);
} catch (Throwable $ex) {
} catch (Exception $ex) {
$this->conn->handleExceptionDuringQuery($ex, $this->sql, $this->params, $this->types);
} finally {
if ($logger !== null) {
......
......@@ -190,66 +190,6 @@ class DataAccessTest extends FunctionalTestCase
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
{
self::assertFalse(
......
......@@ -7,11 +7,11 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\Exception as DriverException;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Statement;
use Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
......@@ -139,7 +139,9 @@ class StatementTest extends TestCase
$this->driverStatement->expects(self::once())
->method('execute')
->will(self::throwException(new Exception('Mock test exception')));
->will(self::throwException(
$this->createMock(DriverException::class)
));
$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