Skip to content

Commit f97f3ef

Browse files
authored
PHPC-2094: Relay command comment when constructing a cursor (#1322)
1 parent 8823d1f commit f97f3ef

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/phongo_execute.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,8 @@ bool phongo_execute_command(zval* manager, php_phongo_command_type_t type, const
498498
if (bson_iter_init_find(&iter, &reply, "cursor") && BSON_ITER_HOLDS_DOCUMENT(&iter)) {
499499
bson_t initial_reply = BSON_INITIALIZER;
500500
bson_t cursor_opts = BSON_INITIALIZER;
501-
bson_error_t error = { 0 };
501+
bson_iter_t iter;
502+
bson_error_t error = { 0 };
502503

503504
bson_copy_to(&reply, &initial_reply);
504505

@@ -514,6 +515,10 @@ bool phongo_execute_command(zval* manager, php_phongo_command_type_t type, const
514515
bson_append_int64(&cursor_opts, "batchSize", -1, command->batch_size);
515516
}
516517

518+
if (bson_iter_init(&iter, command->bson) && bson_iter_find(&iter, "comment")) {
519+
bson_append_value(&cursor_opts, "comment", -1, bson_iter_value(&iter));
520+
}
521+
517522
if (zsession && !mongoc_client_session_append(Z_SESSION_OBJ_P(zsession)->client_session, &cursor_opts, &error)) {
518523
phongo_throw_exception_from_bson_error_t(&error);
519524
bson_destroy(&initial_reply);

tests/command/cursor-comment-001.phpt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
--TEST--
2+
MongoDB\Driver\Command comment applies to getMore
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_server_version('<', '4.4'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
class Test implements MongoDB\Driver\Monitoring\CommandSubscriber
13+
{
14+
public function executeCommand()
15+
{
16+
MongoDB\Driver\Monitoring\addSubscriber($this);
17+
18+
$manager = create_test_manager();
19+
20+
$bulkWrite = new MongoDB\Driver\BulkWrite;
21+
22+
for ($i = 0; $i < 5; $i++) {
23+
$bulkWrite->insert(['_id' => $i]);
24+
}
25+
26+
$writeResult = $manager->executeBulkWrite(NS, $bulkWrite);
27+
printf("Inserted: %d\n", $writeResult->getInsertedCount());
28+
29+
$command = new MongoDB\Driver\Command([
30+
'aggregate' => COLLECTION_NAME,
31+
'pipeline' => [['$match' => new stdClass]],
32+
'comment' => ['foo' => 1],
33+
'cursor' => ['batchSize' => 2]
34+
]);
35+
$cursor = $manager->executeCommand(DATABASE_NAME, $command);
36+
37+
$cursor->toArray();
38+
39+
MongoDB\Driver\Monitoring\removeSubscriber($this);
40+
}
41+
42+
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event)
43+
{
44+
$command = $event->getCommand();
45+
46+
if ($event->getCommandName() === 'aggregate' || $event->getCommandName() === 'getMore') {
47+
printf("%s command includes comment: %s\n", $event->getCommandName(), json_encode($command->comment));
48+
}
49+
}
50+
51+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event)
52+
{
53+
}
54+
55+
public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event)
56+
{
57+
}
58+
}
59+
60+
(new Test)->executeCommand();
61+
62+
?>
63+
===DONE===
64+
<?php exit(0); ?>
65+
--EXPECT--
66+
Inserted: 5
67+
aggregate command includes comment: {"foo":1}
68+
getMore command includes comment: {"foo":1}
69+
getMore command includes comment: {"foo":1}
70+
===DONE===

0 commit comments

Comments
 (0)