Skip to content

Commit 7d067b0

Browse files
authored
Merge pull request #391 from alcaeus/fix-count-queries-data-collector
Fix count queries in data collector
2 parents c96d2ce + 657544a commit 7d067b0

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

DataCollector/PrettyDataCollector.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,19 @@ public function collect(Request $request, Response $response, \Exception $except
100100
$query .= '.storeFile('.$log['count'].', '.$this->bsonEncode($log['options']).')';
101101
} elseif (isset($log['count'])) {
102102
$query .= '.count(';
103-
if ($log['query'] || $log['limit'] || $log['skip']) {
104-
$query .= $this->bsonEncode($log['query']);
105-
if ($log['limit'] || $log['skip']) {
106-
$query .= ', '.$this->bsonEncode($log['limit']);
107-
if ($log['skip']) {
108-
$query .= ', '.$this->bsonEncode($log['skip']);
109-
}
103+
if (isset($log['query']) || isset($log['limit']) || isset($log['skip'])) {
104+
$query .= $this->bsonEncode(isset($log['query']) ? $log['query'] : [], false);
105+
106+
$options = [];
107+
if (isset($log['limit'])) {
108+
$options['limit'] = $log['limit']['limitNum'];
109+
}
110+
if (isset($log['skip'])) {
111+
$options['skip'] = $log['skip']['limitSkip'];
112+
}
113+
114+
if (! empty($options)) {
115+
$query .= ', '.$this->bsonEncode($options, false);
110116
}
111117
}
112118
$query .= ')';
@@ -132,18 +138,18 @@ public function collect(Request $request, Response $response, \Exception $except
132138
$query .= '.execute()';
133139
} elseif (isset($log['find'])) {
134140
$query .= '.find(';
135-
if ($log['query'] || $log['fields']) {
136-
$query .= $this->bsonEncode($log['query']);
137-
if ($log['fields']) {
141+
if (isset($log['query']) || isset($log['fields'])) {
142+
$query .= $this->bsonEncode(isset($log['query']) ? $log['query'] : [], false);
143+
if (!empty($log['fields'])) {
138144
$query .= ', '.$this->bsonEncode($log['fields']);
139145
}
140146
}
141147
$query .= ')';
142148
} elseif (isset($log['findOne'])) {
143149
$query .= '.findOne(';
144-
if ($log['query'] || $log['fields']) {
145-
$query .= $this->bsonEncode($log['query']);
146-
if ($log['fields']) {
150+
if (isset($log['query']) || isset($log['fields'])) {
151+
$query .= $this->bsonEncode(isset($log['query']) ? $log['query'] : [], false);
152+
if (!empty($log['fields'])) {
147153
$query .= ', '.$this->bsonEncode($log['fields']);
148154
}
149155
}

Tests/DataCollector/PrettyDataCollectorTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,31 @@ public function getQueries()
4747
'bin data' => [
4848
['db' => 'foo', 'collection' => 'bar', 'update' => true, 'query' => ['_id' => 'foo'], 'newObj' => ['foo' => new \MongoBinData('junk data', \MongoBinData::BYTE_ARRAY)]],
4949
['use foo;', 'db.bar.update({ "_id": "foo" }, { "foo": new BinData(2, "' . base64_encode('junk data') . '") });'],
50-
]
50+
],
51+
'findWithoutQuery' => [
52+
['db' => 'foo', 'collection' => 'bar', 'find' => true, 'fields' => []],
53+
['use foo;', 'db.bar.find({ });'],
54+
],
55+
'findWithoutFields' => [
56+
['db' => 'foo', 'collection' => 'bar', 'find' => true, 'query' => ['foo' => null]],
57+
['use foo;', 'db.bar.find({ "foo": null });'],
58+
],
59+
'count' => [
60+
['db' => 'foo', 'collection' => 'bar', 'count' => true],
61+
['use foo;', 'db.bar.count();'],
62+
],
63+
'countWithQuery' => [
64+
['db' => 'foo', 'collection' => 'bar', 'count' => true, 'query' => ['foo' => null]],
65+
['use foo;', 'db.bar.count({ "foo": null });'],
66+
],
67+
'countWithSkipOnly' => [
68+
['db' => 'foo', 'collection' => 'bar', 'count' => true, 'skip' => ['skip' => true, 'limitSkip' => 5]],
69+
['use foo;', 'db.bar.count({ }, { "skip": 5 });'],
70+
],
71+
'countWithLimitOnly' => [
72+
['db' => 'foo', 'collection' => 'bar', 'count' => true, 'limit' => ['limit' => true, 'limitNum' => 3]],
73+
['use foo;', 'db.bar.count({ }, { "limit": 3 });'],
74+
],
5175
];
5276
}
5377

@@ -149,7 +173,7 @@ public function testQueryCountVsGridFsStoreFile()
149173
];
150174
$formatted = [
151175
'use foo;',
152-
'db.Route.count({ "path": "/" }, { "limit": true, "limitNum": 5 }, { "skip": true, "limitSkip": 0 });',
176+
'db.Route.count({ "path": "/" }, { "limit": 5, "skip": 0 });',
153177
'db.User.files.storeFile(5, [ ]);',
154178
];
155179

0 commit comments

Comments
 (0)