diff --git a/src/Driver/PDO/Connection.php b/src/Driver/PDO/Connection.php index 4d7b90b6e31..a83937cb818 100644 --- a/src/Driver/PDO/Connection.php +++ b/src/Driver/PDO/Connection.php @@ -2,7 +2,6 @@ namespace Doctrine\DBAL\Driver\PDO; -use Doctrine\DBAL\Driver\Exception as ExceptionInterface; use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\Statement as StatementInterface; @@ -21,22 +20,12 @@ final class Connection implements ServerInfoAwareConnection /** * @internal The connection can be only instantiated by its driver. - * - * @param string $dsn - * @param string|null $user - * @param string|null $password - * @param mixed[]|null $options - * - * @throws ExceptionInterface */ - public function __construct($dsn, $user = null, $password = null, ?array $options = null) + public function __construct(PDO $connection) { - try { - $this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options); - $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - } catch (PDOException $exception) { - throw Exception::new($exception); - } + $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + $this->connection = $connection; } public function exec(string $sql): int diff --git a/src/Driver/PDO/MySQL/Driver.php b/src/Driver/PDO/MySQL/Driver.php index 50c05a40f52..57d1e54efcc 100644 --- a/src/Driver/PDO/MySQL/Driver.php +++ b/src/Driver/PDO/MySQL/Driver.php @@ -4,7 +4,9 @@ use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\PDO\Connection; +use Doctrine\DBAL\Driver\PDO\Exception; use PDO; +use PDOException; final class Driver extends AbstractMySQLDriver { @@ -21,12 +23,18 @@ public function connect(array $params) $driverOptions[PDO::ATTR_PERSISTENT] = true; } - return new Connection( - $this->constructPdoDsn($params), - $params['user'] ?? '', - $params['password'] ?? '', - $driverOptions - ); + try { + $pdo = new PDO( + $this->constructPdoDsn($params), + $params['user'] ?? '', + $params['password'] ?? '', + $driverOptions + ); + } catch (PDOException $exception) { + throw Exception::new($exception); + } + + return new Connection($pdo); } /** diff --git a/src/Driver/PDO/OCI/Driver.php b/src/Driver/PDO/OCI/Driver.php index a2c1f46520f..ced611f5404 100644 --- a/src/Driver/PDO/OCI/Driver.php +++ b/src/Driver/PDO/OCI/Driver.php @@ -4,7 +4,9 @@ use Doctrine\DBAL\Driver\AbstractOracleDriver; use Doctrine\DBAL\Driver\PDO\Connection; +use Doctrine\DBAL\Driver\PDO\Exception; use PDO; +use PDOException; final class Driver extends AbstractOracleDriver { @@ -21,12 +23,18 @@ public function connect(array $params) $driverOptions[PDO::ATTR_PERSISTENT] = true; } - return new Connection( - $this->constructPdoDsn($params), - $params['user'] ?? '', - $params['password'] ?? '', - $driverOptions - ); + try { + $pdo = new PDO( + $this->constructPdoDsn($params), + $params['user'] ?? '', + $params['password'] ?? '', + $driverOptions + ); + } catch (PDOException $exception) { + throw Exception::new($exception); + } + + return new Connection($pdo); } /** diff --git a/src/Driver/PDO/PgSQL/Driver.php b/src/Driver/PDO/PgSQL/Driver.php index 0162a913541..108605f26fc 100644 --- a/src/Driver/PDO/PgSQL/Driver.php +++ b/src/Driver/PDO/PgSQL/Driver.php @@ -4,7 +4,9 @@ use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; use Doctrine\DBAL\Driver\PDO\Connection; +use Doctrine\DBAL\Driver\PDO\Exception; use PDO; +use PDOException; final class Driver extends AbstractPostgreSQLDriver { @@ -21,22 +23,27 @@ public function connect(array $params) $driverOptions[PDO::ATTR_PERSISTENT] = true; } - $connection = new Connection( - $this->_constructPdoDsn($params), - $params['user'] ?? '', - $params['password'] ?? '', - $driverOptions, - ); + try { + $pdo = new PDO( + $this->constructPdoDsn($params), + $params['user'] ?? '', + $params['password'] ?? '', + $driverOptions, + ); + } catch (PDOException $exception) { + throw Exception::new($exception); + } if ( ! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES]) || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true ) { - $connection->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true); + $pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true); } + $connection = new Connection($pdo); + /* defining client_encoding via SET NAMES to avoid inconsistent DSN support - * - the 'client_encoding' connection param only works with postgres >= 9.1 * - passing client_encoding via the 'options' param breaks pgbouncer support */ if (isset($params['charset'])) { @@ -53,7 +60,7 @@ public function connect(array $params) * * @return string The DSN. */ - private function _constructPdoDsn(array $params) + private function constructPdoDsn(array $params) { $dsn = 'pgsql:'; diff --git a/src/Driver/PDO/SQLSrv/Driver.php b/src/Driver/PDO/SQLSrv/Driver.php index c4842504912..a6469cfa0af 100644 --- a/src/Driver/PDO/SQLSrv/Driver.php +++ b/src/Driver/PDO/SQLSrv/Driver.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost; use Doctrine\DBAL\Driver\Exception; use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection; +use Doctrine\DBAL\Driver\PDO\Exception as PDOException; use PDO; use function is_int; @@ -20,12 +21,12 @@ final class Driver extends AbstractSQLServerDriver */ public function connect(array $params) { - $pdoOptions = $dsnOptions = []; + $driverOptions = $dsnOptions = []; if (isset($params['driverOptions'])) { foreach ($params['driverOptions'] as $option => $value) { if (is_int($option)) { - $pdoOptions[$option] = $value; + $driverOptions[$option] = $value; } else { $dsnOptions[$option] = $value; } @@ -33,17 +34,21 @@ public function connect(array $params) } if (! empty($params['persistent'])) { - $pdoOptions[PDO::ATTR_PERSISTENT] = true; + $driverOptions[PDO::ATTR_PERSISTENT] = true; } - return new Connection( - new PDOConnection( - $this->_constructPdoDsn($params, $dsnOptions), + try { + $pdo = new PDO( + $this->constructDsn($params, $dsnOptions), $params['user'] ?? '', $params['password'] ?? '', - $pdoOptions - ) - ); + $driverOptions + ); + } catch (\PDOException $exception) { + throw PDOException::new($exception); + } + + return new Connection(new PDOConnection($pdo)); } /** @@ -56,7 +61,7 @@ public function connect(array $params) * * @throws Exception */ - private function _constructPdoDsn(array $params, array $connectionOptions) + private function constructDsn(array $params, array $connectionOptions) { $dsn = 'sqlsrv:server='; diff --git a/src/Driver/PDO/SQLite/Driver.php b/src/Driver/PDO/SQLite/Driver.php index d85ba28faf6..18bfe2eb034 100644 --- a/src/Driver/PDO/SQLite/Driver.php +++ b/src/Driver/PDO/SQLite/Driver.php @@ -4,7 +4,10 @@ use Doctrine\DBAL\Driver\AbstractSQLiteDriver; use Doctrine\DBAL\Driver\PDO\Connection; +use Doctrine\DBAL\Driver\PDO\Exception; use Doctrine\DBAL\Platforms\SqlitePlatform; +use PDO; +use PDOException; use function array_merge; @@ -34,20 +37,22 @@ public function connect(array $params) unset($driverOptions['userDefinedFunctions']); } - $connection = new Connection( - $this->_constructPdoDsn($params), - $params['user'] ?? '', - $params['password'] ?? '', - $driverOptions - ); - - $pdo = $connection->getWrappedConnection(); + try { + $pdo = new PDO( + $this->constructPdoDsn($params), + $params['user'] ?? '', + $params['password'] ?? '', + $driverOptions + ); + } catch (PDOException $exception) { + throw Exception::new($exception); + } foreach ($this->_userDefinedFunctions as $fn => $data) { $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']); } - return $connection; + return new Connection($pdo); } /** @@ -57,7 +62,7 @@ public function connect(array $params) * * @return string The DSN. */ - protected function _constructPdoDsn(array $params) + protected function constructPdoDsn(array $params) { $dsn = 'sqlite:'; if (isset($params['path'])) { diff --git a/tests/Functional/Driver/PDO/ConnectionTest.php b/tests/Functional/Driver/PDO/ConnectionTest.php index 317efc8288a..21a7fa01574 100644 --- a/tests/Functional/Driver/PDO/ConnectionTest.php +++ b/tests/Functional/Driver/PDO/ConnectionTest.php @@ -38,13 +38,6 @@ protected function tearDown(): void $this->markConnectionNotReusable(); } - public function testThrowsWrappedExceptionOnConstruct(): void - { - $this->expectException(Exception::class); - - new Connection('foo'); - } - public function testThrowsWrappedExceptionOnExec(): void { $this->expectException(Exception::class);