Skip to content

Commit 2dcb459

Browse files
committed
Use wrapper for MongoCollection, fixes #209
1 parent 1c4df65 commit 2dcb459

File tree

5 files changed

+75
-51
lines changed

5 files changed

+75
-51
lines changed

src/Jenssegers/Mongodb/Collection.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php namespace Jenssegers\Mongodb;
2+
3+
use Exception;
4+
use MongoCollection;
5+
use Jenssegers\Mongodb\Connection;
6+
7+
class Collection {
8+
9+
/**
10+
* The connection instance.
11+
*
12+
* @var Connection
13+
*/
14+
protected $connection;
15+
16+
/**
17+
* The MongoCollection instance..
18+
*
19+
* @var MongoCollection
20+
*/
21+
protected $collection;
22+
23+
/**
24+
* Constructor.
25+
*/
26+
public function __construct(Connection $connection, MongoCollection $collection)
27+
{
28+
$this->connection = $connection;
29+
$this->collection = $collection;
30+
}
31+
32+
/**
33+
* Handle dynamic method calls.
34+
*
35+
* @param string $method
36+
* @param array $parameters
37+
* @return mixed
38+
*/
39+
public function __call($method, $parameters)
40+
{
41+
// Build the query string.
42+
$query = $parameters;
43+
foreach ($query as &$param)
44+
{
45+
try
46+
{
47+
$param = json_encode($param);
48+
}
49+
catch (Exception $e)
50+
{
51+
$param = '{...}';
52+
}
53+
}
54+
55+
$start = microtime(true);
56+
57+
// Execute the query.
58+
$result = call_user_func_array(array($this->collection, $method), $parameters);
59+
60+
// Log the query.
61+
$this->connection->logQuery(
62+
$this->collection->getName() . '.' . $method . '(' . join(',', $query) . ')',
63+
array(), $this->connection->getElapsedTime($start));
64+
65+
return $result;
66+
}
67+
68+
}

src/Jenssegers/Mongodb/Connection.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Jenssegers\Mongodb;
22

3+
use Jenssegers\Mongodb\Collection;
34
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
45
use MongoClient;
56

@@ -8,14 +9,14 @@ class Connection extends \Illuminate\Database\Connection {
89
/**
910
* The MongoDB database handler.
1011
*
11-
* @var resource
12+
* @var MongoDB
1213
*/
1314
protected $db;
1415

1516
/**
1617
* The MongoClient connection handler.
1718
*
18-
* @var resource
19+
* @var MongoClient
1920
*/
2021
protected $connection;
2122

@@ -74,7 +75,7 @@ public function table($table)
7475
*/
7576
public function getCollection($name)
7677
{
77-
return $this->db->{$name};
78+
return new Collection($this, $this->db->selectCollection($name));
7879
}
7980

8081
/**

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ public function find($id, $columns = array())
7979
*/
8080
public function getFresh($columns = array())
8181
{
82-
$start = microtime(true);
83-
8482
// If no columns have been specified for the select statement, we will set them
8583
// here to either the passed columns, or the standard default of retrieving
8684
// all of the columns on the table using the "wildcard" column character.
@@ -161,11 +159,6 @@ public function getFresh($columns = array())
161159
// Execute aggregation
162160
$results = $this->collection->aggregate($pipeline);
163161

164-
// Log query
165-
$this->connection->logQuery(
166-
$this->from . '.aggregate(' . json_encode($pipeline) . ')',
167-
array(), $this->connection->getElapsedTime($start));
168-
169162
// Return results
170163
return $results['result'];
171164
}
@@ -179,11 +172,6 @@ public function getFresh($columns = array())
179172
// Execute distinct
180173
$result = $this->collection->distinct($column, $wheres);
181174

182-
// Log query
183-
$this->connection->logQuery(
184-
$this->from . '.distinct("' . $column . '", ' . json_encode($wheres) . ')',
185-
array(), $this->connection->getElapsedTime($start));
186-
187175
return $result;
188176
}
189177

@@ -204,11 +192,6 @@ public function getFresh($columns = array())
204192
if ($this->offset) $cursor->skip($this->offset);
205193
if ($this->limit) $cursor->limit($this->limit);
206194

207-
// Log query
208-
$this->connection->logQuery(
209-
$this->from . '.find(' . json_encode($wheres) . ', ' . json_encode($columns) . ')',
210-
array(), $this->connection->getElapsedTime($start));
211-
212195
// Return results as an array with numeric keys
213196
return iterator_to_array($cursor, false);
214197
}
@@ -327,8 +310,6 @@ public function whereBetween($column, array $values, $boolean = 'and', $not = fa
327310
*/
328311
public function insert(array $values)
329312
{
330-
$start = microtime(true);
331-
332313
// Since every insert gets treated like a batch insert, we will have to detect
333314
// if the user is inserting a single document or an array of documents.
334315
$batch = true;
@@ -347,11 +328,6 @@ public function insert(array $values)
347328
// Batch insert
348329
$result = $this->collection->batchInsert($values);
349330

350-
// Log query
351-
$this->connection->logQuery(
352-
$this->from . '.batchInsert(' . json_encode($values) . ')',
353-
array(), $this->connection->getElapsedTime($start));
354-
355331
return (1 == (int) $result['ok']);
356332
}
357333

@@ -364,15 +340,8 @@ public function insert(array $values)
364340
*/
365341
public function insertGetId(array $values, $sequence = null)
366342
{
367-
$start = microtime(true);
368-
369343
$result = $this->collection->insert($values);
370344

371-
// Log query
372-
$this->connection->logQuery(
373-
$this->from . '.insert(' . json_encode($values) . ')',
374-
array(), $this->connection->getElapsedTime($start));
375-
376345
if (1 == (int) $result['ok'])
377346
{
378347
if (!$sequence)
@@ -467,16 +436,9 @@ public function pluck($column)
467436
*/
468437
public function delete($id = null)
469438
{
470-
$start = microtime(true);
471-
472439
$wheres = $this->compileWheres();
473440
$result = $this->collection->remove($wheres);
474441

475-
// Log query
476-
$this->connection->logQuery(
477-
$this->from . '.remove(' . json_encode($wheres) . ')',
478-
array(), $this->connection->getElapsedTime($start));
479-
480442
if (1 == (int) $result['ok'])
481443
{
482444
return $result['n'];
@@ -623,8 +585,6 @@ public function newQuery()
623585
*/
624586
protected function performUpdate($query, array $options = array())
625587
{
626-
$start = microtime(true);
627-
628588
// Default options
629589
$default = array('multiple' => true);
630590

@@ -634,11 +594,6 @@ protected function performUpdate($query, array $options = array())
634594
$wheres = $this->compileWheres();
635595
$result = $this->collection->update($wheres, $query, $options);
636596

637-
// Log query
638-
$this->connection->logQuery(
639-
$this->from . '.update(' . json_encode($wheres) . ', ' . json_encode($query) . ', ' . json_encode($options) . ')',
640-
array(), $this->connection->getElapsedTime($start));
641-
642597
if (1 == (int) $result['ok'])
643598
{
644599
return $result['n'];

tests/ConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testDb()
3131
public function testCollection()
3232
{
3333
$collection = DB::connection('mongodb')->getCollection('unittest');
34-
$this->assertInstanceOf('MongoCollection', $collection);
34+
$this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection);
3535

3636
$collection = DB::connection('mongodb')->collection('unittests');
3737
$this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', $collection);

tests/QueryBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ public function testRaw()
188188
$this->assertEquals(1, $cursor->count());
189189

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

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

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

0 commit comments

Comments
 (0)