Skip to content

Commit

Permalink
ON DELETE action for foreign keys in Create table
Browse files Browse the repository at this point in the history
  • Loading branch information
vrana committed May 21, 2010
1 parent 4c2268d commit dc4fc82
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
16 changes: 8 additions & 8 deletions adminer/create.inc.php
Expand Up @@ -5,7 +5,7 @@
$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();
Expand Down Expand Up @@ -44,7 +44,7 @@
$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"] != "") {
Expand Down Expand Up @@ -178,12 +178,12 @@
<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
Expand Down
5 changes: 3 additions & 2 deletions adminer/include/editing.inc.php
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions adminer/static/editing.js
Expand Up @@ -83,7 +83,7 @@ function reEscape(s) {
* @return string
*/
function idfEscape(s) {
return '`' + s.replace(/`/, '``') + '`';
return s.replace(/`/, '``');
}

/** Detect foreign key
Expand All @@ -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;
}
Expand Down Expand Up @@ -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');
}
}
}

Expand Down

0 comments on commit dc4fc82

Please sign in to comment.