Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/Plugins/Import/ImportCsv.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
/**
* CSV import plugin for phpMyAdmin
Expand Down Expand Up @@ -64,6 +64,7 @@
private string $columns = '';
private int $maxLines = 0;
private bool $csvHasColumnNames = false;
private bool $emptyValueAsNull = false;
private string $newDatabaseName = '';
private string $newTableName = '';

Expand Down Expand Up @@ -153,6 +154,12 @@
$generalOptions->addProperty($leaf);
}

$leaf = new BoolPropertyItem(
'empty_is_null',
__('Import empty values as NULL'),
);
$generalOptions->addProperty($leaf);

$leaf = new BoolPropertyItem(
'ignore',
__('Do not abort on INSERT error'),
Expand All @@ -179,6 +186,7 @@
$this->columns = $request->getParsedBodyParamAsString('csv_columns', '');
$this->maxLines = min(0, (int) $request->getParsedBodyParamAsStringOrNull('csv_partial_import'));
$this->csvHasColumnNames = $request->getParsedBodyParam('csv_col_names') !== null;
$this->emptyValueAsNull = $request->getParsedBodyParam('csv_empty_is_null') !== null;
$this->newDatabaseName = $request->getParsedBodyParamAsString('csv_new_db_name', '');
$this->newTableName = $request->getParsedBodyParamAsString('csv_new_tbl_name', '');
}
Expand Down Expand Up @@ -386,11 +394,6 @@
$i += $csvTerminatedLen - 1;
}

// unquoted NULL string
if ($needEnd === false && $value === 'NULL') {
$value = null;
}

if ($fail) {
$i = $fallbacki;
$ch = mb_substr($buffer, $i, 1);
Expand Down Expand Up @@ -509,7 +512,7 @@
$sql .= ', ';
}

if ($val === null) {
if ($val === 'NULL' || ($this->emptyValueAsNull && $val === '')) {
$sql .= 'NULL';
} else {
$sql .= $dbi->quoteString($val);
Expand Down Expand Up @@ -562,13 +565,29 @@
$rows[$i] = array_pad($row, $maxCols, 'NULL');
}

if ($this->emptyValueAsNull) {
foreach ($rows as &$row) {
foreach ($row as &$value) {
if ($value !== '') {
continue;
}

$value = 'NULL';
}

unset($value);
}

unset($row);
}

$colNames = [];
/* Remove the first row if it contains the column names */
if ($this->csvHasColumnNames) {
$colNames = array_shift($rows);
}

$colNames = $this->getColumnNames($colNames, $maxCols);

Check failure on line 590 in src/Plugins/Import/ImportCsv.php

View workflow job for this annotation

GitHub Actions / analyse-php (8.2)

Parameter #1 $columnNames of method PhpMyAdmin\Plugins\Import\ImportCsv::getColumnNames() expects list<string>, list<string>|null given.

$tblName = $this->getTableNameFromImport(Current::$database);

Expand Down
Loading