Permalink
Browse files

Export SQL command result (bug #3116854)

  • Loading branch information...
1 parent 0836c34 commit 15715b32ff8efbf7cc26bf35db7059eea363ee03 @vrana committed Feb 15, 2011
Showing with 33 additions and 14 deletions.
  1. +7 −1 adminer/include/adminer.inc.php
  2. +3 −12 adminer/select.inc.php
  3. +23 −1 adminer/sql.inc.php
@@ -612,13 +612,19 @@ function dumpData($table, $style, $query) {
if ($_POST["format"] == "sql" && $style == "TRUNCATE+INSERT") {
echo truncate_sql($table) . ";\n";
}
- $fields = fields($table);
+ if ($_POST["format"] == "sql") {
+ $fields = fields($table);
+ }
$result = $connection->query($query, 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers
if ($result) {
$insert = "";
$buffer = "";
while ($row = $result->fetch_assoc()) {
if ($_POST["format"] != "sql") {
+ if ($style == "table") {
+ dump_csv(array_keys($row));
+ $style = "INSERT";
+ }
dump_csv($row);
} else {
if (!$insert) {
View
@@ -56,30 +56,21 @@
if ($_POST["export"]) {
$adminer->dumpHeaders($TABLE);
$adminer->dumpTable($TABLE, "");
- if (ereg("[ct]sv", $_POST["format"])) { // CSV or TSV
- $row = array_keys($fields);
- if ($select) {
- $row = array();
- foreach ($select as $val) {
- $row[] = (ereg('^`.*`$', $val) ? idf_unescape($val) : $val); //! columns looking like functions
- }
- }
- dump_csv($row);
- }
if (!is_array($_POST["check"]) || $unselected === array()) {
$where2 = $where;
if (is_array($_POST["check"])) {
$where2[] = "($where_check)";
}
- $adminer->dumpData($TABLE, "INSERT", "SELECT $from" . ($where2 ? "\nWHERE " . implode(" AND ", $where2) : "") . $group_by);
+ $query = "SELECT $from" . ($where2 ? "\nWHERE " . implode(" AND ", $where2) : "") . $group_by;
} else {
$union = array();
foreach ($_POST["check"] as $val) {
// where is not unique so OR can't be used
$union[] = "(SELECT" . limit($from, "\nWHERE " . ($where ? implode(" AND ", $where) . " AND " : "") . where_check($val) . $group_by, 1) . ")";
}
- $adminer->dumpData($TABLE, "INSERT", implode(" UNION ALL ", $union));
+ $query = implode(" UNION ALL ", $union);
}
+ $adminer->dumpData($TABLE, "table", $query);
exit;
}
if (!$adminer->selectEmailProcess($where, $foreign_keys)) {
View
@@ -1,4 +1,11 @@
<?php
+if (!$error && $_POST["export"]) {
+ $adminer->dumpHeaders("sql");
+ $adminer->dumpTable("", "");
+ $adminer->dumpData("", "table", $_POST["query"]);
+ exit;
+}
+
restart_session();
$history_all = &get_session("queries");
$history = &$history_all[DB];
@@ -43,6 +50,9 @@
$errors = array();
$parse = '[\'`"]' . ($jush == "pgsql" ? '|\\$[^$]*\\$' : ($jush == "mssql" || $jush == "sqlite" ? '|\\[' : '')) . '|/\\*|-- |#'; //! ` and # not everywhere
$total_start = explode(" ", microtime());
+ parse_str($_COOKIE["adminer_export"], $adminer_export);
+ $dump_format = $adminer->dumpFormat();
+ unset($dump_format["sql"]);
while ($query != "") {
if (!$offset && $jush == "sql" && preg_match('~^\\s*DELIMITER\\s+(.+)~i', $query, $match)) {
$delimiter = $match[1];
@@ -102,14 +112,26 @@
$print = "";
}
select($result, $connection2);
+ echo "<form action='' method='post'>\n";
echo "<p>" . ($result->num_rows ? lang('%d row(s)', $result->num_rows) : "") . $time;
+ $id = "export-$commands";
+ $export = ", <a href='#$id' onclick=\"return !toggle('$id');\">" . lang('Export') . "</a><span id='$id' class='hidden'>: "
+ . html_select("output", $adminer->dumpOutput(), $adminer_export["output"]) . " "
+ . html_select("format", $dump_format, $adminer_export["format"])
+ . " <input type='hidden' name='query' value='" . h($q) . "' />"
+ . " <input type='hidden' name='token' value='$token' />"
+ . " <input type='submit' name='export' value='" . lang('Export') . "' onclick='eventStop(event);'></span>"
+ ;
if ($connection2 && preg_match("~^($space|\\()*SELECT\\b~isU", $q) && ($explain = explain($connection2, $q))) {
$id = "explain-$commands";
- echo ", <a href='#$id' onclick=\"return !toggle('$id');\">EXPLAIN</a>\n";
+ echo ", <a href='#$id' onclick=\"return !toggle('$id');\">EXPLAIN</a>$export\n";
echo "<div id='$id' class='hidden'>\n";
select($explain, $connection2, ($jush == "sql" ? "http://dev.mysql.com/doc/refman/" . substr($connection->server_info, 0, 3) . "/en/explain-output.html#" : ""));
echo "</div>\n";
+ } else {
+ echo "$export\n";
}
+ echo "</form>\n";
}
$start = $end;
} while ($connection->next_result());

0 comments on commit 15715b3

Please sign in to comment.