]> git.joonet.de Git - adminer.git/commitdiff
Foreign keys
authorjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Mon, 2 Jul 2007 10:24:49 +0000 (10:24 +0000)
committerjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Mon, 2 Jul 2007 10:24:49 +0000 (10:24 +0000)
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@5 7c3ca157-0c34-0410-bff1-cbf682f78f5c

functions.inc.php
index.php
select.inc.php
table.inc.php

index e885511d42a40b96c9d8a018a1af4951f66fcc4b..6ad2174308879deafc51ff0d73c0ef7cb5b8f2a9 100644 (file)
@@ -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;
index fb4739c50eb8318678da37d396d9d914c9ec6b91..e5c64bff38770def17ae8b3913d812e628c8baae 100644 (file)
--- a/index.php
+++ b/index.php
@@ -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";
index 3a5097d1f16d43fd9aff58b94e31951472dbabc5..fd9cc31a85f8eabd5a29b67f1d95f95a46ec92a8 100644 (file)
@@ -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 = "&amp;where[$i][col]=" . urlencode($foreign_key[2][$i]) . "&amp;where[$i][op]=%3D&amp;where[$i][val]=" . urlencode($row[$source]) . $val;
+                                       foreach ($foreign_key[2] as $i => $source) {
+                                               $val = "&amp;where[$i][col]=" . urlencode($foreign_key[3][$i]) . "&amp;where[$i][op]=%3D&amp;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']) . '&amp;' . implode('&amp;', 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 "&amp;where[$i][col]=" . urlencode($child[3][$i]) . "&amp;where[$i][op]=%3D&amp;where[$i][val]=" . urlencode($row[$source]);
+                       }
+                       echo '">' . htmlspecialchars($child[1]) . '</a>';
+               }
                echo "</td>";
                echo "</tr>\n";
        }
index 9098f9f168ea54f90e78ae106b364e5153b79d76..5d274953dff9fafd9a3a9a322f2344f0cd2f5cf6 100644 (file)
@@ -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";