Permalink
Browse files

CSV import

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@545 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information...
1 parent 5c7948d commit e7eb28d32c032de597b0dec63117a73a407b1b1b jakubvrana committed Dec 30, 2008
Showing with 51 additions and 30 deletions.
  1. +1 −1 index.php
  2. +3 −0 lang/cs.inc.php
  3. +1 −0 lang/en.inc.php
  4. +46 −29 select.inc.php
View
@@ -100,7 +100,7 @@ function save_coverage() {
if (isset($_GET["default"])) {
$_GET["edit"] = $_GET["default"];
}
- if (isset($_GET["select"]) && $_POST && (!$_POST["delete"] && !$_POST["export"] && !$_POST["save"])) {
+ if (isset($_GET["select"]) && $_POST && (!$_POST["delete"] && !$_POST["export"] && !$_POST["import"] && !$_POST["save"])) {
$_GET["edit"] = $_GET["select"];
}
if (isset($_GET["callf"])) {
View
@@ -207,4 +207,7 @@
'Partitions' => 'Oddílů',
'Partition name' => 'Název oddílu',
'Values' => 'Hodnoty',
+ '%d row(s) has been imported.' => array('Byl importován %d záznam.', 'Byly importovány %d záznamy.', 'Bylo importováno %d záznamů.'),
+ 'CSV Import' => 'Import CSV',
+ 'Import' => 'Import',
);
View
@@ -6,4 +6,5 @@
'%d process(es) has been killed.' => array('%d process has been killed.', '%d processes have been killed.'),
'%d row(s)' => array('%d row', '%d rows'),
'%d item(s) have been affected.' => array('%d item have been affected.', '%d items have been affected.'),
+ '%d row(s) has been imported.' => array('%d row has been imported.', '%d rows has been imported.'),
);
View
@@ -80,38 +80,54 @@
}
exit;
}
- $result = true;
- $affected = 0;
- $command = ($_POST["delete"] ? ($_POST["all"] && !$where ? "TRUNCATE " : "DELETE FROM ") : ($_POST["clone"] ? "INSERT INTO " : "UPDATE ")) . idf_escape($_GET["select"]);
- if (!$_POST["delete"]) {
- $set = array();
- foreach ($fields as $name => $field) {
- $val = process_input($name, $field);
- if ($_POST["clone"]) {
- $set[] = ($val !== false ? $val : idf_escape($name));
- } elseif ($val !== false) {
- $set[] = idf_escape($name) . " = $val";
+ if ($_POST["import"]) {
+ $file = preg_replace("~^\xEF\xBB\xBF~", '', get_file("csv_file")); //! character set
+ $rows = array(); //! packet size
+ preg_match_all('~("[^"]*"|[^"\\n]+)+~', $file, $matches);
+ foreach ($matches[0] as $val) {
+ $row = array();
+ preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $fields);
+ foreach ($fields[1] as $field) {
+ $row[] = "'" . $mysql->escape_string(str_replace('""', '"', preg_replace('~".*"~s', '', $field))) . "'"; //! NULL
}
+ $rows[] = "(" . implode(", ", $row) . ")";
}
- $command .= ($_POST["clone"] ? " SELECT " . implode(", ", $set) . " FROM " . idf_escape($_GET["select"]) : " SET " . implode(", ", $set));
- }
- if (!$_POST["delete"] && !$set) {
- // nothing
- } elseif ($_POST["all"]) {
- $result = queries($command . ($where ? " WHERE " . implode(" AND ", $where) : ""));
- $affected = $mysql->affected_rows;
+ $result = queries("INSERT INTO " . idf_escape($_GET["select"]) . " VALUES " . implode(", ", $rows));
+ query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) has been imported.', $mysql->affected_rows), $result, false, !$result);
} else {
- foreach ((array) $_POST["check"] as $val) {
- parse_str($val, $check);
- $result = queries($command . " WHERE " . implode(" AND ", where($check)) . " LIMIT 1");
- if (!$result) {
- break;
+ $result = true;
+ $affected = 0;
+ $command = ($_POST["delete"] ? ($_POST["all"] && !$where ? "TRUNCATE " : "DELETE FROM ") : ($_POST["clone"] ? "INSERT INTO " : "UPDATE ")) . idf_escape($_GET["select"]);
+ if (!$_POST["delete"]) {
+ $set = array();
+ foreach ($fields as $name => $field) {
+ $val = process_input($name, $field);
+ if ($_POST["clone"]) {
+ $set[] = ($val !== false ? $val : idf_escape($name));
+ } elseif ($val !== false) {
+ $set[] = idf_escape($name) . " = $val";
+ }
+ }
+ $command .= ($_POST["clone"] ? " SELECT " . implode(", ", $set) . " FROM " . idf_escape($_GET["select"]) : " SET " . implode(", ", $set));
+ }
+ if (!$_POST["delete"] && !$set) {
+ // nothing
+ } elseif ($_POST["all"]) {
+ $result = queries($command . ($where ? " WHERE " . implode(" AND ", $where) : ""));
+ $affected = $mysql->affected_rows;
+ } else {
+ foreach ((array) $_POST["check"] as $val) {
+ parse_str($val, $check);
+ $result = queries($command . " WHERE " . implode(" AND ", where($check)) . " LIMIT 1");
+ if (!$result) {
+ break;
+ }
+ $affected += $mysql->affected_rows;
}
- $affected += $mysql->affected_rows;
}
+ query_redirect(queries(), remove_from_uri("page"), lang('%d item(s) have been affected.', $affected), $result, false, !$result);
+ //! display edit page in case of an error
}
- query_redirect(queries(), remove_from_uri("page"), lang('%d item(s) have been affected.', $affected), $result, false, !$result);
- //! display edit page in case of an error
}
page_header(lang('Select') . ": " . htmlspecialchars($_GET["select"]), $error);
@@ -226,6 +242,7 @@ function where_change(op) {
if (!$result) {
echo "<p class='error'>" . htmlspecialchars($mysql->error) . "</p>\n";
} else {
+ echo "<form action='' method='post' enctype='multipart/form-data'>\n";
if (!$result->num_rows) {
echo "<p class='message'>" . lang('No rows.') . "</p>\n";
} else {
@@ -236,7 +253,6 @@ function where_change(op) {
}
}
- echo "<form action='' method='post'>\n";
echo "<table border='1' cellspacing='0' cellpadding='2' class='nowrap'>\n";
for ($j=0; $row = $result->fetch_assoc(); $j++) {
if (!$j) {
@@ -301,9 +317,10 @@ function where_change(op) {
echo " (" . lang('%d row(s)', $found_rows) . ")</p>\n";
echo ($_GET["db"] != "information_schema" ? "<fieldset><legend>" . lang('Edit') . "</legend><div><input type='submit' value='" . lang('Edit') . "' /> <input type='submit' name='clone' value='" . lang('Clone') . "' /> <input type='submit' name='delete' value='" . lang('Delete') . "'$confirm /></div></fieldset>\n" : "");
- echo "<fieldset><legend>" . lang('Export') . "</legend><div><input type='hidden' name='token' value='$token' />$dump_options <input type='submit' name='export' value='" . lang('Export') . "' /></div></fieldset>\n";
- echo "</form>\n";
+ echo "<fieldset><legend>" . lang('Export') . "</legend><div>$dump_options <input type='submit' name='export' value='" . lang('Export') . "' /></div></fieldset>\n";
}
$result->free();
+ echo "<fieldset><legend>" . lang('CSV Import') . "</legend><div><input type='hidden' name='token' value='$token' /><input type='file' name='csv_file' /> <input type='submit' name='import' value='" . lang('Import') . "' /></div></fieldset>\n";
+ echo "</form>\n";
}
}

0 comments on commit e7eb28d

Please sign in to comment.