diff --git a/src/Jenssegers/Mongodb/Builder.php b/src/Jenssegers/Mongodb/Builder.php index 6f50420ba..f4a7f3b57 100644 --- a/src/Jenssegers/Mongodb/Builder.php +++ b/src/Jenssegers/Mongodb/Builder.php @@ -37,6 +37,7 @@ class Builder extends \Illuminate\Database\Query\Builder { public function __construct(Connection $connection) { $this->connection = $connection; + $this->debug = class_exists('\Config') && \Config::get('app.debug'); } /** @@ -59,6 +60,7 @@ public function find($id, $columns = array('*')) */ public function getFresh($columns = array('*')) { + $start = microtime(true); // If no columns have been specified for the select statement, we will set them // here to either the passed columns, or the standard default of retrieving // all of the columns on the table using the "wildcard" column character. @@ -154,9 +156,14 @@ public function getFresh($columns = array('*')) else { $columns = array(); - foreach ($this->columns as $column) + foreach ($this->columns as $columnName => $columnValue) { - $columns[$column] = true; + + // fix for projection operators + if (is_int($columnName)) + $columns[$columnValue] = true; + else #if (is_array($columnValue)) + $columns[$columnName] = $columnValue; } // Execute query and get MongoCursor @@ -167,6 +174,27 @@ public function getFresh($columns = array('*')) if ($this->offset) $cursor->skip($this->offset); if ($this->limit) $cursor->limit($this->limit); + + + + if ($this->debug) { + $explain = $cursor->explain(); + $info = $cursor->info(); + /* + Output debug customization + */ + unset($info['query']);unset($info['fields']);unset($info['ns']);unset($info['started_iterating']); + unset($explain['indexBounds']);unset($explain['allPlans']);unset($explain['oldPlan']); + + \Event::fire('illuminate.query', array( + $this->collection->getName() . ".find(" . json_encode($wheres) . ','.json_encode((object)$columns).','. + json_encode($info) . ')
'. + preg_replace("/\"(.*?)\":/",'\1:',json_encode($explain,JSON_PRETTY_PRINT)) + , + null, $this->connection->getElapsedTime($start), $this->connection->getName() )); + } + + // Return results as an array with numeric keys return iterator_to_array($cursor, false); } @@ -303,7 +331,10 @@ public function insert(array $values) */ public function insertGetId(array $values, $sequence = null) { + $start = microtime(); $result = $this->collection->insert($values); + if ($this->debug) + \Event::fire('illuminate.query', array( $this->collection->getName() . ".insert " . json_encode($values), null, $time = $this->connection->getElapsedTime($start), $this->connection->getName() )); if (1 == (int) $result['ok']) { @@ -391,7 +422,11 @@ public function pluck($column) */ public function delete($id = null) { - $result = $this->collection->remove($this->compileWheres()); + $start = microtime(); + $wheres = $this->compileWheres(); + $result = $this->collection->remove($wheres); + if ($this->debug) + \Event::fire('illuminate.query', array( $this->collection->getName() . ".remove " . json_encode($wheres), null, $time = $this->connection->getElapsedTime($start), $this->connection->getName() )); if (1 == (int) $result['ok']) { @@ -533,8 +568,12 @@ protected function performUpdate($query, array $options = array()) // Merge options and override default options $options = array_merge($default, $options); + $wheres = $this->compileWheres(); + $start = microtime(); + $result = $this->collection->update($wheres, $query, $options); + if ($this->debug) + \Event::fire('illuminate.query', array( $this->collection->getName() . ".update " . json_encode($wheres) . "|||".json_encode($query) . "|||" . json_encode($options), null, $time = $this->connection->getElapsedTime($start), $this->connection->getName() )); - $result = $this->collection->update($this->compileWheres(), $query, $options); if (1 == (int) $result['ok']) { @@ -716,3 +755,4 @@ public function __call($method, $parameters) } } + diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index b021dc840..eae72e6bb 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -107,6 +107,8 @@ public function getMongoClient() return $this->connection; } + + /** * Create a new MongoClient connection. * @@ -174,4 +176,12 @@ public function __call($method, $parameters) return call_user_func_array(array($this->db, $method), $parameters); } + + //-------------------- + public function getElapsedTime($start) + { + return parent::getElapsedTime($start); + } + } +