Skip to content

Commit

Permalink
#619 Convert empty CSV values
Browse files Browse the repository at this point in the history
  • Loading branch information
amazeika committed Sep 16, 2022
1 parent 71552e2 commit 7118903
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ protected function _insertCSV(SplFileObject $file, $table, $offset = 0, $limit =
$rows_per_query = 20;
$queue_count = 0;
$total_count = 0;
$columns = array();

foreach ($file as $i => $row)
{
Expand All @@ -136,6 +137,15 @@ protected function _insertCSV(SplFileObject $file, $table, $offset = 0, $limit =

$query->columns($row);

$schema = $this->getObject('lib:database.table.default', array('name' => $table))->getSchema();

foreach ($row as $column)
{
if (isset($schema->columns[$column])) {
$columns[] = $schema->columns[$column];
}
}

continue;
}

Expand All @@ -147,6 +157,8 @@ protected function _insertCSV(SplFileObject $file, $table, $offset = 0, $limit =

$this->_convertNullDates($row);

$this->_convertEmptyValues($row, $columns);

$query->values($row);
$total_count++;
$queue_count++;
Expand All @@ -171,6 +183,25 @@ protected function _insertCSV(SplFileObject $file, $table, $offset = 0, $limit =
return $total_count;
}

protected function _convertEmptyValues(&$row, $columns)
{
$types = array('int', 'bigint', 'tinyint', 'mediumint', 'smallint', 'time', 'timestamp', 'year', 'date', 'datetime'); // A list of allowed types for the conversion

foreach ($row as $key => $value)
{
if (isset($columns[$key]))
{
$default = $columns[$key]->default;
$required = $columns[$key]->required;
$type = $columns[$key]->type;

if ($value === '' && $default === null && !$required && in_array($type, $types)) {
$row[$key] = null;
}
}
}
}

protected function _convertNullDates(&$row)
{
foreach ($row as $i => $value) {
Expand Down

0 comments on commit 7118903

Please sign in to comment.