From 5ee29cb0ec833296234728aaff15c950246852c2 Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Thu, 7 Aug 2008 10:57:28 +0000 Subject: [PATCH] Select columns, aggregation git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@437 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- index.php | 2 ++ lang/cs.inc.php | 2 +- lang/de.inc.php | 1 - lang/es.inc.php | 1 - lang/nl.inc.php | 1 - lang/sk.inc.php | 1 - lang/zh.inc.php | 1 - select.inc.php | 72 ++++++++++++++++++++++++++++++++++--------------- 8 files changed, 53 insertions(+), 28 deletions(-) diff --git a/index.php b/index.php index 57788663..71ca27bf 100644 --- a/index.php +++ b/index.php @@ -41,6 +41,8 @@ if (isset($_GET["download"])) { $unsigned = array("", "unsigned", "zerofill", "unsigned zerofill"); $enum_length = '\'(?:\'\'|[^\'\\\\]+|\\\\.)*\'|"(?:""|[^"\\\\]+|\\\\.)*"'; $inout = array("IN", "OUT", "INOUT"); + $functions = array("char_length", "from_unixtime", "hex", "lower", "round", "sec_to_time", "time_to_sec", "unix_timestamp", "upper"); + $grouping = array("avg", "count", "distinct", "group_concat", "max", "min", "sum"); $error = ""; if (isset($_GET["table"])) { diff --git a/lang/cs.inc.php b/lang/cs.inc.php index 24b935bc..6b196afd 100644 --- a/lang/cs.inc.php +++ b/lang/cs.inc.php @@ -154,7 +154,7 @@ $translations = array( '%d item(s) have been deleted.' => array('Byl smazán %d záznam.', 'Byly smazány %d záznamy.', 'Bylo smazáno %d záznamů.'), 'all' => 'vše', 'Delete selected' => 'Smazat označené', - 'Truncate table' => 'Promazat tabulku', + 'Truncate result' => 'Promazat výsledek', 'Too big POST data. Reduce the data or increase the "post_max_size" configuration directive.' => 'Příliš velká POST data. Zmenšete data nebo zvyšte hodnotu konfigurační direktivy "post_max_size".', 'Logged as: %s' => 'Přihlášen jako: %s', 'Move up' => 'Přesunout nahoru', diff --git a/lang/de.inc.php b/lang/de.inc.php index f7abc65b..fa726163 100644 --- a/lang/de.inc.php +++ b/lang/de.inc.php @@ -152,7 +152,6 @@ $translations = array( '%d item(s) have been deleted.' => array('%d Artikel gelöscht.', '%d Artikel gelöscht.'), 'all' => 'alle', 'Delete selected' => 'Markierte löschen', - 'Truncate table' => 'Tabelleninhalt löschen (truncate)', 'MySQL version: %s through PHP extension %s' => 'Version MySQL: %s, Zugriff unter Benutzung der PHP-Erweiterung %s', 'around %d row(s)' => array('ungefähren %d Datensatz', 'ungefähren %d Datensätze'), 'Logged as: %s' => 'Angemeldet als: %s', diff --git a/lang/es.inc.php b/lang/es.inc.php index 3a932ce3..f2d1336d 100644 --- a/lang/es.inc.php +++ b/lang/es.inc.php @@ -152,7 +152,6 @@ $translations = array( '%d item(s) have been deleted.' => array('%d item eliminado.', '%d itemes eliminados.'), 'all' => 'todos', 'Delete selected' => 'Eliminar seleccionados', - 'Truncate table' => 'Vaciar tabla (truncate)', 'MySQL version: %s through PHP extension %s' => 'Versión MySQL: %s a través de extensión PHP %s', 'around %d row(s)' => array('acaso %d fila', 'acaso %d filas'), 'Logged as: %s' => 'Logeado como: %s', diff --git a/lang/nl.inc.php b/lang/nl.inc.php index c835dbee..019cd277 100644 --- a/lang/nl.inc.php +++ b/lang/nl.inc.php @@ -154,7 +154,6 @@ $translations = array( '%d item(s) have been deleted.' => array('%d item werd verwijderd.', '%d items warden verwijderd.'), 'all' => 'alle', 'Delete selected' => 'Geselecteerde verwijderen', - 'Truncate table' => 'Tabel leegmaken', 'Logged as: %s' => 'Aangemeld als: %s', 'Too big POST data. Reduce the data or increase the "post_max_size" configuration directive.' => 'POST-data is te groot. Verklein de hoeveelheid data of verhoog de "post_max_size" configuratie.', 'Move up' => 'Omhoog', diff --git a/lang/sk.inc.php b/lang/sk.inc.php index 0c799be4..c8cc2b40 100644 --- a/lang/sk.inc.php +++ b/lang/sk.inc.php @@ -154,7 +154,6 @@ $translations = array( '%d item(s) have been deleted.' => array('%d záznam bol zmazaný.', '%d záznamy boli zmazané.', '%d záznamov bolo zmazaných.'), 'all' => 'všetko', 'Delete selected' => 'Zmazať označené', - 'Truncate table' => 'Vyprázdniť tabuľku', 'Too big POST data. Reduce the data or increase the "post_max_size" configuration directive.' => 'Príliš veľké POST dáta. Zmenšite dáta alebo zvýšte hodnotu konfiguračej direktívy "post_max_size".', 'Logged as: %s' => 'Prihlásený ako: %s', 'Move up' => 'Presunúť hore', diff --git a/lang/zh.inc.php b/lang/zh.inc.php index 6381772b..d963ea16 100644 --- a/lang/zh.inc.php +++ b/lang/zh.inc.php @@ -154,7 +154,6 @@ $translations = array( '%d item(s) have been deleted.' => '%d 个项目被删除', 'all' => '全部', 'Delete selected' => '删除选中', - 'Truncate table' => '截断表', 'Logged as: %s' => '登录为: %s', 'Too big POST data. Reduce the data or increase the "post_max_size" configuration directive.' => '太大的 POST 数据. 减少数据或者增加 "post_max_size" 配置指令.', 'Move up' => '上移', diff --git a/select.inc.php b/select.inc.php index dc139c34..b4d796f2 100644 --- a/select.inc.php +++ b/select.inc.php @@ -19,6 +19,16 @@ foreach ($fields as $key => $field) { $rights += $field["privileges"]; } +$select = array(); +$group = array(); +foreach ((array) $_GET["columns"] as $key => $val) { + if ($val["fun"] == "count" || (in_array($val["col"], $columns, true) && (!$val["fun"] || in_array($val["fun"], $functions) || in_array($val["fun"], $grouping)))) { + $select[$key] = (in_array($val["col"], $columns, true) ? (!$val["fun"] ? idf_escape($val["col"]) : ($val["fun"] == "distinct" ? "COUNT(DISTINCT " : strtoupper("$val[fun](")) . idf_escape($val["col"]) . ")") : "COUNT(*)"); + if (!in_array($val["fun"], $grouping)) { + $group[] = $select[$key]; + } + } +} $where = array(); foreach ($indexes as $i => $index) { if ($index["type"] == "FULLTEXT" && strlen($_GET["fulltext"][$i])) { @@ -39,12 +49,12 @@ foreach ((array) $_GET["where"] as $val) { } $order = array(); foreach ((array) $_GET["order"] as $key => $val) { - if (in_array($val, $columns, true)) { + if (in_array($val, $columns, true)) { //! respect functions $order[] = idf_escape($val) . (isset($_GET["desc"][$key]) ? " DESC" : ""); } } $limit = (isset($_GET["limit"]) ? $_GET["limit"] : "30"); -$from = "FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "") . ($order ? " ORDER BY " . implode(", ", $order) : "") . (strlen($limit) ? " LIMIT " . intval($limit) . (intval($_GET["page"]) ? " OFFSET " . ($limit * $_GET["page"]) : "") : ""); +$from = "FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "") . ($group && count($group) < count($select) ? " GROUP BY " . implode(", ", $group) : "") . ($order ? " ORDER BY " . implode(", ", $order) : "") . (strlen($limit) ? " LIMIT " . intval($limit) . (intval($_GET["page"]) ? " OFFSET " . ($limit * $_GET["page"]) : "") : ""); if ($_POST && !$error) { $result = true; @@ -93,14 +103,46 @@ if (isset($rights["insert"])) { if (!$columns) { echo "

" . lang('Unable to select the table') . ($fields ? "" : ": " . htmlspecialchars($mysql->error)) . ".

\n"; } else { - echo "
\n
" . lang('Search') . "\n"; + echo "\n"; + ?> + +" . lang('Select') . "\n"; if (strlen($_GET["server"])) { echo ''; } echo ''; echo ''; echo "\n"; + $i = 0; + $fun_group = array(lang('Functions') => $functions, lang('Aggregation') => $grouping); + foreach ($select as $key => $val) { + $val = $_GET["columns"][$key]; + echo "
"; + echo "
\n"; + $i++; + } + echo "
"; + echo "
\n"; + echo "
\n"; + echo "
" . lang('Search') . "\n"; foreach ($indexes as $i => $index) { if ($index["type"] == "FULLTEXT") { echo "(" . implode(", ", array_map('htmlspecialchars', $index["columns"])) . ") AGAINST"; @@ -128,20 +170,6 @@ for (var i=0; > i; i++) { document.getElementById('form')['where[' + i + '][op]'].onchange(); } - -function add_row(field) { - var row = field.parentNode.cloneNode(true); - var selects = row.getElementsByTagName('select'); - for (var i=0; i < selects.length; i++) { - selects[i].name = selects[i].name.replace(/[a-z]\[[0-9]+/, '$&1'); - selects[i].selectedIndex = 0; - } - var input = row.getElementsByTagName('input')[0]; - input.name = input.name.replace(/[a-z]\[[0-9]+/, '$&1'); - input.value = ''; - field.parentNode.parentNode.appendChild(row); - field.onchange = function () { }; -} "; @@ -176,7 +204,7 @@ function add_row(field) { echo "\n"; echo "
 
\n"; - $result = $mysql->query("SELECT * $from"); + $result = $mysql->query("SELECT " . ($select ? (count($group) < count($select) ? "SQL_CALC_FOUND_ROWS " : "") . implode(", ", $select) : "*") . " $from"); if (!$result) { echo "

" . htmlspecialchars($mysql->error) . "

\n"; } else { @@ -194,14 +222,14 @@ function add_row(field) { echo "\n"; for ($j=0; $row = $result->fetch_assoc(); $j++) { if (!$j) { - echo ''; + echo '' . (count($select) == count($group) ? '' : ''); foreach ($row as $key => $val) { echo '"; } echo "\n"; } $unique_idf = implode('&', unique_idf($row, $indexes)); - echo '"; + echo '' . (count($select) == count($group) ? '' : ''); foreach ($row as $key => $val) { if (!isset($val)) { $val = "NULL"; @@ -234,9 +262,9 @@ function add_row(field) { echo "\n"; } echo "
' . htmlspecialchars($key) . "
' . lang('edit') . "
' . lang('edit') . '
\n"; - echo "

\n"; + echo "

" . (count($group) == count($select) ? " " : "") . "

\n"; echo "\n"; - if (intval($limit) && ($found_rows = $mysql->result($mysql->query("SELECT COUNT(*) FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "")))) > $limit) { + if (intval($limit) && ($found_rows = $mysql->result($mysql->query(count($group) < count($select) ? " SELECT FOUND_ROWS()" : "SELECT COUNT(*) FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "")))) > $limit) { $max_page = floor(($found_rows - 1) / $limit); echo "

" . lang('Page') . ":"; print_page(0);