From 6b4ee48ae83610d950f683ea87223c0b15e2efbb Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Wed, 4 May 2011 15:53:26 +0200 Subject: [PATCH] Autocomplete for big foreign keys --- adminer/include/version.inc.php | 2 +- changes.txt | 6 ++++++ editor/include/adminer.inc.php | 19 +++++++++++++------ editor/index.php | 2 ++ editor/script.inc.php | 12 ++++++++++++ editor/static/editing.js | 28 ++++++++++++++++++++++++++++ todo.txt | 1 - 7 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 editor/script.inc.php diff --git a/adminer/include/version.inc.php b/adminer/include/version.inc.php index ab0e8360..7dacd883 100644 --- a/adminer/include/version.inc.php +++ b/adminer/include/version.inc.php @@ -1,2 +1,2 @@ $desc) { $options = $this->_foreignKeyOptions($_GET["select"], $name); - if ($options) { + if (is_array($options)) { if ($fields[$name]["null"]) { $options[0] = '(' . lang('empty') . ')'; } @@ -423,9 +423,12 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5 . enum_input("radio", $attrs, $field, ($value || isset($_GET["select"]) ? $value : 0), ($field["null"] ? "" : null)) ; } - $options = $this->_foreignKeyOptions($table, $field["field"]); - if ($options) { - return "" . optionlist($options, $value, true) . ""; + $options = $this->_foreignKeyOptions($table, $field["field"], $value); + if (isset($options)) { + return (is_array($options) + ? "" . optionlist($options, $value, true) . "" + : "" + ); } if (like_bool($field)) { return '"; @@ -563,12 +566,16 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5 } } - function _foreignKeyOptions($table, $column) { + function _foreignKeyOptions($table, $column, $value = null) { + global $connection; if (list($table, $id, $name) = $this->_foreignColumn(column_foreign_keys($table), $column)) { $return = &$this->_values[$table]; if (!isset($return)) { $table_status = table_status($table); - $return = ($table_status["Rows"] > 1000 ? array() : array("" => "") + get_key_vals("SELECT $id, $name FROM " . table($table) . " ORDER BY 2")); + $return = ($table_status["Rows"] > 1000 + ? (isset($value) ? $connection->result("SELECT $name FROM " . table($table) . " WHERE $id = " . q($value)) : "") + : array("" => "") + get_key_vals("SELECT $id, $name FROM " . table($table) . " ORDER BY 2") + ); } return $return; } diff --git a/editor/index.php b/editor/index.php index d902e220..00008550 100644 --- a/editor/index.php +++ b/editor/index.php @@ -19,6 +19,8 @@ if (isset($_GET["download"])) { include "../adminer/edit.inc.php"; } elseif (isset($_GET["select"])) { include "../adminer/select.inc.php"; +} elseif (isset($_GET["script"])) { + include "./script.inc.php"; } else { include "./db.inc.php"; } diff --git a/editor/script.inc.php b/editor/script.inc.php new file mode 100644 index 00000000..1de9ab47 --- /dev/null +++ b/editor/script.inc.php @@ -0,0 +1,12 @@ +_foreignColumn(column_foreign_keys($_GET["source"]), $_GET["field"])) { + $result = $connection->query("SELECT $id, $name FROM " . table($table) . " WHERE $name LIKE " . q("$_GET[value]%") . " ORDER BY 2 LIMIT 11"); + for ($i=0; $i < 10 && ($row = $result->fetch_row()); $i++) { + echo "" . h($row[1]) . "
\n"; + } + if ($i == 10) { + echo "...\n"; + } +} + +exit; // don't print footer diff --git a/editor/static/editing.js b/editor/static/editing.js index 39c9af97..e89fc140 100644 --- a/editor/static/editing.js +++ b/editor/static/editing.js @@ -5,3 +5,31 @@ function bodyLoad(version) { onpopstate(history); } } + +function whisperClick(event, field) { + var el = event.target || event.srcElement; + if (/^a$/i.test(el.tagName) && !(event.button || event.ctrlKey || event.shiftKey || event.altKey || event.metaKey)) { + field.value = el.firstChild.data; + field.previousSibling.value = decodeURIComponent(el.href.replace(/.*=/, '')); + field.nextSibling.style.display = 'none'; + eventStop(event); + return false; + } +} + +function whisper(url, field) { + if (field.orig != field.value) { + field.orig = field.value; + return ajax(url + encodeURIComponent(field.value), function (xmlhttp) { + if (xmlhttp.status && field.orig == field.value) { + field.nextSibling.innerHTML = xmlhttp.responseText; + field.nextSibling.style.display = ''; + var a = field.nextSibling.firstChild; + if (a && a.firstChild.data == field.value) { + field.previousSibling.value = decodeURIComponent(a.href.replace(/.*=/, '')); + a.style.fontWeight = 'bold'; + } + } + }); + } +} diff --git a/todo.txt b/todo.txt index aabcb778..cd13bf5d 100644 --- a/todo.txt +++ b/todo.txt @@ -21,7 +21,6 @@ Three-state checkbox for boolean searches JavaScript data validation - columns containing word email, url, ... Joining tables - PRIMARY KEY (table, joining) Rank, Tree structure -Add whisperer to fields with foreign key to big table MySQL: Data longer than max_allowed_packet can be sent by mysqli_stmt_send_long_data()