Foreign keys
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@5 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
parent
e5f4f7fc98
commit
f4287792db
|
@ -61,16 +61,15 @@ function indexes($table) {
|
|||
}
|
||||
|
||||
function foreign_keys($table) {
|
||||
//! make over to information_schema.key_column_usage
|
||||
static $pattern = '~`((?:[^`]*|``)+)`~';
|
||||
$return = array();
|
||||
$create_table = mysql_result(mysql_query("SHOW CREATE TABLE " . idf_escape($table)), 0, 1);
|
||||
preg_match_all('~FOREIGN KEY \\((.*)\\) REFERENCES (.*) \\((.*)\\)~', $create_table, $matches, PREG_SET_ORDER);
|
||||
preg_match_all('~FOREIGN KEY \\((.+)\\) REFERENCES (?:`(.+)`\\.)?`(.+)` \\((.+)\\)~', $create_table, $matches, PREG_SET_ORDER);
|
||||
foreach ($matches as $match) {
|
||||
preg_match_all($pattern, $match[1], $source);
|
||||
preg_match_all($pattern, $match[3], $target);
|
||||
preg_match_all($pattern, $match[4], $target);
|
||||
foreach ($source[1] as $val) {
|
||||
$return[idf_unescape($val)][] = array(idf_unescape(substr($match[2], 1, -1)), array_map('idf_unescape', $source[1]), array_map('idf_unescape', $target[1]));
|
||||
$return[idf_unescape($val)][] = array(idf_unescape($match[2]), idf_unescape($match[3]), array_map('idf_unescape', $source[1]), array_map('idf_unescape', $target[1]));
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
|
|
|
@ -23,7 +23,7 @@ if (isset($_GET["sql"])) {
|
|||
include "./view.inc.php";
|
||||
} else {
|
||||
page_header(htmlspecialchars($_GET["db"]));
|
||||
$result = mysql_query("SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '" . mysql_real_escape_string($_GET["db"]) . "'");
|
||||
$result = mysql_query("SELECT * FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA = '" . mysql_real_escape_string($_GET["db"]) . "'");
|
||||
if (mysql_num_rows($result)) {
|
||||
echo "<h2>" . lang('Routines') . "</h2>\n";
|
||||
echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
|
||||
|
|
|
@ -39,11 +39,20 @@ $limit = 30;
|
|||
echo "<input type='submit' value='" . lang('Search') . "' />\n";
|
||||
echo "</div></form>\n";
|
||||
$result = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "") . " LIMIT $limit OFFSET " . ($limit * $_GET["page"]));
|
||||
$found_rows = mysql_result(mysql_query(" SELECT FOUND_ROWS()"), 0);
|
||||
$found_rows = mysql_result(mysql_query(" SELECT FOUND_ROWS()"), 0); // space for mysql.trace_mode
|
||||
if (!mysql_num_rows($result)) {
|
||||
echo "<p class='message'>" . lang('No rows.') . "</p>\n";
|
||||
} else {
|
||||
$foreign_keys = foreign_keys($_GET["select"]);
|
||||
$childs = array();
|
||||
$result1 = mysql_query("SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . mysql_real_escape_string($_GET["db"]) . "' AND REFERENCED_TABLE_NAME = '" . mysql_real_escape_string($_GET["select"]) . "' ORDER BY ORDINAL_POSITION");
|
||||
while ($row1 = mysql_fetch_assoc($result1)) {
|
||||
$childs[$row1["CONSTRAINT_NAME"]][0] = $row1["TABLE_SCHEMA"];
|
||||
$childs[$row1["CONSTRAINT_NAME"]][1] = $row1["TABLE_NAME"];
|
||||
$childs[$row1["CONSTRAINT_NAME"]][2][] = $row1["REFERENCED_COLUMN_NAME"];
|
||||
$childs[$row1["CONSTRAINT_NAME"]][3][] = $row1["COLUMN_NAME"];
|
||||
}
|
||||
mysql_free_result($result1);
|
||||
|
||||
echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
|
||||
for ($j=0; $row = mysql_fetch_assoc($result); $j++) {
|
||||
|
@ -59,16 +68,22 @@ if (!mysql_num_rows($result)) {
|
|||
if (count($foreign_keys[$key]) == 1) {
|
||||
$foreign_key = $foreign_keys[$key][0];
|
||||
$val = '">' . "$val</a>";
|
||||
foreach ($foreign_key[1] as $i => $source) {
|
||||
$val = "&where[$i][col]=" . urlencode($foreign_key[2][$i]) . "&where[$i][op]=%3D&where[$i][val]=" . urlencode($row[$source]) . $val;
|
||||
foreach ($foreign_key[2] as $i => $source) {
|
||||
$val = "&where[$i][col]=" . urlencode($foreign_key[3][$i]) . "&where[$i][op]=%3D&where[$i][val]=" . urlencode($row[$source]) . $val;
|
||||
}
|
||||
$val = '<a href="' . htmlspecialchars($SELF) . 'select=' . htmlspecialchars($foreign_key[0]) . $val; // InnoDB support non-UNIQUE keys //! reference to other database
|
||||
$val = '<a href="' . htmlspecialchars(strlen($foreign_key[0]) ? preg_replace('~([?&]db=)[^&]+~', '\\1' . urlencode($foreign_key[0]), $SELF) : $SELF) . 'select=' . htmlspecialchars($foreign_key[1]) . $val; // InnoDB support non-UNIQUE keys //! reference to other database
|
||||
}
|
||||
}
|
||||
echo "<td>$val</td>";
|
||||
}
|
||||
echo '<td><a href="' . htmlspecialchars($SELF) . 'edit=' . urlencode($_GET['select']) . '&' . implode('&', unique_idf($row, $indexes)) . '">edit</a>'; //! views can be unupdatable
|
||||
//! links to referencing tables - information_schema.key_column_usage - REFERENCED_TABLE_SCHEMA, REFERENCED_TABLE_NAME
|
||||
foreach ($childs as $child) {
|
||||
echo ' <a href="' . htmlspecialchars(strlen($child[0]) ? preg_replace('~([?&]db=)[^&]+~', '\\1' . urlencode($child[0]), $SELF) : $SELF) . 'select=' . urlencode($child[1]);
|
||||
foreach ($child[2] as $i => $source) {
|
||||
echo "&where[$i][col]=" . urlencode($child[3][$i]) . "&where[$i][op]=%3D&where[$i][val]=" . urlencode($row[$source]);
|
||||
}
|
||||
echo '">' . htmlspecialchars($child[1]) . '</a>';
|
||||
}
|
||||
echo "</td>";
|
||||
echo "</tr>\n";
|
||||
}
|
||||
|
|
|
@ -24,6 +24,24 @@ if ($indexes) {
|
|||
echo "</table>\n";
|
||||
}
|
||||
|
||||
$result = mysql_query("SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . mysql_real_escape_string($_GET["db"]) . "' AND TABLE_NAME = '" . mysql_real_escape_string($_GET["table"]) . "' AND REFERENCED_TABLE_NAME IS NOT NULL ORDER BY ORDINAL_POSITION");
|
||||
if (mysql_num_rows($result)) {
|
||||
$foreign_keys = array();
|
||||
while ($row = mysql_fetch_assoc($result)) {
|
||||
$foreign_keys[$row["CONSTRAINT_NAME"]][0] = $row["REFERENCED_TABLE_SCHEMA"];
|
||||
$foreign_keys[$row["CONSTRAINT_NAME"]][1] = $row["REFERENCED_TABLE_NAME"];
|
||||
$foreign_keys[$row["CONSTRAINT_NAME"]][2][] = htmlspecialchars($row["COLUMN_NAME"]);
|
||||
$foreign_keys[$row["CONSTRAINT_NAME"]][3][] = htmlspecialchars($row["REFERENCED_COLUMN_NAME"]);
|
||||
}
|
||||
echo "<h3>" . lang('Foreign keys') . "</h3>\n";
|
||||
echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
|
||||
foreach ($foreign_keys as $foreign_key) {
|
||||
echo "<tr><td><em>" . implode("</em>, <em>", $foreign_key[2]) . "</em></td><td>" . (strlen($foreign_key[0]) && $foreign_key[0] !== $_GET["db"] ? "<strong>" . htmlspecialchars($foreign_key[0]) . "</strong>." : "") . "<strong>" . htmlspecialchars($foreign_key[1]) . "</strong>(<em>" . implode("</em>, <em>", $foreign_key[3]) . "</em>)</td></tr>\n";
|
||||
}
|
||||
echo "</table>\n";
|
||||
}
|
||||
mysql_free_result($result);
|
||||
|
||||
$result = mysql_query("SHOW TRIGGERS LIKE '" . mysql_real_escape_string($_GET["table"]) . "'");
|
||||
if (mysql_num_rows($result)) {
|
||||
echo "<h3>" . lang('Triggers') . "</h3>\n";
|
||||
|
|
Loading…
Reference in a new issue