Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Search by foreign keys

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1239 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information...
commit e70157827fd0ecbdd0eb173973f1c1ee8ffd9dd3 1 parent e88a616
jakubvrana authored
Showing with 43 additions and 16 deletions.
  1. +1 −0  changes.txt
  2. +42 −16 editor/include/adminer.inc.php
View
1  changes.txt
@@ -1,6 +1,7 @@
Adminer 2.2.1-dev:
Improve concurrency
Move number of tables to DB info (performance)
+Search by foreign keys (Editor)
Link new item in backward keys (Editor)
Adminer 2.2.0 (released 2009-10-20):
View
58 editor/include/adminer.inc.php
@@ -1,6 +1,7 @@
<?php
class Adminer {
var $operators = array("<=", ">=");
+ var $values = array();
function name() {
return lang('Editor');
@@ -124,10 +125,12 @@ function rowDescriptions($rows, $foreignKeys) {
$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
- $descriptions = array();
- $result = $connection->query("SELECT $id, $name FROM " . idf_escape($foreignKey["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")");
- while ($row = $result->fetch_row()) {
- $descriptions[$row[0]] = $row[1];
+ $descriptions = $this->values[$foreignKey["table"]];
+ if (!$descriptions) {
+ $result = $connection->query("SELECT $id, $name FROM " . idf_escape($foreignKey["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")");
+ while ($row = $result->fetch_row()) {
+ $descriptions[$row[0]] = $row[1];
+ }
}
// use the descriptions
foreach ($rows as $n => $row) {
@@ -177,6 +180,23 @@ function selectColumnsPrint($select, $columns) {
function selectSearchPrint($where, $columns, $indexes) {
//! foreign keys
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;
foreach ((array) $_GET["where"] as $val) {
if (strlen("$val[col]$val[val]")) {
@@ -246,9 +266,9 @@ function selectColumnsProcess($columns, $indexes) {
function selectSearchProcess($fields, $indexes) {
$return = array();
- foreach ((array) $_GET["where"] as $val) {
+ foreach ((array) $_GET["where"] as $key => $val) {
$col = $val["col"];
- if (strlen("$col$val[val]")) {
+ if (strlen(($key < 0 ? "" : $col) . $val["val"])) {
$conds = array();
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"])) {
@@ -374,18 +394,24 @@ function editFunctions($field) {
function editInput($table, $field, $attrs, $value) {
global $connection;
- $foreign_keys = column_foreign_keys($table);
- foreach ((array) $foreign_keys[$field["field"]] as $foreign_key) {
- if (count($foreign_key["source"]) == 1) {
- $id = idf_escape($foreign_key["target"][0]);
- $name = $this->rowDescription($foreign_key["table"]);
+ $foreignKeys = column_foreign_keys($table);
+ foreach ((array) $foreignKeys[$field["field"]] as $foreignKey) {
+ if (count($foreignKey["source"]) == 1) {
+ $id = idf_escape($foreignKey["target"][0]);
+ $name = $this->rowDescription($foreignKey["table"]);
if (strlen($name)) {
- $result = $connection->query("SELECT $id, $name FROM " . idf_escape($foreign_key["table"]) . " ORDER BY 2 LIMIT 1001");
- if ($result->num_rows < 1001) { // optionlist with more than 1000 options would be too big
- $return = array("" => "");
- while ($row = $result->fetch_row()) {
- $return[$row[0]] = $row[1];
+ $return = &$this->values[$foreignKey["table"]];
+ if (!isset($return)) {
+ $result = $connection->query("SELECT $id, $name FROM " . idf_escape($foreignKey["table"]) . " ORDER BY 2 LIMIT 1001");
+ $return = array();
+ 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>";
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.