From 53e5a0902cd8cb8df8d6db5b49c4991906882b7b Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 28 Jun 2020 11:09:05 -0700 Subject: [PATCH] Remove ExceptionConverterDriver --- UPGRADE.md | 4 ++++ src/DBALException.php | 3 +-- src/Driver.php | 14 +++++++++++++ src/Driver/AbstractDB2Driver.php | 3 +-- src/Driver/AbstractMySQLDriver.php | 5 ++--- src/Driver/AbstractOracleDriver.php | 5 ++--- src/Driver/AbstractPostgreSQLDriver.php | 5 ++--- src/Driver/AbstractSQLServerDriver.php | 3 +-- src/Driver/AbstractSQLiteDriver.php | 5 ++--- src/Driver/ExceptionConverterDriver.php | 28 ------------------------- tests/Driver/AbstractDriverTest.php | 22 +++++++++++-------- tests/Functional/ExceptionTest.php | 13 +++++++++--- 12 files changed, 52 insertions(+), 58 deletions(-) delete mode 100644 src/Driver/ExceptionConverterDriver.php diff --git a/UPGRADE.md b/UPGRADE.md index c33aec28061..91033721ea0 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,9 @@ # Upgrade to 3.0 +## The `ExceptionConverterDriver` interface is removed + +All drivers must implement the `convertException()` method which is now part of the `Driver` interface. + ## The `PingableConnection` interface is removed The functionality of pinging the server is no longer supported. diff --git a/src/DBALException.php b/src/DBALException.php index c83ae4a8737..43069567c65 100644 --- a/src/DBALException.php +++ b/src/DBALException.php @@ -3,7 +3,6 @@ namespace Doctrine\DBAL; use Doctrine\DBAL\Driver\Exception as TheDriverException; -use Doctrine\DBAL\Driver\ExceptionConverterDriver; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; @@ -169,7 +168,7 @@ private static function wrapException(Driver $driver, Throwable $driverEx, strin return $driverEx; } - if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof TheDriverException) { + if ($driverEx instanceof TheDriverException) { return $driver->convertException($msg, $driverEx); } diff --git a/src/Driver.php b/src/Driver.php index 837c61a4ad6..3d7b8d386b3 100644 --- a/src/Driver.php +++ b/src/Driver.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\Driver\Exception; +use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; @@ -39,4 +40,17 @@ public function getDatabasePlatform(); * @return AbstractSchemaManager */ public function getSchemaManager(Connection $conn); + + /** + * Converts a given driver-level exception into a DBAL-level driver exception. + * + * Implementors should use the vendor-specific error code and SQLSTATE of the exception + * and instantiate the most appropriate specialized {@link DriverException} subclass. + * + * @param string $message The exception message to use. + * @param Exception $exception The driver exception to convert. + * + * @return DriverException An instance of {@link DriverException} or one of its subclasses. + */ + public function convertException($message, Exception $exception); } diff --git a/src/Driver/AbstractDB2Driver.php b/src/Driver/AbstractDB2Driver.php index 9a3487e2172..997b78c407d 100644 --- a/src/Driver/AbstractDB2Driver.php +++ b/src/Driver/AbstractDB2Driver.php @@ -4,7 +4,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; -use Doctrine\DBAL\Driver\Exception as TheDriverException; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Platforms\DB2Platform; use Doctrine\DBAL\Schema\DB2SchemaManager; @@ -35,7 +34,7 @@ public function getSchemaManager(Connection $conn) * * @return DriverException */ - public function convertException($message, TheDriverException $exception) + public function convertException($message, Exception $exception) { return new DriverException($message, $exception); } diff --git a/src/Driver/AbstractMySQLDriver.php b/src/Driver/AbstractMySQLDriver.php index adc28372188..f1e02359bb0 100644 --- a/src/Driver/AbstractMySQLDriver.php +++ b/src/Driver/AbstractMySQLDriver.php @@ -4,7 +4,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\DBALException; -use Doctrine\DBAL\Driver\Exception as TheDriverException; use Doctrine\DBAL\Exception\ConnectionException; use Doctrine\DBAL\Exception\ConnectionLost; use Doctrine\DBAL\Exception\DeadlockException; @@ -32,7 +31,7 @@ /** * Abstract base implementation of the {@link Driver} interface for MySQL based drivers. */ -abstract class AbstractMySQLDriver implements ExceptionConverterDriver, VersionAwarePlatformDriver +abstract class AbstractMySQLDriver implements VersionAwarePlatformDriver { /** * {@inheritdoc} @@ -40,7 +39,7 @@ abstract class AbstractMySQLDriver implements ExceptionConverterDriver, VersionA * @link https://dev.mysql.com/doc/refman/8.0/en/client-error-reference.html * @link https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html */ - public function convertException($message, TheDriverException $exception) + public function convertException($message, Exception $exception) { switch ($exception->getCode()) { case 1213: diff --git a/src/Driver/AbstractOracleDriver.php b/src/Driver/AbstractOracleDriver.php index 3c7b4661461..d4458e3a22a 100644 --- a/src/Driver/AbstractOracleDriver.php +++ b/src/Driver/AbstractOracleDriver.php @@ -5,7 +5,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\AbstractOracleDriver\EasyConnectString; -use Doctrine\DBAL\Driver\Exception as TheDriverException; use Doctrine\DBAL\Exception\ConnectionException; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; @@ -22,12 +21,12 @@ /** * Abstract base implementation of the {@link Driver} interface for Oracle based drivers. */ -abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver +abstract class AbstractOracleDriver implements Driver { /** * {@inheritdoc} */ - public function convertException($message, TheDriverException $exception) + public function convertException($message, Exception $exception) { switch ($exception->getCode()) { case 1: diff --git a/src/Driver/AbstractPostgreSQLDriver.php b/src/Driver/AbstractPostgreSQLDriver.php index a4e497c2f6f..a69cebba66f 100644 --- a/src/Driver/AbstractPostgreSQLDriver.php +++ b/src/Driver/AbstractPostgreSQLDriver.php @@ -4,7 +4,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\DBALException; -use Doctrine\DBAL\Driver\Exception as TheDriverException; use Doctrine\DBAL\Exception\ConnectionException; use Doctrine\DBAL\Exception\DeadlockException; use Doctrine\DBAL\Exception\DriverException; @@ -28,14 +27,14 @@ /** * Abstract base implementation of the {@link Driver} interface for PostgreSQL based drivers. */ -abstract class AbstractPostgreSQLDriver implements ExceptionConverterDriver, VersionAwarePlatformDriver +abstract class AbstractPostgreSQLDriver implements VersionAwarePlatformDriver { /** * {@inheritdoc} * * @link http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html */ - public function convertException($message, TheDriverException $exception) + public function convertException($message, Exception $exception) { switch ($exception->getSQLState()) { case '40001': diff --git a/src/Driver/AbstractSQLServerDriver.php b/src/Driver/AbstractSQLServerDriver.php index 30213ba4c5a..47661e2d5d6 100644 --- a/src/Driver/AbstractSQLServerDriver.php +++ b/src/Driver/AbstractSQLServerDriver.php @@ -4,7 +4,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; -use Doctrine\DBAL\Driver\Exception as TheDriverException; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Schema\SQLServerSchemaManager; @@ -35,7 +34,7 @@ public function getSchemaManager(Connection $conn) * * @return DriverException */ - public function convertException($message, TheDriverException $exception) + public function convertException($message, Exception $exception) { return new DriverException($message, $exception); } diff --git a/src/Driver/AbstractSQLiteDriver.php b/src/Driver/AbstractSQLiteDriver.php index 2a01c821e3f..e61c308e210 100644 --- a/src/Driver/AbstractSQLiteDriver.php +++ b/src/Driver/AbstractSQLiteDriver.php @@ -4,7 +4,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; -use Doctrine\DBAL\Driver\Exception as TheDriverException; use Doctrine\DBAL\Exception\ConnectionException; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; @@ -25,14 +24,14 @@ /** * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SQLite based drivers. */ -abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver +abstract class AbstractSQLiteDriver implements Driver { /** * {@inheritdoc} * * @link http://www.sqlite.org/c3ref/c_abort.html */ - public function convertException($message, TheDriverException $exception) + public function convertException($message, Exception $exception) { if (strpos($exception->getMessage(), 'database is locked') !== false) { return new LockWaitTimeoutException($message, $exception); diff --git a/src/Driver/ExceptionConverterDriver.php b/src/Driver/ExceptionConverterDriver.php deleted file mode 100644 index 37acf0ae507..00000000000 --- a/src/Driver/ExceptionConverterDriver.php +++ /dev/null @@ -1,28 +0,0 @@ -driver instanceof ExceptionConverterDriver) { - self::markTestSkipped('This test is only intended for exception converter drivers.'); + if ($this->driver instanceof IBMDB2\Driver) { + self::markTestSkipped("The IBM DB2 driver currently doesn't instantiate specialized exceptions"); } - $driverException = $this->getMockBuilder(DriverExceptionInterface::class) - ->setConstructorArgs([$message, $errorCode]) - ->getMock(); - $driverException->method('getSQLState') - ->willReturn($sqlState); + if ($this->driver instanceof AbstractSQLServerDriver) { + self::markTestSkipped("The SQL Server drivers currently don't instantiate specialized exceptions"); + } + + $driverException = $this->getMockForAbstractClass( + AbstractException::class, + [$message, $sqlState, $errorCode] + ); $dbalMessage = 'DBAL exception message'; $dbalException = $this->driver->convertException($dbalMessage, $driverException); diff --git a/tests/Functional/ExceptionTest.php b/tests/Functional/ExceptionTest.php index 2da8e5f620a..ae9776929be 100644 --- a/tests/Functional/ExceptionTest.php +++ b/tests/Functional/ExceptionTest.php @@ -2,7 +2,8 @@ namespace Doctrine\DBAL\Tests\Functional; -use Doctrine\DBAL\Driver\ExceptionConverterDriver; +use Doctrine\DBAL\Driver\AbstractSQLServerDriver; +use Doctrine\DBAL\Driver\IBMDB2; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Exception; @@ -35,11 +36,17 @@ protected function setUp(): void { parent::setUp(); - if ($this->connection->getDriver() instanceof ExceptionConverterDriver) { + $driver = $this->connection->getDriver(); + + if ($driver instanceof IBMDB2\Driver) { + self::markTestSkipped("The IBM DB2 driver currently doesn't instantiate specialized exceptions"); + } + + if (! $driver instanceof AbstractSQLServerDriver) { return; } - self::markTestSkipped('Driver does not support special exception handling.'); + self::markTestSkipped("The SQL Server drivers currently don't instantiate specialized exceptions"); } public function testPrimaryConstraintViolationException(): void