Skip to content

Commit

Permalink
Separate backward keys
Browse files Browse the repository at this point in the history
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@873 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information
jakubvrana committed Jul 22, 2009
1 parent c325cbe commit e5de7ef
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
8 changes: 8 additions & 0 deletions adminer/include/adminer.inc.php
Expand Up @@ -72,6 +72,14 @@ function adminer_select_links($table_status) {
return call_adminer('select_links', '<a href="' . htmlspecialchars($SELF) . (isset($table_status["Engine"]) ? 'table=' : 'view=') . urlencode($_GET['select']) . '">' . lang('Table structure') . '</a>', $table_status);
}

/** Find backward keys for table
* @param string
* @return array $return[$target_table][$key_name][$target_column] = $source_column;
*/
function adminer_backward_keys($table) {
return call_adminer('backward_keys', array(), $table);
}

/** Query printed in select before execution
* @param string query to be executed
* @return string
Expand Down
17 changes: 1 addition & 16 deletions adminer/select.inc.php
Expand Up @@ -285,22 +285,7 @@
}
$descriptions = adminer_row_descriptions($rows, $foreign_keys);

//! Editor only
$backward_keys = array();
$result = $dbh->query("
SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_COLUMN_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = " . $dbh->quote(adminer_database()) . "
AND REFERENCED_TABLE_SCHEMA = " . $dbh->quote(adminer_database()) . "
AND REFERENCED_TABLE_NAME = " . $dbh->quote($_GET["select"]) . "
ORDER BY ORDINAL_POSITION
");
if ($result) {
while ($row = $result->fetch_assoc()) {
$backward_keys[$row["TABLE_NAME"]][$row["CONSTRAINT_NAME"]][$row["COLUMN_NAME"]] = $row["REFERENCED_COLUMN_NAME"];
}
$result->free();
}
$backward_keys = adminer_backward_keys($_GET["select"]);
$table_names = array_keys($backward_keys);
if ($table_names) {
$table_names = array_combine($table_names, array_map('adminer_table_name', array_map('table_status', $table_names)));
Expand Down
24 changes: 22 additions & 2 deletions editor/include/adminer.inc.php
Expand Up @@ -40,6 +40,26 @@ function adminer_select_links($table_status) {
return call_adminer('select_links', "", $table_status);
}

function adminer_backward_keys($table) {
global $dbh;
$return = array();
$result = $dbh->query("
SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_COLUMN_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = " . $dbh->quote(adminer_database()) . "
AND REFERENCED_TABLE_SCHEMA = " . $dbh->quote(adminer_database()) . "
AND REFERENCED_TABLE_NAME = " . $dbh->quote($table) . "
ORDER BY ORDINAL_POSITION
");
if ($result) {
while ($row = $result->fetch_assoc()) {
$return[$row["TABLE_NAME"]][$row["CONSTRAINT_NAME"]][$row["COLUMN_NAME"]] = $row["REFERENCED_COLUMN_NAME"];
}
$result->free();
}
return call_adminer('backward_keys', $return, $table);
}

function adminer_select_query($query) {
return call_adminer('select_query', "<!-- " . str_replace("--", "--><!--", $query) . " -->\n", $query);
}
Expand All @@ -64,7 +84,7 @@ function adminer_row_descriptions($rows, $foreign_keys) {
foreach ($rows as $row) {
$ids[$row[$key]] = $dbh->quote($row[$key]);
}
// select all descriptions
// uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow
$descriptions = array();
$result = $dbh->query("SELECT $id, $name FROM " . idf_escape($foreign_key["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")");
while ($row = $result->fetch_row()) {
Expand All @@ -83,7 +103,7 @@ function adminer_row_descriptions($rows, $foreign_keys) {
}

function adminer_select_val($val, $link) {
return call_adminer('select_val', ($link ? '<a href="' . $link . '">' . $val . '</a>' : ($val == "<i>NULL</i>" ? "" : $val)), $val, $link);
return call_adminer('select_val', ($link ? "<a href=\"$link\">$val</a>" : ($val == "<i>NULL</i>" ? "" : $val)), $val, $link);
}

function adminer_message_query($query) {
Expand Down
1 change: 1 addition & 0 deletions todo.txt
Expand Up @@ -14,6 +14,7 @@ Offer enum and set items in search - whisperer
Use event $intervals + microseconds in relative date functions
Ability to select external style - list downloaded by JavaScript
Table list cache - SHOW TABLE STATUS is slow with big InnoDB tables
Download external files (version checker and JUSH) from trusted HTTPS if Adminer runs in HTTPS (mixed-content warning)
? Column and table names auto-completition in SQL textarea
? Save token also to cookie - for session expiration and login in other window
? Save uploaded files after error to session variable instead of hidden field
Expand Down

0 comments on commit e5de7ef

Please sign in to comment.