From a7d475e3e782c3929521c2b2450f9b189e906285 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Wed, 8 Jan 2014 23:14:37 -0800 Subject: [PATCH] MongoDB: Improve select --- adminer/drivers/elastic.inc.php | 6 +++-- adminer/drivers/mongo.inc.php | 39 ++++++++++++++++++++++---------- adminer/drivers/simpledb.inc.php | 4 ++-- adminer/edit.inc.php | 2 +- adminer/include/adminer.inc.php | 4 ++-- adminer/include/driver.inc.php | 7 ++++-- adminer/select.inc.php | 4 ++-- changes.txt | 2 +- 8 files changed, 44 insertions(+), 24 deletions(-) diff --git a/adminer/drivers/elastic.inc.php b/adminer/drivers/elastic.inc.php index 6652bca0..0d289b6f 100644 --- a/adminer/drivers/elastic.inc.php +++ b/adminer/drivers/elastic.inc.php @@ -88,7 +88,7 @@ if (isset($_GET["elastic"])) { class Min_Driver extends Min_SQL { - function select($table, $select, $where, $group, $order, $limit, $page) { + function select($table, $select, $where, $group, $order, $limit, $page, $print = false) { global $adminer; $query = $adminer->selectQueryBuild($select, $where, $group, $order, $limit, $page); $data = array(); @@ -125,7 +125,9 @@ if (isset($_GET["elastic"])) { $data["query"]["filtered"]["query"] = array("match_all" => array()); } } - echo $adminer->selectQuery($query); + if ($print) { + echo $adminer->selectQuery("$query: " . print_r($data, true)); + } $search = $this->_conn->query($query, $data); if (!$search) { return false; diff --git a/adminer/drivers/mongo.inc.php b/adminer/drivers/mongo.inc.php index 58df5cdc..058508d7 100644 --- a/adminer/drivers/mongo.inc.php +++ b/adminer/drivers/mongo.inc.php @@ -21,7 +21,7 @@ if (isset($_GET["mongo"])) { $options["db"] = $db; } try { - $this->_link = new MongoClient("mongodb://$server", $options); + $this->_link = @new MongoClient("mongodb://$server", $options); return true; } catch (Exception $ex) { $this->error = $ex->getMessage(); @@ -43,6 +43,10 @@ if (isset($_GET["mongo"])) { } } + function quote($string) { + return $string; + } + } class Min_Result { @@ -110,18 +114,23 @@ if (isset($_GET["mongo"])) { class Min_Driver extends Min_SQL { - function select($table, $select, $where, $group, $order, $limit, $page) { + function select($table, $select, $where, $group, $order, $limit, $page, $print = false) { global $connection; - if ($select == array("*")) { - $select = array(); - } else { - $select = array_fill_keys($select, true); + $select = ($select == array("*") + ? array() + : array_fill_keys($select, true) + ); + $sort = array(); + foreach ($order as $val) { + $val = preg_replace('~ DESC$~', '', $val, 1, $count); + $sort[$val] = ($count ? -1 : 1); } - $return = array(); - foreach ($connection->_db->selectCollection($table)->find(array(), $select) as $val) { - $return[] = $val; - } - return new Min_Result($return); + return new Min_Result(iterator_to_array($connection->_db->selectCollection($table) + ->find(array(), $select) + ->sort($sort) + ->limit(+$limit) + ->skip($page * $limit) + )); } } @@ -235,6 +244,10 @@ if (isset($_GET["mongo"])) { function convert_field($field) { } + function unconvert_field($field, $return) { + return $return; + } + function foreign_keys($table) { return array(); } @@ -247,7 +260,9 @@ if (isset($_GET["mongo"])) { } function found_rows($table_status, $where) { - return null; + global $connection; + //! don't call count_rows() + return $connection->_db->selectCollection($_GET["select"])->count($where); } function alter_table($table, $name, $fields, $foreign, $comment, $engine, $collation, $auto_increment, $partitioning) { diff --git a/adminer/drivers/simpledb.inc.php b/adminer/drivers/simpledb.inc.php index d53b20d6..d317e051 100644 --- a/adminer/drivers/simpledb.inc.php +++ b/adminer/drivers/simpledb.inc.php @@ -149,10 +149,10 @@ if (isset($_GET["simpledb"])) { return $return; } - function select($table, $select, $where, $group, $order, $limit, $page) { + function select($table, $select, $where, $group, $order, $limit, $page, $print = false) { global $connection; $connection->next = $_GET["next"]; - $return = parent::select($table, $select, $where, $group, $order, $limit, $page); + $return = parent::select($table, $select, $where, $group, $order, $limit, $page, $print); $connection->next = 0; return $return; } diff --git a/adminer/edit.inc.php b/adminer/edit.inc.php index 2cb08b22..6e303729 100644 --- a/adminer/edit.inc.php +++ b/adminer/edit.inc.php @@ -89,7 +89,7 @@ if ($_POST["save"]) { $select = array("*"); } if ($select) { - $result = $driver->select($TABLE, $select, array($where), $select, array(), (isset($_GET["select"]) ? 2 : 1), 0); + $result = $driver->select($TABLE, $select, array($where), $select, array(), (isset($_GET["select"]) ? 2 : 1), 0, true); $row = $result->fetch_assoc(); if (isset($_GET["select"]) && (!$row || $result->fetch_assoc())) { // $result->num_rows != 1 isn't available in all drivers $row = null; diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index ae967fe1..729c7148 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -176,10 +176,10 @@ username.form['auth[driver]'].onchange(); */ function selectQuery($query) { global $jush; - return ($_GET["edit"] != "" ? "" : "

" . h(str_replace("\n", " ", $query)) . "" + return "

" . h(str_replace("\n", " ", $query)) . "" . (support("sql") ? " " . lang('Edit') . "" : "") . "

" //

- required for IE9 inline edit - ); + ; } /** Description of a row in a table diff --git a/adminer/include/driver.inc.php b/adminer/include/driver.inc.php index a04f1860..0c13b9ea 100644 --- a/adminer/include/driver.inc.php +++ b/adminer/include/driver.inc.php @@ -18,9 +18,10 @@ * @param array result of $adminer->selectOrderProcess() * @param int result of $adminer->selectLimitProcess() * @param int index of page starting at zero + * @param bool whether to print the query * @return Min_Result */ - function select($table, $select, $where, $group, $order, $limit, $page) { + function select($table, $select, $where, $group, $order, $limit, $page, $print = false) { global $adminer, $jush; $is_group = (count($group) < count($select)); $query = $adminer->selectQueryBuild($select, $where, $group, $order, $limit, $page); @@ -33,7 +34,9 @@ "\n" ); } - echo $adminer->selectQuery($query); + if ($print) { + echo $adminer->selectQuery($query); + } return $this->_conn->query($query); } diff --git a/adminer/select.inc.php b/adminer/select.inc.php index 1b552d87..4cfa74da 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -256,7 +256,7 @@ if (!$columns && support("table")) { if ($convert_fields) { $select2[] = substr($convert_fields, 2); } - $result = $driver->select($TABLE, $select2, $where, $group, $order, $limit, $page); + $result = $driver->select($TABLE, $select2, $where, $group, $order, $limit, $page, true); if (!$result) { echo "

" . error() . "\n"; @@ -346,7 +346,7 @@ if (!$columns && support("table")) { } $unique_idf = ""; foreach ($unique_array as $key => $val) { - if (strlen($val) > 64 && ($jush == "sql" || $jush == "pgsql")) { + if (($jush == "sql" || $jush == "pgsql") && strlen($val) > 64) { $key = "MD5(" . (strpos($key, '(') ? $key : idf_escape($key)) . ")"; //! columns looking like functions $val = md5($val); } diff --git a/changes.txt b/changes.txt index 49734e10..093eaf74 100644 --- a/changes.txt +++ b/changes.txt @@ -1,7 +1,7 @@ Adminer 4.0.1-dev: Fix compiled version of Elasticsearch Don't use type=number if a SQL function is used -MongoDB: Count tables, display ObjectIds +MongoDB: Count tables, display ObjectIds, sort, limit, offset, count rows Adminer 4.0.0 (released 2014-01-08): Driver for SimpleDB, MongoDB and Elasticsearch