Permalink
Browse files

Import and export

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@568 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information...
1 parent 0f93a49 commit f7452add47a390183526dfd60c57e376cba32ec3 jakubvrana committed Apr 1, 2009
Showing with 17 additions and 13 deletions.
  1. +1 −1 export.inc.php
  2. +0 −1 lang/cs.inc.php
  3. +16 −11 select.inc.php
View
@@ -1,7 +1,7 @@
<?php
function dump_csv($row) {
foreach ($row as $key => $val) {
- if (preg_match("~[\"\n,]~", $val)) {
+ if (preg_match("~[\"\n,]~", $val) || (isset($val) && !strlen($val))) {
$row[$key] = '"' . str_replace('"', '""', $val) . '"';
}
}
View
@@ -216,5 +216,4 @@
'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',
);
View
@@ -70,29 +70,34 @@
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 @@ 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 selected') . "</legend><div>$dump_options <input type='submit' name='export' value='" . lang('Export') . "' /></div></fieldset>\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";

0 comments on commit f7452ad

Please sign in to comment.