Skip to content
Browse files

Respect max_allowed_packet in CSV import

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@986 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information...
1 parent c5a7e2c commit cb370a8ab724579d1a5e7f319c6142eb6698e650 jakubvrana committed Aug 20, 2009
Showing with 31 additions and 8 deletions.
  1. +30 −8 adminer/select.inc.php
  2. +1 −0 changes.txt
View
38 adminer/select.inc.php
@@ -83,24 +83,46 @@
//! display edit page in case of an error
} elseif (is_string($file = get_file("csv_file"))) {
$file = preg_replace("~^\xEF\xBB\xBF~", '', $file); //! character set
- $cols = "";
- $rows = array(); //! packet size
- preg_match_all('~("[^"]*"|[^"\\n])+~U', $file, $matches);
+ $affected = 0;
+ $length = 0;
+ $result = true;
+ $query = "INSERT INTO " . idf_escape($_GET["select"]);
+ $packet_size = $dbh->result($dbh->query("SELECT @@max_allowed_packet"));
+ $rows = array();
+ preg_match_all('~("[^"]*"|[^"\\n])+~', $file, $matches);
foreach ($matches[0] as $key => $val) {
$row = array();
- preg_match_all('~(("[^"]*")+|[^,]*),~U', "$val,", $matches2);
+ preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $matches2);
if (!$key && !array_diff($matches2[1], array_keys($fields))) { //! doesn't work with column names containing ",\n
// first row corresponds to column names - use it for table structure
- $cols = " (" . implode(", ", array_map('idf_escape', $matches2[1])) . ")";
+ $query .= " (" . implode(", ", array_map('idf_escape', $matches2[1])) . ")";
} else {
foreach ($matches2[1] as $col) {
$row[] = (!strlen($col) ? "NULL" : $dbh->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
}
- $rows[] = "\n(" . implode(", ", $row) . ")";
+ $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 += $dbh->affected_rows;
+ $length = strlen($query);
+ $rows = array();
+ }
+ $rows[] = $s;
}
+ if (!$key) {
+ $query .= " VALUES";
+ $length += strlen($query);
+ }
+ }
+ if ($result) {
+ $result = queries($query . implode(",", $rows));
+ $affected += $dbh->affected_rows;
}
- $result = queries("INSERT INTO " . idf_escape($_GET["select"]) . "$cols VALUES" . implode(",", $rows));
- query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) have been imported.', $dbh->affected_rows), $result, false, !$result);
+ query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) have been imported.', $affected), $result, false, !$result);
} else {
$error = upload_error($file);
}
View
1 changes.txt
@@ -1,5 +1,6 @@
Adminer 2.0.1:
Display column comments in table overview
+Respect max_allowed_packet in CSV import
Fix Editor date format
Fix long SQL query crash (bug #2839231)

0 comments on commit cb370a8

Please sign in to comment.
Something went wrong with that request. Please try again.