Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit cb370a8ab724579d1a5e7f319c6142eb6698e650 1 parent c5a7e2c
jakubvrana authored

Showing 2 changed files with 31 additions and 8 deletions. Show diff stats Hide diff stats

  1. +30 8 adminer/select.inc.php
  2. +1 0  changes.txt
38 adminer/select.inc.php
@@ -83,24 +83,46 @@
83 83 //! display edit page in case of an error
84 84 } elseif (is_string($file = get_file("csv_file"))) {
85 85 $file = preg_replace("~^\xEF\xBB\xBF~", '', $file); //! character set
86   - $cols = "";
87   - $rows = array(); //! packet size
88   - preg_match_all('~("[^"]*"|[^"\\n])+~U', $file, $matches);
  86 + $affected = 0;
  87 + $length = 0;
  88 + $result = true;
  89 + $query = "INSERT INTO " . idf_escape($_GET["select"]);
  90 + $packet_size = $dbh->result($dbh->query("SELECT @@max_allowed_packet"));
  91 + $rows = array();
  92 + preg_match_all('~("[^"]*"|[^"\\n])+~', $file, $matches);
89 93 foreach ($matches[0] as $key => $val) {
90 94 $row = array();
91   - preg_match_all('~(("[^"]*")+|[^,]*),~U', "$val,", $matches2);
  95 + preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $matches2);
92 96 if (!$key && !array_diff($matches2[1], array_keys($fields))) { //! doesn't work with column names containing ",\n
93 97 // first row corresponds to column names - use it for table structure
94   - $cols = " (" . implode(", ", array_map('idf_escape', $matches2[1])) . ")";
  98 + $query .= " (" . implode(", ", array_map('idf_escape', $matches2[1])) . ")";
95 99 } else {
96 100 foreach ($matches2[1] as $col) {
97 101 $row[] = (!strlen($col) ? "NULL" : $dbh->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
98 102 }
99   - $rows[] = "\n(" . implode(", ", $row) . ")";
  103 + $s = "\n(" . implode(", ", $row) . ")";
  104 + $length += 1 + strlen($s); // 1 - separator length
  105 + if ($rows && $length > $packet_size) {
  106 + $result = queries($query . implode(",", $rows));
  107 + if (!$result) {
  108 + break;
  109 + }
  110 + $affected += $dbh->affected_rows;
  111 + $length = strlen($query);
  112 + $rows = array();
  113 + }
  114 + $rows[] = $s;
100 115 }
  116 + if (!$key) {
  117 + $query .= " VALUES";
  118 + $length += strlen($query);
  119 + }
  120 + }
  121 + if ($result) {
  122 + $result = queries($query . implode(",", $rows));
  123 + $affected += $dbh->affected_rows;
101 124 }
102   - $result = queries("INSERT INTO " . idf_escape($_GET["select"]) . "$cols VALUES" . implode(",", $rows));
103   - query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) have been imported.', $dbh->affected_rows), $result, false, !$result);
  125 + query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) have been imported.', $affected), $result, false, !$result);
104 126 } else {
105 127 $error = upload_error($file);
106 128 }
1  changes.txt
... ... @@ -1,5 +1,6 @@
1 1 Adminer 2.0.1:
2 2 Display column comments in table overview
  3 +Respect max_allowed_packet in CSV import
3 4 Fix Editor date format
4 5 Fix long SQL query crash (bug #2839231)
5 6

0 comments on commit cb370a8

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