Skip to content

Commit a6714df

Browse files
committed
Query logging, check PR #45
1 parent 7b2867b commit a6714df

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

src/Jenssegers/Mongodb/Builder.php

+58-5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function find($id, $columns = array('*'))
5959
*/
6060
public function getFresh($columns = array('*'))
6161
{
62+
$start = microtime(true);
63+
6264
// If no columns have been specified for the select statement, we will set them
6365
// here to either the passed columns, or the standard default of retrieving
6466
// all of the columns on the table using the "wildcard" column character.
@@ -138,6 +140,11 @@ public function getFresh($columns = array('*'))
138140
// Execute aggregation
139141
$results = $this->collection->aggregate($pipeline);
140142

143+
// Log query
144+
$this->connection->logQuery(
145+
$this->from . '.aggregate(' . json_encode($pipeline) . ')',
146+
array(), $this->connection->getElapsedTime($start));
147+
141148
// Return results
142149
return $results['result'];
143150
}
@@ -147,7 +154,16 @@ public function getFresh($columns = array('*'))
147154
{
148155
// Return distinct results directly
149156
$column = isset($this->columns[0]) ? $this->columns[0] : '_id';
150-
return $this->collection->distinct($column, $wheres);
157+
158+
// Execute distinct
159+
$result = $this->collection->distinct($column, $wheres);
160+
161+
// Log query
162+
$this->connection->logQuery(
163+
$this->from . '.distinct("' . $column . '", ' . json_encode($wheres) . ')',
164+
array(), $this->connection->getElapsedTime($start));
165+
166+
return $result;
151167
}
152168

153169
// Normal query
@@ -167,6 +183,11 @@ public function getFresh($columns = array('*'))
167183
if ($this->offset) $cursor->skip($this->offset);
168184
if ($this->limit) $cursor->limit($this->limit);
169185

186+
// Log query
187+
$this->connection->logQuery(
188+
$this->from . '.find(' . json_encode($wheres) . ', ' . json_encode($columns) . ')',
189+
array(), $this->connection->getElapsedTime($start));
190+
170191
// Return results as an array with numeric keys
171192
return iterator_to_array($cursor, false);
172193
}
@@ -275,6 +296,8 @@ public function whereBetween($column, array $values, $boolean = 'and')
275296
*/
276297
public function insert(array $values)
277298
{
299+
$start = microtime(true);
300+
278301
// Since every insert gets treated like a batch insert, we will have to detect
279302
// if the user is inserting a single document or an array of documents.
280303
$batch = true;
@@ -291,7 +314,14 @@ public function insert(array $values)
291314
if (!$batch) $values = array($values);
292315

293316
// Batch insert
294-
return $this->collection->batchInsert($values);
317+
$result = $this->collection->batchInsert($values);
318+
319+
// Log query
320+
$this->connection->logQuery(
321+
$this->from . '.batchInsert(' . json_encode($values) . ')',
322+
array(), $this->connection->getElapsedTime($start));
323+
324+
return $result;
295325
}
296326

297327
/**
@@ -303,8 +333,15 @@ public function insert(array $values)
303333
*/
304334
public function insertGetId(array $values, $sequence = null)
305335
{
336+
$start = microtime(true);
337+
306338
$result = $this->collection->insert($values);
307339

340+
// Log query
341+
$this->connection->logQuery(
342+
$this->from . '.insert(' . json_encode($values) . ')',
343+
array(), $this->connection->getElapsedTime($start));
344+
308345
if (1 == (int) $result['ok'])
309346
{
310347
if (!$sequence)
@@ -391,7 +428,15 @@ public function pluck($column)
391428
*/
392429
public function delete($id = null)
393430
{
394-
$result = $this->collection->remove($this->compileWheres());
431+
$start = microtime(true);
432+
433+
$wheres = $this->compileWheres();
434+
$result = $this->collection->remove($wheres);
435+
436+
// Log query
437+
$this->connection->logQuery(
438+
$this->from . '.remove(' . json_encode($wheres) . ')',
439+
array(), $this->connection->getElapsedTime($start));
395440

396441
if (1 == (int) $result['ok'])
397442
{
@@ -414,7 +459,7 @@ public function from($collection)
414459
$this->collection = $this->connection->getCollection($collection);
415460
}
416461

417-
return $this;
462+
return parent::from($collection);
418463
}
419464

420465
/**
@@ -528,13 +573,21 @@ public function newQuery()
528573
*/
529574
protected function performUpdate($query, array $options = array())
530575
{
576+
$start = microtime(true);
577+
531578
// Default options
532579
$default = array('multiple' => true);
533580

534581
// Merge options and override default options
535582
$options = array_merge($default, $options);
536583

537-
$result = $this->collection->update($this->compileWheres(), $query, $options);
584+
$wheres = $this->compileWheres();
585+
$result = $this->collection->update($wheres, $query, $options);
586+
587+
// Log query
588+
$this->connection->logQuery(
589+
$this->from . '.update(' . json_encode($wheres) . ', ' . json_encode($query) . ', ' . json_encode($options) . ')',
590+
array(), $this->connection->getElapsedTime($start));
538591

539592
if (1 == (int) $result['ok'])
540593
{

src/Jenssegers/Mongodb/Connection.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,22 @@ protected function getDsn(array $config)
157157
}
158158
}
159159

160-
// The database name needs to be in the connection string, otherwise it will
160+
// The database name needs to be in the connection string, otherwise it will
161161
// authenticate to the admin database, which may result in permission errors.
162162
return "mongodb://" . implode(',', $hosts) . "/{$database}";
163163
}
164164

165+
/**
166+
* Get the elapsed time since a given starting point.
167+
*
168+
* @param int $start
169+
* @return float
170+
*/
171+
public function getElapsedTime($start)
172+
{
173+
return parent::getElapsedTime($start);
174+
}
175+
165176
/**
166177
* Dynamically pass methods to the connection.
167178
*

0 commit comments

Comments
 (0)