Skip to content

Commit e5fe8c8

Browse files
authored
Merge pull request #2996 from morozov/issues/2953-2.x
Forward compatibility with 3.x
2 parents d9aaf54 + be4253f commit e5fe8c8

File tree

74 files changed

+1414
-802
lines changed

Some content is hidden

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

74 files changed

+1414
-802
lines changed

UPGRADE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212

1313
``Doctrine\DBAL\Connection::TRANSACTION_*`` were moved into ``Doctrine\DBAL\TransactionIsolationLevel`` class without the ``TRANSACTION_`` prefix.
1414

15+
## DEPRECATION: direct usage of the PDO APIs in the DBAL API
16+
17+
1. When calling `Doctrine\DBAL\Driver\Statement` methods, instead of `PDO::PARAM_*` constants, `Doctrine\DBAL\ParameterType` constants should be used.
18+
2. When calling `Doctrine\DBAL\Driver\ResultStatement` methods, instead of `PDO::FETCH_*` constants, `Doctrine\DBAL\FetchMode` constants should be used.
19+
3. When configuring `Doctrine\DBAL\Portability\Connection`, instead of `PDO::CASE_*` constants, `Doctrine\DBAL\ColumnCase` constants should be used.
20+
4. Usage of `PDO::PARAM_INPUT_OUTPUT` in `Doctrine\DBAL\Driver\Statement::bindValue()` is deprecated.
21+
5. Usage of `PDO::FETCH_FUNC` in `Doctrine\DBAL\Driver\ResultStatement::fetch()` is deprecated.
22+
6. Calls to `\PDOStatement` methods on a `\Doctrine\DBAL\Driver\PDOStatement` instance (e.g. `fetchObject()`) are deprecated.
23+
1524
# Upgrade to 2.6
1625

1726
## MINOR BC BREAK: `fetch()` and `fetchAll()` method signatures in `Doctrine\DBAL\Driver\ResultStatement`

docs/en/reference/data-retrieval-and-manipulation.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ Binding Types
180180
-------------
181181

182182
Doctrine DBAL extends PDOs handling of binding types in prepared statements
183-
considerably. Besides the well known ``\PDO::PARAM_*`` constants you
183+
considerably. Besides ``Doctrine\DBAL\ParameterType`` constants, you
184184
can make use of two very powerful additional features.
185185

186186
Doctrine\DBAL\Types Conversion
187187
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188188

189-
If you don't specify an integer (through a ``PDO::PARAM*`` constant) to
189+
If you don't specify an integer (through one of ``Doctrine\DBAL\ParameterType`` constants) to
190190
any of the parameter binding methods but a string, Doctrine DBAL will
191191
ask the type abstraction layer to convert the passed value from
192192
its PHP to a database representation. This way you can pass ``\DateTime``
@@ -271,7 +271,14 @@ be specified as well:
271271
// Same SQL WITHOUT usage of Doctrine\DBAL\Connection::PARAM_INT_ARRAY
272272
$stmt = $conn->executeQuery('SELECT * FROM articles WHERE id IN (?, ?, ?, ?, ?, ?)',
273273
array(1, 2, 3, 4, 5, 6),
274-
array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
274+
array(
275+
ParameterType::INTEGER,
276+
ParameterType::INTEGER,
277+
ParameterType::INTEGER,
278+
ParameterType::INTEGER,
279+
ParameterType::INTEGER,
280+
ParameterType::INTEGER,
281+
)
275282
);
276283
277284
This is much more complicated and is ugly to write generically.
@@ -469,8 +476,11 @@ Quote a value:
469476
.. code-block:: php
470477
471478
<?php
479+
480+
use Doctrine\DBAL\ParameterType;
481+
472482
$quoted = $conn->quote('value');
473-
$quoted = $conn->quote('1234', \PDO::PARAM_INT);
483+
$quoted = $conn->quote('1234', ParameterType::INTEGER);
474484
475485
quoteIdentifier()
476486
~~~~~~~~~~~~~~~~~

docs/en/reference/known-vendor-issues.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,6 @@ The ``PDO_SQLSRV`` driver currently has a bug when binding values to
187187
VARBINARY/BLOB columns with ``bindValue`` in prepared statements.
188188
This raises an implicit conversion from data type error as it tries
189189
to convert a character type value to a binary type value even if
190-
you explicitly define the value as ``\PDO::PARAM_LOB`` type.
190+
you explicitly define the value as ``ParameterType::LARGE_OBJECT`` type.
191191
Therefore it is highly encouraged to use the native ``sqlsrv``
192192
driver instead which does not have this limitation.

docs/en/reference/portability.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ Using the following code block in your initialization will:
5151
.. code-block:: php
5252
5353
<?php
54+
55+
use Doctrine\DBAL\ColumnCase;
56+
use Doctrine\DBAL\Portability\Connection as PortableConnection;
57+
5458
$params = array(
5559
// vendor specific configuration
5660
//...
57-
'wrapperClass' => 'Doctrine\DBAL\Portability\Connection',
58-
'portability' => \Doctrine\DBAL\Portability\Connection::PORTABILITY_ALL,
59-
'fetch_case' => \PDO::CASE_LOWER,
61+
'wrapperClass' => PortableConnection::class,
62+
'portability' => PortableConnection::PORTABILITY_ALL,
63+
'fetch_case' => ColumnCase::LOWER,
6064
);
6165
6266
This sort of portability handling is pretty expensive because all the result
@@ -80,4 +84,4 @@ This functionality is only implemented with Doctrine 2.1 upwards.
8084
Doctrine ships with lists of keywords for every supported vendor. You
8185
can access a keyword list through the schema manager of the vendor you
8286
are currently using or just instantiating it from the ``Doctrine\DBAL\Platforms\Keywords``
83-
namespace.
87+
namespace.

docs/en/reference/security.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ the ``Connection#quote`` method:
151151
152152
<?php
153153
// Parameter quoting
154-
$sql = "SELECT * FROM users WHERE name = " . $connection->quote($_GET['username'], \PDO::PARAM_STR);
154+
$sql = "SELECT * FROM users WHERE name = " . $connection->quote($_GET['username']);
155155
156156
This method is only available for SQL, not for DQL. For DQL you are always encouraged to use prepared
157-
statements not only for security, but also for caching reasons.
157+
statements not only for security, but also for caching reasons.

lib/Doctrine/DBAL/Cache/ArrayStatement.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace Doctrine\DBAL\Cache;
2121

2222
use Doctrine\DBAL\Driver\ResultStatement;
23-
use PDO;
23+
use Doctrine\DBAL\FetchMode;
2424

2525
class ArrayStatement implements \IteratorAggregate, ResultStatement
2626
{
@@ -42,7 +42,7 @@ class ArrayStatement implements \IteratorAggregate, ResultStatement
4242
/**
4343
* @var int
4444
*/
45-
private $defaultFetchMode = PDO::FETCH_BOTH;
45+
private $defaultFetchMode = FetchMode::MIXED;
4646

4747
/**
4848
* @param array $data
@@ -100,23 +100,30 @@ public function getIterator()
100100
*/
101101
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
102102
{
103-
if (isset($this->data[$this->num])) {
104-
$row = $this->data[$this->num++];
105-
$fetchMode = $fetchMode ?: $this->defaultFetchMode;
106-
if ($fetchMode === PDO::FETCH_ASSOC) {
107-
return $row;
108-
} elseif ($fetchMode === PDO::FETCH_NUM) {
109-
return array_values($row);
110-
} elseif ($fetchMode === PDO::FETCH_BOTH) {
111-
return array_merge($row, array_values($row));
112-
} elseif ($fetchMode === PDO::FETCH_COLUMN) {
113-
return reset($row);
114-
} else {
115-
throw new \InvalidArgumentException("Invalid fetch-style given for fetching result.");
116-
}
103+
if (! isset($this->data[$this->num])) {
104+
return false;
117105
}
118106

119-
return false;
107+
$row = $this->data[$this->num++];
108+
$fetchMode = $fetchMode ?: $this->defaultFetchMode;
109+
110+
if ($fetchMode === FetchMode::ASSOCIATIVE) {
111+
return $row;
112+
}
113+
114+
if ($fetchMode === FetchMode::NUMERIC) {
115+
return array_values($row);
116+
}
117+
118+
if ($fetchMode === FetchMode::MIXED) {
119+
return array_merge($row, array_values($row));
120+
}
121+
122+
if ($fetchMode === FetchMode::COLUMN) {
123+
return reset($row);
124+
}
125+
126+
throw new \InvalidArgumentException('Invalid fetch-style given for fetching result.');
120127
}
121128

122129
/**
@@ -137,7 +144,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n
137144
*/
138145
public function fetchColumn($columnIndex = 0)
139146
{
140-
$row = $this->fetch(PDO::FETCH_NUM);
147+
$row = $this->fetch(FetchMode::NUMERIC);
141148

142149
// TODO: verify that return false is the correct behavior
143150
return $row[$columnIndex] ?? false;

lib/Doctrine/DBAL/Cache/ResultCacheStatement.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Doctrine\DBAL\Driver\Statement;
2323
use Doctrine\DBAL\Driver\ResultStatement;
2424
use Doctrine\Common\Cache\Cache;
25-
use PDO;
25+
use Doctrine\DBAL\FetchMode;
2626

2727
/**
2828
* Cache statement for SQL results.
@@ -80,7 +80,7 @@ class ResultCacheStatement implements \IteratorAggregate, ResultStatement
8080
/**
8181
* @var int
8282
*/
83-
private $defaultFetchMode = PDO::FETCH_BOTH;
83+
private $defaultFetchMode = FetchMode::MIXED;
8484

8585
/**
8686
* @param \Doctrine\DBAL\Driver\Statement $stmt
@@ -153,24 +153,32 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
153153
$this->data = [];
154154
}
155155

156-
$row = $this->statement->fetch(PDO::FETCH_ASSOC);
156+
$row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
157+
157158
if ($row) {
158159
$this->data[] = $row;
159160

160161
$fetchMode = $fetchMode ?: $this->defaultFetchMode;
161162

162-
if ($fetchMode == PDO::FETCH_ASSOC) {
163+
if ($fetchMode === FetchMode::ASSOCIATIVE) {
163164
return $row;
164-
} elseif ($fetchMode == PDO::FETCH_NUM) {
165+
}
166+
167+
if ($fetchMode === FetchMode::NUMERIC) {
165168
return array_values($row);
166-
} elseif ($fetchMode == PDO::FETCH_BOTH) {
169+
}
170+
171+
if ($fetchMode === FetchMode::MIXED) {
167172
return array_merge($row, array_values($row));
168-
} elseif ($fetchMode == PDO::FETCH_COLUMN) {
173+
}
174+
175+
if ($fetchMode === FetchMode::COLUMN) {
169176
return reset($row);
170-
} else {
171-
throw new \InvalidArgumentException("Invalid fetch-style given for caching result.");
172177
}
178+
179+
throw new \InvalidArgumentException('Invalid fetch-style given for caching result.');
173180
}
181+
174182
$this->emptied = true;
175183

176184
return false;
@@ -194,7 +202,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n
194202
*/
195203
public function fetchColumn($columnIndex = 0)
196204
{
197-
$row = $this->fetch(PDO::FETCH_NUM);
205+
$row = $this->fetch(FetchMode::NUMERIC);
198206

199207
// TODO: verify that return false is the correct behavior
200208
return $row[$columnIndex] ?? false;

lib/Doctrine/DBAL/ColumnCase.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Doctrine\DBAL;
4+
5+
/**
6+
* Contains portable column case conversions.
7+
*/
8+
final class ColumnCase
9+
{
10+
/**
11+
* Convert column names to upper case.
12+
*
13+
* @see \PDO::CASE_UPPER
14+
*/
15+
public const UPPER = \PDO::CASE_UPPER;
16+
17+
/**
18+
* Convert column names to lower case.
19+
*
20+
* @see \PDO::CASE_LOWER
21+
*/
22+
public const LOWER = \PDO::CASE_LOWER;
23+
24+
/**
25+
* This class cannot be instantiated.
26+
*/
27+
private function __construct()
28+
{
29+
}
30+
}

lib/Doctrine/DBAL/Connection.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
2323
use Doctrine\DBAL\Exception\InvalidArgumentException;
24-
use PDO;
2524
use Closure;
2625
use Exception;
2726
use Doctrine\DBAL\Types\Type;
@@ -83,14 +82,14 @@ class Connection implements DriverConnection
8382
*
8483
* @var int
8584
*/
86-
const PARAM_INT_ARRAY = 101;
85+
public const PARAM_INT_ARRAY = ParameterType::INTEGER + self::ARRAY_PARAM_OFFSET;
8786

8887
/**
8988
* Represents an array of strings to be expanded by Doctrine SQL parsing.
9089
*
9190
* @var int
9291
*/
93-
const PARAM_STR_ARRAY = 102;
92+
public const PARAM_STR_ARRAY = ParameterType::STRING + self::ARRAY_PARAM_OFFSET;
9493

9594
/**
9695
* Offset by which PARAM_* constants are detected as arrays of the param type.
@@ -195,7 +194,7 @@ class Connection implements DriverConnection
195194
/**
196195
* @var int
197196
*/
198-
protected $defaultFetchMode = PDO::FETCH_ASSOC;
197+
protected $defaultFetchMode = FetchMode::ASSOCIATIVE;
199198

200199
/**
201200
* Initializes a new instance of the Connection class.
@@ -563,7 +562,7 @@ public function setFetchMode($fetchMode)
563562
*/
564563
public function fetchAssoc($statement, array $params = [], array $types = [])
565564
{
566-
return $this->executeQuery($statement, $params, $types)->fetch(PDO::FETCH_ASSOC);
565+
return $this->executeQuery($statement, $params, $types)->fetch(FetchMode::ASSOCIATIVE);
567566
}
568567

569568
/**
@@ -578,7 +577,7 @@ public function fetchAssoc($statement, array $params = [], array $types = [])
578577
*/
579578
public function fetchArray($statement, array $params = [], array $types = [])
580579
{
581-
return $this->executeQuery($statement, $params, $types)->fetch(PDO::FETCH_NUM);
580+
return $this->executeQuery($statement, $params, $types)->fetch(FetchMode::NUMERIC);
582581
}
583582

584583
/**
@@ -808,7 +807,7 @@ private function extractTypeValues(array $columnList, array $types)
808807
$typeValues = [];
809808

810809
foreach ($columnList as $columnIndex => $columnName) {
811-
$typeValues[] = $types[$columnName] ?? \PDO::PARAM_STR;
810+
$typeValues[] = $types[$columnName] ?? ParameterType::STRING;
812811
}
813812

814813
return $typeValues;
@@ -1592,7 +1591,7 @@ private function getBindingInfo($value, $type)
15921591
$value = $type->convertToDatabaseValue($value, $this->getDatabasePlatform());
15931592
$bindingType = $type->getBindingType();
15941593
} else {
1595-
$bindingType = $type; // PDO::PARAM_* constants
1594+
$bindingType = $type;
15961595
}
15971596

15981597
return [$value, $bindingType];

0 commit comments

Comments
 (0)