]> git.joonet.de Git - adminer.git/commitdiff
Export SQL command result (bug #3116854)
authorJakub Vrana <jakub@vrana.cz>
Tue, 15 Feb 2011 16:22:50 +0000 (17:22 +0100)
committerJakub Vrana <jakub@vrana.cz>
Tue, 15 Feb 2011 16:22:50 +0000 (17:22 +0100)
adminer/include/adminer.inc.php
adminer/select.inc.php
adminer/sql.inc.php

index 65e0dffc1b16d9251596243161e135a0703e2bc1..de9915606361c285762717a0ee07bfbd7f2c6445 100644 (file)
@@ -612,13 +612,19 @@ DROP PROCEDURE adminer_alter;
                        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) {
index 0d8146120cc096fa3cbefc5e43477c1aa6e2585c..94850652b3a9ea68eda51aa79f3979564277ed98 100644 (file)
@@ -56,30 +56,21 @@ if ($_POST && !$error) {
        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)) {
index 2ffb4eea11881e70aeef4acab9c363b4606e925f..48bca21ea1a2971400871b99b599cd3f8a13985d 100644 (file)
@@ -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 @@ if (!$error && $_POST) {
                $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 @@ if (!$error && $_POST) {
                                                                                $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());