CSV import
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@545 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
parent
5c7948dd54
commit
e7eb28d32c
|
@ -100,7 +100,7 @@ if (isset($_GET["download"])) {
|
||||||
if (isset($_GET["default"])) {
|
if (isset($_GET["default"])) {
|
||||||
$_GET["edit"] = $_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"];
|
$_GET["edit"] = $_GET["select"];
|
||||||
}
|
}
|
||||||
if (isset($_GET["callf"])) {
|
if (isset($_GET["callf"])) {
|
||||||
|
|
|
@ -207,4 +207,7 @@ $translations = array(
|
||||||
'Partitions' => 'Oddílů',
|
'Partitions' => 'Oddílů',
|
||||||
'Partition name' => 'Název oddílu',
|
'Partition name' => 'Název oddílu',
|
||||||
'Values' => 'Hodnoty',
|
'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',
|
||||||
);
|
);
|
||||||
|
|
|
@ -6,4 +6,5 @@ $translations = array(
|
||||||
'%d process(es) has been killed.' => array('%d process has been killed.', '%d processes have been killed.'),
|
'%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 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 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.'),
|
||||||
);
|
);
|
||||||
|
|
|
@ -80,38 +80,54 @@ if ($_POST && !$error) {
|
||||||
}
|
}
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
$result = true;
|
if ($_POST["import"]) {
|
||||||
$affected = 0;
|
$file = preg_replace("~^\xEF\xBB\xBF~", '', get_file("csv_file")); //! character set
|
||||||
$command = ($_POST["delete"] ? ($_POST["all"] && !$where ? "TRUNCATE " : "DELETE FROM ") : ($_POST["clone"] ? "INSERT INTO " : "UPDATE ")) . idf_escape($_GET["select"]);
|
$rows = array(); //! packet size
|
||||||
if (!$_POST["delete"]) {
|
preg_match_all('~("[^"]*"|[^"\\n]+)+~', $file, $matches);
|
||||||
$set = array();
|
foreach ($matches[0] as $val) {
|
||||||
foreach ($fields as $name => $field) {
|
$row = array();
|
||||||
$val = process_input($name, $field);
|
preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $fields);
|
||||||
if ($_POST["clone"]) {
|
foreach ($fields[1] as $field) {
|
||||||
$set[] = ($val !== false ? $val : idf_escape($name));
|
$row[] = "'" . $mysql->escape_string(str_replace('""', '"', preg_replace('~".*"~s', '', $field))) . "'"; //! NULL
|
||||||
} elseif ($val !== false) {
|
|
||||||
$set[] = idf_escape($name) . " = $val";
|
|
||||||
}
|
}
|
||||||
|
$rows[] = "(" . implode(", ", $row) . ")";
|
||||||
}
|
}
|
||||||
$command .= ($_POST["clone"] ? " SELECT " . implode(", ", $set) . " FROM " . idf_escape($_GET["select"]) : " SET " . implode(", ", $set));
|
$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);
|
||||||
if (!$_POST["delete"] && !$set) {
|
|
||||||
// nothing
|
|
||||||
} elseif ($_POST["all"]) {
|
|
||||||
$result = queries($command . ($where ? " WHERE " . implode(" AND ", $where) : ""));
|
|
||||||
$affected = $mysql->affected_rows;
|
|
||||||
} else {
|
} else {
|
||||||
foreach ((array) $_POST["check"] as $val) {
|
$result = true;
|
||||||
parse_str($val, $check);
|
$affected = 0;
|
||||||
$result = queries($command . " WHERE " . implode(" AND ", where($check)) . " LIMIT 1");
|
$command = ($_POST["delete"] ? ($_POST["all"] && !$where ? "TRUNCATE " : "DELETE FROM ") : ($_POST["clone"] ? "INSERT INTO " : "UPDATE ")) . idf_escape($_GET["select"]);
|
||||||
if (!$result) {
|
if (!$_POST["delete"]) {
|
||||||
break;
|
$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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$affected += $mysql->affected_rows;
|
$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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
page_header(lang('Select') . ": " . htmlspecialchars($_GET["select"]), $error);
|
||||||
|
|
||||||
|
@ -226,6 +242,7 @@ for (var i=0; <?php echo $i; ?> > i; i++) {
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
echo "<p class='error'>" . htmlspecialchars($mysql->error) . "</p>\n";
|
echo "<p class='error'>" . htmlspecialchars($mysql->error) . "</p>\n";
|
||||||
} else {
|
} else {
|
||||||
|
echo "<form action='' method='post' enctype='multipart/form-data'>\n";
|
||||||
if (!$result->num_rows) {
|
if (!$result->num_rows) {
|
||||||
echo "<p class='message'>" . lang('No rows.') . "</p>\n";
|
echo "<p class='message'>" . lang('No rows.') . "</p>\n";
|
||||||
} else {
|
} else {
|
||||||
|
@ -236,7 +253,6 @@ for (var i=0; <?php echo $i; ?> > i; i++) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "<form action='' method='post'>\n";
|
|
||||||
echo "<table border='1' cellspacing='0' cellpadding='2' class='nowrap'>\n";
|
echo "<table border='1' cellspacing='0' cellpadding='2' class='nowrap'>\n";
|
||||||
for ($j=0; $row = $result->fetch_assoc(); $j++) {
|
for ($j=0; $row = $result->fetch_assoc(); $j++) {
|
||||||
if (!$j) {
|
if (!$j) {
|
||||||
|
@ -301,9 +317,10 @@ for (var i=0; <?php echo $i; ?> > i; i++) {
|
||||||
echo " (" . lang('%d row(s)', $found_rows) . ")</p>\n";
|
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 ($_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 "<fieldset><legend>" . lang('Export') . "</legend><div>$dump_options <input type='submit' name='export' value='" . lang('Export') . "' /></div></fieldset>\n";
|
||||||
echo "</form>\n";
|
|
||||||
}
|
}
|
||||||
$result->free();
|
$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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue