Skip to content

Commit

Permalink
Accept PDO as a Connection constructor argument
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Nov 5, 2021
1 parent b3ae8f4 commit 9d1169f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 63 deletions.
19 changes: 4 additions & 15 deletions src/Driver/PDO/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
20 changes: 14 additions & 6 deletions src/Driver/PDO/MySQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

/**
Expand Down
20 changes: 14 additions & 6 deletions src/Driver/PDO/OCI/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

/**
Expand Down
25 changes: 16 additions & 9 deletions src/Driver/PDO/PgSQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'])) {
Expand All @@ -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:';

Expand Down
25 changes: 15 additions & 10 deletions src/Driver/PDO/SQLSrv/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,30 +21,34 @@ 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;
}
}
}

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));
}

/**
Expand All @@ -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=';

Expand Down
25 changes: 15 additions & 10 deletions src/Driver/PDO/SQLite/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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'])) {
Expand Down
7 changes: 0 additions & 7 deletions tests/Functional/Driver/PDO/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9d1169f

Please sign in to comment.