Skip to content

Commit

Permalink
Merge pull request maicong#1 from maicong/dev
Browse files Browse the repository at this point in the history
New stable version 2.0.0
  • Loading branch information
maicong committed Feb 23, 2016
2 parents 3f44d32 + f6093cb commit a32c4e2
Show file tree
Hide file tree
Showing 18 changed files with 2,559 additions and 82 deletions.
664 changes: 664 additions & 0 deletions DBMGT/Adminer/adminer.css

Large diffs are not rendered by default.

817 changes: 817 additions & 0 deletions DBMGT/Adminer/adminer.php

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions DBMGT/Adminer/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
function adminer_object() {
// required to run any plugin
include_once __DIR__ . "/plugins/plugin.php";

// autoloader
foreach (glob("plugins/*.php") as $filename) {
include_once "./$filename";
}

$plugins = array(
// specify enabled plugins here
new AdminerDumpAlter,
new AdminerDumpBz2,
new AdminerDumpDate,
new AdminerDumpJson,
new AdminerDumpXml,
new AdminerDumpZip,
new AdminerEditTextarea,
new AdminerEnumOption,
new AdminerJsonColumn,
new AdminerTranslation
);

/* It is possible to combine customization and plugins:
class AdminerCustomization extends AdminerPlugin {
}
return new AdminerCustomization($plugins);
*/

return new AdminerPlugin($plugins);
}

// include original Adminer or Adminer Editor
include __DIR__ . "/adminer.php";
155 changes: 155 additions & 0 deletions DBMGT/Adminer/plugins/dump-alter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

/** Exports one database (e.g. development) so that it can be synced with other database (e.g. production)
* @link https://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 AdminerDumpAlter {

function dumpFormat() {
if (DRIVER == 'server') {
return array('sql_alter' => 'Alter');
}
}

function _database() {
// drop old tables
$query = "SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE()";
echo "DELIMITER ;;
CREATE PROCEDURE adminer_alter (INOUT alter_command text) BEGIN
DECLARE _table_name, _engine, _table_collation varchar(64);
DECLARE _table_comment varchar(64);
DECLARE done bool DEFAULT 0;
DECLARE tables CURSOR FOR $query;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN tables;
REPEAT
FETCH tables INTO _table_name, _engine, _table_collation, _table_comment;
IF NOT done THEN
CASE _table_name";
foreach (get_rows($query) as $row) {
$comment = q($row["ENGINE"] == "InnoDB" ? preg_replace('~(?:(.+); )?InnoDB free: .*~', '\\1', $row["TABLE_COMMENT"]) : $row["TABLE_COMMENT"]);
echo "
WHEN " . q($row["TABLE_NAME"]) . " THEN
" . (isset($row["ENGINE"]) ? "IF _engine != '$row[ENGINE]' OR _table_collation != '$row[TABLE_COLLATION]' OR _table_comment != $comment THEN
ALTER TABLE " . idf_escape($row["TABLE_NAME"]) . " ENGINE=$row[ENGINE] COLLATE=$row[TABLE_COLLATION] COMMENT=$comment;
END IF" : "BEGIN END") . ";";
}
echo "
ELSE
SET alter_command = CONCAT(alter_command, 'DROP TABLE `', REPLACE(_table_name, '`', '``'), '`;\\n');
END CASE;
END IF;
UNTIL done END REPEAT;
CLOSE tables;
END;;
DELIMITER ;
CALL adminer_alter(@adminer_alter);
DROP PROCEDURE adminer_alter;
SELECT @adminer_alter;
";
}

function dumpDatabase($db) {
static $first = true;
if ($_POST["format"] == "sql_alter") {
if ($first) {
$first = false;
echo "SET @adminer_alter = '';\n\n";
register_shutdown_function(array($this, '_database'));
} else {
$this->_database();
}
return true;
}
}

function dumpTable($table, $style, $is_view = false) {
if ($_POST["format"] == "sql_alter") {
$create = create_sql($table, $_POST["auto_increment"]);
if ($is_view) {
echo substr_replace($create, " OR REPLACE", 6, 0) . ";\n\n";
} else {
echo substr_replace($create, " IF NOT EXISTS", 12, 0) . ";\n\n";
// create procedure which iterates over original columns and adds new and removes old
$query = "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLLATION_NAME, COLUMN_TYPE, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . q($table) . " ORDER BY ORDINAL_POSITION";
echo "DELIMITER ;;
CREATE PROCEDURE adminer_alter (INOUT alter_command text) BEGIN
DECLARE _column_name, _collation_name, after varchar(64) DEFAULT '';
DECLARE _column_type, _column_default text;
DECLARE _is_nullable char(3);
DECLARE _extra varchar(30);
DECLARE _column_comment varchar(255);
DECLARE done, set_after bool DEFAULT 0;
DECLARE add_columns text DEFAULT '";
$fields = array();
$after = "";
foreach (get_rows($query) as $row) {
$default = $row["COLUMN_DEFAULT"];
$row["default"] = ($default !== null ? q($default) : "NULL");
$row["after"] = q($after); //! rgt AFTER lft, lft AFTER id doesn't work
$row["alter"] = escape_string(idf_escape($row["COLUMN_NAME"])
. " $row[COLUMN_TYPE]"
. ($row["COLLATION_NAME"] ? " COLLATE $row[COLLATION_NAME]" : "")
. ($default !== null ? " DEFAULT " . ($default == "CURRENT_TIMESTAMP" ? $default : $row["default"]) : "")
. ($row["IS_NULLABLE"] == "YES" ? "" : " NOT NULL")
. ($row["EXTRA"] ? " $row[EXTRA]" : "")
. ($row["COLUMN_COMMENT"] ? " COMMENT " . q($row["COLUMN_COMMENT"]) : "")
. ($after ? " AFTER " . idf_escape($after) : " FIRST")
);
echo ", ADD $row[alter]";
$fields[] = $row;
$after = $row["COLUMN_NAME"];
}
echo "';
DECLARE columns CURSOR FOR $query;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
SET @alter_table = '';
OPEN columns;
REPEAT
FETCH columns INTO _column_name, _column_default, _is_nullable, _collation_name, _column_type, _extra, _column_comment;
IF NOT done THEN
SET set_after = 1;
CASE _column_name";
foreach ($fields as $row) {
echo "
WHEN " . q($row["COLUMN_NAME"]) . " THEN
SET add_columns = REPLACE(add_columns, ', ADD $row[alter]', IF(
_column_default <=> $row[default] AND _is_nullable = '$row[IS_NULLABLE]' AND _collation_name <=> " . (isset($row["COLLATION_NAME"]) ? "'$row[COLLATION_NAME]'" : "NULL") . " AND _column_type = " . q($row["COLUMN_TYPE"]) . " AND _extra = '$row[EXTRA]' AND _column_comment = " . q($row["COLUMN_COMMENT"]) . " AND after = $row[after]
, '', ', MODIFY $row[alter]'));"; //! don't replace in comment
}
echo "
ELSE
SET @alter_table = CONCAT(@alter_table, ', DROP ', '`', REPLACE(_column_name, '`', '``'), '`');
SET set_after = 0;
END CASE;
IF set_after THEN
SET after = _column_name;
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE columns;
IF @alter_table != '' OR add_columns != '' THEN
SET alter_command = CONCAT(alter_command, 'ALTER TABLE " . table($table) . "', SUBSTR(CONCAT(add_columns, @alter_table), 2), ';\\n');
END IF;
END;;
DELIMITER ;
CALL adminer_alter(@adminer_alter);
DROP PROCEDURE adminer_alter;
";
//! indexes
}
return true;
}
}

function dumpData() {
if ($_POST["format"] == "sql_alter") {
return true;
}
}
}
41 changes: 41 additions & 0 deletions DBMGT/Adminer/plugins/dump-bz2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/** Dump to Bzip2 format
* @link https://www.adminer.org/plugins/#use
* @uses bzopen(), tempnam("")
* @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 AdminerDumpBz2 {
/** @access protected */
var $filename, $fp;

function dumpOutput() {
if (!function_exists('bzopen')) {
return array();
}
return array('bz2' => 'bzip2');
}

function _bz2($string, $state) {
bzwrite($this->fp, $string);
if ($state & PHP_OUTPUT_HANDLER_END) {
bzclose($this->fp);
$return = file_get_contents($this->filename);
unlink($this->filename);
return $return;
}
return "";
}

function dumpHeaders($identifier, $multi_table = false) {
if ($_POST["output"] == "bz2") {
$this->filename = tempnam("", "bz2");
$this->fp = bzopen($this->filename, 'w');
header("Content-Type: application/x-bzip");
ob_start(array($this, '_bz2'), 1e6);
}
}

}
16 changes: 16 additions & 0 deletions DBMGT/Adminer/plugins/dump-date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/** Include current date and time in export filename
* @link https://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 AdminerDumpDate {

function dumpFilename($identifier) {
$connection = connection();
return friendly_url(($identifier != "" ? $identifier : (SERVER != "" ? SERVER : "localhost")) . "-" . $connection->result("SELECT NOW()"));
}

}
62 changes: 62 additions & 0 deletions DBMGT/Adminer/plugins/dump-json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/** Dump to JSON format
* @link https://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";
}
}

}
56 changes: 56 additions & 0 deletions DBMGT/Adminer/plugins/dump-xml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/** Dump to XML format in structure <database name=""><table name=""><column name="">value
* @link https://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 AdminerDumpXml {
/** @access protected */
var $database = false;

function dumpFormat() {
return array('xml' => 'XML');
}

function dumpTable($table, $style, $is_view = false) {
if ($_POST["format"] == "xml") {
return true;
}
}

function _database() {
echo "</database>\n";
}

function dumpData($table, $style, $query) {
if ($_POST["format"] == "xml") {
if (!$this->database) {
$this->database = true;
echo "<database name='" . h(DB) . "'>\n";
register_shutdown_function(array($this, '_database'));
}
$connection = connection();
$result = $connection->query($query, 1);
if ($result) {
while ($row = $result->fetch_assoc()) {
echo "\t<table name='" . h($table) . "'>\n";
foreach ($row as $key => $val) {
echo "\t\t<column name='" . h($key) . "'" . (isset($val) ? "" : " null='null'") . ">" . h($val) . "</column>\n";
}
echo "\t</table>\n";
}
}
return true;
}
}

function dumpHeaders($identifier, $multi_table = false) {
if ($_POST["format"] == "xml") {
header("Content-Type: text/xml; charset=utf-8");
return "xml";
}
}

}
Loading

0 comments on commit a32c4e2

Please sign in to comment.