Simplify process_fields()

This commit is contained in:
Jakub Vrana 2013-05-08 11:43:53 -07:00
parent 046da00eb6
commit c38655418b
3 changed files with 51 additions and 55 deletions

View file

@ -17,8 +17,11 @@ if ($TABLE != "") {
$row = $_POST; $row = $_POST;
$row["fields"] = (array) $row["fields"]; $row["fields"] = (array) $row["fields"];
if ($row["auto_increment_col"]) {
$row["fields"][$row["auto_increment_col"]]["auto_increment"] = true;
}
if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"] && !$_POST["down"]) { if ($_POST && !process_fields($row["fields"]) && !$error) {
if ($_POST["drop"]) { if ($_POST["drop"]) {
query_redirect("DROP TABLE " . table($TABLE), substr(ME, 0, -1), lang('Table has been dropped.')); query_redirect("DROP TABLE " . table($TABLE), substr(ME, 0, -1), lang('Table has been dropped.'));
} else { } else {
@ -104,43 +107,38 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"]
page_header(($TABLE != "" ? lang('Alter table') : lang('Create table')), $error, array("table" => $TABLE), $TABLE); page_header(($TABLE != "" ? lang('Alter table') : lang('Create table')), $error, array("table" => $TABLE), $TABLE);
if ($_POST) { if (!$_POST) {
if ($row["auto_increment_col"]) {
$row["fields"][$row["auto_increment_col"]]["auto_increment"] = true;
}
process_fields($row["fields"]);
} elseif ($TABLE != "") {
$row = $orig_status;
$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) {
$field["has_default"] = isset($field["default"]);
$row["fields"][] = $field;
}
if (support("partitioning")) {
$from = "FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = " . q(DB) . " AND TABLE_NAME = " . q($TABLE);
$result = $connection->query("SELECT PARTITION_METHOD, PARTITION_ORDINAL_POSITION, PARTITION_EXPRESSION $from ORDER BY PARTITION_ORDINAL_POSITION DESC LIMIT 1");
list($row["partition_by"], $row["partitions"], $row["partition"]) = $result->fetch_row();
$row["partition_names"] = array();
$row["partition_values"] = array();
foreach (get_rows("SELECT PARTITION_NAME, PARTITION_DESCRIPTION $from AND PARTITION_NAME != '' ORDER BY PARTITION_ORDINAL_POSITION") as $row1) {
$row["partition_names"][] = $row1["PARTITION_NAME"];
$row["partition_values"][] = $row1["PARTITION_DESCRIPTION"];
}
$row["partition_names"][] = "";
}
} else {
$row = array( $row = array(
"Engine" => $_COOKIE["adminer_engine"], "Engine" => $_COOKIE["adminer_engine"],
"fields" => array(array("field" => "", "type" => (isset($types["int"]) ? "int" : (isset($types["integer"]) ? "integer" : "")))), "fields" => array(array("field" => "", "type" => (isset($types["int"]) ? "int" : (isset($types["integer"]) ? "integer" : "")))),
"partition_names" => array(""), "partition_names" => array(""),
); );
if ($TABLE != "") {
$row = $orig_status;
$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) {
$field["has_default"] = isset($field["default"]);
$row["fields"][] = $field;
}
if (support("partitioning")) {
$from = "FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = " . q(DB) . " AND TABLE_NAME = " . q($TABLE);
$result = $connection->query("SELECT PARTITION_METHOD, PARTITION_ORDINAL_POSITION, PARTITION_EXPRESSION $from ORDER BY PARTITION_ORDINAL_POSITION DESC LIMIT 1");
list($row["partition_by"], $row["partitions"], $row["partition"]) = $result->fetch_row();
$row["partition_names"] = array();
$row["partition_values"] = array();
foreach (get_rows("SELECT PARTITION_NAME, PARTITION_DESCRIPTION $from AND PARTITION_NAME != '' ORDER BY PARTITION_ORDINAL_POSITION") as $row1) {
$row["partition_names"][] = $row1["PARTITION_NAME"];
$row["partition_values"][] = $row1["PARTITION_DESCRIPTION"];
}
$row["partition_names"][] = "";
}
}
} }
$collations = collations(); $collations = collations();

View file

@ -260,7 +260,7 @@ function edit_fields($fields, $collations, $type = "TABLE", $foreign_keys = arra
/** Move fields up and down or add field /** Move fields up and down or add field
* @param array * @param array
* @return null * @return bool
*/ */
function process_fields(&$fields) { function process_fields(&$fields) {
ksort($fields); ksort($fields);
@ -278,8 +278,7 @@ function process_fields(&$fields) {
} }
$offset++; $offset++;
} }
} } elseif ($_POST["down"]) {
if ($_POST["down"]) {
$found = false; $found = false;
foreach ($fields as $key => $field) { foreach ($fields as $key => $field) {
if (isset($field["field"]) && $found) { if (isset($field["field"]) && $found) {
@ -292,11 +291,13 @@ function process_fields(&$fields) {
} }
$offset++; $offset++;
} }
} } elseif ($_POST["add"]) {
$fields = array_values($fields); $fields = array_values($fields);
if ($_POST["add"]) {
array_splice($fields, key($_POST["add"]), 0, array(array())); array_splice($fields, key($_POST["add"]), 0, array(array()));
} elseif (!$_POST["drop_col"]) {
return false;
} }
return true;
} }
/** Callback used in routine() /** Callback used in routine()

View file

@ -4,22 +4,19 @@ $routine = (isset($_GET["function"]) ? "FUNCTION" : "PROCEDURE");
$row = $_POST; $row = $_POST;
$row["fields"] = (array) $row["fields"]; $row["fields"] = (array) $row["fields"];
if ($_POST) { if ($_POST && !process_fields($row["fields"]) && !$error) {
if (!$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"] && !$_POST["down"]) { $temp_name = "$row[name]_adminer_" . uniqid();
$temp_name = "$row[name]_adminer_" . uniqid(); drop_create(
drop_create( "DROP $routine " . idf_escape($PROCEDURE),
"DROP $routine " . idf_escape($PROCEDURE), create_routine($routine, $row),
create_routine($routine, $row), create_routine($routine, array("name" => $temp_name) + $row),
create_routine($routine, array("name" => $temp_name) + $row), "DROP $routine " . idf_escape($temp_name),
"DROP $routine " . idf_escape($temp_name), substr(ME, 0, -1),
substr(ME, 0, -1), lang('Routine has been dropped.'),
lang('Routine has been dropped.'), lang('Routine has been altered.'),
lang('Routine has been altered.'), lang('Routine has been created.'),
lang('Routine has been created.'), $PROCEDURE
$PROCEDURE );
);
}
process_fields($row["fields"]);
} }
page_header(($PROCEDURE != "" ? (isset($_GET["function"]) ? lang('Alter function') : lang('Alter procedure')) . ": " . h($PROCEDURE) : (isset($_GET["function"]) ? lang('Create function') : lang('Create procedure'))), $error); page_header(($PROCEDURE != "" ? (isset($_GET["function"]) ? lang('Alter function') : lang('Alter procedure')) . ": " . h($PROCEDURE) : (isset($_GET["function"]) ? lang('Create function') : lang('Create procedure'))), $error);