2007-07-02 05:51:26 +00:00
|
|
|
<?php
|
2009-08-29 13:57:50 +00:00
|
|
|
$TABLE = $_GET["create"];
|
2008-12-12 14:03:02 +00:00
|
|
|
$partition_by = array('HASH', 'LINEAR HASH', 'KEY', 'LINEAR KEY', 'RANGE', 'LIST');
|
|
|
|
|
2009-08-29 13:57:50 +00:00
|
|
|
$referencable_primary = referencable_primary($TABLE);
|
2009-07-02 22:37:10 +00:00
|
|
|
$foreign_keys = array();
|
|
|
|
foreach ($referencable_primary as $table_name => $field) {
|
2010-05-20 20:06:34 +00:00
|
|
|
$foreign_keys[str_replace("`", "``", $table_name) . "`" . str_replace("`", "``", $field["field"])] = $table_name; // not idf_escape() - used in JS
|
2009-07-02 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:47:15 +00:00
|
|
|
$orig_fields = array();
|
|
|
|
$orig_status = array();
|
2010-01-09 23:33:41 +00:00
|
|
|
if ($TABLE != "") {
|
2009-08-29 13:57:50 +00:00
|
|
|
$orig_fields = fields($TABLE);
|
2009-09-09 19:47:15 +00:00
|
|
|
$orig_status = table_status($TABLE);
|
2008-07-24 12:03:28 +00:00
|
|
|
}
|
2011-03-17 09:07:58 +00:00
|
|
|
if ($_POST && !$_POST["fields"]) {
|
|
|
|
$_POST["fields"] = array();
|
|
|
|
}
|
2008-07-24 12:03:28 +00:00
|
|
|
|
2008-05-07 11:22:08 +00:00
|
|
|
if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"] && !$_POST["down"]) {
|
2010-03-01 22:59:08 +00:00
|
|
|
if ($_POST["drop"]) {
|
2010-05-11 14:45:04 +00:00
|
|
|
query_redirect("DROP TABLE " . table($TABLE), substr(ME, 0, -1), lang('Table has been dropped.'));
|
2010-03-01 22:59:08 +00:00
|
|
|
} else {
|
2010-04-21 12:01:32 +00:00
|
|
|
$fields = array();
|
2012-07-15 21:38:45 +00:00
|
|
|
$all_fields = array();
|
|
|
|
$use_all_fields = false;
|
2010-04-21 12:01:32 +00:00
|
|
|
$foreign = array();
|
2010-03-01 22:59:08 +00:00
|
|
|
ksort($_POST["fields"]);
|
|
|
|
$orig_field = reset($orig_fields);
|
2012-07-15 21:38:45 +00:00
|
|
|
$after = " FIRST";
|
2010-03-01 22:59:08 +00:00
|
|
|
foreach ($_POST["fields"] as $key => $field) {
|
2010-05-21 13:07:59 +00:00
|
|
|
$foreign_key = $foreign_keys[$field["type"]];
|
2012-05-14 06:54:07 +00:00
|
|
|
$type_field = ($foreign_key !== null ? $referencable_primary[$foreign_key] : $field); //! can collide with user defined type
|
2010-03-01 22:59:08 +00:00
|
|
|
if ($field["field"] != "") {
|
2010-05-21 13:07:59 +00:00
|
|
|
if (!$field["has_default"]) {
|
|
|
|
$field["default"] = null;
|
|
|
|
}
|
|
|
|
if ($key == $_POST["auto_increment_col"]) {
|
|
|
|
$field["auto_increment"] = true;
|
|
|
|
}
|
|
|
|
$process_field = process_field($field, $type_field);
|
2012-07-15 21:38:45 +00:00
|
|
|
$all_fields[] = array($field["orig"], $process_field, $after);
|
2010-05-21 13:07:59 +00:00
|
|
|
if ($process_field != process_field($orig_field, $orig_field)) {
|
|
|
|
$fields[] = array($field["orig"], $process_field, $after);
|
2012-07-15 21:38:45 +00:00
|
|
|
if ($field["orig"] != "" || $after) {
|
|
|
|
$use_all_fields = true;
|
|
|
|
}
|
2010-05-21 13:07:59 +00:00
|
|
|
}
|
2012-05-14 06:54:07 +00:00
|
|
|
if ($foreign_key !== null) {
|
2012-07-15 21:38:45 +00:00
|
|
|
$foreign[idf_escape($field["field"])] = ($TABLE != "" && $jush != "sqlite" ? "ADD" : " ") . " FOREIGN KEY (" . idf_escape($field["field"]) . ") REFERENCES " . table($foreign_keys[$field["type"]]) . " (" . idf_escape($type_field["field"]) . ")" . (ereg("^($on_actions)\$", $field["on_delete"]) ? " ON DELETE $field[on_delete]" : "");
|
2010-03-01 22:59:08 +00:00
|
|
|
}
|
2012-07-15 21:38:45 +00:00
|
|
|
$after = " AFTER " . idf_escape($field["field"]);
|
2010-03-01 22:59:08 +00:00
|
|
|
} elseif ($field["orig"] != "") {
|
2012-07-15 21:38:45 +00:00
|
|
|
$use_all_fields = true;
|
2010-04-21 12:01:32 +00:00
|
|
|
$fields[] = array($field["orig"]);
|
2010-03-01 22:59:08 +00:00
|
|
|
}
|
|
|
|
if ($field["orig"] != "") {
|
|
|
|
$orig_field = next($orig_fields);
|
2012-07-15 21:38:45 +00:00
|
|
|
if (!$orig_field) {
|
|
|
|
$after = "";
|
|
|
|
}
|
2010-03-01 22:59:08 +00:00
|
|
|
}
|
2008-12-12 14:03:02 +00:00
|
|
|
}
|
2010-04-21 12:01:32 +00:00
|
|
|
$partitioning = "";
|
2010-03-01 22:59:08 +00:00
|
|
|
if (in_array($_POST["partition_by"], $partition_by)) {
|
|
|
|
$partitions = array();
|
|
|
|
if ($_POST["partition_by"] == 'RANGE' || $_POST["partition_by"] == 'LIST') {
|
|
|
|
foreach (array_filter($_POST["partition_names"]) as $key => $val) {
|
|
|
|
$value = $_POST["partition_values"][$key];
|
|
|
|
$partitions[] = "\nPARTITION " . idf_escape($val) . " VALUES " . ($_POST["partition_by"] == 'RANGE' ? "LESS THAN" : "IN") . ($value != "" ? " ($value)" : " MAXVALUE"); //! SQL injection
|
|
|
|
}
|
2009-07-23 16:31:28 +00:00
|
|
|
}
|
2010-04-21 12:01:32 +00:00
|
|
|
$partitioning .= "\nPARTITION BY $_POST[partition_by]($_POST[partition])" . ($partitions // $_POST["partition"] can be expression, not only column
|
2010-03-01 22:59:08 +00:00
|
|
|
? " (" . implode(",", $partitions) . "\n)"
|
2010-10-22 21:36:56 +00:00
|
|
|
: ($_POST["partitions"] ? " PARTITIONS " . (+$_POST["partitions"]) : "")
|
2010-03-01 22:59:08 +00:00
|
|
|
);
|
2012-09-09 07:00:59 +00:00
|
|
|
} elseif (support("partitioning") && ereg("partitioned", $orig_status["Create_options"])) {
|
2010-04-21 12:01:32 +00:00
|
|
|
$partitioning .= "\nREMOVE PARTITIONING";
|
2010-03-01 22:59:08 +00:00
|
|
|
}
|
2010-04-21 12:01:32 +00:00
|
|
|
$message = lang('Table has been altered.');
|
|
|
|
if ($TABLE == "") {
|
2010-03-01 22:59:08 +00:00
|
|
|
cookie("adminer_engine", $_POST["Engine"]);
|
2010-04-21 12:01:32 +00:00
|
|
|
$message = lang('Table has been created.');
|
2007-08-04 19:38:01 +00:00
|
|
|
}
|
2011-09-10 11:06:59 +00:00
|
|
|
$name = trim($_POST["name"]);
|
|
|
|
queries_redirect(ME . "table=" . urlencode($name), $message, alter_table(
|
2010-04-21 12:01:32 +00:00
|
|
|
$TABLE,
|
2011-09-10 11:06:59 +00:00
|
|
|
$name,
|
2012-07-15 21:38:45 +00:00
|
|
|
($jush == "sqlite" && ($use_all_fields || $foreign) ? $all_fields : $fields),
|
2010-04-21 12:01:32 +00:00
|
|
|
$foreign,
|
|
|
|
$_POST["Comment"],
|
|
|
|
($_POST["Engine"] && $_POST["Engine"] != $orig_status["Engine"] ? $_POST["Engine"] : ""),
|
|
|
|
($_POST["Collation"] && $_POST["Collation"] != $orig_status["Collation"] ? $_POST["Collation"] : ""),
|
2010-10-22 22:02:24 +00:00
|
|
|
($_POST["Auto_increment"] != "" ? +$_POST["Auto_increment"] : ""),
|
2010-04-21 12:01:32 +00:00
|
|
|
$partitioning
|
|
|
|
));
|
2007-07-02 05:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-20 12:12:55 +00:00
|
|
|
|
2010-01-09 23:33:41 +00:00
|
|
|
page_header(($TABLE != "" ? lang('Alter table') : lang('Create table')), $error, array("table" => $TABLE), $TABLE);
|
2007-07-02 05:51:26 +00:00
|
|
|
|
2009-07-27 11:31:54 +00:00
|
|
|
$row = array(
|
|
|
|
"Engine" => $_COOKIE["adminer_engine"],
|
2010-12-07 23:25:01 +00:00
|
|
|
"fields" => array(array("field" => "", "type" => (isset($types["int"]) ? "int" : (isset($types["integer"]) ? "integer" : "")))),
|
2009-07-27 11:31:54 +00:00
|
|
|
"partition_names" => array(""),
|
|
|
|
);
|
2007-07-02 05:51:26 +00:00
|
|
|
if ($_POST) {
|
2007-07-09 22:15:11 +00:00
|
|
|
$row = $_POST;
|
2007-07-19 14:33:24 +00:00
|
|
|
if ($row["auto_increment_col"]) {
|
2008-05-30 07:08:11 +00:00
|
|
|
$row["fields"][$row["auto_increment_col"]]["auto_increment"] = true;
|
2007-07-09 06:12:22 +00:00
|
|
|
}
|
2008-05-30 07:08:11 +00:00
|
|
|
process_fields($row["fields"]);
|
2010-01-09 23:33:41 +00:00
|
|
|
} elseif ($TABLE != "") {
|
2009-09-09 19:47:15 +00:00
|
|
|
$row = $orig_status;
|
2009-08-29 13:57:50 +00:00
|
|
|
$row["name"] = $TABLE;
|
2009-08-21 16:54:10 +00:00
|
|
|
$row["fields"] = array();
|
2010-02-09 16:48:40 +00:00
|
|
|
if (!$_GET["auto_increment"]) { // don't prefill by original Auto_increment for the sake of performance and not reusing deleted ids
|
|
|
|
$row["Auto_increment"] = "";
|
|
|
|
}
|
2009-08-21 16:54:10 +00:00
|
|
|
foreach ($orig_fields as $field) {
|
|
|
|
$field["has_default"] = isset($field["default"]);
|
|
|
|
$row["fields"][] = $field;
|
|
|
|
}
|
2010-04-21 12:01:32 +00:00
|
|
|
if (support("partitioning")) {
|
2010-10-13 16:04:40 +00:00
|
|
|
$from = "FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = " . q(DB) . " AND TABLE_NAME = " . q($TABLE);
|
2010-12-16 09:52:59 +00:00
|
|
|
$result = $connection->query("SELECT PARTITION_METHOD, PARTITION_ORDINAL_POSITION, PARTITION_EXPRESSION $from ORDER BY PARTITION_ORDINAL_POSITION DESC LIMIT 1");
|
2008-12-12 14:03:02 +00:00
|
|
|
list($row["partition_by"], $row["partitions"], $row["partition"]) = $result->fetch_row();
|
|
|
|
$row["partition_names"] = array();
|
|
|
|
$row["partition_values"] = array();
|
2010-10-13 15:53:59 +00:00
|
|
|
foreach (get_rows("SELECT PARTITION_NAME, PARTITION_DESCRIPTION $from AND PARTITION_NAME != '' ORDER BY PARTITION_ORDINAL_POSITION") as $row1) {
|
2008-12-12 14:03:02 +00:00
|
|
|
$row["partition_names"][] = $row1["PARTITION_NAME"];
|
|
|
|
$row["partition_values"][] = $row1["PARTITION_DESCRIPTION"];
|
|
|
|
}
|
2009-05-31 14:26:54 +00:00
|
|
|
$row["partition_names"][] = "";
|
2008-12-12 14:03:02 +00:00
|
|
|
}
|
2007-07-09 06:12:22 +00:00
|
|
|
}
|
2007-07-05 14:36:35 +00:00
|
|
|
$collations = collations();
|
2009-03-02 13:38:00 +00:00
|
|
|
|
2009-10-16 12:26:16 +00:00
|
|
|
$engines = engines();
|
2009-09-13 22:17:56 +00:00
|
|
|
// case of engine may differ
|
|
|
|
foreach ($engines as $engine) {
|
|
|
|
if (!strcasecmp($engine, $row["Engine"])) {
|
|
|
|
$row["Engine"] = $engine;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-02 05:51:26 +00:00
|
|
|
?>
|
2007-07-09 14:47:46 +00:00
|
|
|
|
2007-07-05 14:36:35 +00:00
|
|
|
<form action="" method="post" id="form">
|
2007-07-02 05:51:26 +00:00
|
|
|
<p>
|
2013-01-31 05:35:51 +00:00
|
|
|
<?php echo lang('Table name'); ?>: <input name="name" maxlength="64" value="<?php echo h($row["name"]); ?>" autocapitalize="off">
|
2010-12-07 23:42:22 +00:00
|
|
|
<?php if ($TABLE == "" && !$_POST) { ?><script type='text/javascript'>document.getElementById('form')['name'].focus();</script><?php } ?>
|
2009-10-16 12:26:16 +00:00
|
|
|
<?php echo ($engines ? html_select("Engine", array("" => "(" . lang('engine') . ")") + $engines, $row["Engine"]) : ""); ?>
|
2010-05-27 14:10:17 +00:00
|
|
|
<?php echo ($collations && !ereg("sqlite|mssql", $jush) ? html_select("Collation", array("" => "(" . lang('collation') . ")") + $collations, $row["Collation"]) : ""); ?>
|
2009-10-27 15:22:53 +00:00
|
|
|
<input type="submit" value="<?php echo lang('Save'); ?>">
|
2010-01-28 15:17:20 +00:00
|
|
|
<table cellspacing="0" id="edit-fields" class="nowrap">
|
2011-06-07 13:00:18 +00:00
|
|
|
<?php
|
|
|
|
$comments = ($_POST ? $_POST["comments"] : $row["Comment"] != "");
|
|
|
|
if (!$_POST && !$comments) {
|
|
|
|
foreach ($row["fields"] as $field) {
|
|
|
|
if ($field["comment"] != "") {
|
|
|
|
$comments = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-02 01:45:26 +00:00
|
|
|
edit_fields($row["fields"], $collations, "TABLE", $foreign_keys, $comments);
|
2011-06-07 13:00:18 +00:00
|
|
|
?>
|
2007-07-16 23:08:55 +00:00
|
|
|
</table>
|
2007-07-19 14:33:24 +00:00
|
|
|
<p>
|
2013-01-31 05:35:51 +00:00
|
|
|
<?php echo lang('Auto Increment'); ?>: <input type="number" name="Auto_increment" size="6" value="<?php echo h($row["Auto_increment"]); ?>">
|
2012-09-22 19:24:16 +00:00
|
|
|
<label class="jsonly"><input type="checkbox" id="defaults" name="defaults" value="1" checked onclick="columnShow(this.checked, 5);"><?php echo lang('Default values'); ?></label>
|
|
|
|
<?php if (!$_POST["defaults"]) { ?><script type="text/javascript">editingHideDefaults()</script><?php } ?>
|
2013-01-31 05:35:51 +00:00
|
|
|
<?php echo (support("comment") ? checkbox("comments", 1, $comments, lang('Comment'), "columnShow(this.checked, 6); toggle('Comment'); if (this.checked) this.form['Comment'].focus();", true) . ' <input name="Comment" id="Comment" value="' . h($row["Comment"]) . '" maxlength="' . ($connection->server_info >= 5.5 ? 2048 : 60) . '"' . ($comments ? '' : ' class="hidden"') . '>' : ''); ?>
|
2007-07-02 05:51:26 +00:00
|
|
|
<p>
|
2009-07-11 20:30:40 +00:00
|
|
|
<input type="submit" value="<?php echo lang('Save'); ?>">
|
2011-03-25 22:59:27 +00:00
|
|
|
<?php if ($_GET["create"] != "") { ?><input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"<?php echo confirm(); ?>><?php } ?>
|
2009-04-29 11:07:27 +00:00
|
|
|
<?php
|
2010-04-21 12:01:32 +00:00
|
|
|
if (support("partitioning")) {
|
2009-04-29 11:07:27 +00:00
|
|
|
$partition_table = ereg('RANGE|LIST', $row["partition_by"]);
|
2010-02-24 10:39:44 +00:00
|
|
|
print_fieldset("partition", lang('Partition by'), $row["partition_by"]);
|
2009-04-29 11:07:27 +00:00
|
|
|
?>
|
2008-12-12 14:03:02 +00:00
|
|
|
<p>
|
2009-12-17 13:22:28 +00:00
|
|
|
<?php echo html_select("partition_by", array(-1 => "") + $partition_by, $row["partition_by"], "partitionByChange(this);"); ?>
|
2009-07-28 16:20:50 +00:00
|
|
|
(<input name="partition" value="<?php echo h($row["partition"]); ?>">)
|
2012-12-05 21:11:36 +00:00
|
|
|
<?php echo lang('Partitions'); ?>: <input type="number" name="partitions" class="size" value="<?php echo h($row["partitions"]); ?>"<?php echo ($partition_table || !$row["partition_by"] ? " class='hidden'" : ""); ?>>
|
2009-05-01 10:41:33 +00:00
|
|
|
<table cellspacing="0" id="partition-table"<?php echo ($partition_table ? "" : " class='hidden'"); ?>>
|
2009-07-11 20:30:40 +00:00
|
|
|
<thead><tr><th><?php echo lang('Partition name'); ?><th><?php echo lang('Values'); ?></thead>
|
2008-12-12 14:03:02 +00:00
|
|
|
<?php
|
2010-05-20 20:06:34 +00:00
|
|
|
foreach ($row["partition_names"] as $key => $val) {
|
|
|
|
echo '<tr>';
|
2013-01-31 05:35:51 +00:00
|
|
|
echo '<td><input name="partition_names[]" value="' . h($val) . '"' . ($key == count($row["partition_names"]) - 1 ? ' onchange="partitionNameChange(this);"' : '') . ' autocapitalize="off">';
|
2010-05-20 20:06:34 +00:00
|
|
|
echo '<td><input name="partition_values[]" value="' . h($row["partition_values"][$key]) . '">';
|
|
|
|
}
|
|
|
|
?>
|
2008-12-12 14:03:02 +00:00
|
|
|
</table>
|
2010-02-24 10:39:44 +00:00
|
|
|
</div></fieldset>
|
2010-05-21 13:42:17 +00:00
|
|
|
<?php
|
|
|
|
}
|
|
|
|
?>
|
2013-04-02 01:45:26 +00:00
|
|
|
<input type="hidden" name="token" value="<?php echo $token; ?>">
|
2007-07-02 05:51:26 +00:00
|
|
|
</form>
|