Skip to content

Commit

Permalink
Result: make column types detection lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Nov 15, 2020
1 parent 4f0be1a commit dd7f413
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Result/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}


Expand Down Expand Up @@ -107,14 +109,15 @@ public function getAdapter(): IResultAdapter
public function setValueNormalization(bool $enabled = false): void
{
if ($enabled === true) {
$this->initColumnConversions();
$this->initColumnTypes();
} else {
$this->toIntColumns = [];
$this->toFloatColumns = [];
$this->toStringColumns = [];
$this->toBoolColumns = [];
$this->toDateTimeColumns = [];
$this->toDriverColumns = [];
$this->typesDetected = true;
}
}

Expand Down Expand Up @@ -195,7 +198,7 @@ function ($name): string {
}


protected function initColumnConversions(): void
protected function initColumnTypes(): void
{
$this->toIntColumns = [];
$this->toFloatColumns = [];
Expand Down Expand Up @@ -228,6 +231,8 @@ protected function initColumnConversions(): void
$this->toDriverColumns[] = [$key, $nativeType];
}
}

$this->typesDetected = true;
}


Expand All @@ -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) {
Expand Down

0 comments on commit dd7f413

Please sign in to comment.