diff --git a/src/Driver/IBMDB2/Connection.php b/src/Driver/IBMDB2/Connection.php index 6655801dd89..80941fe16ca 100644 --- a/src/Driver/IBMDB2/Connection.php +++ b/src/Driver/IBMDB2/Connection.php @@ -2,9 +2,7 @@ namespace Doctrine\DBAL\Driver\IBMDB2; -use Doctrine\DBAL\Driver\Exception; use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionError; -use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionFailed; use Doctrine\DBAL\Driver\IBMDB2\Exception\PrepareFailed; use Doctrine\DBAL\Driver\IBMDB2\Exception\StatementError; use Doctrine\DBAL\Driver\Result as ResultInterface; @@ -17,12 +15,10 @@ use function assert; use function db2_autocommit; use function db2_commit; -use function db2_connect; use function db2_escape_string; use function db2_exec; use function db2_last_insert_id; use function db2_num_rows; -use function db2_pconnect; use function db2_prepare; use function db2_rollback; use function db2_server_info; @@ -35,33 +31,16 @@ final class Connection implements ServerInfoAwareConnection { /** @var resource */ - private $conn; + private $connection; /** * @internal The connection can be only instantiated by its driver. * - * @param array $driverOptions - * - * @throws Exception + * @param resource $connection */ - public function __construct( - string $database, - bool $persistent, - string $username, - string $password, - array $driverOptions = [] - ) { - if ($persistent) { - $conn = db2_pconnect($database, $username, $password, $driverOptions); - } else { - $conn = db2_connect($database, $username, $password, $driverOptions); - } - - if ($conn === false) { - throw ConnectionFailed::new(); - } - - $this->conn = $conn; + public function __construct($connection) + { + $this->connection = $connection; } /** @@ -69,7 +48,7 @@ public function __construct( */ public function getServerVersion() { - $serverInfo = db2_server_info($this->conn); + $serverInfo = db2_server_info($this->connection); assert($serverInfo instanceof stdClass); return $serverInfo->DBMS_VER; @@ -77,7 +56,7 @@ public function getServerVersion() public function prepare(string $sql): DriverStatement { - $stmt = @db2_prepare($this->conn, $sql); + $stmt = @db2_prepare($this->connection, $sql); if ($stmt === false) { throw PrepareFailed::new(error_get_last()); @@ -107,7 +86,7 @@ public function quote($value, $type = ParameterType::STRING) public function exec(string $sql): int { - $stmt = @db2_exec($this->conn, $sql); + $stmt = @db2_exec($this->connection, $sql); if ($stmt === false) { throw StatementError::new(); @@ -129,7 +108,7 @@ public function lastInsertId($name = null) ); } - return db2_last_insert_id($this->conn); + return db2_last_insert_id($this->connection); } /** @@ -137,7 +116,7 @@ public function lastInsertId($name = null) */ public function beginTransaction() { - $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF); + $result = db2_autocommit($this->connection, DB2_AUTOCOMMIT_OFF); assert(is_bool($result)); return $result; @@ -148,11 +127,11 @@ public function beginTransaction() */ public function commit() { - if (! db2_commit($this->conn)) { - throw ConnectionError::new($this->conn); + if (! db2_commit($this->connection)) { + throw ConnectionError::new($this->connection); } - $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON); + $result = db2_autocommit($this->connection, DB2_AUTOCOMMIT_ON); assert(is_bool($result)); return $result; @@ -163,11 +142,11 @@ public function commit() */ public function rollBack() { - if (! db2_rollback($this->conn)) { - throw ConnectionError::new($this->conn); + if (! db2_rollback($this->connection)) { + throw ConnectionError::new($this->connection); } - $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON); + $result = db2_autocommit($this->connection, DB2_AUTOCOMMIT_ON); assert(is_bool($result)); return $result; diff --git a/src/Driver/IBMDB2/Driver.php b/src/Driver/IBMDB2/Driver.php index c08165520de..621ebf05ff2 100644 --- a/src/Driver/IBMDB2/Driver.php +++ b/src/Driver/IBMDB2/Driver.php @@ -3,6 +3,7 @@ namespace Doctrine\DBAL\Driver\IBMDB2; use Doctrine\DBAL\Driver\AbstractDB2Driver; +use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionFailed; final class Driver extends AbstractDB2Driver { @@ -13,12 +14,22 @@ final class Driver extends AbstractDB2Driver */ public function connect(array $params) { - return new Connection( - DataSourceName::fromConnectionParameters($params)->toString(), - isset($params['persistent']) && $params['persistent'] === true, - $params['user'] ?? '', - $params['password'] ?? '', - $params['driverOptions'] ?? [] - ); + $dataSourceName = DataSourceName::fromConnectionParameters($params)->toString(); + + $username = $params['user'] ?? ''; + $password = $params['password'] ?? ''; + $driverOptions = $params['driverOptions'] ?? []; + + if (! empty($params['persistent'])) { + $connection = db2_pconnect($dataSourceName, $username, $password, $driverOptions); + } else { + $connection = db2_connect($dataSourceName, $username, $password, $driverOptions); + } + + if ($connection === false) { + throw ConnectionFailed::new(); + } + + return new Connection($connection); } } diff --git a/src/Driver/Mysqli/Connection.php b/src/Driver/Mysqli/Connection.php index dd269b0280f..f6d41a71029 100644 --- a/src/Driver/Mysqli/Connection.php +++ b/src/Driver/Mysqli/Connection.php @@ -2,9 +2,7 @@ namespace Doctrine\DBAL\Driver\Mysqli; -use Doctrine\DBAL\Driver\Exception; use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError; -use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionFailed; use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\Statement as DriverStatement; @@ -24,48 +22,14 @@ final class Connection implements ServerInfoAwareConnection public const OPTION_FLAGS = 'flags'; /** @var mysqli */ - private $conn; + private $connection; /** * @internal The connection can be only instantiated by its driver. - * - * @param iterable $preInitializers - * @param iterable $postInitializers - * - * @throws Exception */ - public function __construct( - ?string $host = null, - ?string $username = null, - ?string $password = null, - ?string $database = null, - ?int $port = null, - ?string $socket = null, - int $flags = 0, - iterable $preInitializers = [], - iterable $postInitializers = [] - ) { - $connection = new mysqli(); - - foreach ($preInitializers as $initializer) { - $initializer->initialize($connection); - } - - try { - $success = @$connection->real_connect($host, $username, $password, $database, $port, $socket, $flags); - } catch (mysqli_sql_exception $e) { - throw ConnectionFailed::upcast($e); - } - - if (! $success) { - throw ConnectionFailed::new($connection); - } - - foreach ($postInitializers as $initializer) { - $initializer->initialize($connection); - } - - $this->conn = $connection; + public function __construct(mysqli $connection) + { + $this->connection = $connection; } /** @@ -77,7 +41,7 @@ public function __construct( */ public function getWrappedResourceHandle() { - return $this->conn; + return $this->connection; } /** @@ -90,14 +54,14 @@ public function getWrappedResourceHandle() */ public function getServerVersion() { - $serverInfos = $this->conn->get_server_info(); + $serverInfos = $this->connection->get_server_info(); if (stripos($serverInfos, 'mariadb') !== false) { return $serverInfos; } - $majorVersion = floor($this->conn->server_version / 10000); - $minorVersion = floor(($this->conn->server_version - $majorVersion * 10000) / 100); - $patchVersion = floor($this->conn->server_version - $majorVersion * 10000 - $minorVersion * 100); + $majorVersion = floor($this->connection->server_version / 10000); + $minorVersion = floor(($this->connection->server_version - $majorVersion * 10000) / 100); + $patchVersion = floor($this->connection->server_version - $majorVersion * 10000 - $minorVersion * 100); return $majorVersion . '.' . $minorVersion . '.' . $patchVersion; } @@ -105,13 +69,13 @@ public function getServerVersion() public function prepare(string $sql): DriverStatement { try { - $stmt = $this->conn->prepare($sql); + $stmt = $this->connection->prepare($sql); } catch (mysqli_sql_exception $e) { throw ConnectionError::upcast($e); } if ($stmt === false) { - throw ConnectionError::new($this->conn); + throw ConnectionError::new($this->connection); } return new Statement($stmt); @@ -127,22 +91,22 @@ public function query(string $sql): ResultInterface */ public function quote($value, $type = ParameterType::STRING) { - return "'" . $this->conn->escape_string($value) . "'"; + return "'" . $this->connection->escape_string($value) . "'"; } public function exec(string $sql): int { try { - $result = $this->conn->query($sql); + $result = $this->connection->query($sql); } catch (mysqli_sql_exception $e) { throw ConnectionError::upcast($e); } if ($result === false) { - throw ConnectionError::new($this->conn); + throw ConnectionError::new($this->connection); } - return $this->conn->affected_rows; + return $this->connection->affected_rows; } /** @@ -158,7 +122,7 @@ public function lastInsertId($name = null) ); } - return $this->conn->insert_id; + return $this->connection->insert_id; } /** @@ -166,7 +130,7 @@ public function lastInsertId($name = null) */ public function beginTransaction() { - $this->conn->begin_transaction(); + $this->connection->begin_transaction(); return true; } @@ -177,7 +141,7 @@ public function beginTransaction() public function commit() { try { - return $this->conn->commit(); + return $this->connection->commit(); } catch (mysqli_sql_exception $e) { return false; } @@ -189,7 +153,7 @@ public function commit() public function rollBack() { try { - return $this->conn->rollback(); + return $this->connection->rollback(); } catch (mysqli_sql_exception $e) { return false; } diff --git a/src/Driver/Mysqli/Driver.php b/src/Driver/Mysqli/Driver.php index d459863d8a3..41a66a732b5 100644 --- a/src/Driver/Mysqli/Driver.php +++ b/src/Driver/Mysqli/Driver.php @@ -3,10 +3,13 @@ namespace Doctrine\DBAL\Driver\Mysqli; use Doctrine\DBAL\Driver\AbstractMySQLDriver; +use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionFailed; use Doctrine\DBAL\Driver\Mysqli\Exception\HostRequired; use Doctrine\DBAL\Driver\Mysqli\Initializer\Charset; use Doctrine\DBAL\Driver\Mysqli\Initializer\Options; use Doctrine\DBAL\Driver\Mysqli\Initializer\Secure; +use mysqli; +use mysqli_sql_exception; use function count; @@ -47,17 +50,35 @@ public function connect(array $params) $preInitializers = $this->withSecure($preInitializers, $params); $postInitializers = $this->withCharset($postInitializers, $params); - return new Connection( - $host, - $params['user'] ?? null, - $params['password'] ?? null, - $params['dbname'] ?? null, - $params['port'] ?? null, - $params['unix_socket'] ?? null, - $flags, - $preInitializers, - $postInitializers - ); + $connection = new mysqli(); + + foreach ($preInitializers as $initializer) { + $initializer->initialize($connection); + } + + try { + $success = @$connection->real_connect( + $host, + $params['user'] ?? null, + $params['password'] ?? null, + $params['dbname'] ?? null, + $params['port'] ?? null, + $params['unix_socket'] ?? null, + $flags + ); + } catch (mysqli_sql_exception $e) { + throw ConnectionFailed::upcast($e); + } + + if (! $success) { + throw ConnectionFailed::new($connection); + } + + foreach ($postInitializers as $initializer) { + $initializer->initialize($connection); + } + + return new Connection($connection); } /** diff --git a/src/Driver/OCI8/Connection.php b/src/Driver/OCI8/Connection.php index 12149e58129..67a5071706c 100644 --- a/src/Driver/OCI8/Connection.php +++ b/src/Driver/OCI8/Connection.php @@ -2,8 +2,6 @@ namespace Doctrine\DBAL\Driver\OCI8; -use Doctrine\DBAL\Driver\Exception; -use Doctrine\DBAL\Driver\OCI8\Exception\ConnectionFailed; use Doctrine\DBAL\Driver\OCI8\Exception\Error; use Doctrine\DBAL\Driver\OCI8\Exception\SequenceDoesNotExist; use Doctrine\DBAL\Driver\Result as ResultInterface; @@ -17,54 +15,27 @@ use function is_float; use function is_int; use function oci_commit; -use function oci_connect; -use function oci_pconnect; use function oci_rollback; use function oci_server_version; use function preg_match; use function str_replace; -use const OCI_NO_AUTO_COMMIT; - final class Connection implements ServerInfoAwareConnection { /** @var resource */ - protected $dbh; + protected $connection; /** @var ExecutionMode */ private $executionMode; /** - * Creates a Connection to an Oracle Database using oci8 extension. - * * @internal The connection can be only instantiated by its driver. * - * @param string $username - * @param string $password - * @param string $db - * @param string $charset - * @param int $sessionMode - * @param bool $persistent - * - * @throws Exception + * @param resource $connection */ - public function __construct( - $username, - $password, - $db, - $charset = '', - $sessionMode = OCI_NO_AUTO_COMMIT, - $persistent = false - ) { - $dbh = $persistent - ? @oci_pconnect($username, $password, $db, $charset, $sessionMode) - : @oci_connect($username, $password, $db, $charset, $sessionMode); - - if ($dbh === false) { - throw ConnectionFailed::new(); - } - - $this->dbh = $dbh; + public function __construct($connection) + { + $this->connection = $connection; $this->executionMode = new ExecutionMode(); } @@ -73,10 +44,10 @@ public function __construct( */ public function getServerVersion() { - $version = oci_server_version($this->dbh); + $version = oci_server_version($this->connection); if ($version === false) { - throw Error::new($this->dbh); + throw Error::new($this->connection); } assert(preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches) === 1); @@ -86,7 +57,7 @@ public function getServerVersion() public function prepare(string $sql): DriverStatement { - return new Statement($this->dbh, $sql, $this->executionMode); + return new Statement($this->connection, $sql, $this->executionMode); } public function query(string $sql): ResultInterface @@ -156,8 +127,8 @@ public function beginTransaction() */ public function commit() { - if (! oci_commit($this->dbh)) { - throw Error::new($this->dbh); + if (! oci_commit($this->connection)) { + throw Error::new($this->connection); } $this->executionMode->enableAutoCommit(); @@ -170,8 +141,8 @@ public function commit() */ public function rollBack() { - if (! oci_rollback($this->dbh)) { - throw Error::new($this->dbh); + if (! oci_rollback($this->connection)) { + throw Error::new($this->connection); } $this->executionMode->enableAutoCommit(); diff --git a/src/Driver/OCI8/Driver.php b/src/Driver/OCI8/Driver.php index 351ffb87ae6..ac1419d55f2 100644 --- a/src/Driver/OCI8/Driver.php +++ b/src/Driver/OCI8/Driver.php @@ -3,6 +3,10 @@ namespace Doctrine\DBAL\Driver\OCI8; use Doctrine\DBAL\Driver\AbstractOracleDriver; +use Doctrine\DBAL\Driver\OCI8\Exception\ConnectionFailed; + +use function oci_connect; +use function oci_pconnect; use const OCI_NO_AUTO_COMMIT; @@ -18,14 +22,24 @@ final class Driver extends AbstractOracleDriver */ public function connect(array $params) { - return new Connection( - $params['user'] ?? '', - $params['password'] ?? '', - $this->_constructDsn($params), - $params['charset'] ?? '', - $params['sessionMode'] ?? OCI_NO_AUTO_COMMIT, - $params['persistent'] ?? false - ); + $username = $params['user'] ?? ''; + $password = $params['password'] ?? ''; + $charset = $params['charset'] ?? ''; + $sessionMode = $params['sessionMode'] ?? OCI_NO_AUTO_COMMIT; + + $connectionString = $this->getEasyConnectString($params); + + if (! empty($params['persistent'])) { + $connection = @oci_pconnect($username, $password, $connectionString, $charset, $sessionMode); + } else { + $connection = @oci_connect($username, $password, $connectionString, $charset, $sessionMode); + } + + if ($connection === false) { + throw ConnectionFailed::new(); + } + + return new Connection($connection); } /** diff --git a/src/Driver/SQLSrv/Connection.php b/src/Driver/SQLSrv/Connection.php index b8f9f27b41d..2f6d2fda0a7 100644 --- a/src/Driver/SQLSrv/Connection.php +++ b/src/Driver/SQLSrv/Connection.php @@ -2,7 +2,6 @@ namespace Doctrine\DBAL\Driver\SQLSrv; -use Doctrine\DBAL\Driver\Exception; use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\SQLSrv\Exception\Error; @@ -15,8 +14,6 @@ use function sprintf; use function sqlsrv_begin_transaction; use function sqlsrv_commit; -use function sqlsrv_configure; -use function sqlsrv_connect; use function sqlsrv_query; use function sqlsrv_rollback; use function sqlsrv_rows_affected; @@ -26,29 +23,16 @@ final class Connection implements ServerInfoAwareConnection { /** @var resource */ - protected $conn; + protected $connection; /** * @internal The connection can be only instantiated by its driver. * - * @param string $serverName - * @param mixed[] $connectionOptions - * - * @throws Exception + * @param resource $connection */ - public function __construct($serverName, $connectionOptions) + public function __construct($connection) { - if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) { - throw Error::new(); - } - - $conn = sqlsrv_connect($serverName, $connectionOptions); - - if ($conn === false) { - throw Error::new(); - } - - $this->conn = $conn; + $this->connection = $connection; } /** @@ -56,14 +40,14 @@ public function __construct($serverName, $connectionOptions) */ public function getServerVersion() { - $serverInfo = sqlsrv_server_info($this->conn); + $serverInfo = sqlsrv_server_info($this->connection); return $serverInfo['SQLServerVersion']; } public function prepare(string $sql): DriverStatement { - return new Statement($this->conn, $sql); + return new Statement($this->connection, $sql); } public function query(string $sql): ResultInterface @@ -89,7 +73,7 @@ public function quote($value, $type = ParameterType::STRING) public function exec(string $sql): int { - $stmt = sqlsrv_query($this->conn, $sql); + $stmt = sqlsrv_query($this->connection, $sql); if ($stmt === false) { throw Error::new(); @@ -130,7 +114,7 @@ public function lastInsertId($name = null) */ public function beginTransaction() { - if (! sqlsrv_begin_transaction($this->conn)) { + if (! sqlsrv_begin_transaction($this->connection)) { throw Error::new(); } @@ -142,7 +126,7 @@ public function beginTransaction() */ public function commit() { - if (! sqlsrv_commit($this->conn)) { + if (! sqlsrv_commit($this->connection)) { throw Error::new(); } @@ -154,7 +138,7 @@ public function commit() */ public function rollBack() { - if (! sqlsrv_rollback($this->conn)) { + if (! sqlsrv_rollback($this->connection)) { throw Error::new(); } diff --git a/src/Driver/SQLSrv/Driver.php b/src/Driver/SQLSrv/Driver.php index efa9ccfe908..085b22412d3 100644 --- a/src/Driver/SQLSrv/Driver.php +++ b/src/Driver/SQLSrv/Driver.php @@ -4,6 +4,10 @@ use Doctrine\DBAL\Driver\AbstractSQLServerDriver; use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost; +use Doctrine\DBAL\Driver\SQLSrv\Exception\Error; + +use function sqlsrv_configure; +use function sqlsrv_connect; /** * Driver for ext/sqlsrv. @@ -51,6 +55,16 @@ public function connect(array $params) $driverOptions['ReturnDatesAsStrings'] = 1; } - return new Connection($serverName, $driverOptions); + if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) { + throw Error::new(); + } + + $connection = sqlsrv_connect($serverName, $driverOptions); + + if ($connection === false) { + throw Error::new(); + } + + return new Connection($connection); } } diff --git a/tests/Functional/Driver/IBMDB2/ConnectionTest.php b/tests/Functional/Driver/IBMDB2/ConnectionTest.php index 656032f2a33..a9e1479bfbe 100644 --- a/tests/Functional/Driver/IBMDB2/ConnectionTest.php +++ b/tests/Functional/Driver/IBMDB2/ConnectionTest.php @@ -2,8 +2,6 @@ namespace Doctrine\DBAL\Tests\Functional\Driver\IBMDB2; -use Doctrine\DBAL\Driver\IBMDB2\Connection; -use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionFailed; use Doctrine\DBAL\Driver\IBMDB2\Exception\PrepareFailed; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\TestUtil; @@ -30,17 +28,11 @@ protected function tearDown(): void $this->markConnectionNotReusable(); } - public function testConnectionFailure(): void - { - $this->expectException(ConnectionFailed::class); - new Connection('garbage', false, '', ''); - } - public function testPrepareFailure(): void { $driverConnection = $this->connection->getWrappedConnection(); - $re = new ReflectionProperty($driverConnection, 'conn'); + $re = new ReflectionProperty($driverConnection, 'connection'); $re->setAccessible(true); $conn = $re->getValue($driverConnection); db2_close($conn);