1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-12 01:24:17 +02:00

FOUND_ROWS only with GROUP BY

Default order only by common indexes in Editor
Descending default order with date in Editor

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@930 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana
2009-07-28 14:21:05 +00:00
parent 98b88eb15e
commit 07e1ae10f5
3 changed files with 34 additions and 18 deletions

View File

@@ -266,7 +266,7 @@ class Adminer {
* @param array * @param array
* @return array expressions to join by AND * @return array expressions to join by AND
*/ */
function selectSearchProcess($indexes, $fields) { function selectSearchProcess($fields, $indexes) {
global $dbh; global $dbh;
$return = array(); $return = array();
foreach ($indexes as $i => $index) { foreach ($indexes as $i => $index) {
@@ -297,14 +297,13 @@ class Adminer {
/** Process order box in select /** Process order box in select
* @param array * @param array
* @param array result of selectColumnsProcess()
* @param array * @param array
* @return array expressions to join by comma * @return array expressions to join by comma
*/ */
function selectOrderProcess($columns, $select, $indexes) { function selectOrderProcess($fields, $indexes) {
$return = array(); $return = array();
foreach ((array) $_GET["order"] as $key => $val) { foreach ((array) $_GET["order"] as $key => $val) {
if (isset($columns[$val]) || in_array($val, $select, true)) { if (isset($fields[$val]) || preg_match('~^[A-Z0-9_]+\\(`(?:[^`]+|``)+`\\)$~', $val)) {
$return[] = idf_escape($val) . (isset($_GET["desc"][$key]) ? " DESC" : ""); $return[] = idf_escape($val) . (isset($_GET["desc"][$key]) ? " DESC" : "");
} }
} }

View File

@@ -24,8 +24,8 @@ function apply_sql_function($function, $column) {
} }
list($select, $group) = $adminer->selectColumnsProcess($columns, $indexes); list($select, $group) = $adminer->selectColumnsProcess($columns, $indexes);
$where = $adminer->selectSearchProcess($indexes, $fields); $where = $adminer->selectSearchProcess($fields, $indexes);
$order = $adminer->selectOrderProcess($columns, $select, $indexes); $order = $adminer->selectOrderProcess($fields, $indexes);
$limit = $adminer->selectLimitProcess(); $limit = $adminer->selectLimitProcess();
$from = ($select ? implode(", ", $select) : "*") . " FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : ""); $from = ($select ? implode(", ", $select) : "*") . " FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "");
$group_by = ($group && count($group) < count($select) ? " GROUP BY " . implode(", ", $group) : "") . ($order ? " ORDER BY " . implode(", ", $order) : ""); $group_by = ($group && count($group) < count($select) ? " GROUP BY " . implode(", ", $group) : "") . ($order ? " ORDER BY " . implode(", ", $order) : "");
@@ -140,7 +140,7 @@ if (!$columns) {
$adminer->selectActionPrint($text_length); $adminer->selectActionPrint($text_length);
echo "</form>\n"; echo "</form>\n";
$query = "SELECT " . (intval($limit) && count($group) < count($select) ? "SQL_CALC_FOUND_ROWS " : "") . $from . $group_by . (strlen($limit) ? " LIMIT " . intval($limit) . (intval($_GET["page"]) ? " OFFSET " . ($limit * $_GET["page"]) : "") : ""); $query = "SELECT " . (intval($limit) && $group && count($group) < count($select) ? "SQL_CALC_FOUND_ROWS " : "") . $from . $group_by . (strlen($limit) ? " LIMIT " . intval($limit) . (intval($_GET["page"]) ? " OFFSET " . ($limit * $_GET["page"]) : "") : "");
echo $adminer->selectQuery($query); echo $adminer->selectQuery($query);
$result = $dbh->query($query); $result = $dbh->query($query);
@@ -158,7 +158,7 @@ if (!$columns) {
} }
$result->free(); $result->free();
// use count($rows) without LIMIT, COUNT(*) without grouping, FOUND_ROWS otherwise (slowest) // use count($rows) without LIMIT, COUNT(*) without grouping, FOUND_ROWS otherwise (slowest)
$found_rows = (intval($limit) && count($group) < count($select) $found_rows = (intval($limit) && $group && count($group) < count($select)
? $dbh->result($dbh->query(" SELECT FOUND_ROWS()")) // space to allow mysql.trace_mode ? $dbh->result($dbh->query(" SELECT FOUND_ROWS()")) // space to allow mysql.trace_mode
: count($rows) : count($rows)
); );

View File

@@ -152,13 +152,13 @@ ORDER BY ORDINAL_POSITION"); //! requires MySQL 5
function selectOrderPrint($order, $columns, $indexes) { function selectOrderPrint($order, $columns, $indexes) {
//! desc //! desc
$orders = array(); $orders = array();
foreach ($indexes as $i => $index) { foreach ($indexes as $key => $index) {
$order = array(); $order = array();
foreach ($index["columns"] as $val) { foreach ($index["columns"] as $val) {
$order[] = $this->fieldName(array("field" => $val, "comment" => $columns[$val])); $order[] = $this->fieldName(array("field" => $val, "comment" => $columns[$val]));
} }
if (count(array_filter($order, 'strlen')) > 1) { if (count(array_filter($order, 'strlen')) > 1 && $key != "PRIMARY") {
$orders[$i] = implode(", ", $order); $orders[$key] = implode(", ", $order);
} }
} }
if ($orders) { if ($orders) {
@@ -200,7 +200,7 @@ ORDER BY ORDINAL_POSITION"); //! requires MySQL 5
return array(array(), array()); return array(array(), array());
} }
function selectSearchProcess($indexes, $fields) { function selectSearchProcess($fields, $indexes) {
$return = array(); $return = array();
foreach ((array) $_GET["where"] as $val) { foreach ((array) $_GET["where"] as $val) {
if (strlen("$val[col]$val[val]")) { if (strlen("$val[col]$val[val]")) {
@@ -223,16 +223,33 @@ ORDER BY ORDINAL_POSITION"); //! requires MySQL 5
return $return; return $return;
} }
function selectOrderProcess($columns, $select, $indexes) { function selectOrderProcess($fields, $indexes) {
if ($_GET["order"]) { if ($_GET["order"]) {
return array(idf_escape($_GET["order"][0]) . (isset($_GET["desc"][0]) ? " DESC" : "")); return array(idf_escape($_GET["order"][0]) . (isset($_GET["desc"][0]) ? " DESC" : ""));
} }
if ($_GET["index_order"]) { $index = $indexes[$_GET["index_order"]];
return $indexes[$_GET["index_order"]]["columns"]; if (!strlen($_GET["index_order"])) {
foreach ($indexes as $index) {
if ($index["type"] == "INDEX") {
break;
}
}
} }
unset($indexes["PRIMARY"]); if (!$index) {
$index = reset($indexes); return array();
return ($index ? $index["columns"] : array()); }
$desc = false;
foreach ($index["columns"] as $val) {
if (ereg('date|timestamp', $fields[$val]["type"])) {
$desc = true;
break;
}
}
$return = array();
foreach ($index["columns"] as $val) {
$return[] = idf_escape($val) . ($desc ? " DESC" : "");
}
return $return;
} }
function selectLimitProcess() { function selectLimitProcess() {