Use ON DUPLICATE KEY UPDATE for CSV import
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1049 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
parent
87703b16e6
commit
e895368453
|
@ -88,45 +88,27 @@ if ($_POST && !$error) {
|
||||||
//! display edit page in case of an error
|
//! display edit page in case of an error
|
||||||
} elseif (is_string($file = get_file("csv_file", true))) {
|
} elseif (is_string($file = get_file("csv_file", true))) {
|
||||||
$file = preg_replace("~^\xEF\xBB\xBF~", '', $file); //! character set
|
$file = preg_replace("~^\xEF\xBB\xBF~", '', $file); //! character set
|
||||||
$affected = 0;
|
|
||||||
$length = 0;
|
|
||||||
$result = true;
|
$result = true;
|
||||||
$dbh->query("SET foreign_key_checks = 0");
|
$cols = array_keys($fields);
|
||||||
$query = "REPLACE " . idf_escape($TABLE); // ON DUPLICATE KEY UPDATE would require one query per record
|
|
||||||
$packet_size = $dbh->result($dbh->query("SELECT @@max_allowed_packet"));
|
|
||||||
$rows = array();
|
|
||||||
preg_match_all('~("[^"]*"|[^"\\n])+~', $file, $matches);
|
preg_match_all('~("[^"]*"|[^"\\n])+~', $file, $matches);
|
||||||
|
$affected = count($matches[0]);
|
||||||
foreach ($matches[0] as $key => $val) {
|
foreach ($matches[0] as $key => $val) {
|
||||||
$row = array();
|
|
||||||
preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $matches2);
|
preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $matches2);
|
||||||
if (!$key && !array_diff($matches2[1], array_keys($fields))) { //! doesn't work with column names containing ",\n
|
if (!$key && !array_diff($matches2[1], $cols)) { //! doesn't work with column names containing ",\n
|
||||||
// first row corresponds to column names - use it for table structure
|
// first row corresponds to column names - use it for table structure
|
||||||
$query .= " (" . implode(", ", array_map('idf_escape', $matches2[1])) . ")";
|
$cols = $matches2[1];
|
||||||
|
$affected--;
|
||||||
} else {
|
} else {
|
||||||
foreach ($matches2[1] as $col) {
|
$set = "";
|
||||||
$row[] = (!strlen($col) ? "NULL" : $dbh->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
|
foreach ($matches2[1] as $i => $col) {
|
||||||
|
$set .= ", " . idf_escape($cols[$i]) . " = " . (!strlen($col) ? "NULL" : $dbh->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
|
||||||
}
|
}
|
||||||
$s = "\n(" . implode(", ", $row) . ")";
|
$set = substr($set, 2);
|
||||||
$length += 1 + strlen($s); // 1 - separator length
|
$result = queries("INSERT INTO " . idf_escape($_GET["select"]) . " SET $set ON DUPLICATE KEY UPDATE $set");
|
||||||
if ($rows && $length > $packet_size) {
|
|
||||||
$result = queries($query . implode(",", $rows));
|
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$affected += count($rows);
|
|
||||||
$length = strlen($query);
|
|
||||||
$rows = array();
|
|
||||||
}
|
}
|
||||||
$rows[] = $s;
|
|
||||||
}
|
|
||||||
if (!$key) {
|
|
||||||
$query .= " VALUES";
|
|
||||||
$length += strlen($query);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($result) {
|
|
||||||
$result = queries($query . implode(",", $rows));
|
|
||||||
$affected += count($rows);
|
|
||||||
}
|
}
|
||||||
query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) have been imported.', $affected), $result, false, !$result);
|
query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) have been imported.', $affected), $result, false, !$result);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue