1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-07 15:16:44 +02:00

ON DELETE action for foreign keys in Create table

This commit is contained in:
Jakub Vrana
2010-05-20 22:06:34 +02:00
parent 84d068df8b
commit 946eb154f6
3 changed files with 14 additions and 10 deletions

View 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($types[$field["type"]])) {
$foreign[] = ($TABLE != "" ? "ADD " : " ") . "FOREIGN KEY (" . idf_escape($field["field"]) . ") REFERENCES " . idf_escape($foreign_keys[$field["type"]]) . " (" . 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"]);

View 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

View 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');
}
}
}