44
55use PgSql ;
66
7- class Result implements ColumnValueParser, \Countable
7+ class Result implements \Countable
88{
99 protected PgSql \Result $ queryResource ;
1010
@@ -28,8 +28,7 @@ class Result implements ColumnValueParser, \Countable
2828 /** @var array<string, string>|null */
2929 private array |null $ columnsDataTypes = null ;
3030
31- /** @var array<string, bool> */
32- private array $ parsedColumns = [];
31+ private ColumnValueParser |null $ columnValueParser = null ;
3332
3433
3534 /**
@@ -126,11 +125,7 @@ public function fetch(): Row|null
126125 return null ;
127126 }
128127
129- // Detecting column data types need valid query result and it can be closed before columns data types
130- // ...are detected, that's why we're detecting it with the first fetch (before first row is created)
131- $ this ->detectColumnDataTypes ();
132-
133- $ row = $ this ->rowFactory ->create ($ this , $ data );
128+ $ row = $ this ->rowFactory ->create ($ this ->getColumnValueParser (), $ data );
134129
135130 if ($ this ->rowFetchMutator !== null ) {
136131 call_user_func ($ this ->rowFetchMutator , $ row );
@@ -406,30 +401,10 @@ public function getColumns(): array
406401 }
407402
408403
409- public function parseColumnValue (string $ column , mixed $ rawValue ): mixed
410- {
411- \assert (($ rawValue === null ) || \is_string ($ rawValue )); // database result all values as string or null
412- $ value = $ this ->dataTypeParser ->parse ($ this ->getColumnType ($ column ), $ rawValue );
413-
414- $ this ->parsedColumns [$ column ] = true ;
415-
416- return $ value ;
417- }
418-
419-
420404 /**
421405 * @return array<string, string>
422406 */
423407 private function getColumnsDataTypes (): array
424- {
425- $ this ->detectColumnDataTypes ();
426- \assert ($ this ->columnsDataTypes !== null );
427-
428- return $ this ->columnsDataTypes ;
429- }
430-
431-
432- private function detectColumnDataTypes (): void
433408 {
434409 if ($ this ->columnsDataTypes === null ) {
435410 $ this ->columnsDataTypes = [];
@@ -455,17 +430,18 @@ private function detectColumnDataTypes(): void
455430 $ this ->columnsDataTypes [$ name ] = $ type ;
456431 }
457432 }
433+
434+ return $ this ->columnsDataTypes ;
458435 }
459436
460437
461- /**
462- * @return array<string, bool>|null null = no column was used
463- */
464- public function getParsedColumns (): array |null
438+ public function getColumnValueParser (): ColumnValueParser
465439 {
466- return $ this ->parsedColumns === []
467- ? null
468- : $ this ->parsedColumns + \array_fill_keys ($ this ->getColumns (), false );
440+ if ($ this ->columnValueParser === null ) {
441+ $ this ->columnValueParser = new ColumnValueParser ($ this ->dataTypeParser , $ this ->getColumnsDataTypes ());
442+ }
443+
444+ return $ this ->columnValueParser ;
469445 }
470446
471447}
0 commit comments