Skip to content

Commit

Permalink
Use ON DUPLICATE KEY UPDATE for CSV import
Browse files Browse the repository at this point in the history
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1049 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information
jakubvrana committed Aug 30, 2009
1 parent 87703b1 commit e895368
Showing 1 changed file with 12 additions and 30 deletions.
42 changes: 12 additions & 30 deletions adminer/select.inc.php
Expand Up @@ -88,46 +88,28 @@
//! display edit page in case of an error
} elseif (is_string($file = get_file("csv_file", true))) {
$file = preg_replace("~^\xEF\xBB\xBF~", '', $file); //! character set
$affected = 0;
$length = 0;
$result = true;
$dbh->query("SET foreign_key_checks = 0");
$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();
$cols = array_keys($fields);
preg_match_all('~("[^"]*"|[^"\\n])+~', $file, $matches);
$affected = count($matches[0]);
foreach ($matches[0] as $key => $val) {
$row = array();
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
$query .= " (" . implode(", ", array_map('idf_escape', $matches2[1])) . ")";
$cols = $matches2[1];
$affected--;
} else {
foreach ($matches2[1] as $col) {
$row[] = (!strlen($col) ? "NULL" : $dbh->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
$set = "";
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) . ")";
$length += 1 + strlen($s); // 1 - separator length
if ($rows && $length > $packet_size) {
$result = queries($query . implode(",", $rows));
if (!$result) {
break;
}
$affected += count($rows);
$length = strlen($query);
$rows = array();
$set = substr($set, 2);
$result = queries("INSERT INTO " . idf_escape($_GET["select"]) . " SET $set ON DUPLICATE KEY UPDATE $set");
if (!$result) {
break;
}
$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);
} else {
$error = upload_error($file);
Expand Down

0 comments on commit e895368

Please sign in to comment.