]> git.joonet.de Git - adminer.git/commitdiff
Search by foreign keys
authorjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Thu, 12 Nov 2009 15:02:34 +0000 (15:02 +0000)
committerjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Thu, 12 Nov 2009 15:02:34 +0000 (15:02 +0000)
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1239 7c3ca157-0c34-0410-bff1-cbf682f78f5c

changes.txt
editor/include/adminer.inc.php

index e8e71653b7902a6244613a4de04e944b3c93c3b2..b9806ef17c941f907a109a6c711198cb4e7b037a 100644 (file)
@@ -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):
index 2490b985b36b84c6a1977a256011779d16f249ac..24b3cd01f3e5b3b4b63a009089a1c09d44887fd3 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 class Adminer {
        var $operators = array("<=", ">=");
+       var $values = array();
        
        function name() {
                return lang('Editor');
@@ -124,10 +125,12 @@ ORDER BY ORDINAL_POSITION");
                                                        $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 @@ ORDER BY ORDINAL_POSITION");
        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 @@ ORDER BY ORDINAL_POSITION");
        
        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 @@ ORDER BY ORDINAL_POSITION");
        
        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>";
                                        }
                                }