8000 [DoctrineMongoDBBundle] fixed pretty queries · jaimesuez/symfony@51106a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 51106a8

Browse files
committed
[DoctrineMongoDBBundle] fixed pretty queries
1 parent 129d7c7 commit 51106a8

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

src/Symfony/Bundle/DoctrineMongoDBBundle/Logger/DoctrineMongoDBLogger.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(LoggerInterface $logger = null, $prefix = 'MongoDB q
5454
*
5555
* @param array $query A query log array from Doctrine
5656
*/
57-
public function logQuery($query)
57+
public function logQuery(array $query)
5858
{
5959
$this->queries[] = $query;
6060
$this->processed = false;
@@ -107,6 +107,12 @@ protected function processQueries()
107107
$grouped = array();
108108
$ordered = array();
109109
foreach ($this->queries as $query) {
110+
if (!isset($query['query']) || !isset($query['fields'])) {
111+
// no grouping necessary
112+
$ordered[] = array($query);
113+
continue;
114+
}
115+
110116
$cursor = serialize($query['query']).serialize($query['fields']);
111117

112118
// append if issued from cursor (currently just "sort")
@@ -180,15 +186,21 @@ protected function processQueries()
180186
} elseif (isset($log['execute'])) {
181187
$query .= '.execute()';
182188
} elseif (isset($log['find'])) {
183-
$query .= '.find('.static::bsonEncode($log['query']);
184-
if (!empty($log['fields'])) {
185-
$query .= ', '.static::bsonEncode($log['fields']);
189+
$query .= '.find(';
190+
if ($log['query'] || $log['fields']) {
191+
$query .= static::bsonEncode($log['query']);
192+
if ($log['fields']) {
193+
$query .= ', '.static::bsonEncode($log['fields']);
194+
}
186195
}
187196
$query .= ')';
188197
} elseif (isset($log['findOne'])) {
189-
$query .= '.findOne('.static::bsonEncode($log['query']);
190-
if (!empty($log['fields'])) {
191-
$query .= ', '.static::bsonEncode($log['fields']);
198+
$query .= '.findOne(';
199+
if ($log['query'] || $log['fields']) {
200+
$query .= static::bsonEncode($log['query']);
201+
if ($log['fields']) {
202+
$query .= ', '.static::bsonEncode($log['fields']);
203+
}
192204
}
193205
$query .= ')';
194206
} elseif (isset($log['getDBRef'])) {
@@ -215,6 +227,15 @@ protected function processQueries()
215227
}
216228
}
217229
}
230+
231+
if (!empty($query)) {
232+
if ('.' == $query[0]) {
233+
$query = 'db'.$query;
234+
}
235+
236+
$this->formattedQueries[$i++] = $query.';';
237+
++$this->nbRealQueries;
238+
}
218239
}
219240

220241
static protected function bsonEncode($query, $array = true)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\DoctrineMongoDBBundle\Tests\Logger;
13+
14+
use Symfony\Bundle\DoctrineMongoDBBundle\Logger\DoctrineMongoDBLogger;
15+
16+
class DoctrineMongoDBLoggerTest extends \PHPUnit_Framework_TestCase
17+
{
18+
protected $logger;
19+
20+
protected function setUp()
21+
{
22+
$this->logger = new DoctrineMongoDBLogger();
23+
}
24+
25+
/**
26+
* @dataProvider getQueries
27+
*/
28+
public function testLogger($query, $formatted)
29+
{
30+
$this->logger->logQuery($query);
31+
32+
$this->assertEquals($formatted, $this->logger->getQueries());
33+
}
34+
35+
public function getQueries()
36+
{
37+
return array(
38+
// batchInsert
39+
array(
40+
array('db' => 'foo', 'collection' => 'bar', 'batchInsert' => true, 'num' => 1, 'data' => array('foo'), 'options' => array()),
41+
array('use foo;', 'db.bar.batchInsert(**1 item(s)**);'),
42+
),
43+
// find
44+
array(
45+
array('db' => 'foo', 'collection' => 'bar', 'find' => true, 'query' => array(), 'fields' => array()),
46+
array('use foo;', 'db.bar.find();'),
47+
),
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)
0