1717 */
1818class Result implements \Iterator
1919{
20- private ?\ PDOStatement $ pdoStatement = null ;
20+ private ?Drivers \ Result $ result = null ;
2121
2222 /** @var callable(array, Result): array */
2323 private readonly mixed $ normalizer ;
@@ -44,7 +44,7 @@ public function __construct(
4444 if (str_starts_with ($ queryString , ':: ' )) {
4545 $ connection ->getConnectionDriver ()->{substr ($ queryString , 2 )}();
4646 } else {
47- $ this ->pdoStatement = $ connection ->getConnectionDriver ()->query ($ queryString , $ params );
47+ $ this ->result = $ connection ->getConnectionDriver ()->query ($ queryString , $ params );
4848 }
4949 } catch (\PDOException $ e ) {
5050 $ e = $ connection ->getDatabaseEngine ()->convertException ($ e );
@@ -65,15 +65,6 @@ public function getConnection(): Connection
6565 }
6666
6767
68- /**
69- * @internal
70- */
71- public function getPdoStatement (): ?\PDOStatement
72- {
73- return $ this ->pdoStatement ;
74- }
75-
76-
7768 public function getQueryString (): string
7869 {
7970 return $ this ->queryString ;
@@ -88,13 +79,13 @@ public function getParameters(): array
8879
8980 public function getColumnCount (): ?int
9081 {
91- return $ this ->pdoStatement ? $ this -> pdoStatement -> columnCount () : null ;
82+ return $ this ->result ?->getColumnCount() ;
9283 }
9384
9485
9586 public function getRowCount (): ?int
9687 {
97- return $ this ->pdoStatement ? $ this -> pdoStatement -> rowCount () : null ;
88+ return $ this ->result ?->getRowCount() ;
9889 }
9990
10091
@@ -169,14 +160,13 @@ public function valid(): bool
169160 */
170161 public function fetch (): ?Row
171162 {
172- $ data = $ this ->pdoStatement ? $ this ->pdoStatement ->fetch () : null ;
173- if (!$ data ) {
174- $ this ->pdoStatement ->closeCursor ();
163+ $ data = $ this ->result ?->fetch();
164+ if ($ data === null ) {
175165 return null ;
176166
177- } elseif ($ this ->lastRow === null && count ($ data ) !== $ this ->pdoStatement -> columnCount ()) {
178- $ duplicates = Helpers:: findDuplicates ( $ this ->pdoStatement );
179- trigger_error ("Found duplicate columns in database result set: $ duplicates. " );
167+ } elseif ($ this ->lastRow === null && count ($ data ) !== $ this ->result -> getColumnCount ()) {
168+ $ duplicates = array_filter ( array_count_values ( array_column ( $ this ->result -> getColumnsInfo (), ' name ' )), fn ( $ val ) => $ val > 1 );
169+ trigger_error ("Found duplicate columns in database result set: ' " . implode ( " ', ' " , array_keys ( $ duplicates)) . " ' . " );
180170 }
181171
182172 $ row = new Row ;
@@ -191,6 +181,13 @@ public function fetch(): ?Row
191181 }
192182
193183
184+ /** @internal */
185+ public function fetchAssociative (): ?array
186+ {
187+ return $ this ->result ?->fetch();
188+ }
189+
190+
194191 /**
195192 * Fetches single field.
196193 */
@@ -249,17 +246,10 @@ public function resolveColumnConverters(): array
249246 $ res = [];
250247 $ engine = $ this ->connection ->getDatabaseEngine ();
251248 $ converter = $ this ->connection ->getTypeConverter ();
252- $ metaTypeKey = $ this ->connection ->getConnectionDriver ()->metaTypeKey ;
253- $ count = $ this ->pdoStatement ->columnCount ();
254- for ($ i = 0 ; $ i < $ count ; $ i ++) {
255- $ meta = $ this ->pdoStatement ->getColumnMeta ($ i );
256- if (isset ($ meta [$ metaTypeKey ])) {
257- $ res [$ meta ['name ' ]] = $ engine ->resolveColumnConverter ([
258- 'nativeType ' => $ meta [$ metaTypeKey ],
259- 'length ' => $ meta ['len ' ],
260- 'precision ' => $ meta ['precision ' ],
261- ], $ converter );
262- }
249+ foreach ($ this ->result ->getColumnsInfo () as $ meta ) {
250+ $ res [$ meta ['name ' ]] = isset ($ meta ['nativeType ' ])
251+ ? $ engine ->resolveColumnConverter ($ meta , $ converter )
252+ : null ;
263253 }
264254 return $ this ->converters = $ res ;
265255 }
0 commit comments