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$ $Id$
$Source$ $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> 2005-11-20 Sebastian Mendel <cybot_tm@users.sourceforge.net>
* added test/theme.php: for testing themes * added test/theme.php: for testing themes
* phpinfo.php: * phpinfo.php:

View File

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

View File

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

View File

@@ -14,7 +14,6 @@ if ($import_type == 'table') {
array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'), array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'),
array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 1), array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 1),
array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', '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' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 1),
array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2), array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2),
array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'), array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'),
@@ -93,10 +92,20 @@ if ($import_type == 'table') {
$values = array(); $values = array();
// Grab some SQL queries out of it // Grab some SQL queries out of it
$i = 0; $i = 0;
$lasti = -1;
$finish = FALSE; $finish = FALSE;
$ch = $buffer[$i]; $ch = $buffer[$i];
while ($i < $len) { 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) { if ($ch == $csv_terminated) {
$values[] = ''; $values[] = '';
if ($i == $len - 1) break; if ($i == $len - 1) break;
@@ -105,28 +114,19 @@ if ($import_type == 'table') {
continue; continue;
} }
// Grab one field // Grab one field
if ($ch == $csv_enclosed || isset($csv_enclosed_optionally)) { if ($ch == $csv_enclosed) {
if ($ch == $csv_enclosed) { $need_end = TRUE;
$need_end = TRUE; if ($i == $len - 1) break;
if ($i == $len - 1) break; $i++;
$i++; $ch = $buffer[$i];
$ch = $buffer[$i]; } else {
} else { $need_end = FALSE;
$need_end = FALSE; }
} $fail = FALSE;
$fail = FALSE; $value = '';
$value = ''; while(($need_end && $ch != $csv_enclosed) || (!$need_end && !($ch == $csv_terminated || $ch == $csv_new_line || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n"))))) {
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 ($ch == $csv_escaped) {
if ($i == $len - 1) {
$fail = TRUE;
break;
}
$i++;
$ch = $buffer[$i];
}
$value .= $ch;
if ($i == $len - 1) { if ($i == $len - 1) {
$fail = TRUE; $fail = TRUE;
break; break;
@@ -134,21 +134,31 @@ if ($import_type == 'table') {
$i++; $i++;
$ch = $buffer[$i]; $ch = $buffer[$i];
} }
if ($fail) break; $value .= $ch;
$values[] = $value; if ($i == $len - 1) {
if ($ch == $csv_enclosed) { $fail = TRUE;
if ($i == $len - 1) break; 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];
} }
$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 // End of line
@@ -189,16 +199,16 @@ if ($import_type == 'table') {
$buffer = substr($buffer, $i + 1); $buffer = substr($buffer, $i + 1);
$len = strlen($buffer); $len = strlen($buffer);
$i = 0; $i = 0;
$ch = $buffer[$i]; $lasti = -1;
$ch = $buffer[0];
} }
} // End of parser loop } // End of parser loop
} // End of import loop } // End of import loop
PMA_importRunQuery(); PMA_importRunQuery();
if (count($values) != 0) { if (count($values) != 0 && !$error) {
$message = $strInvalidCSVInput; $message = $strInvalidCSVInput;
$show_error_header = TRUE; $show_error_header = TRUE;
$error = TRUE; $error = TRUE;
break;
} }
// Commit any possible data in buffers // 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' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'),
array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 1), array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 1),
array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', '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' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 1),
array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2), array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2),
array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'), array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'),
@@ -65,9 +64,6 @@ if ($import_type == 'table') {
$sql .= ' FIELDS TERMINATED BY \'' . PMA_sqlAddslashes($ldi_terminated) . '\''; $sql .= ' FIELDS TERMINATED BY \'' . PMA_sqlAddslashes($ldi_terminated) . '\'';
} }
if (strlen($ldi_enclosed) > 0) { if (strlen($ldi_enclosed) > 0) {
if (isset($ldi_enclosed_optionally)){
$sql .= ' OPTIONALLY';
}
$sql .= ' ENCLOSED BY \'' . PMA_sqlAddslashes($ldi_enclosed) . '\''; $sql .= ' ENCLOSED BY \'' . PMA_sqlAddslashes($ldi_enclosed) . '\'';
} }
if (strlen($ldi_escaped) > 0) { if (strlen($ldi_escaped) > 0) {