Skip to content

Commit ef22d72

Browse files
committed
Replace fetch mode with explicit API
1 parent fd0c14e commit ef22d72

File tree

66 files changed

+1308
-1005
lines changed

Some content is hidden

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

66 files changed

+1308
-1005
lines changed

src/Cache/ArrayStatement.php

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44

55
namespace Doctrine\DBAL\Cache;
66

7-
use ArrayIterator;
7+
use Doctrine\DBAL\Driver\FetchUtils;
88
use Doctrine\DBAL\Driver\ResultStatement;
9-
use Doctrine\DBAL\FetchMode;
10-
use InvalidArgumentException;
11-
use IteratorAggregate;
12-
use function array_merge;
9+
use Traversable;
1310
use function array_values;
1411
use function count;
1512
use function reset;
16-
use function sprintf;
1713

18-
final class ArrayStatement implements IteratorAggregate, ResultStatement
14+
final class ArrayStatement implements ResultStatement
1915
{
2016
/** @var mixed[] */
2117
private $data;
@@ -57,69 +53,100 @@ public function rowCount() : int
5753
/**
5854
* {@inheritdoc}
5955
*/
60-
public function getIterator()
56+
public function fetchNumeric()
6157
{
62-
$data = $this->fetchAll();
58+
$row = $this->fetch();
6359

64-
return new ArrayIterator($data);
60+
if ($row === false) {
61+
return false;
62+
}
63+
64+
return array_values($row);
6565
}
6666

6767
/**
6868
* {@inheritdoc}
6969
*/
70-
public function fetch(int $fetchMode = FetchMode::ASSOCIATIVE)
70+
public function fetchAssociative()
7171
{
72-
if (! isset($this->data[$this->num])) {
73-
return false;
74-
}
75-
76-
$row = $this->data[$this->num++];
72+
return $this->fetch();
73+
}
7774

78-
if ($fetchMode === FetchMode::ASSOCIATIVE) {
79-
return $row;
80-
}
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function fetchOne()
79+
{
80+
$row = $this->fetch();
8181

82-
if ($fetchMode === FetchMode::NUMERIC) {
83-
return array_values($row);
82+
if ($row === false) {
83+
return false;
8484
}
8585

86-
if ($fetchMode === FetchMode::MIXED) {
87-
return array_merge($row, array_values($row));
88-
}
86+
return reset($row);
87+
}
8988

90-
if ($fetchMode === FetchMode::COLUMN) {
91-
return reset($row);
92-
}
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function fetchAllNumeric() : array
93+
{
94+
return FetchUtils::fetchAllNumericByOne($this);
95+
}
9396

94-
throw new InvalidArgumentException(
95-
sprintf('Invalid fetch mode given for fetching result, %d given.', $fetchMode)
96-
);
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function fetchAllAssociative() : array
101+
{
102+
return FetchUtils::fetchAllAssociativeByOne($this);
97103
}
98104

99105
/**
100106
* {@inheritdoc}
101107
*/
102-
public function fetchAll(int $fetchMode = FetchMode::ASSOCIATIVE) : array
108+
public function fetchColumn() : array
103109
{
104-
$rows = [];
105-
while ($row = $this->fetch($fetchMode)) {
106-
$rows[] = $row;
110+
return FetchUtils::fetchColumnByOne($this);
111+
}
112+
113+
/**
114+
* @return Traversable<int,array<int,mixed>>
115+
*/
116+
public function iterateNumeric() : Traversable
117+
{
118+
foreach ($this->data as $row) {
119+
yield array_values($row);
107120
}
121+
}
108122

109-
return $rows;
123+
/**
124+
* @return Traversable<int,array<string,mixed>>
125+
*/
126+
public function iterateAssociative() : Traversable
127+
{
128+
yield from $this->data;
110129
}
111130

112131
/**
113-
* {@inheritdoc}
132+
* @return Traversable<int,mixed>
114133
*/
115-
public function fetchColumn()
134+
public function iterateColumn() : Traversable
116135
{
117-
$row = $this->fetch(FetchMode::NUMERIC);
136+
foreach ($this->data as $row) {
137+
yield reset($row);
138+
}
139+
}
118140

119-
if ($row === false) {
141+
/**
142+
* @return mixed|false
143+
*/
144+
private function fetch()
145+
{
146+
if (! isset($this->data[$this->num])) {
120147
return false;
121148
}
122149

123-
return $row[0];
150+
return $this->data[$this->num++];
124151
}
125152
}

src/Cache/ResultCacheStatement.php

Lines changed: 112 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
namespace Doctrine\DBAL\Cache;
66

7-
use ArrayIterator;
87
use Doctrine\Common\Cache\Cache;
8+
use Doctrine\DBAL\Driver\DriverException;
9+
use Doctrine\DBAL\Driver\FetchUtils;
910
use Doctrine\DBAL\Driver\ResultStatement;
10-
use Doctrine\DBAL\FetchMode;
11-
use InvalidArgumentException;
12-
use IteratorAggregate;
13-
use function array_merge;
11+
use Traversable;
1412
use function array_values;
15-
use function reset;
1613

1714
/**
1815
* Cache statement for SQL results.
@@ -27,7 +24,7 @@
2724
* Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
2825
* This means that the memory usage for cached results might increase by using this feature.
2926
*/
30-
final class ResultCacheStatement implements IteratorAggregate, ResultStatement
27+
final class ResultCacheStatement implements ResultStatement
3128
{
3229
/** @var Cache */
3330
private $resultCache;
@@ -51,7 +48,7 @@ final class ResultCacheStatement implements IteratorAggregate, ResultStatement
5148
*/
5249
private $emptied = false;
5350

54-
/** @var mixed[] */
51+
/** @var array<int,array<mixed>> */
5552
private $data;
5653

5754
public function __construct(ResultStatement $stmt, Cache $resultCache, string $cacheKey, string $realKey, int $lifetime)
@@ -90,82 +87,94 @@ public function columnCount() : int
9087
/**
9188
* {@inheritdoc}
9289
*/
93-
public function getIterator()
90+
public function fetchNumeric()
9491
{
95-
$data = $this->fetchAll();
92+
$row = $this->fetch();
9693

97-
return new ArrayIterator($data);
94+
if ($row === false) {
95+
return false;
96+
}
97+
98+
return array_values($row);
9899
}
99100

100101
/**
101102
* {@inheritdoc}
102103
*/
103-
public function fetch(int $fetchMode = FetchMode::ASSOCIATIVE)
104+
public function fetchAssociative()
104105
{
105-
if ($this->data === null) {
106-
$this->data = [];
107-
}
108-
109-
$row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
110-
111-
if ($row !== false) {
112-
$this->data[] = $row;
113-
114-
if ($fetchMode === FetchMode::ASSOCIATIVE) {
115-
return $row;
116-
}
117-
118-
if ($fetchMode === FetchMode::NUMERIC) {
119-
return array_values($row);
120-
}
121-
122-
if ($fetchMode === FetchMode::MIXED) {
123-
return array_merge($row, array_values($row));
124-
}
125-
126-
if ($fetchMode === FetchMode::COLUMN) {
127-
return reset($row);
128-
}
129-
130-
throw new InvalidArgumentException('Invalid fetch-style given for caching result.');
131-
}
132-
133-
$this->emptied = true;
134-
135-
return false;
106+
return $this->fetch();
136107
}
137108

138109
/**
139110
* {@inheritdoc}
140111
*/
141-
public function fetchAll(int $fetchMode = FetchMode::ASSOCIATIVE) : array
112+
public function fetchOne()
142113
{
143-
$data = $this->statement->fetchAll($fetchMode);
144-
145-
if ($fetchMode === FetchMode::COLUMN) {
146-
foreach ($data as $key => $value) {
147-
$data[$key] = [$value];
148-
}
149-
}
114+
return FetchUtils::fetchOneFromNumeric($this);
115+
}
150116

151-
$this->data = $data;
152-
$this->emptied = true;
117+
/**
118+
* {@inheritdoc}
119+
*/
120+
public function fetchAllNumeric() : array
121+
{
122+
return $this->store(
123+
$this->statement->fetchAllAssociative(),
124+
true
125+
);
126+
}
153127

154-
return $this->data;
128+
/**
129+
* {@inheritdoc}
130+
*/
131+
public function fetchAllAssociative() : array
132+
{
133+
return $this->store(
134+
$this->statement->fetchAllAssociative(),
135+
true
136+
);
155137
}
156138

157139
/**
158140
* {@inheritdoc}
159141
*/
160-
public function fetchColumn()
142+
public function fetchColumn() : array
161143
{
162-
$row = $this->fetch(FetchMode::NUMERIC);
144+
return $this->store(
145+
$this->statement->fetchAllAssociative(),
146+
true
147+
);
148+
}
163149

164-
if ($row === false) {
165-
return false;
166-
}
150+
/**
151+
* @return Traversable<int,array<int,mixed>>
152+
*
153+
* @throws DriverException
154+
*/
155+
public function iterateNumeric() : Traversable
156+
{
157+
return FetchUtils::iterateNumericByOne($this);
158+
}
159+
160+
/**
161+
* @return Traversable<int,array<string,mixed>>
162+
*
163+
* @throws DriverException
164+
*/
165+
public function iterateAssociative() : Traversable
166+
{
167+
return FetchUtils::iterateAssociativeByOne($this);
168+
}
167169

168-
return $row[0];
170+
/**
171+
* @return Traversable<int,mixed>
172+
*
173+
* @throws DriverException
174+
*/
175+
public function iterateColumn() : Traversable
176+
{
177+
return FetchUtils::iterateColumnByOne($this);
169178
}
170179

171180
/**
@@ -183,4 +192,47 @@ public function rowCount() : int
183192
{
184193
return $this->statement->rowCount();
185194
}
195+
196+
/**
197+
* @return array<string,mixed>|false
198+
*
199+
* @throws DriverException
200+
*/
201+
private function fetch()
202+
{
203+
if ($this->data === null) {
204+
$this->data = [];
205+
}
206+
207+
$row = $this->statement->fetchAssociative();
208+
209+
if ($row !== false) {
210+
$this->data[] = $row;
211+
212+
return $row;
213+
}
214+
215+
$this->emptied = true;
216+
217+
return false;
218+
}
219+
220+
/**
221+
* @param array<int,array<mixed>> $data
222+
*
223+
* @return array<int,array<mixed>>
224+
*/
225+
private function store(array $data, bool $isArray) : array
226+
{
227+
if (! $isArray) {
228+
foreach ($data as $key => $value) {
229+
$data[$key] = [$value];
230+
}
231+
}
232+
233+
$this->data = $data;
234+
$this->emptied = true;
235+
236+
return $this->data;
237+
}
186238
}

0 commit comments

Comments
 (0)