Export only selected columns
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@678 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
parent
f81740967f
commit
00ae3cce49
|
@ -116,41 +116,43 @@ DROP PROCEDURE adminer_alter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function dump_data($table, $style, $from = "") {
|
function dump_data($table, $style, $select = "") {
|
||||||
global $dbh, $max_packet;
|
global $dbh, $max_packet;
|
||||||
if ($style) {
|
if ($style) {
|
||||||
if ($_POST["format"] != "csv" && $style == "TRUNCATE+INSERT") {
|
if ($_POST["format"] != "csv" && $style == "TRUNCATE+INSERT") {
|
||||||
echo "TRUNCATE " . idf_escape($table) . ";\n";
|
echo "TRUNCATE " . idf_escape($table) . ";\n";
|
||||||
}
|
}
|
||||||
$result = $dbh->query("SELECT * " . ($from ? $from : "FROM " . idf_escape($table))); //! enum and set as numbers, binary as _binary, microtime
|
$result = $dbh->query(($select ? $select : "SELECT * FROM " . idf_escape($table))); //! enum and set as numbers, binary as _binary, microtime
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$insert = "INSERT INTO " . idf_escape($table) . " VALUES ";
|
|
||||||
$length = 0;
|
$length = 0;
|
||||||
while ($row = $result->fetch_assoc()) {
|
while ($row = $result->fetch_assoc()) {
|
||||||
if ($_POST["format"] == "csv") {
|
if ($_POST["format"] == "csv") {
|
||||||
dump_csv($row);
|
dump_csv($row);
|
||||||
} elseif ($style == "INSERT+UPDATE") {
|
|
||||||
$set = array();
|
|
||||||
foreach ($row as $key => $val) {
|
|
||||||
$row[$key] = (isset($val) ? "'" . $dbh->escape_string($val) . "'" : "NULL");
|
|
||||||
$set[] = idf_escape($key) . " = " . (isset($val) ? "'" . $dbh->escape_string($val) . "'" : "NULL");
|
|
||||||
}
|
|
||||||
echo "INSERT INTO " . idf_escape($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES (" . implode(", ", $row) . ") ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n";
|
|
||||||
} else {
|
} else {
|
||||||
|
$insert = "INSERT INTO " . idf_escape($table) . ($select ? " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ")" : "") . " VALUES ";
|
||||||
|
$row2 = array();
|
||||||
foreach ($row as $key => $val) {
|
foreach ($row as $key => $val) {
|
||||||
$row[$key] = (isset($val) ? "'" . $dbh->escape_string($val) . "'" : "NULL");
|
$row2[$key] = (isset($val) ? "'" . $dbh->escape_string($val) . "'" : "NULL");
|
||||||
}
|
}
|
||||||
$s = "(" . implode(", ", $row) . ")";
|
if ($style == "INSERT+UPDATE") {
|
||||||
if (!$length) {
|
$set = array();
|
||||||
echo $insert, $s;
|
foreach ($row as $key => $val) {
|
||||||
$length = strlen($insert) + strlen($s);
|
$set[] = idf_escape($key) . " = " . (isset($val) ? "'" . $dbh->escape_string($val) . "'" : "NULL");
|
||||||
|
}
|
||||||
|
echo "$insert (" . implode(", ", $row2) . ") ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n";
|
||||||
} else {
|
} else {
|
||||||
$length += 2 + strlen($s);
|
$s = "(" . implode(", ", $row2) . ")";
|
||||||
if ($length < $max_packet) {
|
if (!$length) {
|
||||||
echo ", ", $s;
|
echo $insert, $s;
|
||||||
} else {
|
|
||||||
echo ";\n", $insert, $s;
|
|
||||||
$length = strlen($insert) + strlen($s);
|
$length = strlen($insert) + strlen($s);
|
||||||
|
} else {
|
||||||
|
$length += 2 + strlen($s);
|
||||||
|
if ($length < $max_packet) {
|
||||||
|
echo ", ", $s;
|
||||||
|
} else {
|
||||||
|
echo ";\n", $insert, $s;
|
||||||
|
$length = strlen($insert) + strlen($s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,12 +72,13 @@ if ($_POST && !$error) {
|
||||||
if ($_POST["export"]) {
|
if ($_POST["export"]) {
|
||||||
dump_headers($_GET["select"]);
|
dump_headers($_GET["select"]);
|
||||||
dump_table($_GET["select"], "");
|
dump_table($_GET["select"], "");
|
||||||
|
$from = "SELECT " . ($select ? implode(", ", $select) : "*") . " FROM " . idf_escape($_GET["select"]);
|
||||||
if (is_array($_POST["check"])) {
|
if (is_array($_POST["check"])) {
|
||||||
foreach ($_POST["check"] as $val) {
|
foreach ($_POST["check"] as $val) {
|
||||||
dump_data($_GET["select"], "INSERT", "FROM " . idf_escape($_GET["select"]) . " WHERE " . implode(" AND ", where_check($val)) . " LIMIT 1");
|
dump_data($_GET["select"], "INSERT", "$from WHERE " . implode(" AND ", where_check($val)) . " LIMIT 1");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dump_data($_GET["select"], "INSERT", ($where ? "FROM " . idf_escape($_GET["select"]) . " WHERE " . implode(" AND ", $where) : ""));
|
dump_data($_GET["select"], "INSERT", $from . ($where ? " WHERE " . implode(" AND ", $where) : ""));
|
||||||
}
|
}
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue