Skip to content

Commit

Permalink
[DBAL-3079] Reworked the usage of PDO in PDOConnection from inheritan…
Browse files Browse the repository at this point in the history
…ce to composition
  • Loading branch information
morozov committed Nov 2, 2019
1 parent dc47f56 commit d65f130
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 79 deletions.
17 changes: 5 additions & 12 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Throwable;
use function array_key_exists;
use function assert;
use function func_get_args;
use function implode;
use function is_int;
use function is_string;
Expand Down Expand Up @@ -990,25 +989,19 @@ public function project($query, array $params, Closure $function)
}

/**
* Executes an SQL statement, returning a result set as a Statement object.
*
* @return \Doctrine\DBAL\Driver\Statement
*
* @throws DBALException
* {@inheritDoc}
*/
public function query()
public function query(string $sql)
{
$connection = $this->getWrappedConnection();

$args = func_get_args();

$logger = $this->_config->getSQLLogger();
$logger->startQuery($args[0]);
$logger->startQuery($sql);

try {
$statement = $connection->query(...$args);
$statement = $connection->query($sql);
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]);
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}

$statement->setFetchMode($this->defaultFetchMode);
Expand Down
9 changes: 3 additions & 6 deletions lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function array_rand;
use function assert;
use function count;
use function func_get_args;

/**
* Master-Slave Connection
Expand Down Expand Up @@ -342,17 +341,15 @@ public function rollbackSavepoint($savepoint)
/**
* {@inheritDoc}
*/
public function query()
public function query(string $sql)
{
$this->connect('master');
assert($this->_conn instanceof DriverConnection);

$args = func_get_args();

$logger = $this->getConfiguration()->getSQLLogger();
$logger->startQuery($args[0]);
$logger->startQuery($sql);

$statement = $this->_conn->query(...$args);
$statement = $this->_conn->query($sql);

$statement->setFetchMode($this->defaultFetchMode);

Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/DBAL/Driver/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Driver;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\ParameterType;

/**
Expand All @@ -25,8 +26,10 @@ public function prepare($prepareString);
* Executes an SQL statement, returning a result set as a Statement object.
*
* @return Statement
*
* @throws DBALException
*/
public function query();
public function query(string $sql);

/**
* Quotes a string for use in a query.
Expand Down
5 changes: 1 addition & 4 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use function db2_rollback;
use function db2_server_info;
use function db2_stmt_errormsg;
use function func_get_args;

class DB2Connection implements Connection, ServerInfoAwareConnection
{
Expand Down Expand Up @@ -89,10 +88,8 @@ public function prepare($sql)
/**
* {@inheritdoc}
*/
public function query()
public function query(string $sql)
{
$args = func_get_args();
$sql = $args[0];
$stmt = $this->prepare($sql);
$stmt->execute();

Expand Down
5 changes: 1 addition & 4 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use const MYSQLI_SERVER_PUBLIC_KEY;
use function defined;
use function floor;
use function func_get_args;
use function in_array;
use function ini_get;
use function mysqli_errno;
Expand Down Expand Up @@ -134,10 +133,8 @@ public function prepare($prepareString)
/**
* {@inheritdoc}
*/
public function query()
public function query(string $sql)
{
$args = func_get_args();
$sql = $args[0];
$stmt = $this->prepare($sql);
$stmt->execute();

Expand Down
6 changes: 1 addition & 5 deletions lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use const OCI_DEFAULT;
use const OCI_NO_AUTO_COMMIT;
use function addcslashes;
use function func_get_args;
use function is_float;
use function is_int;
use function oci_commit;
Expand Down Expand Up @@ -111,11 +110,8 @@ public function prepare($prepareString)
/**
* {@inheritdoc}
*/
public function query()
public function query(string $sql)
{
$args = func_get_args();
$sql = $args[0];
//$fetchMode = $args[1];
$stmt = $this->prepare($sql);
$stmt->execute();

Expand Down
82 changes: 65 additions & 17 deletions lib/Doctrine/DBAL/Driver/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

use Doctrine\DBAL\ParameterType;
use PDO;
use function func_get_args;
use function assert;

/**
* PDO implementation of the Connection interface.
*
* Used by all PDO-based drivers.
*/
class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
class PDOConnection implements Connection, ServerInfoAwareConnection
{
/** @var PDO */
private $connection;

/**
* @param string $dsn
* @param string|null $user
Expand All @@ -23,8 +27,8 @@ class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
public function __construct($dsn, $user = null, $password = null, ?array $options = null)
{
try {
parent::__construct($dsn, (string) $user, (string) $password, (array) $options);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -36,7 +40,7 @@ public function __construct($dsn, $user = null, $password = null, ?array $option
public function exec($statement)
{
try {
return parent::exec($statement);
return $this->connection->exec($statement);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -47,17 +51,17 @@ public function exec($statement)
*/
public function getServerVersion()
{
return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
}

/**
* {@inheritdoc}
*/
public function prepare($prepareString, $driverOptions = [])
public function prepare($prepareString)
{
try {
return $this->createStatement(
parent::prepare($prepareString, $driverOptions)
$this->connection->prepare($prepareString)
);
} catch (\PDOException $exception) {
throw new PDOException($exception);
Expand All @@ -67,14 +71,13 @@ public function prepare($prepareString, $driverOptions = [])
/**
* {@inheritdoc}
*/
public function query()
public function query(string $sql)
{
$args = func_get_args();

try {
return $this->createStatement(
parent::query(...$args)
);
$stmt = $this->connection->query($sql);
assert($stmt instanceof \PDOStatement);

return $this->createStatement($stmt);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -85,7 +88,7 @@ public function query()
*/
public function quote($input, $type = ParameterType::STRING)
{
return parent::quote($input, $type);
return $this->connection->quote($input, $type);
}

/**
Expand All @@ -95,10 +98,10 @@ public function lastInsertId($name = null)
{
try {
if ($name === null) {
return parent::lastInsertId();
return $this->connection->lastInsertId();
}

return parent::lastInsertId($name);
return $this->connection->lastInsertId($name);
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand All @@ -119,4 +122,49 @@ protected function createStatement(\PDOStatement $stmt) : PDOStatement
{
return new PDOStatement($stmt);
}

/**
* {@inheritDoc}
*/
public function beginTransaction()
{
return $this->connection->beginTransaction();
}

/**
* {@inheritDoc}
*/
public function commit()
{
return $this->connection->commit();
}

/**
* {@inheritDoc}
*/
public function rollBack()
{
return $this->connection->rollBack();
}

/**
* {@inheritDoc}
*/
public function errorCode()
{
return $this->connection->errorCode();
}

/**
* {@inheritDoc}
*/
public function errorInfo()
{
return $this->connection->errorInfo();
}

public function getWrappedConnection() : PDO
{
return $this->connection;
}
}
8 changes: 4 additions & 4 deletions lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Driver extends AbstractPostgreSQLDriver
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
try {
$pdo = new PDOConnection(
$connection = new PDOConnection(
$this->_constructPdoDsn($params),
$username,
$password,
Expand All @@ -32,18 +32,18 @@ public function connect(array $params, $username = null, $password = null, array
|| $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
)
) {
$pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
$connection->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
}

/* 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'])) {
$pdo->exec('SET NAMES \'' . $params['charset'] . '\'');
$connection->exec('SET NAMES \'' . $params['charset'] . '\'');
}

return $pdo;
return $connection;
} catch (PDOException $e) {
throw DBALException::driverException($this, $e);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function connect(array $params, $username = null, $password = null, array
}

try {
$pdo = new PDOConnection(
$connection = new PDOConnection(
$this->_constructPdoDsn($params),
$username,
$password,
Expand All @@ -45,11 +45,13 @@ public function connect(array $params, $username = null, $password = null, array
throw DBALException::driverException($this, $ex);
}

$pdo = $connection->getWrappedConnection();

foreach ($this->_userDefinedFunctions as $fn => $data) {
$pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
}

return $pdo;
return $connection;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\ParameterType;
use function assert;
use function func_get_args;
use function is_float;
use function is_int;
use function is_resource;
Expand Down Expand Up @@ -151,11 +150,9 @@ public function prepare($prepareString)
/**
* {@inheritdoc}
*/
public function query()
public function query(string $sql)
{
$args = func_get_args();
$stmt = $this->prepare($args[0]);

$stmt = $this->prepare($sql);
$stmt->execute();

return $stmt;
Expand Down
5 changes: 1 addition & 4 deletions lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\ParameterType;
use const SQLSRV_ERR_ERRORS;
use function func_get_args;
use function is_float;
use function is_int;
use function sprintf;
Expand Down Expand Up @@ -83,10 +82,8 @@ public function prepare($sql)
/**
* {@inheritDoc}
*/
public function query()
public function query(string $sql)
{
$args = func_get_args();
$sql = $args[0];
$stmt = $this->prepare($sql);
$stmt->execute();

Expand Down
Loading

0 comments on commit d65f130

Please sign in to comment.