CSV import fixes:

- Drop optional escaping as this should not have any real functionality.
 - Do not overwrite error message from import in case of single SQL query is being executed.
 - Make enclosing behaviour same as in MySQL LOAD DATA (bug #1363532).
This commit is contained in:
Michal Čihař
2005-11-23 10:34:52 +00:00
parent b366aa4b64
commit f9ab10666a
5 changed files with 59 additions and 47 deletions

View File

@@ -5,6 +5,14 @@ phpMyAdmin - Changelog
$Id$
$Source$
2005-11-23 Michal Čihař <michal@cihar.com>
* config.default.php, libraries/import/csv.php, libraries/import/ldi.php:
Drop optional escaping as this should not have any real functionality.
* libraries/import.lib.php: Do not overwrite error message from import in
case of single SQL query is being executed.
* libraries/import/csv.php: Make enclosing behaviour same as in MySQL LOAD
DATA (bug #1363532).
2005-11-20 Sebastian Mendel <cybot_tm@users.sourceforge.net>
* added test/theme.php: for testing themes
* phpinfo.php:

View File

@@ -316,14 +316,12 @@ $cfg['Import']['skip_queries'] = '0';
$cfg['Import']['csv_replace'] = FALSE;
$cfg['Import']['csv_terminated'] = ';';
$cfg['Import']['csv_enclosed'] = '"';
$cfg['Import']['csv_enclosed_optionally'] = FALSE;
$cfg['Import']['csv_escaped'] = '\\';
$cfg['Import']['csv_new_line'] = 'auto';
$cfg['Import']['csv_columns'] = '';
$cfg['Import']['ldi_replace'] = FALSE;
$cfg['Import']['ldi_terminated'] = ';';
$cfg['Import']['ldi_enclosed'] = '"';
$cfg['Import']['ldi_enclosed_optionally'] = FALSE;
$cfg['Import']['ldi_escaped'] = '\\';
$cfg['Import']['ldi_new_line'] = 'auto';
$cfg['Import']['ldi_columns'] = '';

View File

@@ -80,7 +80,7 @@ function PMA_importRunQuery($sql = '', $full = '') {
$sql_query .= $import_run_buffer['full'];
}
$executed_queries++;
if ($run_query && $finished && empty($sql) && (
if ($run_query && $finished && empty($sql) && !$error && (
(!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW)/i', $import_run_buffer['sql'])) ||
($executed_queries == 1)
)) {

View File

@@ -14,7 +14,6 @@ if ($import_type == 'table') {
array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'),
array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 1),
array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 1),
array('type' => 'bool', 'name' => 'enclosed_optionally', 'text' => 'strEnclosingOptional'),
array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 1),
array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2),
array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'),
@@ -93,10 +92,20 @@ if ($import_type == 'table') {
$values = array();
// Grab some SQL queries out of it
$i = 0;
$lasti = -1;
$finish = FALSE;
$ch = $buffer[$i];
while ($i < $len) {
// Deadlock protection
if ($lasti == $i) {
$message = $strInvalidCSVInput;
$show_error_header = TRUE;
$error = TRUE;
break;
}
$lasti = $i;
// Grab empty field
if ($ch == $csv_terminated) {
$values[] = '';
if ($i == $len - 1) break;
@@ -105,28 +114,19 @@ if ($import_type == 'table') {
continue;
}
// Grab one field
if ($ch == $csv_enclosed || isset($csv_enclosed_optionally)) {
if ($ch == $csv_enclosed) {
$need_end = TRUE;
if ($i == $len - 1) break;
$i++;
$ch = $buffer[$i];
} else {
$need_end = FALSE;
}
$fail = FALSE;
$value = '';
while(($need_end && $ch != $csv_enclosed) || (!$need_end && !($ch == $csv_terminated || $ch == $csv_new_line || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n"))))) {
if ($ch == $csv_escaped) {
if ($i == $len - 1) {
$fail = TRUE;
break;
}
$i++;
$ch = $buffer[$i];
}
$value .= $ch;
// Grab one field
if ($ch == $csv_enclosed) {
$need_end = TRUE;
if ($i == $len - 1) break;
$i++;
$ch = $buffer[$i];
} else {
$need_end = FALSE;
}
$fail = FALSE;
$value = '';
while(($need_end && $ch != $csv_enclosed) || (!$need_end && !($ch == $csv_terminated || $ch == $csv_new_line || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n"))))) {
if ($ch == $csv_escaped) {
if ($i == $len - 1) {
$fail = TRUE;
break;
@@ -134,21 +134,31 @@ if ($import_type == 'table') {
$i++;
$ch = $buffer[$i];
}
if ($fail) break;
$values[] = $value;
if ($ch == $csv_enclosed) {
if ($i == $len - 1) break;
$i++;
$ch = $buffer[$i];
}
if ($ch == $csv_new_line || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n"))) {
$finish = TRUE;
}
if ($ch == $csv_terminated) {
if ($i == $len - 1) break;
$i++;
$ch = $buffer[$i];
$value .= $ch;
if ($i == $len - 1) {
$fail = TRUE;
break;
}
$i++;
$ch = $buffer[$i];
}
if ($fail) break;
$values[] = $value;
// Need to strip trailing enclosing char?
if ($need_end && $ch == $csv_enclosed) {
if ($i == $len - 1) break;
$i++;
$ch = $buffer[$i];
}
// Are we at the end?
if ($ch == $csv_new_line || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n"))) {
$finish = TRUE;
}
// Go to next char
if ($ch == $csv_terminated) {
if ($i == $len - 1) break;
$i++;
$ch = $buffer[$i];
}
// End of line
@@ -189,16 +199,16 @@ if ($import_type == 'table') {
$buffer = substr($buffer, $i + 1);
$len = strlen($buffer);
$i = 0;
$ch = $buffer[$i];
$lasti = -1;
$ch = $buffer[0];
}
} // End of parser loop
} // End of import loop
PMA_importRunQuery();
if (count($values) != 0) {
if (count($values) != 0 && !$error) {
$message = $strInvalidCSVInput;
$show_error_header = TRUE;
$error = TRUE;
break;
}
// Commit any possible data in buffers

View File

@@ -33,7 +33,6 @@ if ($import_type == 'table') {
array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'),
array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 1),
array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 1),
array('type' => 'bool', 'name' => 'enclosed_optionally', 'text' => 'strEnclosingOptional'),
array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 1),
array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2),
array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'),
@@ -65,9 +64,6 @@ if ($import_type == 'table') {
$sql .= ' FIELDS TERMINATED BY \'' . PMA_sqlAddslashes($ldi_terminated) . '\'';
}
if (strlen($ldi_enclosed) > 0) {
if (isset($ldi_enclosed_optionally)){
$sql .= ' OPTIONALLY';
}
$sql .= ' ENCLOSED BY \'' . PMA_sqlAddslashes($ldi_enclosed) . '\'';
}
if (strlen($ldi_escaped) > 0) {