Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 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 Expand Up @@ -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);
}
Expand Down Expand Up @@ -119,14 +108,6 @@ public function lastInsertId($name = null)
}
}

/**
* Creates a wrapped statement
*/
protected function createStatement(PDOStatement $stmt): Statement
{
return new Statement($stmt);
}

/**
* {@inheritDoc}
*/
Expand Down
22 changes: 15 additions & 7 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 All @@ -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'] !== '') {
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
33 changes: 19 additions & 14 deletions src/Driver/PDO/SQLite/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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);
}

/**
Expand All @@ -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'])) {
Expand Down