Skip to content

Commit

Permalink
Merge pull request #3803 from morozov/issues/3798
Browse files Browse the repository at this point in the history
Backport PDO-related changes from master to 3.0.x
  • Loading branch information
morozov committed Jan 8, 2020
2 parents 683438c + dcc3f2d commit 228b18b
Show file tree
Hide file tree
Showing 49 changed files with 511 additions and 805 deletions.
48 changes: 48 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
# Upgrade to 3.0

## BC BREAK User-provided `PDO` instance is no longer supported

In order to share the same `PDO` instances between DBAL and other components, initialize the connection in DBAL and access it using `Connection::getWrappedConnection()->getWrappedConnection()`.

## 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\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:

use Doctrine\DBAL\Portability\Connection;

$params = array(
'wrapperClass' => Connection::class,
'fetch_case' => PDO::CASE_LOWER,
);

$stmt->bindValue(1, 1, PDO::PARAM_INT);
$stmt->fetchAll(PDO::FETCH_COLUMN);

After:

use Doctrine\DBAL\ColumnCase;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Portability\Connection;

$params = array(
'wrapperClass' => Connection::class,
'fetch_case' => ColumnCase::LOWER,
);

$stmt->bindValue(1, 1, ParameterType::INTEGER);
$stmt->fetchAll(FetchMode::COLUMN);

## BC BREAK: Removed dbal:import CLI command

The `dbal:import` CLI command has been removed since it only worked with PDO-based drivers by relying on a non-documented behavior of the extension, and it was impossible to make it work with other drivers.
Please use other database client applications for import, e.g.:

* For MySQL and MariaDB: `mysql [dbname] < data.sql`.
* For PostgreSQL: `psql [dbname] < data.sql`.
* For SQLite: `sqlite3 /path/to/file.db < data.sql`.

# Upgrade to 2.10

## Deprecated `Doctrine\DBAL\Event\ConnectionEventArgs` methods
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
],
"require": {
"php": "^7.2",
"ext-pdo": "*",
"doctrine/cache": "^1.0",
"doctrine/event-manager": "^1.0"
},
Expand Down
11 changes: 5 additions & 6 deletions lib/Doctrine/DBAL/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\DBAL\FetchMode;
use InvalidArgumentException;
use IteratorAggregate;
use PDO;
use function array_merge;
use function array_values;
use function count;
Expand Down Expand Up @@ -59,9 +58,9 @@ public function columnCount()
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
if ($arg2 !== null || $arg3 !== null) {
if (count($args) > 0) {
throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()');
}

Expand All @@ -83,7 +82,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
public function fetch($fetchMode = null, ...$args)
{
if (! isset($this->data[$this->num])) {
return false;
Expand Down Expand Up @@ -114,10 +113,10 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$rows = [];
while ($row = $this->fetch($fetchMode)) {
while ($row = $this->fetch($fetchMode, ...$args)) {
$rows[] = $row;
}

Expand Down
11 changes: 5 additions & 6 deletions lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Doctrine\DBAL\FetchMode;
use InvalidArgumentException;
use IteratorAggregate;
use PDO;
use function array_merge;
use function array_values;
use function assert;
Expand Down Expand Up @@ -105,7 +104,7 @@ public function columnCount()
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
public function setFetchMode($fetchMode, ...$args)
{
$this->defaultFetchMode = $fetchMode;

Expand All @@ -125,7 +124,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
public function fetch($fetchMode = null, ...$args)
{
if ($this->data === null) {
$this->data = [];
Expand Down Expand Up @@ -165,9 +164,9 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
public function fetchAll($fetchMode = null, ...$args)
{
$data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
$data = $this->statement->fetchAll($fetchMode, ...$args);

if ($fetchMode === FetchMode::COLUMN) {
foreach ($data as $key => $value) {
Expand Down Expand Up @@ -203,7 +202,7 @@ public function fetchColumn($columnIndex = 0)
*
* @return int The number of rows.
*/
public function rowCount()
public function rowCount() : int
{
assert($this->statement instanceof Statement);

Expand Down
6 changes: 2 additions & 4 deletions lib/Doctrine/DBAL/ColumnCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Doctrine\DBAL;

use PDO;

/**
* Contains portable column case conversions.
*/
Expand All @@ -14,14 +12,14 @@ final class ColumnCase
*
* @see \PDO::CASE_UPPER
*/
public const UPPER = PDO::CASE_UPPER;
public const UPPER = 1;

/**
* Convert column names to lower case.
*
* @see \PDO::CASE_LOWER
*/
public const LOWER = PDO::CASE_LOWER;
public const LOWER = 2;

/**
* This class cannot be instantiated.
Expand Down
63 changes: 18 additions & 45 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 @@ -188,12 +187,6 @@ public function __construct(
$this->_driver = $driver;
$this->params = $params;

if (isset($params['pdo'])) {
$this->_conn = $params['pdo'];
$this->isConnected = true;
unset($this->params['pdo']);
}

if (isset($params['platform'])) {
if (! $params['platform'] instanceof Platforms\AbstractPlatform) {
throw DBALException::invalidPlatformType($params['platform']);
Expand Down Expand Up @@ -847,18 +840,16 @@ public function fetchAll($sql, array $params = [], $types = [])
/**
* Prepares an SQL statement.
*
* @param string $statement The SQL statement to prepare.
*
* @return DriverStatement The prepared statement.
* @param string $sql The SQL statement to prepare.
*
* @throws DBALException
*/
public function prepare($statement)
public function prepare(string $sql) : DriverStatement
{
try {
$stmt = new Statement($statement, $this);
$stmt = new Statement($sql, $this);
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}

$stmt->setFetchMode($this->defaultFetchMode);
Expand All @@ -881,7 +872,7 @@ public function prepare($statement)
*
* @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,11 +920,9 @@ public function executeQuery($query, array $params = [], $types = [], ?QueryCach
* @param int[]|string[] $types The types the previous parameters are in.
* @param QueryCacheProfile $qcp The query cache profile.
*
* @return ResultStatement
*
* @throws CacheException
*/
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp)
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) : ResultStatement
{
$resultCache = $qcp->getResultCacheDriver() ?? $this->_config->getResultCacheImpl();

Expand Down Expand Up @@ -994,27 +983,21 @@ 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) : ResultStatement
{
$connection = $this->getWrappedConnection();

$args = func_get_args();

$logger = $this->_config->getSQLLogger();
if ($logger) {
$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 All @@ -1036,11 +1019,9 @@ public function query()
* @param mixed[] $params The query parameters.
* @param int[]|string[] $types The parameter types.
*
* @return int The number of affected rows.
*
* @throws DBALException
*/
public function executeUpdate($query, array $params = [], array $types = [])
public function executeUpdate(string $query, array $params = [], array $types = []) : int
{
$connection = $this->getWrappedConnection();

Expand Down Expand Up @@ -1077,15 +1058,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 DBALException
* {@inheritDoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$connection = $this->getWrappedConnection();

Expand Down Expand Up @@ -1527,13 +1502,11 @@ public function convertToPHPValue($value, $type)
* @internal Duck-typing used on the $stmt parameter to support driver statements as well as
* raw PDOStatement instances.
*
* @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to.
* @param mixed[] $params The map/list of named/positional parameters.
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping types).
*
* @return void
* @param DriverStatement $stmt The statement to bind the values to.
* @param mixed[] $params The map/list of named/positional parameters.
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping 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
19 changes: 9 additions & 10 deletions lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use InvalidArgumentException;
use function array_rand;
use function assert;
use function count;
use function func_get_args;

/**
* Master-Slave Connection
Expand Down Expand Up @@ -219,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 @@ -302,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 @@ -342,19 +343,17 @@ public function rollbackSavepoint($savepoint)
/**
* {@inheritDoc}
*/
public function query()
public function query(string $sql) : ResultStatement
{
$this->connect('master');
assert($this->_conn instanceof DriverConnection);

$args = func_get_args();

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

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

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

Expand All @@ -368,10 +367,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);
}
}

0 comments on commit 228b18b

Please sign in to comment.