1616/**
1717 * Represents a database result set.
1818 */
19- class Result implements \Iterator
19+ class Result implements \IteratorAggregate
2020{
21- private Row |false |null $ lastRow = null ;
22- private int $ lastRowKey = -1 ;
21+ private bool $ fetched = false ;
2322
2423 /** @var Row[] */
2524 private array $ rows ;
@@ -93,42 +92,20 @@ public function dump(): void
9392 }
9493
9594
96- /********************* interface Iterator ****************d*g**/
95+ /********************* interface IteratorAggregate ****************d*g**/
9796
9897
99- public function rewind (): void
98+ /** @return \Generator<Row> */
99+ public function getIterator (): \Generator
100100 {
101- if ($ this ->lastRow === false ) {
101+ if ($ this ->fetched ) {
102102 throw new Nette \InvalidStateException (self ::class . ' implements only one way iterator. ' );
103103 }
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-
118104
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 ;
129108 }
130-
131- return $ this ->fetch () !== null ;
132109 }
133110
134111
@@ -146,13 +123,15 @@ public function fetchAssoc(?string $path = null): ?array
146123
147124 $ data = $ this ->result ?->fetch();
148125 if ($ data === null ) {
126+ $ this ->fetched = true ;
149127 return null ;
150128
151- } elseif ($ this ->lastRow === null && count ($ data ) !== $ this ->result ->getColumnCount ()) {
129+ } elseif (! $ this ->fetched && count ($ data ) !== $ this ->result ->getColumnCount ()) {
152130 $ duplicates = array_filter (array_count_values (array_column ($ this ->result ->getColumnsInfo (), 'name ' )), fn ($ val ) => $ val > 1 );
153131 trigger_error ("Found duplicate columns in database result set: ' " . implode ("', ' " , array_keys ($ duplicates )) . "'. " );
154132 }
155133
134+ $ this ->fetched = true ;
156135 return $ this ->normalizeRow ($ data );
157136 }
158137
@@ -163,12 +142,7 @@ public function fetchAssoc(?string $path = null): ?array
163142 public function fetch (): ?Row
164143 {
165144 $ 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 );
172146 }
173147
174148
@@ -218,8 +192,7 @@ public function fetchPairs(string|int|\Closure|null $keyOrCallback = null, strin
218192 */
219193 public function fetchAll (): array
220194 {
221- $ this ->rows ??= iterator_to_array ($ this );
222- return $ this ->rows ;
195+ return $ this ->rows ??= iterator_to_array ($ this );
223196 }
224197
225198
0 commit comments