Allow editing foreign keys pointing to tables in other database/schema (bug #694)

This commit is contained in:
Jakub Vrana 2019-08-21 15:00:11 +02:00
parent 03961bbe1b
commit c6fc6b63e8
3 changed files with 31 additions and 10 deletions

View file

@ -53,13 +53,29 @@ if ($_POST) {
<form action="" method="post">
<p>
<?php
if ($row["db"] == "" && $row["ns"] == "") {
$source = array_keys(fields($TABLE)); //! no text and blob
$target = ($TABLE === $row["table"] ? $source : array_keys(fields($row["table"])));
$referencable = array_keys(array_filter(table_status('', true), 'fk_support'));
echo lang('Target table') . ": ";
echo html_select("table", $referencable, $row["table"], "this.form['change-js'].value = '1'; this.form.submit();");
?>
$source = array_keys(fields($TABLE)); //! no text and blob
if ($row["db"] != "") {
$connection->select_db($row["db"]);
}
if ($row["ns"] != "") {
set_schema($row["ns"]);
}
$target = ($TABLE === $row["table"] ? $source : array_keys(fields($row["table"])));
$referencable = array_keys(array_filter(table_status('', true), 'fk_support'));
$onchange = "this.form['change-js'].value = '1'; this.form.submit();";
echo lang('Target table') . ": " . html_select("table", $referencable, $row["table"], $onchange) . "\n";
if ($jush == "pgsql") {
echo lang('Schema') . ": " . html_select("ns", $adminer->schemas(), $row["ns"] ? $row["ns"] : $_GET["ns"], $onchange);
} elseif ($jush != "sqlite") {
$dbs = array();
foreach ($adminer->databases() as $db) {
if (!information_schema($db)) {
$dbs[] = $db;
}
}
echo lang('DB') . ": " . html_select("db", $dbs, $row["db"] ? $row["db"] : $_GET["db"], $onchange);
}
?>
<input type="hidden" name="change-js" value="">
<noscript><p><input type="submit" name="change" value="<?php echo lang('Change'); ?>"></noscript>
<table cellspacing="0">
@ -87,7 +103,6 @@ foreach ($row["source"] as $key => $val) {
<p>
<input type="submit" value="<?php echo lang('Save'); ?>">
<noscript><p><input type="submit" name="add" value="<?php echo lang('Add column'); ?>"></noscript>
<?php } ?>
<?php if ($name != "") { ?><input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"><?php echo confirm(lang('Drop %s?', $name)); ?><?php } ?>
<input type="hidden" name="token" value="<?php echo $token; ?>">
</form>

View file

@ -470,12 +470,17 @@ function remove_definer($query) {
}
/** Format foreign key to use in SQL query
* @param array ("table" => string, "source" => array, "target" => array, "on_delete" => one of $on_actions, "on_update" => one of $on_actions)
* @param array ("db" => string, "ns" => string, "table" => string, "source" => array, "target" => array, "on_delete" => one of $on_actions, "on_update" => one of $on_actions)
* @return string
*/
function format_foreign_key($foreign_key) {
global $on_actions;
return " FOREIGN KEY (" . implode(", ", array_map('idf_escape', $foreign_key["source"])) . ") REFERENCES " . table($foreign_key["table"])
$db = $foreign_key["db"];
$ns = $foreign_key["ns"];
return " FOREIGN KEY (" . implode(", ", array_map('idf_escape', $foreign_key["source"])) . ") REFERENCES "
. ($db != "" && $db != $_GET["db"] ? idf_escape($db) . "." : "")
. ($ns != "" && $ns != $_GET["ns"] ? idf_escape($ns) . "." : "")
. table($foreign_key["table"])
. " (" . implode(", ", array_map('idf_escape', $foreign_key["target"])) . ")" //! reuse $name - check in older MySQL versions
. (preg_match("~^($on_actions)\$~", $foreign_key["on_delete"]) ? " ON DELETE $foreign_key[on_delete]" : "")
. (preg_match("~^($on_actions)\$~", $foreign_key["on_update"]) ? " ON UPDATE $foreign_key[on_update]" : "")

View file

@ -1,4 +1,5 @@
Adminer 4.7.3-dev:
Allow editing foreign keys pointing to tables in other database/schema (bug #694)
MySQL: Speed up displaying tables in large databases (bug #700, regression from 4.7.2)
MySQL: Allow editing rows identified by negative floats (bug #695)
MySQL: Skip editing generated columns