]> git.joonet.de Git - adminer.git/commitdiff
JSON export plugin (http://forum.zdrojak.cz/?topic=808)
authorJakub Vrana <jakub@vrana.cz>
Fri, 8 Feb 2013 03:04:36 +0000 (19:04 -0800)
committerJakub Vrana <jakub@vrana.cz>
Fri, 8 Feb 2013 03:04:36 +0000 (19:04 -0800)
adminer/plugin.php
plugins/dump-json.php [new file with mode: 0644]

index dac12efe85961f4958d909696c2d08766743c3bd..534b50f1db9e288197cf81cc44a7016ca7bc9915 100644 (file)
@@ -11,6 +11,7 @@ function adminer_object() {
        $plugins = array(
                // specify enabled plugins here
                new AdminerDatabaseHide(array('information_schema')),
+               new AdminerDumpJson,
                new AdminerDumpZip,
                new AdminerDumpXml,
                //~ new AdminerSqlLog("past-" . rtrim(`git describe --tags --abbrev=0`) . ".sql"),
diff --git a/plugins/dump-json.php b/plugins/dump-json.php
new file mode 100644 (file)
index 0000000..97a5a9b
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+
+/** Dump to JSON format
+* @link http://www.adminer.org/plugins/#use
+* @author Jakub Vrana, http://www.vrana.cz/
+* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
+* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
+*/
+class AdminerDumpJson {
+       /** @access protected */
+       var $database = false;
+       
+       function dumpFormat() {
+               return array('json' => 'JSON');
+       }
+
+       function dumpTable($table, $style, $is_view = false) {
+               if ($_POST["format"] == "json") {
+                       return true;
+               }
+       }
+       
+       function _database() {
+               echo "}\n";
+       }
+       
+       function dumpData($table, $style, $query) {
+               if ($_POST["format"] == "json") {
+                       if ($this->database) {
+                               echo ",\n";
+                       } else {
+                               $this->database = true;
+                               echo "{\n";
+                               register_shutdown_function(array($this, '_database'));
+                       }
+                       $connection = connection();
+                       $result = $connection->query($query, 1);
+                       if ($result) {
+                               echo '"' . addcslashes($table, "\r\n\"\\") . "\": [\n";
+                               $first = true;
+                               while ($row = $result->fetch_assoc()) {
+                                       echo ($first ? "" : ", ");
+                                       $first = false;
+                                       foreach ($row as $key => $val) {
+                                               json_row($key, $val);
+                                       }
+                                       json_row("");
+                               }
+                               echo "]";
+                       }
+                       return true;
+               }
+       }
+
+       function dumpHeaders($identifier, $multi_table = false) {
+               if ($_POST["format"] == "json") {
+                       header("Content-Type: application/json; charset=utf-8");
+                       return "json";
+               }
+       }
+
+}