From 22c44271b4054c51db0ebd0148cbd97e6dc8bf41 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Fri, 5 Nov 2021 09:21:12 -0700 Subject: [PATCH] Accept PDO as a Connection constructor argument --- src/Driver/PDO/Connection.php | 29 +++++----------------------- src/Driver/PDO/MySQL/Driver.php | 22 ++++++++++++++------- src/Driver/PDO/OCI/Driver.php | 20 +++++++++++++------ src/Driver/PDO/PgSQL/Driver.php | 25 +++++++++++++++--------- src/Driver/PDO/SQLSrv/Driver.php | 25 ++++++++++++++---------- src/Driver/PDO/SQLite/Driver.php | 33 ++++++++++++++++++-------------- 6 files changed, 84 insertions(+), 70 deletions(-) diff --git a/src/Driver/PDO/Connection.php b/src/Driver/PDO/Connection.php index 4d7b90b6e31..31f38524293 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 @@ -71,7 +60,7 @@ public function prepare(string $sql): StatementInterface $stmt = $this->connection->prepare($sql); assert($stmt instanceof PDOStatement); - return $this->createStatement($stmt); + return new Statement($stmt); } catch (PDOException $exception) { throw Exception::new($exception); } @@ -119,14 +108,6 @@ public function lastInsertId($name = null) } } - /** - * Creates a wrapped statement - */ - protected function createStatement(PDOStatement $stmt): Statement - { - return new Statement($stmt); - } - /** * {@inheritDoc} */ diff --git a/src/Driver/PDO/MySQL/Driver.php b/src/Driver/PDO/MySQL/Driver.php index 50c05a40f52..cf2fd3efa84 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); } /** @@ -36,7 +44,7 @@ public function connect(array $params) * * @return string The DSN. */ - protected function constructPdoDsn(array $params) + private function constructPdoDsn(array $params) { $dsn = 'mysql:'; if (isset($params['host']) && $params['host'] !== '') { 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..3dc0d923fb4 100644 --- a/src/Driver/PDO/SQLite/Driver.php +++ b/src/Driver/PDO/SQLite/Driver.php @@ -4,14 +4,17 @@ 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; final class Driver extends AbstractSQLiteDriver { /** @var mixed[] */ - protected $_userDefinedFunctions = [ + private $userDefinedFunctions = [ 'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1], 'mod' => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2], 'locate' => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1], @@ -27,27 +30,29 @@ public function connect(array $params) $driverOptions = $params['driverOptions'] ?? []; if (isset($driverOptions['userDefinedFunctions'])) { - $this->_userDefinedFunctions = array_merge( - $this->_userDefinedFunctions, + $this->userDefinedFunctions = array_merge( + $this->userDefinedFunctions, $driverOptions['userDefinedFunctions'] ); 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) { + 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) + private function constructPdoDsn(array $params) { $dsn = 'sqlite:'; if (isset($params['path'])) {