Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb query events hooks. Using in conjunction with "loic-sharma/profi... #45

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions src/Jenssegers/Mongodb/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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) . '<i style="color:#f60">,'.json_encode((object)$columns).',</i>'.
json_encode($info) . ')<br/>'.
preg_replace("/\"(.*?)\":/",'<b style="color:#F54E39;">\1</b><em >:</em>',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);
}
Expand Down Expand Up @@ -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'])
{
Expand Down Expand Up @@ -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'])
{
Expand Down Expand Up @@ -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'])
{
Expand Down Expand Up @@ -716,3 +755,4 @@ public function __call($method, $parameters)
}

}

10 changes: 10 additions & 0 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public function getMongoClient()
return $this->connection;
}



/**
* Create a new MongoClient connection.
*
Expand Down Expand Up @@ -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);
}

}