Skip to content

Commit

Permalink
Query logging, check PR mongodb#45
Browse files Browse the repository at this point in the history
  • Loading branch information
mnphpexpert committed Oct 5, 2013
1 parent 38b88ef commit 8adb2a9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
63 changes: 58 additions & 5 deletions src/Jenssegers/Mongodb/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'];
}
Expand All @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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)
Expand Down Expand Up @@ -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'])
{
Expand All @@ -414,7 +459,7 @@ public function from($collection)
$this->collection = $this->connection->getCollection($collection);
}

return $this;
return parent::from($collection);
}

/**
Expand Down Expand Up @@ -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'])
{
Expand Down
13 changes: 12 additions & 1 deletion src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 8adb2a9

Please sign in to comment.