2007-07-02 05:51:26 +00:00
|
|
|
<?php
|
2025-03-05 11:28:53 +01:00
|
|
|
namespace Adminer;
|
|
|
|
|
|
2009-08-29 13:57:50 +00:00
|
|
|
$TABLE = $_GET["create"];
|
2013-07-19 12:37:12 -07:00
|
|
|
$partition_by = array();
|
|
|
|
|
foreach (array('HASH', 'LINEAR HASH', 'KEY', 'LINEAR KEY', 'RANGE', 'LIST') as $key) {
|
|
|
|
|
$partition_by[$key] = $key;
|
|
|
|
|
}
|
2008-12-12 14:03:02 +00:00
|
|
|
|
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 22:06:34 +02: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();
|
2013-06-24 09:08:26 -07:00
|
|
|
$table_status = array();
|
2010-01-09 23:33:41 +00:00
|
|
|
if ($TABLE != "") {
|
2009-08-29 13:57:50 +00:00
|
|
|
$orig_fields = fields($TABLE);
|
2025-03-26 01:34:48 +01:00
|
|
|
$table_status = table_status1($TABLE);
|
|
|
|
|
if (count($table_status) < 2) { // there's only the Name field
|
2013-06-24 09:08:26 -07:00
|
|
|
$error = lang('No tables.');
|
|
|
|
|
}
|
2008-07-24 12:03:28 +00:00
|
|
|
}
|
2013-05-08 08:43:15 -07:00
|
|
|
|
|
|
|
|
$row = $_POST;
|
|
|
|
|
$row["fields"] = (array) $row["fields"];
|
2013-05-08 11:43:53 -07:00
|
|
|
if ($row["auto_increment_col"]) {
|
|
|
|
|
$row["fields"][$row["auto_increment_col"]]["auto_increment"] = true;
|
|
|
|
|
}
|
2008-07-24 12:03:28 +00:00
|
|
|
|
2019-01-22 13:13:27 +01:00
|
|
|
if ($_POST) {
|
2025-03-12 22:32:49 +01:00
|
|
|
save_settings(array("comments" => $_POST["comments"], "defaults" => $_POST["defaults"]));
|
2019-01-22 13:13:27 +01:00
|
|
|
}
|
|
|
|
|
|
2013-05-08 11:43:53 -07:00
|
|
|
if ($_POST && !process_fields($row["fields"]) && !$error) {
|
2010-03-01 22:59:08 +00:00
|
|
|
if ($_POST["drop"]) {
|
2013-07-03 12:32:52 -07:00
|
|
|
queries_redirect(substr(ME, 0, -1), lang('Table has been dropped.'), drop_tables(array($TABLE)));
|
2010-03-01 22:59:08 +00:00
|
|
|
} else {
|
2010-04-21 12:01:32 +00:00
|
|
|
$fields = array();
|
2012-07-15 14:38:45 -07: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
|
|
|
$orig_field = reset($orig_fields);
|
2012-07-15 14:38:45 -07:00
|
|
|
$after = " FIRST";
|
2013-07-24 16:26:41 -07:00
|
|
|
|
2013-05-08 08:43:15 -07:00
|
|
|
foreach ($row["fields"] as $key => $field) {
|
2010-05-21 15:07:59 +02:00
|
|
|
$foreign_key = $foreign_keys[$field["type"]];
|
2012-05-13 23:54:07 -07: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"] != "") {
|
2025-03-07 05:01:00 +01:00
|
|
|
if (!$field["generated"]) {
|
2010-05-21 15:07:59 +02:00
|
|
|
$field["default"] = null;
|
|
|
|
|
}
|
|
|
|
|
$process_field = process_field($field, $type_field);
|
2012-07-15 14:38:45 -07:00
|
|
|
$all_fields[] = array($field["orig"], $process_field, $after);
|
2025-02-20 10:45:03 +01:00
|
|
|
if (!$orig_field || $process_field !== process_field($orig_field, $orig_field)) {
|
2010-05-21 15:07:59 +02:00
|
|
|
$fields[] = array($field["orig"], $process_field, $after);
|
2012-07-15 14:38:45 -07:00
|
|
|
if ($field["orig"] != "" || $after) {
|
|
|
|
|
$use_all_fields = true;
|
|
|
|
|
}
|
2010-05-21 15:07:59 +02:00
|
|
|
}
|
2012-05-13 23:54:07 -07:00
|
|
|
if ($foreign_key !== null) {
|
2025-03-06 17:51:20 +01:00
|
|
|
$foreign[idf_escape($field["field"])] = ($TABLE != "" && JUSH != "sqlite" ? "ADD" : " ") . format_foreign_key(array(
|
2013-08-09 15:16:15 -07:00
|
|
|
'table' => $foreign_keys[$field["type"]],
|
|
|
|
|
'source' => array($field["field"]),
|
|
|
|
|
'target' => array($type_field["field"]),
|
|
|
|
|
'on_delete' => $field["on_delete"],
|
|
|
|
|
));
|
2010-03-01 22:59:08 +00:00
|
|
|
}
|
2012-07-15 14:38:45 -07:00
|
|
|
$after = " AFTER " . idf_escape($field["field"]);
|
2010-03-01 22:59:08 +00:00
|
|
|
} elseif ($field["orig"] != "") {
|
2012-07-15 14:38:45 -07: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 14:38:45 -07:00
|
|
|
if (!$orig_field) {
|
|
|
|
|
$after = "";
|
|
|
|
|
}
|
2010-03-01 22:59:08 +00:00
|
|
|
}
|
2008-12-12 14:03:02 +00:00
|
|
|
}
|
2013-07-24 16:26:41 -07:00
|
|
|
|
2010-04-21 12:01:32 +00:00
|
|
|
$partitioning = "";
|
2024-09-22 00:33:55 +02:00
|
|
|
if (support("partitioning")) {
|
|
|
|
|
if (isset($partition_by[$row["partition_by"]])) {
|
2025-03-11 21:43:59 +01:00
|
|
|
$params = array();
|
|
|
|
|
foreach ($row as $key => $val) {
|
|
|
|
|
if (preg_match('~^partition~', $key)) {
|
|
|
|
|
$params[$key] = $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-22 00:33:55 +02:00
|
|
|
foreach ($params["partition_names"] as $key => $name) {
|
2025-02-20 08:35:42 +01:00
|
|
|
if ($name == "") {
|
2024-09-22 00:33:55 +02:00
|
|
|
unset($params["partition_names"][$key]);
|
|
|
|
|
unset($params["partition_values"][$key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($params != get_partitions_info($TABLE)) {
|
|
|
|
|
$partitions = array();
|
|
|
|
|
if ($params["partition_by"] == 'RANGE' || $params["partition_by"] == 'LIST') {
|
|
|
|
|
foreach ($params["partition_names"] as $key => $name) {
|
|
|
|
|
$value = $params["partition_values"][$key];
|
|
|
|
|
$partitions[] = "\n PARTITION " . idf_escape($name) . " VALUES " . ($params["partition_by"] == 'RANGE' ? "LESS THAN" : "IN") . ($value != "" ? " ($value)" : " MAXVALUE"); //! SQL injection
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// $params["partition"] can be expression, not only column
|
2025-02-20 08:35:42 +01:00
|
|
|
$partitioning .= "\nPARTITION BY $params[partition_by]($params[partition])";
|
2024-09-22 00:33:55 +02:00
|
|
|
if ($partitions) {
|
|
|
|
|
$partitioning .= " (" . implode(",", $partitions) . "\n)";
|
|
|
|
|
} elseif ($params["partitions"]) {
|
2025-02-20 08:35:42 +01:00
|
|
|
$partitioning .= " PARTITIONS " . (+$params["partitions"]);
|
2024-09-22 00:33:55 +02:00
|
|
|
}
|
2010-03-01 22:59:08 +00:00
|
|
|
}
|
2024-09-22 00:33:55 +02:00
|
|
|
} elseif (preg_match("~partitioned~", $table_status["Create_options"])) {
|
|
|
|
|
$partitioning .= "\nREMOVE PARTITIONING";
|
2009-07-23 16:31:28 +00:00
|
|
|
}
|
2010-03-01 22:59:08 +00:00
|
|
|
}
|
2013-07-24 16:26:41 -07:00
|
|
|
|
2010-04-21 12:01:32 +00:00
|
|
|
$message = lang('Table has been altered.');
|
|
|
|
|
if ($TABLE == "") {
|
2013-05-08 08:43:15 -07:00
|
|
|
cookie("adminer_engine", $row["Engine"]);
|
2010-04-21 12:01:32 +00:00
|
|
|
$message = lang('Table has been created.');
|
2007-08-04 19:38:01 +00:00
|
|
|
}
|
2013-05-08 08:43:15 -07:00
|
|
|
$name = trim($row["name"]);
|
2013-07-24 16:26:41 -07:00
|
|
|
|
2013-07-06 10:31:21 -07:00
|
|
|
queries_redirect(ME . (support("table") ? "table=" : "select=") . urlencode($name), $message, alter_table(
|
2010-04-21 12:01:32 +00:00
|
|
|
$TABLE,
|
2011-09-10 13:06:59 +02:00
|
|
|
$name,
|
2025-03-06 17:51:20 +01:00
|
|
|
(JUSH == "sqlite" && ($use_all_fields || $foreign) ? $all_fields : $fields),
|
2010-04-21 12:01:32 +00:00
|
|
|
$foreign,
|
2014-11-18 09:50:02 -08:00
|
|
|
($row["Comment"] != $table_status["Comment"] ? $row["Comment"] : null),
|
2013-06-24 09:08:26 -07:00
|
|
|
($row["Engine"] && $row["Engine"] != $table_status["Engine"] ? $row["Engine"] : ""),
|
|
|
|
|
($row["Collation"] && $row["Collation"] != $table_status["Collation"] ? $row["Collation"] : ""),
|
2014-11-24 18:17:43 -08:00
|
|
|
($row["Auto_increment"] != "" ? number($row["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
|
|
|
|
2013-07-05 01:31:53 -07:00
|
|
|
page_header(($TABLE != "" ? lang('Alter table') : lang('Create table')), $error, array("table" => $TABLE), h($TABLE));
|
2007-07-02 05:51:26 +00:00
|
|
|
|
2013-05-08 11:43:53 -07:00
|
|
|
if (!$_POST) {
|
2025-03-06 12:35:20 +01:00
|
|
|
$types = $driver->types();
|
2013-05-08 08:43:15 -07:00
|
|
|
$row = array(
|
|
|
|
|
"Engine" => $_COOKIE["adminer_engine"],
|
2018-02-01 13:55:11 +01:00
|
|
|
"fields" => array(array("field" => "", "type" => (isset($types["int"]) ? "int" : (isset($types["integer"]) ? "integer" : "")), "on_update" => "")),
|
2013-05-08 08:43:15 -07:00
|
|
|
"partition_names" => array(""),
|
|
|
|
|
);
|
2013-07-24 16:26:41 -07:00
|
|
|
|
2013-05-08 11:43:53 -07:00
|
|
|
if ($TABLE != "") {
|
2013-06-24 09:08:26 -07:00
|
|
|
$row = $table_status;
|
2013-05-08 11:43:53 -07:00
|
|
|
$row["name"] = $TABLE;
|
|
|
|
|
$row["fields"] = array();
|
|
|
|
|
if (!$_GET["auto_increment"]) { // don't prefill by original Auto_increment for the sake of performance and not reusing deleted ids
|
|
|
|
|
$row["Auto_increment"] = "";
|
|
|
|
|
}
|
|
|
|
|
foreach ($orig_fields as $field) {
|
2025-03-07 05:01:00 +01:00
|
|
|
$field["generated"] = $field["generated"] ?: (isset($field["default"]) ? "DEFAULT" : "");
|
2013-05-08 11:43:53 -07:00
|
|
|
$row["fields"][] = $field;
|
|
|
|
|
}
|
2013-07-24 16:26:41 -07:00
|
|
|
|
2013-05-08 11:43:53 -07:00
|
|
|
if (support("partitioning")) {
|
2024-09-22 00:33:55 +02:00
|
|
|
$row += get_partitions_info($TABLE);
|
|
|
|
|
$row["partition_names"][] = "";
|
|
|
|
|
$row["partition_values"][] = "";
|
2013-05-08 11:43:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
2007-07-09 06:12:22 +00:00
|
|
|
}
|
2009-03-02 13:38:00 +00:00
|
|
|
|
2013-05-01 18:28:04 -07:00
|
|
|
$collations = collations();
|
2025-03-25 21:58:43 +01:00
|
|
|
if (is_array(reset($collations))) {
|
|
|
|
|
$collations = call_user_func_array('array_merge', array_values($collations));
|
|
|
|
|
}
|
2025-03-18 13:41:24 +01:00
|
|
|
$engines = $driver->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>
|
2025-03-16 23:54:25 +01:00
|
|
|
<?php
|
|
|
|
|
if (support("columns") || $TABLE == "") {
|
2025-03-18 12:57:29 +01:00
|
|
|
echo lang('Table name') . ": <input name='name'" . ($TABLE == "" && !$_POST ? " autofocus" : "") . " data-maxlength='64' value='" . h($row["name"]) . "' autocapitalize='off'>\n";
|
2025-03-21 23:41:46 +01:00
|
|
|
echo ($engines ? html_select("Engine", array("" => "(" . lang('engine') . ")") + $engines, $row["Engine"]) . on_help("event.target.value", 1) . script("qsl('select').onchange = helpClose;") . "\n" : "");
|
2025-03-10 11:53:34 +01:00
|
|
|
if ($collations) {
|
2025-03-25 21:58:43 +01:00
|
|
|
echo "<datalist id='collations'>" . optionlist($collations) . "</datalist>\n";
|
2025-03-10 11:53:34 +01:00
|
|
|
echo (preg_match("~sqlite|mssql~", JUSH) ? "" : "<input list='collations' name='Collation' value='" . h($row["Collation"]) . "' placeholder='(" . lang('collation') . ")'>");
|
|
|
|
|
}
|
2025-03-17 14:26:56 +01:00
|
|
|
echo "<input type='submit' value='" . lang('Save') . "'>\n";
|
2025-03-16 23:54:25 +01:00
|
|
|
}
|
2013-07-06 10:31:21 -07:00
|
|
|
|
2025-03-16 23:54:25 +01:00
|
|
|
if (support("columns")) {
|
|
|
|
|
echo "<div class='scrollable'>\n";
|
|
|
|
|
echo "<table id='edit-fields' class='nowrap'>\n";
|
2025-03-05 09:14:49 +01:00
|
|
|
edit_fields($row["fields"], $collations, "TABLE", $foreign_keys);
|
2025-03-16 23:54:25 +01:00
|
|
|
echo "</table>\n";
|
|
|
|
|
echo script("editFields();");
|
|
|
|
|
echo "</div>\n<p>\n";
|
2025-03-17 14:26:56 +01:00
|
|
|
echo lang('Auto Increment') . ": <input type='number' name='Auto_increment' class='size' value='" . h($row["Auto_increment"]) . "'>\n";
|
2025-03-16 23:54:25 +01:00
|
|
|
echo checkbox("defaults", 1, ($_POST ? $_POST["defaults"] : get_setting("defaults")), lang('Default values'), "columnShow(this.checked, 5)", "jsonly");
|
2025-03-12 22:32:49 +01:00
|
|
|
$comments = ($_POST ? $_POST["comments"] : get_setting("comments"));
|
2025-03-05 09:14:49 +01:00
|
|
|
echo (support("comment")
|
|
|
|
|
? checkbox("comments", 1, $comments, lang('Comment'), "editingCommentsClick(this, true);", "jsonly")
|
|
|
|
|
. ' ' . (preg_match('~\n~', $row["Comment"])
|
|
|
|
|
? "<textarea name='Comment' rows='2' cols='20'" . ($comments ? "" : " class='hidden'") . ">" . h($row["Comment"]) . "</textarea>"
|
|
|
|
|
: '<input name="Comment" value="' . h($row["Comment"]) . '" data-maxlength="' . (min_version(5.5) ? 2048 : 60) . '"' . ($comments ? "" : " class='hidden'") . '>'
|
|
|
|
|
)
|
|
|
|
|
: '')
|
|
|
|
|
;
|
|
|
|
|
?>
|
2007-07-02 05:51:26 +00:00
|
|
|
<p>
|
2013-07-10 13:13:36 -07:00
|
|
|
<input type="submit" value="<?php echo lang('Save'); ?>">
|
2013-07-06 10:31:21 -07:00
|
|
|
<?php } ?>
|
|
|
|
|
|
2025-03-05 10:01:26 +01:00
|
|
|
<?php if ($TABLE != "") { ?>
|
|
|
|
|
<input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"><?php echo confirm(lang('Drop %s?', $TABLE)); ?>
|
|
|
|
|
<?php } ?>
|
2009-04-29 11:07:27 +00:00
|
|
|
<?php
|
2010-04-21 12:01:32 +00:00
|
|
|
if (support("partitioning")) {
|
2013-07-24 16:26:41 -07:00
|
|
|
$partition_table = preg_match('~RANGE|LIST~', $row["partition_by"]);
|
2010-02-24 10:39:44 +00:00
|
|
|
print_fieldset("partition", lang('Partition by'), $row["partition_by"]);
|
2025-03-21 23:41:46 +01:00
|
|
|
echo "<p>" . html_select("partition_by", array("" => "") + $partition_by, $row["partition_by"]) . on_help("event.target.value.replace(/./, 'PARTITION BY \$&')", 1) . script("qsl('select').onchange = partitionByChange;");
|
2025-03-16 23:54:25 +01:00
|
|
|
echo "(<input name='partition' value='" . h($row["partition"]) . "'>)\n";
|
|
|
|
|
echo lang('Partitions') . ": <input type='number' name='partitions' class='size" . ($partition_table || !$row["partition_by"] ? " hidden" : "") . "' value='" . h($row["partitions"]) . "'>\n";
|
|
|
|
|
echo "<table id='partition-table'" . ($partition_table ? "" : " class='hidden'") . ">\n";
|
|
|
|
|
echo "<thead><tr><th>" . lang('Partition name') . "<th>" . lang('Values') . "</thead>\n";
|
2025-03-05 09:14:49 +01:00
|
|
|
foreach ($row["partition_names"] as $key => $val) {
|
|
|
|
|
echo '<tr>';
|
|
|
|
|
echo '<td><input name="partition_names[]" value="' . h($val) . '" autocapitalize="off">';
|
|
|
|
|
echo ($key == count($row["partition_names"]) - 1 ? script("qsl('input').oninput = partitionNameChange;") : '');
|
|
|
|
|
echo '<td><input name="partition_values[]" value="' . h($row["partition_values"][$key]) . '">';
|
|
|
|
|
}
|
2025-03-16 23:54:25 +01:00
|
|
|
echo "</table>\n</div></fieldset>\n";
|
2010-05-21 15:42:17 +02:00
|
|
|
}
|
2025-03-17 18:45:08 +01:00
|
|
|
echo input_token();
|
2010-05-21 15:42:17 +02:00
|
|
|
?>
|
2007-07-02 05:51:26 +00:00
|
|
|
</form>
|