Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added referencing foreign keys to table structure #5

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions adminer/drivers/pgsql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,33 @@ function foreign_keys($table) {
return $return;
}

function foreign_keys_to($table) {
$return = array();

foreach (get_rows("SELECT pg_constraint.oid, pg_class.relname, conname AS name, (SELECT nspname FROM pg_namespace WHERE oid = connamespace) AS ns, pg_get_constraintdef(pg_constraint.oid) AS definition
FROM pg_constraint
JOIN pg_class ON pg_class.oid = pg_constraint.conrelid
WHERE confrelid = (SELECT oid FROM pg_class WHERE relname = " . q($table) . ")
AND contype = 'f'::char
ORDER BY confkey, pg_class.relname, conname"
) as $row) {
if(preg_match('~FOREIGN KEY\s*\((.+)\)\s*REFERENCES (.+)\((.+)\)(.*)$~iA', $row['definition'], $match)) {
$row['source'] = array_map('trim', explode(',', $match[1]));
$row['table'] = $row['relname'];
$row['target'] = array_map('trim', explode(',', $match[3]));

$row['on_delete'] = (preg_match('~ON DELETE (CASCADE|SET NULL|RESTRICT|NO ACTION)~', $match[4], $match2)) ? $match2[1] : 'NO ACTION';
$row['on_update'] = (preg_match('~ON UPDATE (CASCADE|SET NULL|RESTRICT|NO ACTION)~', $match[4], $match2)) ? $match2[1] : 'NO ACTION';

$return[] = $row;
} else {
// error in parsing
}
}

return $return;
}

function view($name) {
global $connection;
return array("select" => $connection->result("SELECT pg_get_viewdef(" . q($name) . ")"));
Expand Down
23 changes: 23 additions & 0 deletions adminer/table.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@
if ($jush != "sqlite") {
echo '<p><a href="' . h(ME) . 'foreign=' . urlencode($TABLE) . '">' . lang('Add foreign key') . "</a>\n";
}

if (function_exists('foreign_keys_to') && ($foreign_keys = foreign_keys_to($TABLE))) {
echo "<h3>" . lang('Referencing foreign keys') . "</h3>\n";
echo "<table cellspacing='0'>\n";
echo "<thead><tr><th>" . lang('Target') . "<td>" . lang('Source') . "<td>" . lang('ON DELETE') . "<td>" . lang('ON UPDATE') . ($jush != "sqlite" ? "<td>&nbsp;" : "") . "</thead>\n";
foreach ($foreign_keys as $foreign_key) {
$name = $foreign_key['name'];
$link = ($foreign_key["db"] != "" ? "<b>" . h($foreign_key["db"]) . "</b>." : "") . h($foreign_key["table"]);
echo "<tr title='" . h($name) . "'>";
echo "<th><i>" . implode("</i>, <i>", array_map('h', $foreign_key["target"])) . "</i>";
echo "<td><a href='" . h($foreign_key["db"] != "" ? preg_replace('~db=[^&]*~', "db=" . urlencode($foreign_key["db"]), ME) : ($foreign_key["ns"] != "" ? preg_replace('~ns=[^&]*~', "ns=" . urlencode($foreign_key["ns"]), ME) : ME)) . "table=" . urlencode($foreign_key["table"]) . "'>"
. ($foreign_key["db"] != "" ? "<b>" . h($foreign_key["db"]) . "</b>." : "") . ($foreign_key["ns"] != "" ? "<b>" . h($foreign_key["ns"]) . "</b>." : "") . h($foreign_key["table"])
. "</a>"
;
echo "(<i>" . implode("</i>, <i>", array_map('h', $foreign_key["source"])) . "</i>)";
echo "<td>$foreign_key[on_delete]\n";
echo "<td>$foreign_key[on_update]\n";
if ($jush != "sqlite") {
echo '<td><a href="' . h(ME . 'foreign=' . urlencode($foreign_key['table']) . '&name=' . urlencode($name)) . '">' . lang('Alter') . '</a>';
}
}
echo "</table>\n";
}
}

if (support("trigger")) {
Expand Down