Search by foreign keys

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1239 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana 2009-11-12 15:02:34 +00:00
parent e88a6162f2
commit e70157827f
2 changed files with 43 additions and 16 deletions

View file

@ -1,6 +1,7 @@
Adminer 2.2.1-dev: Adminer 2.2.1-dev:
Improve concurrency Improve concurrency
Move number of tables to DB info (performance) Move number of tables to DB info (performance)
Search by foreign keys (Editor)
Link new item in backward keys (Editor) Link new item in backward keys (Editor)
Adminer 2.2.0 (released 2009-10-20): Adminer 2.2.0 (released 2009-10-20):

View file

@ -1,6 +1,7 @@
<?php <?php
class Adminer { class Adminer {
var $operators = array("<=", ">="); var $operators = array("<=", ">=");
var $values = array();
function name() { function name() {
return lang('Editor'); return lang('Editor');
@ -124,10 +125,12 @@ ORDER BY ORDINAL_POSITION");
$ids[$row[$key]] = exact_value($row[$key]); $ids[$row[$key]] = exact_value($row[$key]);
} }
// uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow // uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow
$descriptions = array(); $descriptions = $this->values[$foreignKey["table"]];
$result = $connection->query("SELECT $id, $name FROM " . idf_escape($foreignKey["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")"); if (!$descriptions) {
while ($row = $result->fetch_row()) { $result = $connection->query("SELECT $id, $name FROM " . idf_escape($foreignKey["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")");
$descriptions[$row[0]] = $row[1]; while ($row = $result->fetch_row()) {
$descriptions[$row[0]] = $row[1];
}
} }
// use the descriptions // use the descriptions
foreach ($rows as $n => $row) { foreach ($rows as $n => $row) {
@ -177,6 +180,23 @@ ORDER BY ORDINAL_POSITION");
function selectSearchPrint($where, $columns, $indexes) { function selectSearchPrint($where, $columns, $indexes) {
//! foreign keys //! foreign keys
echo '<fieldset><legend>' . lang('Search') . "</legend><div>\n"; echo '<fieldset><legend>' . lang('Search') . "</legend><div>\n";
$keys = array();
foreach ((array) $_GET["where"] as $key => $val) {
$keys[$val["col"]] = $key;
}
$i = -1;
foreach ($columns as $name => $desc) {
$key = $keys[$name];
$options = $this->editInput($_GET["select"], array("field" => $name), " name='where[$i][val]'", $_GET["where"][$key]["val"]);
if ($options) {
unset($columns[$name]);
if (isset($key)) {
unset($_GET["where"][$key]);
}
echo "<div>" . h($desc) . "<input type='hidden' name='where[$i][col]' value='" . h($name) . "'><input type='hidden' name='where[$i][op]' value='='>: $options</div>\n";
$i--;
}
}
$i = 0; $i = 0;
foreach ((array) $_GET["where"] as $val) { foreach ((array) $_GET["where"] as $val) {
if (strlen("$val[col]$val[val]")) { if (strlen("$val[col]$val[val]")) {
@ -246,9 +266,9 @@ ORDER BY ORDINAL_POSITION");
function selectSearchProcess($fields, $indexes) { function selectSearchProcess($fields, $indexes) {
$return = array(); $return = array();
foreach ((array) $_GET["where"] as $val) { foreach ((array) $_GET["where"] as $key => $val) {
$col = $val["col"]; $col = $val["col"];
if (strlen("$col$val[val]")) { if (strlen(($key < 0 ? "" : $col) . $val["val"])) {
$conds = array(); $conds = array();
foreach ((strlen($col) ? array($col => $fields[$col]) : $fields) as $name => $field) { foreach ((strlen($col) ? array($col => $fields[$col]) : $fields) as $name => $field) {
if (strlen($col) || is_numeric($val["val"]) || !ereg('int|float|double|decimal', $field["type"])) { if (strlen($col) || is_numeric($val["val"]) || !ereg('int|float|double|decimal', $field["type"])) {
@ -374,18 +394,24 @@ ORDER BY ORDINAL_POSITION");
function editInput($table, $field, $attrs, $value) { function editInput($table, $field, $attrs, $value) {
global $connection; global $connection;
$foreign_keys = column_foreign_keys($table); $foreignKeys = column_foreign_keys($table);
foreach ((array) $foreign_keys[$field["field"]] as $foreign_key) { foreach ((array) $foreignKeys[$field["field"]] as $foreignKey) {
if (count($foreign_key["source"]) == 1) { if (count($foreignKey["source"]) == 1) {
$id = idf_escape($foreign_key["target"][0]); $id = idf_escape($foreignKey["target"][0]);
$name = $this->rowDescription($foreign_key["table"]); $name = $this->rowDescription($foreignKey["table"]);
if (strlen($name)) { if (strlen($name)) {
$result = $connection->query("SELECT $id, $name FROM " . idf_escape($foreign_key["table"]) . " ORDER BY 2 LIMIT 1001"); $return = &$this->values[$foreignKey["table"]];
if ($result->num_rows < 1001) { // optionlist with more than 1000 options would be too big if (!isset($return)) {
$return = array("" => ""); $result = $connection->query("SELECT $id, $name FROM " . idf_escape($foreignKey["table"]) . " ORDER BY 2 LIMIT 1001");
while ($row = $result->fetch_row()) { $return = array();
$return[$row[0]] = $row[1]; if ($result->num_rows < 1001) { // optionlist with more than 1000 options would be too big
$return[""] = "";
while ($row = $result->fetch_row()) {
$return[$row[0]] = $row[1];
}
} }
}
if ($return) {
return "<select$attrs>" . optionlist($return, $value, true) . "</select>"; return "<select$attrs>" . optionlist($return, $value, true) . "</select>";
} }
} }