]> git.joonet.de Git - adminer.git/commitdiff
MongoDB: Improve select
authorJakub Vrana <jakub@vrana.cz>
Thu, 9 Jan 2014 07:14:37 +0000 (23:14 -0800)
committerJakub Vrana <jakub@vrana.cz>
Thu, 9 Jan 2014 07:14:37 +0000 (23:14 -0800)
adminer/drivers/elastic.inc.php
adminer/drivers/mongo.inc.php
adminer/drivers/simpledb.inc.php
adminer/edit.inc.php
adminer/include/adminer.inc.php
adminer/include/driver.inc.php
adminer/select.inc.php
changes.txt

index 6652bca02b43c3a98f24e2a3680ebc777d1c3935..0d289b6f6e8ff9c428c6604a88144a74638b952b 100644 (file)
@@ -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;
index 58df5cdcc773a44a9fc304055c6c48b88f0734fd..058508d7b8a50775cb05a261ce9c362b339ff994 100644 (file)
@@ -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);
-                       }
-                       $return = array();
-                       foreach ($connection->_db->selectCollection($table)->find(array(), $select) as $val) {
-                               $return[] = $val;
+                       $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 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) {
index d53b20d64e25bad595d8c3deeb929939d566afe3..d317e051556b23db81f172176e2fefa3213e6279 100644 (file)
@@ -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;
                }
index 2cb08b222d8fc4575394fd89510c54f503816fb0..6e3037298f4760bdea7653317f46cbbd5dad5f29 100644 (file)
@@ -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;
index ae967fe152128dc5304b9bed5186d0b9017e96d8..729c71484d6a9e04dfacf5f0c0abefe6835b0310 100644 (file)
@@ -176,10 +176,10 @@ username.form['auth[driver]'].onchange();
        */
        function selectQuery($query) {
                global $jush;
-               return ($_GET["edit"] != "" ? "" : "<p><code class='jush-$jush'>" . h(str_replace("\n", " ", $query)) . "</code>"
+               return "<p><code class='jush-$jush'>" . h(str_replace("\n", " ", $query)) . "</code>"
                        . (support("sql") ? " <a href='" . h(ME) . "sql=" . urlencode($query) . "'>" . lang('Edit') . "</a>" : "")
                        . "</p>" // </p> - required for IE9 inline edit
-               );
+               ;
        }
 
        /** Description of a row in a table
index a04f18603a504b80b7ddf21f15bb2ee0830c1f7b..0c13b9ea32f12d589e2733ce2fa9fe47a78abc93 100644 (file)
        * @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);
        }
        
index 1b552d87e022fcc559966e20ea3802cc2a3b1ce0..4cfa74da9c459e354d749961e9398a0f76d7df78 100644 (file)
@@ -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 "<p class='error'>" . 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);
                                        }
index 49734e109b2718f5a44e7e5d38ecddb7e853c96a..093eaf740dfff7eb515df743e7d41df94c84be19 100644 (file)
@@ -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