Alter database

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@450 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana 2008-08-19 10:32:34 +00:00
parent 9cb4e22ac7
commit 4951444bcb
2 changed files with 49 additions and 6 deletions

View file

@ -73,10 +73,53 @@ if ($_POST) {
}
}
$result->free();
foreach ($views as $view => $style) {
dump_table($view, $style);
foreach ($views as $view => $style1) {
dump_table($view, $style1, true);
}
}
if ($mysql->server_info >= 5 && $style == "CREATE, ALTER" && $_POST["format"] != "csv") {
$query = "SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE()";
?>
DELIMITER ;;
CREATE PROCEDURE phpminadmin_drop () BEGIN
DECLARE _table_name, _engine, _table_collation varchar(64);
DECLARE _table_comment varchar(64);
DECLARE done bool DEFAULT 0;
DECLARE tables CURSOR FOR <?php echo $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<?php
$result = $mysql->query($query);
while ($row = $result->fetch_assoc()) {
$comment = $mysql->escape_string($row["ENGINE"] == "InnoDB" ? preg_replace('~(?:(.+); )?InnoDB free: .*~', '\\1', $row["TABLE_COMMENT"]) : $row["TABLE_COMMENT"]);
echo "
WHEN '" . $mysql->escape_string($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") . ";";
}
$result->free();
?>
ELSE
SET @alter_table = CONCAT('DROP TABLE `', REPLACE(_table_name, '`', '``'), '`');
PREPARE alter_command FROM @alter_table;
EXECUTE alter_command;
DROP PREPARE alter_command;
END CASE;
END IF;
UNTIL done END REPEAT;
CLOSE tables;
END;;
DELIMITER ;
CALL phpminadmin_drop;
DROP PROCEDURE phpminadmin_drop;
<?php
}
}
}
exit;

View file

@ -8,7 +8,7 @@ function dump_csv($row) {
echo implode(",", $row) . "\n";
}
function dump_table($table, $style) {
function dump_table($table, $style, $is_view = false) {
global $mysql, $max_packet, $types;
if ($_POST["format"] == "csv") {
echo "\xef\xbb\xbf";
@ -19,10 +19,11 @@ function dump_table($table, $style) {
$result = $mysql->query("SHOW CREATE TABLE " . idf_escape($table));
if ($result) {
if ($style == "DROP, CREATE") {
echo "DROP TABLE " . idf_escape($table) . ";\n";
echo "DROP " . ($is_view ? "VIEW" : "TABLE") . " " . idf_escape($table) . ";\n";
}
$create = $mysql->result($result, 1);
echo ($style == "CREATE, ALTER" ? preg_replace('~^CREATE TABLE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n\n";
$result->free();
echo ($style != "CREATE, ALTER" ? $create : ($is_view ? substr_replace($create, " OR REPLACE", 6, 0) : substr_replace($create, " IF NOT EXISTS", 12, 0))) . ";\n\n";
if ($max_packet < 1073741824) { // protocol limit
$row_size = 21 + strlen(idf_escape($table));
foreach (fields($table) as $field) {
@ -34,7 +35,6 @@ function dump_table($table, $style) {
echo "SET max_allowed_packet = $max_packet, GLOBAL max_allowed_packet = $max_packet;\n";
}
}
$result->free();
}
if ($mysql->server_info >= 5) {
$result = $mysql->query("SHOW TRIGGERS LIKE '" . $mysql->escape_string(addcslashes($table, "%_")) . "'");