From c38655418bf69ebedd5d00f3de1709077c97effc Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Wed, 8 May 2013 11:43:53 -0700 Subject: [PATCH] Simplify process_fields() --- adminer/create.inc.php | 64 ++++++++++++++++----------------- adminer/include/editing.inc.php | 13 +++---- adminer/procedure.inc.php | 29 +++++++-------- 3 files changed, 51 insertions(+), 55 deletions(-) diff --git a/adminer/create.inc.php b/adminer/create.inc.php index 67472e80..208396b8 100644 --- a/adminer/create.inc.php +++ b/adminer/create.inc.php @@ -17,8 +17,11 @@ if ($TABLE != "") { $row = $_POST; $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"]) { query_redirect("DROP TABLE " . table($TABLE), substr(ME, 0, -1), lang('Table has been dropped.')); } 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); -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 { +if (!$_POST) { $row = array( "Engine" => $_COOKIE["adminer_engine"], "fields" => array(array("field" => "", "type" => (isset($types["int"]) ? "int" : (isset($types["integer"]) ? "integer" : "")))), "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(); diff --git a/adminer/include/editing.inc.php b/adminer/include/editing.inc.php index ef312a52..11698251 100644 --- a/adminer/include/editing.inc.php +++ b/adminer/include/editing.inc.php @@ -260,7 +260,7 @@ function edit_fields($fields, $collations, $type = "TABLE", $foreign_keys = arra /** Move fields up and down or add field * @param array -* @return null +* @return bool */ function process_fields(&$fields) { ksort($fields); @@ -278,8 +278,7 @@ function process_fields(&$fields) { } $offset++; } - } - if ($_POST["down"]) { + } elseif ($_POST["down"]) { $found = false; foreach ($fields as $key => $field) { if (isset($field["field"]) && $found) { @@ -292,11 +291,13 @@ function process_fields(&$fields) { } $offset++; } - } - $fields = array_values($fields); - if ($_POST["add"]) { + } elseif ($_POST["add"]) { + $fields = array_values($fields); array_splice($fields, key($_POST["add"]), 0, array(array())); + } elseif (!$_POST["drop_col"]) { + return false; } + return true; } /** Callback used in routine() diff --git a/adminer/procedure.inc.php b/adminer/procedure.inc.php index 9e1d0868..c3f56e30 100644 --- a/adminer/procedure.inc.php +++ b/adminer/procedure.inc.php @@ -4,22 +4,19 @@ $routine = (isset($_GET["function"]) ? "FUNCTION" : "PROCEDURE"); $row = $_POST; $row["fields"] = (array) $row["fields"]; -if ($_POST) { - if (!$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"] && !$_POST["down"]) { - $temp_name = "$row[name]_adminer_" . uniqid(); - drop_create( - "DROP $routine " . idf_escape($PROCEDURE), - create_routine($routine, $row), - create_routine($routine, array("name" => $temp_name) + $row), - "DROP $routine " . idf_escape($temp_name), - substr(ME, 0, -1), - lang('Routine has been dropped.'), - lang('Routine has been altered.'), - lang('Routine has been created.'), - $PROCEDURE - ); - } - process_fields($row["fields"]); +if ($_POST && !process_fields($row["fields"]) && !$error) { + $temp_name = "$row[name]_adminer_" . uniqid(); + drop_create( + "DROP $routine " . idf_escape($PROCEDURE), + create_routine($routine, $row), + create_routine($routine, array("name" => $temp_name) + $row), + "DROP $routine " . idf_escape($temp_name), + substr(ME, 0, -1), + lang('Routine has been dropped.'), + lang('Routine has been altered.'), + lang('Routine has been created.'), + $PROCEDURE + ); } page_header(($PROCEDURE != "" ? (isset($_GET["function"]) ? lang('Alter function') : lang('Alter procedure')) . ": " . h($PROCEDURE) : (isset($_GET["function"]) ? lang('Create function') : lang('Create procedure'))), $error);