From dd7f413cd2801ae8e931b5e611183cb2952ff364 Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Sun, 15 Nov 2020 15:57:04 +0100 Subject: [PATCH] Result: make column types detection lazy --- src/Result/Result.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Result/Result.php b/src/Result/Result.php index a6ed838a..b740a203 100644 --- a/src/Result/Result.php +++ b/src/Result/Result.php @@ -37,6 +37,9 @@ class Result implements SeekableIterator, Countable /** @var IDriver */ private $driver; + /** @var bool */ + private $typesDetected = false; + /** @var string[] list of columns which should be casted to int */ private $toIntColumns; @@ -67,7 +70,6 @@ public function __construct(IResultAdapter $adapter, IDriver $driver) $this->adapter = $adapter; $this->driver = $driver; $this->applicationTimeZone = new DateTimeZone(date_default_timezone_get()); - $this->initColumnConversions(); } @@ -107,7 +109,7 @@ public function getAdapter(): IResultAdapter public function setValueNormalization(bool $enabled = false): void { if ($enabled === true) { - $this->initColumnConversions(); + $this->initColumnTypes(); } else { $this->toIntColumns = []; $this->toFloatColumns = []; @@ -115,6 +117,7 @@ public function setValueNormalization(bool $enabled = false): void $this->toBoolColumns = []; $this->toDateTimeColumns = []; $this->toDriverColumns = []; + $this->typesDetected = true; } } @@ -195,7 +198,7 @@ function ($name): string { } - protected function initColumnConversions(): void + protected function initColumnTypes(): void { $this->toIntColumns = []; $this->toFloatColumns = []; @@ -228,6 +231,8 @@ protected function initColumnConversions(): void $this->toDriverColumns[] = [$key, $nativeType]; } } + + $this->typesDetected = true; } @@ -237,6 +242,10 @@ protected function initColumnConversions(): void */ protected function normalize(array $data): array { + if (!$this->typesDetected) { + $this->initColumnTypes(); + } + foreach ($this->toDriverColumns as $meta) { [$column, $nativeType] = $meta; if ($data[$column] !== null) {