Skip to content

Commit

Permalink
Use wrapper for MongoCollection, fixes #209
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Jun 17, 2014
1 parent 1c4df65 commit 2dcb459
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 51 deletions.
68 changes: 68 additions & 0 deletions src/Jenssegers/Mongodb/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php namespace Jenssegers\Mongodb;

use Exception;
use MongoCollection;
use Jenssegers\Mongodb\Connection;

class Collection {

/**
* The connection instance.
*
* @var Connection
*/
protected $connection;

/**
* The MongoCollection instance..
*
* @var MongoCollection
*/
protected $collection;

/**
* Constructor.
*/
public function __construct(Connection $connection, MongoCollection $collection)
{
$this->connection = $connection;
$this->collection = $collection;
}

/**
* Handle dynamic method calls.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
// Build the query string.
$query = $parameters;
foreach ($query as &$param)
{
try
{
$param = json_encode($param);
}
catch (Exception $e)
{
$param = '{...}';
}
}

$start = microtime(true);

// Execute the query.
$result = call_user_func_array(array($this->collection, $method), $parameters);

// Log the query.
$this->connection->logQuery(
$this->collection->getName() . '.' . $method . '(' . join(',', $query) . ')',
array(), $this->connection->getElapsedTime($start));

return $result;
}

}
7 changes: 4 additions & 3 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Jenssegers\Mongodb;

use Jenssegers\Mongodb\Collection;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use MongoClient;

Expand All @@ -8,14 +9,14 @@ class Connection extends \Illuminate\Database\Connection {
/**
* The MongoDB database handler.
*
* @var resource
* @var MongoDB
*/
protected $db;

/**
* The MongoClient connection handler.
*
* @var resource
* @var MongoClient
*/
protected $connection;

Expand Down Expand Up @@ -74,7 +75,7 @@ public function table($table)
*/
public function getCollection($name)
{
return $this->db->{$name};
return new Collection($this, $this->db->selectCollection($name));
}

/**
Expand Down
45 changes: 0 additions & 45 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ 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 @@ -161,11 +159,6 @@ 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 @@ -179,11 +172,6 @@ public function getFresh($columns = array())
// 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;
}

Expand All @@ -204,11 +192,6 @@ 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 @@ -327,8 +310,6 @@ public function whereBetween($column, array $values, $boolean = 'and', $not = fa
*/
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 @@ -347,11 +328,6 @@ public function insert(array $values)
// Batch insert
$result = $this->collection->batchInsert($values);

// Log query
$this->connection->logQuery(
$this->from . '.batchInsert(' . json_encode($values) . ')',
array(), $this->connection->getElapsedTime($start));

return (1 == (int) $result['ok']);
}

Expand All @@ -364,15 +340,8 @@ 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 @@ -467,16 +436,9 @@ public function pluck($column)
*/
public function delete($id = null)
{
$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'])
{
return $result['n'];
Expand Down Expand Up @@ -623,8 +585,6 @@ public function newQuery()
*/
protected function performUpdate($query, array $options = array())
{
$start = microtime(true);

// Default options
$default = array('multiple' => true);

Expand All @@ -634,11 +594,6 @@ protected function performUpdate($query, array $options = array())
$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'])
{
return $result['n'];
Expand Down
2 changes: 1 addition & 1 deletion tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testDb()
public function testCollection()
{
$collection = DB::connection('mongodb')->getCollection('unittest');
$this->assertInstanceOf('MongoCollection', $collection);
$this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection);

$collection = DB::connection('mongodb')->collection('unittests');
$this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', $collection);
Expand Down
4 changes: 2 additions & 2 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ public function testRaw()
$this->assertEquals(1, $cursor->count());

$collection = DB::collection('users')->raw();
$this->assertInstanceOf('MongoCollection', $collection);
$this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection);

$collection = User::raw();
$this->assertInstanceOf('MongoCollection', $collection);
$this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection);

$results = DB::collection('users')->whereRaw(array('age' => 20))->get();
$this->assertEquals(1, count($results));
Expand Down

0 comments on commit 2dcb459

Please sign in to comment.