Refactor analyzeTable
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
parent
84523a4a05
commit
f8c08b45f4
@ -609,7 +609,7 @@ class Import
|
||||
return -1;
|
||||
}
|
||||
|
||||
public function detectType(ColumnType|null $lastCumulativeType, string|null $cell): ColumnType
|
||||
public function detectType(ColumnType|null $lastCumulativeType, string $cell): ColumnType
|
||||
{
|
||||
/**
|
||||
* If numeric, determine if decimal, int or bigint
|
||||
@ -652,27 +652,19 @@ class Import
|
||||
*/
|
||||
public function analyzeTable(ImportTable $table): array
|
||||
{
|
||||
/* Get number of rows in table */
|
||||
/* Get number of columns */
|
||||
$numberOfColumns = count($table->columns);
|
||||
|
||||
$columns = [];
|
||||
for ($i = 0; $i < $numberOfColumns; ++$i) {
|
||||
$columns[] = new AnalysedColumn(ColumnType::None, 0);
|
||||
}
|
||||
|
||||
/* Analyze each column */
|
||||
for ($i = 0; $i < $numberOfColumns; ++$i) {
|
||||
/* Analyze the column in each row */
|
||||
foreach ($table->rows as $row) {
|
||||
$cellValue = $row[$i];
|
||||
/* Determine type of the current cell */
|
||||
$currType = $this->detectType($columns[$i]->type, $cellValue === null ? null : (string) $cellValue);
|
||||
/* Determine size of the current cell */
|
||||
$detectedType = $this->detectType($columns[$i]->type, (string) $cellValue);
|
||||
$columns[$i]->size = $this->detectSize(
|
||||
$columns[$i]->size,
|
||||
$columns[$i]->type,
|
||||
$currType,
|
||||
$detectedType,
|
||||
(string) $cellValue,
|
||||
);
|
||||
|
||||
@ -680,29 +672,23 @@ class Import
|
||||
* If a type for this column has already been declared,
|
||||
* only alter it if it was a number and a varchar was found
|
||||
*/
|
||||
if ($currType === ColumnType::None) {
|
||||
if ($detectedType === ColumnType::None) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($currType === ColumnType::Varchar) {
|
||||
$columns[$i]->type = ColumnType::Varchar;
|
||||
} elseif ($currType === ColumnType::Decimal) {
|
||||
if ($columns[$i]->type !== ColumnType::Varchar) {
|
||||
$columns[$i]->type = ColumnType::Decimal;
|
||||
}
|
||||
} elseif ($currType === ColumnType::BigInt) {
|
||||
if ($columns[$i]->type !== ColumnType::Varchar && $columns[$i]->type !== ColumnType::Decimal) {
|
||||
$columns[$i]->type = ColumnType::BigInt;
|
||||
}
|
||||
} elseif ($currType === ColumnType::Int) {
|
||||
if (
|
||||
$columns[$i]->type !== ColumnType::Varchar
|
||||
&& $columns[$i]->type !== ColumnType::Decimal
|
||||
&& $columns[$i]->type !== ColumnType::BigInt
|
||||
) {
|
||||
$columns[$i]->type = ColumnType::Int;
|
||||
}
|
||||
}
|
||||
$columns[$i]->type = match ($columns[$i]->type) {
|
||||
default => 0,
|
||||
ColumnType::Int => 1,
|
||||
ColumnType::BigInt => 2,
|
||||
ColumnType::Decimal => 3,
|
||||
ColumnType::Varchar => 4,
|
||||
} >= match ($detectedType) {
|
||||
default => 0,
|
||||
ColumnType::Int => 1,
|
||||
ColumnType::BigInt => 2,
|
||||
ColumnType::Decimal => 3,
|
||||
ColumnType::Varchar => 4,
|
||||
} ? $columns[$i]->type : $detectedType;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -173,11 +173,11 @@ class ImportTest extends AbstractTestCase
|
||||
* @param ColumnType $expected Expected result of the function
|
||||
* @param ColumnType|null $type Last cumulative column type (VARCHAR or INT or
|
||||
* BIGINT or DECIMAL or NONE)
|
||||
* @param string|null $cell String representation of the cell for which a
|
||||
* best-fit type is to be determined
|
||||
* @param string $cell String representation of the cell for which a
|
||||
* best-fit type is to be determined
|
||||
*/
|
||||
#[DataProvider('provDetectType')]
|
||||
public function testDetectType(ColumnType $expected, ColumnType|null $type, string|null $cell): void
|
||||
public function testDetectType(ColumnType $expected, ColumnType|null $type, string $cell): void
|
||||
{
|
||||
self::assertSame($expected, $this->import->detectType($type, $cell));
|
||||
}
|
||||
@ -185,7 +185,7 @@ class ImportTest extends AbstractTestCase
|
||||
/**
|
||||
* Data provider for testDetectType
|
||||
*
|
||||
* @return array{ColumnType, ColumnType|null, string|null}[]
|
||||
* @return array{ColumnType, ColumnType|null, string}[]
|
||||
*/
|
||||
public static function provDetectType(): array
|
||||
{
|
||||
@ -194,8 +194,8 @@ class ImportTest extends AbstractTestCase
|
||||
[ColumnType::None, ColumnType::None, 'NULL'],
|
||||
[ColumnType::Int, ColumnType::Int, 'NULL'],
|
||||
[ColumnType::Varchar, ColumnType::Varchar, 'NULL'],
|
||||
[ColumnType::Varchar, null, null],
|
||||
[ColumnType::Varchar, ColumnType::Int, null],
|
||||
[ColumnType::Varchar, null, ''],
|
||||
[ColumnType::Varchar, ColumnType::Int, ''],
|
||||
[ColumnType::Int, ColumnType::Int, '10'],
|
||||
[ColumnType::Decimal, ColumnType::Decimal, '10.2'],
|
||||
[ColumnType::Decimal, ColumnType::Int, '10.2'],
|
||||
|
||||
Loading…
Reference in New Issue
Block a user