Skip to content

PHPC-539: Include read concern in Query debug output #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/MongoDB/Query.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,23 @@ HashTable *php_phongo_query_get_debug_info(zval *object, int *is_temp TSRMLS_DC)
ADD_ASSOC_LONG_EX(&retval, "limit", intern->limit);
ADD_ASSOC_LONG_EX(&retval, "batch_size", intern->batch_size);

if (intern->read_concern) {
#if PHP_VERSION_ID >= 70000
zval read_concern;

php_phongo_read_concern_to_zval(&read_concern, intern->read_concern);
ADD_ASSOC_ZVAL_EX(&retval, "readConcern", &read_concern);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does ADD_ASSOC_ZVAL_EX copy the read_concern zval? If not, it's value is going to be out of scope when the function ends. I would at least run the tests covering this with valgrind.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valgrind reported no issues. I poked into PHP 7's code for add_assoc_zval_ex() and it does appear to copy the zval argument. See: http://lxr.php.net/xref/PHP_7_0/Zend/zend_hash.c#552 and references to the pData argument therein. It's used with ZVAL_COPY_VALUE() when assigning the index.

#else
zval *read_concern = NULL;
MAKE_STD_ZVAL(read_concern);

php_phongo_read_concern_to_zval(read_concern, intern->read_concern);
ADD_ASSOC_ZVAL_EX(&retval, "readConcern", read_concern);
#endif
} else {
ADD_ASSOC_NULL_EX(&retval, "readConcern");
}

return Z_ARRVAL(retval);

} /* }}} */
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/query-sort-003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ object(MongoDB\Driver\Query)#%d (%d) {
int(0)
["batch_size"]=>
int(0)
["readConcern"]=>
NULL
}
string(21) "MongoDB\Driver\Cursor"
aaliyah.kertzmann
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/query-sort-004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ foreach ($cursor as $document) {
<?php exit(0); ?>
--EXPECTF--
Inserted: 5
object(MongoDB\Driver\Query)#%d (6) {
object(MongoDB\Driver\Query)#%d (%d) {
["query"]=>
object(stdClass)#%d (2) {
["$orderby"]=>
Expand All @@ -62,6 +62,8 @@ object(MongoDB\Driver\Query)#%d (6) {
int(0)
["batch_size"]=>
int(0)
["readConcern"]=>
NULL
}
0
1
Expand Down
65 changes: 65 additions & 0 deletions tests/query/query-debug-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
MongoDB\Driver\Query debug output
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

var_dump(new MongoDB\Driver\Query(
['a' => 123],
[
'limit' => 5,
'modifiers' => [
'$comment' => 'foo',
'$maxTimeMS' => 500,
],
'projection' => ['c' => 1],
'readConcern' => new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::LOCAL),
'skip' => 10,
'sort' => ['b' => -1],
]
));

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
object(MongoDB\Driver\Query)#%d (%d) {
["query"]=>
object(stdClass)#%d (%d) {
["$comment"]=>
string(3) "foo"
["$maxTimeMS"]=>
int(500)
["$orderby"]=>
object(stdClass)#%d (%d) {
["b"]=>
int(-1)
}
["$query"]=>
object(stdClass)#%d (%d) {
["a"]=>
int(123)
}
}
["selector"]=>
object(stdClass)#%d (%d) {
["c"]=>
int(1)
}
["flags"]=>
int(0)
["skip"]=>
int(10)
["limit"]=>
int(5)
["batch_size"]=>
int(0)
["readConcern"]=>
array(1) {
["level"]=>
string(5) "local"
}
}
===DONE===