16
16
/**
17
17
* Represents a database result set.
18
18
*/
19
- class Result implements \Iterator
19
+ class Result implements \IteratorAggregate
20
20
{
21
- private Row |false |null $ lastRow = null ;
22
- private int $ lastRowKey = -1 ;
21
+ private bool $ fetched = false ;
23
22
24
23
/** @var Row[] */
25
24
private array $ rows ;
@@ -93,42 +92,20 @@ public function dump(): void
93
92
}
94
93
95
94
96
- /********************* interface Iterator ****************d*g**/
95
+ /********************* interface IteratorAggregate ****************d*g**/
97
96
98
97
99
- public function rewind (): void
98
+ /** @return \Generator<Row> */
99
+ public function getIterator (): \Generator
100
100
{
101
- if ($ this ->lastRow === false ) {
101
+ if ($ this ->fetched ) {
102
102
throw new Nette \InvalidStateException (self ::class . ' implements only one way iterator. ' );
103
103
}
104
- }
105
-
106
-
107
- public function current (): Row |false |null
108
- {
109
- return $ this ->lastRow ;
110
- }
111
-
112
-
113
- public function key (): int
114
- {
115
- return $ this ->lastRowKey ;
116
- }
117
-
118
104
119
- public function next (): void
120
- {
121
- $ this ->lastRow = false ;
122
- }
123
-
124
-
125
- public function valid (): bool
126
- {
127
- if ($ this ->lastRow ) {
128
- return true ;
105
+ $ counter = 0 ;
106
+ while (($ row = $ this ->fetch ()) !== null ) {
107
+ yield $ counter ++ => $ row ;
129
108
}
130
-
131
- return $ this ->fetch () !== null ;
132
109
}
133
110
134
111
@@ -146,13 +123,15 @@ public function fetchAssoc(?string $path = null): ?array
146
123
147
124
$ data = $ this ->result ?->fetch();
148
125
if ($ data === null ) {
126
+ $ this ->fetched = true ;
149
127
return null ;
150
128
151
- } elseif ($ this ->lastRow === null && count ($ data ) !== $ this ->result ->getColumnCount ()) {
129
+ } elseif (! $ this ->fetched && count ($ data ) !== $ this ->result ->getColumnCount ()) {
152
130
$ duplicates = array_filter (array_count_values (array_column ($ this ->result ->getColumnsInfo (), 'name ' )), fn ($ val ) => $ val > 1 );
153
131
trigger_error ("Found duplicate columns in database result set: ' " . implode ("', ' " , array_keys ($ duplicates )) . "'. " );
154
132
}
155
133
134
+ $ this ->fetched = true ;
156
135
return $ this ->normalizeRow ($ data );
157
136
}
158
137
@@ -163,12 +142,7 @@ public function fetchAssoc(?string $path = null): ?array
163
142
public function fetch (): ?Row
164
143
{
165
144
$ data = $ this ->fetchAssoc ();
166
- if ($ data === null ) {
167
- return null ;
168
- }
169
-
170
- $ this ->lastRowKey ++;
171
- return $ this ->lastRow = Arrays::toObject ($ data , new Row );
145
+ return $ data === null ? null : Arrays::toObject ($ data , new Row );
172
146
}
173
147
174
148
@@ -218,8 +192,7 @@ public function fetchPairs(string|int|\Closure|null $keyOrCallback = null, strin
218
192
*/
219
193
public function fetchAll (): array
220
194
{
221
- $ this ->rows ??= iterator_to_array ($ this );
222
- return $ this ->rows ;
195
+ return $ this ->rows ??= iterator_to_array ($ this );
223
196
}
224
197
225
198
0 commit comments