From ced9de9d2993bee03a35a8ff233a52e5a3407f0f Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Thu, 2 Jul 2009 22:37:10 +0000 Subject: [PATCH] Create single column foreign key in table structure git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@778 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- adminer/create.inc.php | 18 +++++++++--- adminer/event.inc.php | 5 ++-- adminer/functions.js | 46 ++++++++++++++++++++++++++++++- adminer/include/editing.inc.php | 32 ++++++++++++++++----- adminer/include/functions.inc.php | 4 +-- adminer/include/version.inc.php | 2 +- changes.txt | 3 ++ todo.txt | 1 - 8 files changed, 92 insertions(+), 19 deletions(-) diff --git a/adminer/create.inc.php b/adminer/create.inc.php index 62fffc45..2c727112 100644 --- a/adminer/create.inc.php +++ b/adminer/create.inc.php @@ -1,6 +1,12 @@ $field) { + $foreign_keys[idf_escape($table_name) . "." . idf_escape($field["field"])] = $table_name; +} + if (strlen($_GET["create"])) { $orig_fields = fields($_GET["create"]); } @@ -28,9 +34,10 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"] ksort($_POST["fields"]); $after = "FIRST"; foreach ($_POST["fields"] as $key => $field) { - if (strlen($field["field"]) && isset($types[$field["type"]])) { - $fields[] = "\n" . (!strlen($_GET["create"]) ? " " : (strlen($field["orig"]) ? "CHANGE " . idf_escape($field["orig"]) . " " : "ADD ")) - . idf_escape($field["field"]) . process_type($field) + $type_field = (isset($types[$field["type"]]) ? $field : $referencable_primary[$foreign_keys[$field["type"]]]); + if (strlen($field["field"]) && $type_field) { + $fields[] = "\n" . (strlen($_GET["create"]) ? (strlen($field["orig"]) ? "CHANGE " . idf_escape($field["orig"]) . " " : "ADD ") : " ") + . idf_escape($field["field"]) . process_type($type_field) . ($field["null"] ? " NULL" : " NOT NULL") // NULL for timestamp . (strlen($_GET["create"]) && strlen($field["orig"]) && isset($orig_fields[$field["orig"]]["default"]) && $field["type"] != "timestamp" ? " DEFAULT " . $dbh->quote($orig_fields[$field["orig"]]["default"]) : "") //! timestamp . ($key == $_POST["auto_increment_col"] ? " AUTO_INCREMENT$auto_increment_index" : "") @@ -38,6 +45,9 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"] . (strlen($_GET["create"]) ? " $after" : "") ; $after = "AFTER " . idf_escape($field["field"]); + if (!isset($types[$field["type"]])) { + $fields[] = (strlen($_GET["create"]) ? " ADD" : "") . " FOREIGN KEY (" . idf_escape($field["field"]) . ") REFERENCES " . idf_escape($foreign_keys[$field["type"]]) . " (" . idf_escape($type_field["field"]) . ")"; + } } elseif (strlen($field["orig"])) { $fields[] = "\nDROP " . idf_escape($field["orig"]); } @@ -129,7 +139,7 @@ if ($suhosin && count($row["fields"]) > $suhosin) {

- +

: " /> diff --git a/adminer/event.inc.php b/adminer/event.inc.php index 25caf3ff..21eec46f 100644 --- a/adminer/event.inc.php +++ b/adminer/event.inc.php @@ -5,7 +5,7 @@ $statuses = array("ENABLED" => "ENABLE", "DISABLED" => "DISABLE", "SLAVESIDE_DIS if ($_POST && !$error) { if ($_POST["drop"]) { query_redirect("DROP EVENT " . idf_escape($_GET["event"]), substr($SELF, 0, -1), lang('Event has been dropped.')); - } elseif (in_array($_POST["INTERVAL_FIELD"], $intervals) && in_array($_POST["STATUS"], $statuses)) { + } elseif (in_array($_POST["INTERVAL_FIELD"], $intervals) && isset($statuses[$_POST["STATUS"]])) { $schedule = "\nON SCHEDULE " . ($_POST["INTERVAL_VALUE"] ? "EVERY " . $dbh->quote($_POST["INTERVAL_VALUE"]) . " $_POST[INTERVAL_FIELD]" . ($_POST["STARTS"] ? " STARTS " . $dbh->quote($_POST["STARTS"]) : "") @@ -17,7 +17,7 @@ if ($_POST && !$error) { ? "ALTER EVENT " . idf_escape($_GET["event"]) . $schedule . ($_GET["event"] != $_POST["EVENT_NAME"] ? "\nRENAME TO " . idf_escape($_POST["EVENT_NAME"]) : "") : "CREATE EVENT " . idf_escape($_POST["EVENT_NAME"]) . $schedule - ) . "\n$_POST[STATUS] COMMENT " . $dbh->quote($_POST["EVENT_COMMENT"]) + ) . "\n" . $statuses[$_POST["STATUS"]] . " COMMENT " . $dbh->quote($_POST["EVENT_COMMENT"]) . " DO\n$_POST[EVENT_DEFINITION]" , substr($SELF, 0, -1), (strlen($_GET["event"]) ? lang('Event has been altered.') : lang('Event has been created.'))); } @@ -30,7 +30,6 @@ if ($_POST) { } elseif (strlen($_GET["event"])) { $result = $dbh->query("SELECT * FROM information_schema.EVENTS WHERE EVENT_SCHEMA = " . $dbh->quote($_GET["db"]) . " AND EVENT_NAME = " . $dbh->quote($_GET["event"])); $row = $result->fetch_assoc(); - $row["STATUS"] = $statuses[$row["STATUS"]]; $result->free(); } ?> diff --git a/adminer/functions.js b/adminer/functions.js index f7e6980e..2f5750cf 100644 --- a/adminer/functions.js +++ b/adminer/functions.js @@ -65,6 +65,48 @@ function select_add_row(field) { var added = '.', row_count; +function re_escape(s) { + return s.replace(/[\[\]\\^$*+?.(){|}]/, '\\$&'); +} + +function idf_escape(s) { + return '`' + s.replace(/`/, '``') + '`'; +} + +function editing_name_change(field) { + var name = field.name.substr(0, field.name.length - 7); + var type = field.form[name + '[type]']; + var opts = type.options; + var table = re_escape(field.value); + var column = ''; + var match; + if (match = /(.+)_(.+)/.exec(table)) { // limited to columns not containing underscores + table = match[1]; + column = match[2]; + } + var plural = '(?:e?s)?'; + var tab_col = table + plural + '_?' + column; + var re = new RegExp('^' + idf_escape(table + plural) + '\\.' + idf_escape(column) + '$' + + '|^' + idf_escape(tab_col) + '\\.' + + '|\\.' + idf_escape(tab_col) + '$' + + '|^' + idf_escape(column + plural) + '\\.' + idf_escape(table) + '$' + , 'i'); + var candidate; // don't select anything with ambiguous match (like column `id`) + for (var i = opts.length; i--; ) { + if (re.test(opts[i].value)) { + if (candidate) { + return false; + } else { + candidate = i; + } + } + } + if (candidate) { + opts.selectedIndex = candidate; + editing_type_change(type); + } +} + function editing_add_row(button, allowed) { if (allowed && row_count >= allowed) { return false; @@ -91,7 +133,9 @@ function editing_add_row(button, allowed) { tags2[i].value = ''; } } - tags[0].onchange = function () { }; + tags[0].onchange = function () { + editing_name_change(tags[0]); + }; row.parentNode.insertBefore(row2, row.nextSibling); added += '0'; row_count++; diff --git a/adminer/include/editing.inc.php b/adminer/include/editing.inc.php index 02b6dfa4..abb9da6b 100644 --- a/adminer/include/editing.inc.php +++ b/adminer/include/editing.inc.php @@ -99,14 +99,32 @@ function process_input($name, $field) { } } -function edit_type($key, $field, $collations) { +function referencable_primary($self) { + $return = array(); // table_name => field + foreach (table_status_referencable() as $table_name => $table) { + if ($table_name != $self) { + foreach (fields($table_name) as $field) { + if ($field["primary"]) { + if ($return[$table_name]) { // multi column primary key + unset($return[$table_name]); + break; + } + $return[$table_name] = $field; + } + } + } + } + return $return; +} + +function edit_type($key, $field, $collations, $foreign_keys = array()) { global $types, $unsigned, $inout; ?> - + " size="3" /> ' . optionlist($collations, $field["collation"]) . ''; -echo ($unsigned ? " ' : ''); +echo "'; +echo ($unsigned ? " ' : ''); ?> > -" 1 ? "" : " onchange='editing_add_row(this, $allowed);'"); ?> maxlength="64" />" /> - +" onchange=" 1 ? "" : "editing_add_row(this, $allowed); "); ?>editing_name_change(this);" maxlength="64" />" /> + checked="checked" /> checked="checked" /> diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 514d1e51..580a2ba4 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -19,8 +19,8 @@ function optionlist($options, $selected = null) { if (is_array($v)) { $return .= ''; } - foreach ((is_array($v) ? $v : array($v)) as $val) { - $return .= '' . htmlspecialchars($val) . ''; + foreach ((is_array($v) ? $v : array($k => $v)) as $key => $val) { + $return .= '' . htmlspecialchars($val) . ''; } if (is_array($v)) { $return .= ''; diff --git a/adminer/include/version.inc.php b/adminer/include/version.inc.php index 5d1cd816..54ba5207 100644 --- a/adminer/include/version.inc.php +++ b/adminer/include/version.inc.php @@ -1,2 +1,2 @@ Edit default values directly in table creation -Single column foreign key definition in table creation Offer enum and set items in search - whisperer Use event $intervals + microseconds in relative date functions Ability to select external style - list downloaded by JavaScript