diff --git a/src/Jenssegers/Mongodb/Builder.php b/src/Jenssegers/Mongodb/Builder.php index 6f50420ba..1c1d0fec2 100644 --- a/src/Jenssegers/Mongodb/Builder.php +++ b/src/Jenssegers/Mongodb/Builder.php @@ -59,6 +59,8 @@ 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. @@ -138,6 +140,11 @@ public function getFresh($columns = array('*')) // Execute aggregation $results = $this->collection->aggregate($pipeline); + // Log query + $this->connection->logQuery( + $this->from . '.aggregate(' . json_encode($pipeline) . ')', + array(), $this->connection->getElapsedTime($start)); + // Return results return $results['result']; } @@ -147,7 +154,16 @@ public function getFresh($columns = array('*')) { // Return distinct results directly $column = isset($this->columns[0]) ? $this->columns[0] : '_id'; - return $this->collection->distinct($column, $wheres); + + // Execute distinct + $result = $this->collection->distinct($column, $wheres); + + // Log query + $this->connection->logQuery( + $this->from . '.distinct("' . $column . '", ' . json_encode($wheres) . ')', + array(), $this->connection->getElapsedTime($start)); + + return $result; } // Normal query @@ -167,6 +183,11 @@ public function getFresh($columns = array('*')) if ($this->offset) $cursor->skip($this->offset); if ($this->limit) $cursor->limit($this->limit); + // Log query + $this->connection->logQuery( + $this->from . '.find(' . json_encode($wheres) . ', ' . json_encode($columns) . ')', + array(), $this->connection->getElapsedTime($start)); + // Return results as an array with numeric keys return iterator_to_array($cursor, false); } @@ -275,6 +296,8 @@ public function whereBetween($column, array $values, $boolean = 'and') */ public function insert(array $values) { + $start = microtime(true); + // Since every insert gets treated like a batch insert, we will have to detect // if the user is inserting a single document or an array of documents. $batch = true; @@ -291,7 +314,14 @@ public function insert(array $values) if (!$batch) $values = array($values); // Batch insert - return $this->collection->batchInsert($values); + $result = $this->collection->batchInsert($values); + + // Log query + $this->connection->logQuery( + $this->from . '.batchInsert(' . json_encode($values) . ')', + array(), $this->connection->getElapsedTime($start)); + + return $result; } /** @@ -303,8 +333,15 @@ public function insert(array $values) */ public function insertGetId(array $values, $sequence = null) { + $start = microtime(true); + $result = $this->collection->insert($values); + // Log query + $this->connection->logQuery( + $this->from . '.insert(' . json_encode($values) . ')', + array(), $this->connection->getElapsedTime($start)); + if (1 == (int) $result['ok']) { if (!$sequence) @@ -391,7 +428,15 @@ public function pluck($column) */ public function delete($id = null) { - $result = $this->collection->remove($this->compileWheres()); + $start = microtime(true); + + $wheres = $this->compileWheres(); + $result = $this->collection->remove($wheres); + + // Log query + $this->connection->logQuery( + $this->from . '.remove(' . json_encode($wheres) . ')', + array(), $this->connection->getElapsedTime($start)); if (1 == (int) $result['ok']) { @@ -414,7 +459,7 @@ public function from($collection) $this->collection = $this->connection->getCollection($collection); } - return $this; + return parent::from($collection); } /** @@ -528,13 +573,21 @@ public function newQuery() */ protected function performUpdate($query, array $options = array()) { + $start = microtime(true); + // Default options $default = array('multiple' => true); // Merge options and override default options $options = array_merge($default, $options); - $result = $this->collection->update($this->compileWheres(), $query, $options); + $wheres = $this->compileWheres(); + $result = $this->collection->update($wheres, $query, $options); + + // Log query + $this->connection->logQuery( + $this->from . '.update(' . json_encode($wheres) . ', ' . json_encode($query) . ', ' . json_encode($options) . ')', + array(), $this->connection->getElapsedTime($start)); if (1 == (int) $result['ok']) { diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index b021dc840..3b0293ae3 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -157,11 +157,22 @@ protected function getDsn(array $config) } } - // The database name needs to be in the connection string, otherwise it will + // The database name needs to be in the connection string, otherwise it will // authenticate to the admin database, which may result in permission errors. return "mongodb://" . implode(',', $hosts) . "/{$database}"; } + /** + * Get the elapsed time since a given starting point. + * + * @param int $start + * @return float + */ + public function getElapsedTime($start) + { + return parent::getElapsedTime($start); + } + /** * Dynamically pass methods to the connection. *