Skip to content

Commit 228b18b

Browse files
authored
Merge pull request #3803 from morozov/issues/3798
Backport PDO-related changes from master to 3.0.x
2 parents 683438c + dcc3f2d commit 228b18b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+511
-805
lines changed

UPGRADE.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,51 @@
1+
# Upgrade to 3.0
2+
3+
## BC BREAK User-provided `PDO` instance is no longer supported
4+
5+
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()`.
6+
7+
## BC BREAK: the PDO symbols are no longer part of the DBAL API
8+
9+
1. The support of `PDO::PARAM_*`, `PDO::FETCH_*`, `PDO::CASE_*` and `PDO::PARAM_INPUT_OUTPUT` constants in the DBAL API is removed.
10+
2. `\Doctrine\DBAL\Driver\PDOConnection` does not extend `\PDO` anymore. Please use `\Doctrine\DBAL\Driver\PDOConnection::getWrappedConnection()` to access the underlying `PDO` object.
11+
3. `\Doctrine\DBAL\Driver\PDOStatement` does not extend `\PDOStatement` anymore.
12+
13+
Before:
14+
15+
use Doctrine\DBAL\Portability\Connection;
16+
17+
$params = array(
18+
'wrapperClass' => Connection::class,
19+
'fetch_case' => PDO::CASE_LOWER,
20+
);
21+
22+
$stmt->bindValue(1, 1, PDO::PARAM_INT);
23+
$stmt->fetchAll(PDO::FETCH_COLUMN);
24+
25+
After:
26+
27+
use Doctrine\DBAL\ColumnCase;
28+
use Doctrine\DBAL\FetchMode;
29+
use Doctrine\DBAL\ParameterType;
30+
use Doctrine\DBAL\Portability\Connection;
31+
32+
$params = array(
33+
'wrapperClass' => Connection::class,
34+
'fetch_case' => ColumnCase::LOWER,
35+
);
36+
37+
$stmt->bindValue(1, 1, ParameterType::INTEGER);
38+
$stmt->fetchAll(FetchMode::COLUMN);
39+
40+
## BC BREAK: Removed dbal:import CLI command
41+
42+
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.
43+
Please use other database client applications for import, e.g.:
44+
45+
* For MySQL and MariaDB: `mysql [dbname] < data.sql`.
46+
* For PostgreSQL: `psql [dbname] < data.sql`.
47+
* For SQLite: `sqlite3 /path/to/file.db < data.sql`.
48+
149
# Upgrade to 2.10
250

351
## Deprecated `Doctrine\DBAL\Event\ConnectionEventArgs` methods

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
],
3434
"require": {
3535
"php": "^7.2",
36-
"ext-pdo": "*",
3736
"doctrine/cache": "^1.0",
3837
"doctrine/event-manager": "^1.0"
3938
},

lib/Doctrine/DBAL/Cache/ArrayStatement.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Doctrine\DBAL\FetchMode;
88
use InvalidArgumentException;
99
use IteratorAggregate;
10-
use PDO;
1110
use function array_merge;
1211
use function array_values;
1312
use function count;
@@ -59,9 +58,9 @@ public function columnCount()
5958
/**
6059
* {@inheritdoc}
6160
*/
62-
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
61+
public function setFetchMode($fetchMode, ...$args)
6362
{
64-
if ($arg2 !== null || $arg3 !== null) {
63+
if (count($args) > 0) {
6564
throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()');
6665
}
6766

@@ -83,7 +82,7 @@ public function getIterator()
8382
/**
8483
* {@inheritdoc}
8584
*/
86-
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
85+
public function fetch($fetchMode = null, ...$args)
8786
{
8887
if (! isset($this->data[$this->num])) {
8988
return false;
@@ -114,10 +113,10 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
114113
/**
115114
* {@inheritdoc}
116115
*/
117-
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
116+
public function fetchAll($fetchMode = null, ...$args)
118117
{
119118
$rows = [];
120-
while ($row = $this->fetch($fetchMode)) {
119+
while ($row = $this->fetch($fetchMode, ...$args)) {
121120
$rows[] = $row;
122121
}
123122

lib/Doctrine/DBAL/Cache/ResultCacheStatement.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Doctrine\DBAL\FetchMode;
1010
use InvalidArgumentException;
1111
use IteratorAggregate;
12-
use PDO;
1312
use function array_merge;
1413
use function array_values;
1514
use function assert;
@@ -105,7 +104,7 @@ public function columnCount()
105104
/**
106105
* {@inheritdoc}
107106
*/
108-
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
107+
public function setFetchMode($fetchMode, ...$args)
109108
{
110109
$this->defaultFetchMode = $fetchMode;
111110

@@ -125,7 +124,7 @@ public function getIterator()
125124
/**
126125
* {@inheritdoc}
127126
*/
128-
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
127+
public function fetch($fetchMode = null, ...$args)
129128
{
130129
if ($this->data === null) {
131130
$this->data = [];
@@ -165,9 +164,9 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
165164
/**
166165
* {@inheritdoc}
167166
*/
168-
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
167+
public function fetchAll($fetchMode = null, ...$args)
169168
{
170-
$data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
169+
$data = $this->statement->fetchAll($fetchMode, ...$args);
171170

172171
if ($fetchMode === FetchMode::COLUMN) {
173172
foreach ($data as $key => $value) {
@@ -203,7 +202,7 @@ public function fetchColumn($columnIndex = 0)
203202
*
204203
* @return int The number of rows.
205204
*/
206-
public function rowCount()
205+
public function rowCount() : int
207206
{
208207
assert($this->statement instanceof Statement);
209208

lib/Doctrine/DBAL/ColumnCase.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Doctrine\DBAL;
44

5-
use PDO;
6-
75
/**
86
* Contains portable column case conversions.
97
*/
@@ -14,14 +12,14 @@ final class ColumnCase
1412
*
1513
* @see \PDO::CASE_UPPER
1614
*/
17-
public const UPPER = PDO::CASE_UPPER;
15+
public const UPPER = 1;
1816

1917
/**
2018
* Convert column names to lower case.
2119
*
2220
* @see \PDO::CASE_LOWER
2321
*/
24-
public const LOWER = PDO::CASE_LOWER;
22+
public const LOWER = 2;
2523

2624
/**
2725
* This class cannot be instantiated.

lib/Doctrine/DBAL/Connection.php

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Throwable;
2424
use function array_key_exists;
2525
use function assert;
26-
use function func_get_args;
2726
use function implode;
2827
use function is_int;
2928
use function is_string;
@@ -188,12 +187,6 @@ public function __construct(
188187
$this->_driver = $driver;
189188
$this->params = $params;
190189

191-
if (isset($params['pdo'])) {
192-
$this->_conn = $params['pdo'];
193-
$this->isConnected = true;
194-
unset($this->params['pdo']);
195-
}
196-
197190
if (isset($params['platform'])) {
198191
if (! $params['platform'] instanceof Platforms\AbstractPlatform) {
199192
throw DBALException::invalidPlatformType($params['platform']);
@@ -847,18 +840,16 @@ public function fetchAll($sql, array $params = [], $types = [])
847840
/**
848841
* Prepares an SQL statement.
849842
*
850-
* @param string $statement The SQL statement to prepare.
851-
*
852-
* @return DriverStatement The prepared statement.
843+
* @param string $sql The SQL statement to prepare.
853844
*
854845
* @throws DBALException
855846
*/
856-
public function prepare($statement)
847+
public function prepare(string $sql) : DriverStatement
857848
{
858849
try {
859-
$stmt = new Statement($statement, $this);
850+
$stmt = new Statement($sql, $this);
860851
} catch (Throwable $ex) {
861-
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
852+
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
862853
}
863854

864855
$stmt->setFetchMode($this->defaultFetchMode);
@@ -881,7 +872,7 @@ public function prepare($statement)
881872
*
882873
* @throws DBALException
883874
*/
884-
public function executeQuery($query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null)
875+
public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
885876
{
886877
if ($qcp !== null) {
887878
return $this->executeCacheQuery($query, $params, $types, $qcp);
@@ -929,11 +920,9 @@ public function executeQuery($query, array $params = [], $types = [], ?QueryCach
929920
* @param int[]|string[] $types The types the previous parameters are in.
930921
* @param QueryCacheProfile $qcp The query cache profile.
931922
*
932-
* @return ResultStatement
933-
*
934923
* @throws CacheException
935924
*/
936-
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp)
925+
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) : ResultStatement
937926
{
938927
$resultCache = $qcp->getResultCacheDriver() ?? $this->_config->getResultCacheImpl();
939928

@@ -994,27 +983,21 @@ public function project($query, array $params, Closure $function)
994983
}
995984

996985
/**
997-
* Executes an SQL statement, returning a result set as a Statement object.
998-
*
999-
* @return \Doctrine\DBAL\Driver\Statement
1000-
*
1001-
* @throws DBALException
986+
* {@inheritDoc}
1002987
*/
1003-
public function query()
988+
public function query(string $sql) : ResultStatement
1004989
{
1005990
$connection = $this->getWrappedConnection();
1006991

1007-
$args = func_get_args();
1008-
1009992
$logger = $this->_config->getSQLLogger();
1010993
if ($logger) {
1011-
$logger->startQuery($args[0]);
994+
$logger->startQuery($sql);
1012995
}
1013996

1014997
try {
1015-
$statement = $connection->query(...$args);
998+
$statement = $connection->query($sql);
1016999
} catch (Throwable $ex) {
1017-
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]);
1000+
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
10181001
}
10191002

10201003
$statement->setFetchMode($this->defaultFetchMode);
@@ -1036,11 +1019,9 @@ public function query()
10361019
* @param mixed[] $params The query parameters.
10371020
* @param int[]|string[] $types The parameter types.
10381021
*
1039-
* @return int The number of affected rows.
1040-
*
10411022
* @throws DBALException
10421023
*/
1043-
public function executeUpdate($query, array $params = [], array $types = [])
1024+
public function executeUpdate(string $query, array $params = [], array $types = []) : int
10441025
{
10451026
$connection = $this->getWrappedConnection();
10461027

@@ -1077,15 +1058,9 @@ public function executeUpdate($query, array $params = [], array $types = [])
10771058
}
10781059

10791060
/**
1080-
* Executes an SQL statement and return the number of affected rows.
1081-
*
1082-
* @param string $statement
1083-
*
1084-
* @return int The number of affected rows.
1085-
*
1086-
* @throws DBALException
1061+
* {@inheritDoc}
10871062
*/
1088-
public function exec($statement)
1063+
public function exec(string $statement) : int
10891064
{
10901065
$connection = $this->getWrappedConnection();
10911066

@@ -1527,13 +1502,11 @@ public function convertToPHPValue($value, $type)
15271502
* @internal Duck-typing used on the $stmt parameter to support driver statements as well as
15281503
* raw PDOStatement instances.
15291504
*
1530-
* @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to.
1531-
* @param mixed[] $params The map/list of named/positional parameters.
1532-
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping types).
1533-
*
1534-
* @return void
1505+
* @param DriverStatement $stmt The statement to bind the values to.
1506+
* @param mixed[] $params The map/list of named/positional parameters.
1507+
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping types).
15351508
*/
1536-
private function _bindTypedValues($stmt, array $params, array $types)
1509+
private function _bindTypedValues(DriverStatement $stmt, array $params, array $types) : void
15371510
{
15381511
// Check whether parameters are positional or named. Mixing is not allowed, just like in PDO.
15391512
if (is_int(key($params))) {

lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
use Doctrine\DBAL\Connection;
88
use Doctrine\DBAL\Driver;
99
use Doctrine\DBAL\Driver\Connection as DriverConnection;
10+
use Doctrine\DBAL\Driver\ResultStatement;
11+
use Doctrine\DBAL\Driver\Statement;
1012
use Doctrine\DBAL\Event\ConnectionEventArgs;
1113
use Doctrine\DBAL\Events;
1214
use InvalidArgumentException;
1315
use function array_rand;
1416
use function assert;
1517
use function count;
16-
use function func_get_args;
1718

1819
/**
1920
* Master-Slave Connection
@@ -219,7 +220,7 @@ protected function chooseConnectionConfiguration($connectionName, $params)
219220
/**
220221
* {@inheritDoc}
221222
*/
222-
public function executeUpdate($query, array $params = [], array $types = [])
223+
public function executeUpdate(string $query, array $params = [], array $types = []) : int
223224
{
224225
$this->connect('master');
225226

@@ -302,7 +303,7 @@ public function insert($tableName, array $data, array $types = [])
302303
/**
303304
* {@inheritDoc}
304305
*/
305-
public function exec($statement)
306+
public function exec(string $statement) : int
306307
{
307308
$this->connect('master');
308309

@@ -342,19 +343,17 @@ public function rollbackSavepoint($savepoint)
342343
/**
343344
* {@inheritDoc}
344345
*/
345-
public function query()
346+
public function query(string $sql) : ResultStatement
346347
{
347348
$this->connect('master');
348349
assert($this->_conn instanceof DriverConnection);
349350

350-
$args = func_get_args();
351-
352351
$logger = $this->getConfiguration()->getSQLLogger();
353352
if ($logger) {
354-
$logger->startQuery($args[0]);
353+
$logger->startQuery($sql);
355354
}
356355

357-
$statement = $this->_conn->query(...$args);
356+
$statement = $this->_conn->query($sql);
358357

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

@@ -368,10 +367,10 @@ public function query()
368367
/**
369368
* {@inheritDoc}
370369
*/
371-
public function prepare($statement)
370+
public function prepare(string $sql) : Statement
372371
{
373372
$this->connect('master');
374373

375-
return parent::prepare($statement);
374+
return parent::prepare($sql);
376375
}
377376
}

0 commit comments

Comments
 (0)