Permalink
Browse files

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
  • Loading branch information...
1 parent 87703b1 commit e89536845303f9785dcc61b2ffc8ed59b161dab5 jakubvrana committed Aug 30, 2009
Showing with 12 additions and 30 deletions.
  1. +12 −30 adminer/select.inc.php
View
42 adminer/select.inc.php
@@ -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);

0 comments on commit e895368

Please sign in to comment.