From f7452add47a390183526dfd60c57e376cba32ec3 Mon Sep 17 00:00:00 2001
From: jakubvrana
Date: Wed, 1 Apr 2009 10:57:34 +0000
Subject: [PATCH] Import and export
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@568 7c3ca157-0c34-0410-bff1-cbf682f78f5c
---
export.inc.php | 2 +-
lang/cs.inc.php | 1 -
select.inc.php | 27 ++++++++++++++++-----------
3 files changed, 17 insertions(+), 13 deletions(-)
diff --git a/export.inc.php b/export.inc.php
index 54877caf..7027046c 100644
--- a/export.inc.php
+++ b/export.inc.php
@@ -1,7 +1,7 @@
$val) {
- if (preg_match("~[\"\n,]~", $val)) {
+ if (preg_match("~[\"\n,]~", $val) || (isset($val) && !strlen($val))) {
$row[$key] = '"' . str_replace('"', '""', $val) . '"';
}
}
diff --git a/lang/cs.inc.php b/lang/cs.inc.php
index a5915774..7c97aaf2 100644
--- a/lang/cs.inc.php
+++ b/lang/cs.inc.php
@@ -216,5 +216,4 @@ $translations = array(
'Stop on error' => 'Zastavit při chybě',
'Maximum number of allowed fields exceeded. Please increase %s and %s.' => 'Byl překročen maximální povolený počet polí. Zvyšte prosím %s a %s.',
'(anywhere)' => '(kdekoliv)',
- 'Export selected' => 'Export označených',
);
diff --git a/select.inc.php b/select.inc.php
index 9955d20a..938e7fb1 100644
--- a/select.inc.php
+++ b/select.inc.php
@@ -70,29 +70,34 @@ if ($_POST && !$error) {
if ($_POST["export"]) {
dump_headers($_GET["select"]);
dump_table($_GET["select"], "");
- if ($_POST["all"]) {
- dump_data($_GET["select"], "INSERT", ($where ? "FROM " . idf_escape($_GET["select"]) . " WHERE " . implode(" AND ", $where) : ""));
- } else {
- foreach ((array) $_POST["check"] as $val) {
+ if (is_array($_POST["check"])) {
+ foreach ($_POST["check"] as $val) {
parse_str($val, $check);
dump_data($_GET["select"], "INSERT", "FROM " . idf_escape($_GET["select"]) . " WHERE " . implode(" AND ", where($check)) . " LIMIT 1");
}
+ } else {
+ dump_data($_GET["select"], "INSERT", ($where ? "FROM " . idf_escape($_GET["select"]) . " WHERE " . implode(" AND ", $where) : ""));
}
exit;
}
if ($_POST["import"]) {
$file = preg_replace("~^\xEF\xBB\xBF~", '', get_file("csv_file")); //! character set
+ $cols = "";
$rows = array(); //! packet size
preg_match_all('~("[^"]*"|[^"\\n]+)+~', $file, $matches);
- foreach ($matches[0] as $val) {
+ foreach ($matches[0] as $key => $val) {
$row = array();
- preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $fields);
- foreach ($fields[1] as $field) {
- $row[] = "'" . $mysql->escape_string(str_replace('""', '"', preg_replace('~".*"~s', '', $field))) . "'"; //! NULL
+ preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $matches2);
+ if (!$key && !array_diff($matches2[1], array_keys($fields))) { //! doesn't work with column names containing ",\n
+ $cols = " (" . implode(", ", array_map('idf_escape', $matches2[1])) . ")";
+ } else {
+ foreach ($matches2[1] as $col) {
+ $row[] = (!strlen($col) ? "NULL" : "'" . $mysql->escape_string(str_replace('""', '"', preg_replace('~^".*"$~s', '', $col))) . "'");
+ }
+ $rows[] = "(" . implode(", ", $row) . ")";
}
- $rows[] = "(" . implode(", ", $row) . ")";
}
- $result = queries("INSERT INTO " . idf_escape($_GET["select"]) . " VALUES " . implode(", ", $rows));
+ $result = queries("INSERT INTO " . idf_escape($_GET["select"]) . "$cols VALUES " . implode(", ", $rows));
query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) has been imported.', $mysql->affected_rows), $result, false, !$result);
} else {
$result = true;
@@ -325,7 +330,7 @@ for (var i=0; > i; i++) {
echo " (" . lang('%d row(s)', $found_rows) . ")
\n";
echo ($_GET["db"] != "information_schema" ? "\n" : "");
- echo "\n";
+ echo "\n";
}
$result->free();
echo "\n";