Skip to content

Commit e5c49ba

Browse files
committed
Replace the concept of the statement fetch modes with an explicit API
1 parent 67944fc commit e5c49ba

File tree

69 files changed

+549
-2150
lines changed

Some content is hidden

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

69 files changed

+549
-2150
lines changed

UPGRADE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Upgrade to 3.0
22

3+
## Removed `FetchMode` and the corresponding methods
4+
5+
1. The `FetchMode` class and the `setFetchMode()` method of the `Connection` and `Statement` interfaces are removed.
6+
2. The `Statement::fetch()` method is replaced with `fetchNumeric()`, `fetchAssociative()` and `fetchOne()`.
7+
3. The `Statement::fetchAll()` method is replaced with `fetchAllNumeric()`, `fetchAllAssociative()` and `fechColumn()`.
8+
4. The `Statement::fetchColumn()` method is replaced with `fetchOne()`.
9+
5. The `Connection::fetchArray()` and `fetchAssoc()` methods are replaced with `fetchNumeric()` and `fetchAssociative()` respectively.
10+
6. The `StatementIterator` class is removed. The usage of a `Statement` object as `Traversable` is no longer possible. Use `iterateNumeric()`, `iterateAssociative()` and `iterateColumn()` instead.
11+
7. Fetching data in mixed mode (former `FetchMode::MIXED`) is no longer possible.
12+
313
## BC BREAK: Dropped handling of one-based numeric arrays of parameters in `Statement::execute()`
414

515
The statement implementations no longer detect whether `$params` is a zero- or one-based array. A zero-based numeric array is expected.

docs/en/reference/caching.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object is closed:
4242

4343
<?php
4444
$stmt = $conn->executeCacheQuery($query, $params, $types, new QueryCacheProfile(0, "some key"));
45-
$data = $stmt->fetchAll();
45+
$data = $stmt->fetchAllAssociative();
4646
$stmt->closeCursor(); // at this point the result is cached
4747

4848
.. warning::

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ the query until there are no more rows:
4141
4242
<?php
4343
44-
while ($row = $stmt->fetch()) {
44+
while (($row = $stmt->fetchAssociative()) !== false) {
4545
echo $row['headline'];
4646
}
4747
@@ -308,7 +308,7 @@ Prepare a given SQL statement and return the
308308
<?php
309309
$statement = $conn->prepare('SELECT * FROM user');
310310
$statement->execute();
311-
$users = $statement->fetchAll();
311+
$users = $statement->fetchAllAssociative();
312312
313313
/*
314314
array(
@@ -346,7 +346,7 @@ parameters to the execute method, then returning the statement:
346346
347347
<?php
348348
$statement = $conn->executeQuery('SELECT * FROM user WHERE username = ?', array('jwage'));
349-
$user = $statement->fetch();
349+
$user = $statement->fetchAssociative();
350350
351351
/*
352352
array(
@@ -360,15 +360,15 @@ to perform necessary type conversions between actual input
360360
parameters and expected database values. See the
361361
:ref:`Types <mappingMatrix>` section for more information.
362362

363-
fetchAll()
364-
~~~~~~~~~~
363+
fetchAllAssociative()
364+
~~~~~~~~~~~~~~~~~~~~~
365365

366366
Execute the query and fetch all results into an array:
367367

368368
.. code-block:: php
369369
370370
<?php
371-
$users = $conn->fetchAll('SELECT * FROM user');
371+
$users = $conn->fetchAllAssociative('SELECT * FROM user');
372372
373373
/*
374374
array(
@@ -379,15 +379,15 @@ Execute the query and fetch all results into an array:
379379
)
380380
*/
381381
382-
fetchArray()
383-
~~~~~~~~~~~~
382+
fetchNumeric()
383+
~~~~~~~~~~~~~~
384384

385385
Numeric index retrieval of first result row of the given query:
386386

387387
.. code-block:: php
388388
389389
<?php
390-
$user = $conn->fetchArray('SELECT * FROM user WHERE username = ?', array('jwage'));
390+
$user = $conn->fetchNumeric('SELECT * FROM user WHERE username = ?', array('jwage'));
391391
392392
/*
393393
array(
@@ -396,26 +396,26 @@ Numeric index retrieval of first result row of the given query:
396396
)
397397
*/
398398
399-
fetchColumn()
400-
~~~~~~~~~~~~~
399+
fetchOne()
400+
~~~~~~~~~~
401401

402-
Retrieve only the given column of the first result row.
402+
Retrieve only the value of the first column of the first result row.
403403

404404
.. code-block:: php
405405
406406
<?php
407-
$username = $conn->fetchColumn('SELECT username FROM user WHERE id = ?', array(1), 0);
407+
$username = $conn->fetchOne('SELECT username FROM user WHERE id = ?', array(1), 0);
408408
echo $username; // jwage
409409
410-
fetchAssoc()
411-
~~~~~~~~~~~~
410+
fetchAssociative()
411+
~~~~~~~~~~~~~~~~~~
412412

413-
Retrieve assoc row of the first result row.
413+
Retrieve associative array of the first result row.
414414

415415
.. code-block:: php
416416
417417
<?php
418-
$user = $conn->fetchAssoc('SELECT * FROM user WHERE username = ?', array('jwage'));
418+
$user = $conn->fetchAssociative('SELECT * FROM user WHERE username = ?', array('jwage'));
419419
/*
420420
array(
421421
'username' => 'jwage',

src/Cache/ArrayStatement.php

Lines changed: 13 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22

33
namespace Doctrine\DBAL\Cache;
44

5-
use ArrayIterator;
65
use Doctrine\DBAL\Driver\FetchUtils;
76
use Doctrine\DBAL\Driver\ResultStatement;
8-
use Doctrine\DBAL\FetchMode;
9-
use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement;
10-
use InvalidArgumentException;
11-
use IteratorAggregate;
12-
use function array_merge;
137
use function array_values;
148
use function count;
159
use function reset;
1610

17-
class ArrayStatement implements IteratorAggregate, ResultStatement, ForwardCompatibleResultStatement
11+
class ArrayStatement implements ResultStatement
1812
{
1913
/** @var mixed[] */
2014
private $data;
@@ -25,9 +19,6 @@ class ArrayStatement implements IteratorAggregate, ResultStatement, ForwardCompa
2519
/** @var int */
2620
private $num = 0;
2721

28-
/** @var int */
29-
private $defaultFetchMode = FetchMode::MIXED;
30-
3122
/**
3223
* @param mixed[] $data
3324
*/
@@ -59,97 +50,12 @@ public function columnCount()
5950
return $this->columnCount;
6051
}
6152

62-
/**
63-
* {@inheritdoc}
64-
*
65-
* @deprecated Use one of the fetch- or iterate-related methods.
66-
*/
67-
public function setFetchMode($fetchMode)
68-
{
69-
$this->defaultFetchMode = $fetchMode;
70-
71-
return true;
72-
}
73-
74-
/**
75-
* {@inheritdoc}
76-
*
77-
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
78-
*/
79-
public function getIterator()
80-
{
81-
$data = $this->fetchAll();
82-
83-
return new ArrayIterator($data);
84-
}
85-
86-
/**
87-
* {@inheritdoc}
88-
*
89-
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
90-
*/
91-
public function fetch($fetchMode = null)
92-
{
93-
if (! isset($this->data[$this->num])) {
94-
return false;
95-
}
96-
97-
$row = $this->data[$this->num++];
98-
$fetchMode = $fetchMode ?? $this->defaultFetchMode;
99-
100-
if ($fetchMode === FetchMode::ASSOCIATIVE) {
101-
return $row;
102-
}
103-
104-
if ($fetchMode === FetchMode::NUMERIC) {
105-
return array_values($row);
106-
}
107-
108-
if ($fetchMode === FetchMode::MIXED) {
109-
return array_merge($row, array_values($row));
110-
}
111-
112-
if ($fetchMode === FetchMode::COLUMN) {
113-
return reset($row);
114-
}
115-
116-
throw new InvalidArgumentException('Invalid fetch-style given for fetching result.');
117-
}
118-
119-
/**
120-
* {@inheritdoc}
121-
*
122-
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchColumn() instead.
123-
*/
124-
public function fetchAll($fetchMode = null)
125-
{
126-
$rows = [];
127-
while ($row = $this->fetch($fetchMode)) {
128-
$rows[] = $row;
129-
}
130-
131-
return $rows;
132-
}
133-
134-
/**
135-
* {@inheritdoc}
136-
*
137-
* @deprecated Use fetchOne() instead.
138-
*/
139-
public function fetchColumn()
140-
{
141-
$row = $this->fetch(FetchMode::NUMERIC);
142-
143-
// TODO: verify that return false is the correct behavior
144-
return $row[0] ?? false;
145-
}
146-
14753
/**
14854
* {@inheritdoc}
14955
*/
15056
public function fetchNumeric()
15157
{
152-
$row = $this->doFetch();
58+
$row = $this->fetch();
15359

15460
if ($row === false) {
15561
return false;
@@ -163,15 +69,15 @@ public function fetchNumeric()
16369
*/
16470
public function fetchAssociative()
16571
{
166-
return $this->doFetch();
72+
return $this->fetch();
16773
}
16874

16975
/**
17076
* {@inheritdoc}
17177
*/
17278
public function fetchOne()
17379
{
174-
$row = $this->doFetch();
80+
$row = $this->fetch();
17581

17682
if ($row === false) {
17783
return false;
@@ -196,10 +102,18 @@ public function fetchAllAssociative() : array
196102
return FetchUtils::fetchAllAssociative($this);
197103
}
198104

105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function fetchColumn() : array
109+
{
110+
return FetchUtils::fetchColumn($this);
111+
}
112+
199113
/**
200114
* @return mixed|false
201115
*/
202-
private function doFetch()
116+
private function fetch()
203117
{
204118
if (! isset($this->data[$this->num])) {
205119
return false;

0 commit comments

Comments
 (0)