Skip to content

Commit

Permalink
Merge pull request #3080 from morozov/issues/3079
Browse files Browse the repository at this point in the history
[DBAL-3079] Reworked the usage of PDO in PDOConnection from inheritance to composition
  • Loading branch information
Ocramius authored Apr 13, 2018
2 parents c78e0e5 + 48bd0ca commit fe6bd76
Show file tree
Hide file tree
Showing 30 changed files with 224 additions and 229 deletions.
3 changes: 2 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ The Doctrine\DBAL\Version class is no longer available: please refrain from chec
## BC BREAK: the PDO symbols are no longer part of the DBAL API

1. The support of `PDO::PARAM_*`, `PDO::FETCH_*`, `PDO::CASE_*` and `PDO::PARAM_INPUT_OUTPUT` constants in the DBAL API is removed.
2. `\Doctrine\DBAL\Driver\PDOStatement` does not extend `\PDOStatement` anymore.
2. `\Doctrine\DBAL\Driver\PDOConnection` does not extend `\PDO` anymore. Please use `\Doctrine\DBAL\Driver\PDOConnection::getWrappedConnection()` to access the underlying `PDO` object.
3. `\Doctrine\DBAL\Driver\PDOStatement` does not extend `\PDOStatement` anymore.

Before:

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function fetchColumn($columnIndex = 0)
*
* @return int The number of rows.
*/
public function rowCount()
public function rowCount() : int
{
return $this->statement->rowCount();
}
Expand Down
94 changes: 35 additions & 59 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Doctrine\DBAL;

use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Closure;
use Exception;
Expand All @@ -17,7 +19,6 @@
use Throwable;
use function array_key_exists;
use function array_merge;
use function func_get_args;
use function implode;
use function is_int;
use function is_string;
Expand Down Expand Up @@ -856,18 +857,16 @@ public function fetchAll($sql, array $params = [], $types = [])
/**
* Prepares an SQL statement.
*
* @param string $statement The SQL statement to prepare.
* @param string $sql The SQL statement to prepare.
*
* @return \Doctrine\DBAL\Statement The prepared statement.
*
* @throws \Doctrine\DBAL\DBALException
* @throws DBALException
*/
public function prepare($statement)
public function prepare(string $sql) : DriverStatement
{
try {
$stmt = new Statement($statement, $this);
$stmt = new Statement($sql, $this);
} catch (Exception $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}

$stmt->setFetchMode($this->defaultFetchMode);
Expand All @@ -881,16 +880,14 @@ public function prepare($statement)
* If the query is parametrized, a prepared statement is used.
* If an SQLLogger is configured, the execution is logged.
*
* @param string $query The SQL query to execute.
* @param array $params The parameters to bind to the query, if any.
* @param array $types The types the previous parameters are in.
* @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.
* @param string $query The SQL query to execute.
* @param mixed[] $params The parameters to bind to the query, if any.
* @param (int|string|Type)[] $types The types the previous parameters are in.
* @param QueryCacheProfile|null $qcp The query cache profile, optional.
*
* @return \Doctrine\DBAL\Driver\Statement The executed statement.
*
* @throws \Doctrine\DBAL\DBALException
* @throws DBALException
*/
public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null)
public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
{
if ($qcp !== null) {
return $this->executeCacheQuery($query, $params, $types, $qcp);
Expand Down Expand Up @@ -929,16 +926,14 @@ public function executeQuery($query, array $params = [], $types = [], QueryCache
/**
* Executes a caching query.
*
* @param string $query The SQL query to execute.
* @param array $params The parameters to bind to the query, if any.
* @param array $types The types the previous parameters are in.
* @param \Doctrine\DBAL\Cache\QueryCacheProfile $qcp The query cache profile.
*
* @return \Doctrine\DBAL\Driver\ResultStatement
* @param string $query The SQL query to execute.
* @param mixed[] $params The parameters to bind to the query, if any.
* @param (int|string)|Type[] $types The types the previous parameters are in.
* @param QueryCacheProfile $qcp The query cache profile.
*
* @throws \Doctrine\DBAL\Cache\CacheException
* @throws DBALException
*/
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp)
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) : ResultStatement
{
$resultCache = $qcp->getResultCacheDriver() ?: $this->_config->getResultCacheImpl();
if ( ! $resultCache) {
Expand Down Expand Up @@ -993,25 +988,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 \Doctrine\DBAL\DBALException
* {@inheritDoc}
*/
public function query()
public function query(string $sql) : ResultStatement
{
$this->connect();

$args = func_get_args();

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

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

$statement->setFetchMode($this->defaultFetchMode);
Expand All @@ -1027,15 +1016,13 @@ public function query()
*
* This method supports PDO binding types as well as DBAL mapping types.
*
* @param string $query The SQL query.
* @param array $params The query parameters.
* @param array $types The parameter types.
*
* @return int The number of affected rows.
* @param string $query The SQL query.
* @param mixed[] $params The query parameters.
* @param (int|string|Type)[] $types The parameter types.
*
* @throws \Doctrine\DBAL\DBALException
* @throws DBALException
*/
public function executeUpdate($query, array $params = [], array $types = [])
public function executeUpdate(string $query, array $params = [], array $types = []) : int
{
$this->connect();

Expand Down Expand Up @@ -1067,15 +1054,9 @@ public function executeUpdate($query, array $params = [], array $types = [])
}

/**
* Executes an SQL statement and return the number of affected rows.
*
* @param string $statement
*
* @return int The number of affected rows.
*
* @throws \Doctrine\DBAL\DBALException
* {@inheritDoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$this->connect();

Expand Down Expand Up @@ -1479,16 +1460,11 @@ public function convertToPHPValue($value, $type)
* Binds a set of parameters, some or all of which are typed with a PDO binding type
* or DBAL mapping type, to a given statement.
*
* @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to.
* @param array $params The map/list of named/positional parameters.
* @param array $types The parameter types (PDO binding types or DBAL mapping types).
*
* @return void
*
* @internal Duck-typing used on the $stmt parameter to support driver statements as well as
* raw PDOStatement instances.
* @param DriverStatement $stmt The statement to bind the values to.
* @param mixed[] $params The map/list of named/positional parameters.
* @param (int|string|Type)[] $types The parameter types.
*/
private function _bindTypedValues($stmt, array $params, array $types)
private function _bindTypedValues(DriverStatement $stmt, array $params, array $types) : void
{
// Check whether parameters are positional or named. Mixing is not allowed, just like in PDO.
if (is_int(key($params))) {
Expand Down
28 changes: 12 additions & 16 deletions lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Doctrine\DBAL\Connections;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use function array_rand;
use function count;
use function func_get_args;

/**
* Master-Slave Connection
Expand Down Expand Up @@ -86,10 +87,7 @@ class MasterSlaveConnection extends Connection
/**
* Creates Master Slave Connection.
*
* @param array $params
* @param \Doctrine\DBAL\Driver $driver
* @param \Doctrine\DBAL\Configuration|null $config
* @param \Doctrine\Common\EventManager|null $eventManager
* @param mixed[] $params
*
* @throws \InvalidArgumentException
*/
Expand Down Expand Up @@ -222,7 +220,7 @@ protected function chooseConnectionConfiguration($connectionName, $params)
/**
* {@inheritDoc}
*/
public function executeUpdate($query, array $params = [], array $types = [])
public function executeUpdate(string $query, array $params = [], array $types = []) : int
{
$this->connect('master');

Expand Down Expand Up @@ -305,7 +303,7 @@ public function insert($tableName, array $data, array $types = [])
/**
* {@inheritDoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$this->connect('master');

Expand Down Expand Up @@ -345,16 +343,14 @@ public function rollbackSavepoint($savepoint)
/**
* {@inheritDoc}
*/
public function query()
public function query(string $sql) : ResultStatement
{
$this->connect('master');

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

$logger->stopQuery();

Expand All @@ -364,10 +360,10 @@ public function query()
/**
* {@inheritDoc}
*/
public function prepare($statement)
public function prepare(string $sql) : Statement
{
$this->connect('master');

return parent::prepare($statement);
return parent::prepare($sql);
}
}
17 changes: 6 additions & 11 deletions 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 @@ -16,19 +17,15 @@ interface Connection
{
/**
* Prepares a statement for execution and returns a Statement object.
*
* @param string $prepareString
*
* @return \Doctrine\DBAL\Driver\Statement
*/
public function prepare($prepareString);
public function prepare(string $sql) : Statement;

/**
* Executes an SQL statement, returning a result set as a Statement object.
*
* @return \Doctrine\DBAL\Driver\Statement
* @throws DBALException
*/
public function query();
public function query(string $sql) : ResultStatement;

/**
* Quotes a string for use in a query.
Expand All @@ -43,11 +40,9 @@ public function quote($input, $type = ParameterType::STRING);
/**
* Executes an SQL statement and return the number of affected rows.
*
* @param string $statement
*
* @return int
* @throws DBALException
*/
public function exec($statement);
public function exec(string $statement) : int;

/**
* Returns the ID of the last inserted row or sequence value.
Expand Down
11 changes: 5 additions & 6 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Doctrine\DBAL\Driver\IBMDB2;

use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\ParameterType;
use const DB2_AUTOCOMMIT_OFF;
use const DB2_AUTOCOMMIT_ON;
Expand All @@ -21,7 +23,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 @@ -73,7 +74,7 @@ public function requiresQueryForServerVersion()
/**
* {@inheritdoc}
*/
public function prepare($sql)
public function prepare(string $sql) : DriverStatement
{
$stmt = @db2_prepare($this->_conn, $sql);
if ( ! $stmt) {
Expand All @@ -86,10 +87,8 @@ public function prepare($sql)
/**
* {@inheritdoc}
*/
public function query()
public function query(string $sql) : ResultStatement
{
$args = func_get_args();
$sql = $args[0];
$stmt = $this->prepare($sql);
$stmt->execute();

Expand All @@ -113,7 +112,7 @@ public function quote($input, $type = ParameterType::STRING)
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$stmt = @db2_exec($this->_conn, $statement);

Expand Down
4 changes: 1 addition & 3 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use function db2_num_rows;
use function db2_stmt_error;
use function db2_stmt_errormsg;
use function func_get_args;
use function func_num_args;
use function gettype;
use function is_object;
use function is_string;
Expand Down Expand Up @@ -312,7 +310,7 @@ public function fetchColumn($columnIndex = 0)
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return (@db2_num_rows($this->_stmt)) ? : 0;
}
Expand Down
Loading

0 comments on commit fe6bd76

Please sign in to comment.