From cb370a8ab724579d1a5e7f319c6142eb6698e650 Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Thu, 20 Aug 2009 15:05:35 +0000 Subject: [PATCH] Respect max_allowed_packet in CSV import git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@986 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- adminer/select.inc.php | 38 ++++++++++++++++++++++++++++++-------- changes.txt | 1 + 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/adminer/select.inc.php b/adminer/select.inc.php index 13a5e380..a72e9aad 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -83,24 +83,46 @@ if ($_POST && !$error) { //! 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); } } - $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); + if ($result) { + $result = queries($query . implode(",", $rows)); + $affected += $dbh->affected_rows; + } + query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) have been imported.', $affected), $result, false, !$result); } else { $error = upload_error($file); } diff --git a/changes.txt b/changes.txt index 8ab9aa5a..06ec09bb 100644 --- a/changes.txt +++ b/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)