]> git.joonet.de Git - adminer.git/commitdiff
ON DELETE action for foreign keys in Create table
authorJakub Vrana <jakub@vrana.cz>
Thu, 20 May 2010 20:06:34 +0000 (22:06 +0200)
committerJakub Vrana <jakub@vrana.cz>
Fri, 21 May 2010 18:30:53 +0000 (20:30 +0200)
adminer/create.inc.php
adminer/include/editing.inc.php
adminer/static/editing.js

index 101ac2abbcab503de9b8fa2aa654106782072180..22fe647c37d10d13d7ef989831abb410f80375b6 100644 (file)
@@ -5,7 +5,7 @@ $partition_by = array('HASH', 'LINEAR HASH', 'KEY', 'LINEAR KEY', 'RANGE', 'LIST
 $referencable_primary = referencable_primary($TABLE);
 $foreign_keys = array();
 foreach ($referencable_primary as $table_name => $field) {
-       $foreign_keys[idf_escape($table_name) . "." . idf_escape($field["field"])] = $table_name;
+       $foreign_keys[str_replace("`", "``", $table_name) . "`" . str_replace("`", "``", $field["field"])] = $table_name; // not idf_escape() - used in JS
 }
 
 $orig_fields = array();
@@ -44,7 +44,7 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"]
                                        $fields[] = array($field["orig"], $process_field, $after);
                                }
                                if (isset($foreign_key)) {
-                                       $foreign[] = ($TABLE != "" ? "ADD" : " ") . " FOREIGN KEY (" . idf_escape($field["field"]) . ") REFERENCES " . idf_escape($foreign_key) . " (" . idf_escape($type_field["field"]) . ")";
+                                               $foreign[] = ($TABLE != "" ? "ADD" : " ") . " FOREIGN KEY (" . idf_escape($field["field"]) . ") REFERENCES " . idf_escape($foreign_keys[$field["type"]]) . " (" . idf_escape($type_field["field"]) . ")" . (in_array($field["on_delete"], $on_actions) ? " ON DELETE $field[on_delete]" : "");
                                }
                                $after = "AFTER " . idf_escape($field["field"]);
                        } elseif ($field["orig"] != "") {
@@ -178,12 +178,12 @@ if (support("partitioning")) {
 <table cellspacing="0" id="partition-table"<?php echo ($partition_table ? "" : " class='hidden'"); ?>>
 <thead><tr><th><?php echo lang('Partition name'); ?><th><?php echo lang('Values'); ?></thead>
 <?php
-       foreach ($row["partition_names"] as $key => $val) {
-               echo '<tr>';
-               echo '<td><input name="partition_names[]" value="' . h($val) . '"' . ($key == count($row["partition_names"]) - 1 ? ' onchange="partitionNameChange(this);"' : '') . '>';
-               echo '<td><input name="partition_values[]" value="' . h($row["partition_values"][$key]) . '">';
-       }
-       ?>
+foreach ($row["partition_names"] as $key => $val) {
+       echo '<tr>';
+       echo '<td><input name="partition_names[]" value="' . h($val) . '"' . ($key == count($row["partition_names"]) - 1 ? ' onchange="partitionNameChange(this);"' : '') . '>';
+       echo '<td><input name="partition_values[]" value="' . h($row["partition_values"][$key]) . '">';
+}
+?>
 </table>
 </div></fieldset>
 <?php
index aa8ff07a6829ba32068ea8f7cb3cc20bb660cdb9..4034522d4f13e89f38449700e1b348d49cb8c789 100644 (file)
@@ -108,12 +108,13 @@ function referencable_primary($self) {
 * @return null
 */
 function edit_type($key, $field, $collations, $foreign_keys = array()) {
-       global $structured_types, $unsigned, $inout;
+       global $structured_types, $unsigned, $inout, $on_actions;
        ?>
 <td><select name="<?php echo $key; ?>[type]" class="type" onfocus="lastType = selectValue(this);" onchange="editingTypeChange(this);"><?php echo optionlist($structured_types + ($foreign_keys ? array(lang('Foreign keys') => $foreign_keys) : array()), $field["type"]); ?></select>
 <td><input name="<?php echo $key; ?>[length]" value="<?php echo h($field["length"]); ?>" size="3" onfocus="editingLengthFocus(this);"><td><?php
        echo "<select name='$key" . "[collation]'" . (ereg('(char|text|enum|set)$', $field["type"]) ? "" : " class='hidden'") . '><option value="">(' . lang('collation') . ')' . optionlist($collations, $field["collation"]) . '</select>';
-       echo ($unsigned ? "<select name='$key" . "[unsigned]'" . (!$field["type"] || ereg('(int|float|double|decimal)$', $field["type"]) ? "" : " class='hidden'") . '><option>' . optionlist($unsigned, $field["unsigned"]) . '</select> ' : ' '); // space for IE
+       echo ($unsigned ? "<select name='$key" . "[unsigned]'" . (!$field["type"] || ereg('(int|float|double|decimal)$', $field["type"]) ? "" : " class='hidden'") . '><option>' . optionlist($unsigned, $field["unsigned"]) . '</select>' : '');
+       echo ($foreign_keys ? "<select name='$key" . "[on_delete]'" . (ereg("`", $field["type"]) ? "" : " class='hidden'") . "><option value=''>(" . lang('ON DELETE') . ")" . optionlist($on_actions, $field["on_delete"]) . "</select> " : " "); // space for IE
 }
 
 /** Filter length value including enums
index b3f2251eb568948c1dfb5c07c54f9e61c3735afa..4a3f03bd3c6f61e65e4dbf76a3fecc97c9757bf9 100644 (file)
@@ -83,7 +83,7 @@ function reEscape(s) {
 * @return string
 */
 function idfEscape(s) {
-       return '`' + s.replace(/`/, '``') + '`';
+       return s.replace(/`/, '``');
 }
 
 /** Detect foreign key
@@ -102,14 +102,14 @@ function editingNameChange(field) {
        }
        var plural = '(?:e?s)?';
        var tabCol = table + plural + '_?' + column;
-       var re = new RegExp('(^' + idfEscape(table + plural) + '\\.' + idfEscape(column) + '$' // table_column
-               + '|^' + idfEscape(tabCol) + '\\.' // table
-               + '|^' + idfEscape(column + plural) + '\\.' + idfEscape(table) + '$' // column_table
-               + ')|\\.' + idfEscape(tabCol) + '$' // column
+       var re = new RegExp('(^' + idfEscape(table + plural) + '`' + idfEscape(column) + '$' // table_column
+               + '|^' + idfEscape(tabCol) + '`' // table
+               + '|^' + idfEscape(column + plural) + '`' + idfEscape(table) + '$' // column_table
+               + ')|`' + idfEscape(tabCol) + '$' // column
        , 'i');
        var candidate; // don't select anything with ambiguous match (like column `id`)
        for (var i = opts.length; i--; ) {
-               if (opts[i].value.substr(0, 1) != '`') { // common type
+               if (!/`/.test(opts[i].value)) { // common type
                        if (i == opts.length - 2 && candidate && !match[1] && name == 'fields[1]') { // single target table, link to column, first field - probably `id`
                                return false;
                        }
@@ -215,6 +215,9 @@ function editingTypeChange(type) {
                if (el.name == name + '[unsigned]') {
                        el.className = (/(int|float|double|decimal)$/.test(text) ? '' : 'hidden');
                }
+               if (el.name == name + '[on_delete]') {
+                       el.className = (/`/.test(text) ? '' : 'hidden');
+               }
        }
 }