Skip to content

Commit 770c2b5

Browse files
committed
PHPC-136: var_dump() CommandResult and QueryResult
1 parent 1c31f2f commit 770c2b5

File tree

8 files changed

+342
-131
lines changed

8 files changed

+342
-131
lines changed

php_phongo.c

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <bson.h>
3131
#include <mongoc.h>
3232
#include <mongoc-cursor-cursorid-private.h>
33+
#include <mongoc-read-prefs-private.h>
3334

3435

3536
/* PHP Core stuff */
@@ -1070,6 +1071,106 @@ void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS
10701071
bson_oid_to_string(oid, intern->oid);
10711072
} /* }}} */
10721073

1074+
1075+
void php_phongo_read_preference_to_zval(zval *retval, mongoc_read_prefs_t *read_prefs) /* {{{ */
1076+
{
1077+
1078+
array_init_size(retval, 2);
1079+
1080+
add_assoc_long_ex(retval, ZEND_STRS("mode"), read_prefs->mode);
1081+
if (read_prefs->tags.len) {
1082+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
1083+
1084+
MAKE_STD_ZVAL(state.zchild);
1085+
bson_to_zval(bson_get_data(&read_prefs->tags), read_prefs->tags.len, &state);
1086+
add_assoc_zval_ex(retval, ZEND_STRS("tags"), state.zchild);
1087+
} else {
1088+
add_assoc_null_ex(retval, ZEND_STRS("tags"));
1089+
}
1090+
} /* }}} */
1091+
1092+
void php_phongo_result_to_zval(zval *retval, php_phongo_result_t *result) /* {{{ */
1093+
{
1094+
1095+
array_init_size(retval, 4);
1096+
1097+
if (result->cursor) {
1098+
zval *cursor = NULL;
1099+
1100+
MAKE_STD_ZVAL(cursor);
1101+
array_init_size(cursor, 19);
1102+
1103+
add_assoc_long_ex(cursor, ZEND_STRS("stamp"), result->cursor->stamp);
1104+
1105+
#define _ADD_BOOL(z, field) add_assoc_bool_ex(z, ZEND_STRS(#field), result->cursor->field)
1106+
_ADD_BOOL(cursor, is_command);
1107+
_ADD_BOOL(cursor, sent);
1108+
_ADD_BOOL(cursor, done);
1109+
_ADD_BOOL(cursor, failed);
1110+
_ADD_BOOL(cursor, end_of_event);
1111+
_ADD_BOOL(cursor, in_exhaust);
1112+
_ADD_BOOL(cursor, redir_primary);
1113+
_ADD_BOOL(cursor, has_fields);
1114+
#undef _ADD_BOOL
1115+
1116+
{
1117+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
1118+
1119+
MAKE_STD_ZVAL(state.zchild);
1120+
bson_to_zval(bson_get_data(&result->cursor->query), result->cursor->query.len, &state);
1121+
add_assoc_zval_ex(cursor, ZEND_STRS("query"), state.zchild);
1122+
}
1123+
{
1124+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
1125+
1126+
MAKE_STD_ZVAL(state.zchild);
1127+
bson_to_zval(bson_get_data(&result->cursor->fields), result->cursor->fields.len, &state);
1128+
add_assoc_zval_ex(cursor, ZEND_STRS("fields"), state.zchild);
1129+
}
1130+
{
1131+
zval *read_preference = NULL;
1132+
1133+
MAKE_STD_ZVAL(read_preference);
1134+
php_phongo_read_preference_to_zval(read_preference, result->cursor->read_prefs);
1135+
add_assoc_zval_ex(cursor, ZEND_STRS("read_preference"), read_preference);
1136+
}
1137+
1138+
#define _ADD_INT(z, field) add_assoc_long_ex(z, ZEND_STRS(#field), result->cursor->field)
1139+
_ADD_INT(cursor, flags);
1140+
_ADD_INT(cursor, skip);
1141+
_ADD_INT(cursor, limit);
1142+
_ADD_INT(cursor, count);
1143+
_ADD_INT(cursor, batch_size);
1144+
#undef _ADD_INT
1145+
1146+
add_assoc_string_ex(cursor, ZEND_STRS("ns"), result->cursor->ns, 1);
1147+
{
1148+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
1149+
1150+
MAKE_STD_ZVAL(state.zchild);
1151+
bson_to_zval(bson_get_data(result->cursor->current), result->cursor->current->len, &state);
1152+
add_assoc_zval_ex(cursor, ZEND_STRS("current_doc"), state.zchild);
1153+
}
1154+
add_assoc_zval_ex(retval, ZEND_STRS("cursor"), cursor);
1155+
} else {
1156+
add_assoc_null_ex(retval, ZEND_STRS("cursor"));
1157+
}
1158+
1159+
if (result->firstBatch) {
1160+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
1161+
1162+
MAKE_STD_ZVAL(state.zchild);
1163+
bson_to_zval(bson_get_data(result->firstBatch), result->firstBatch->len, &state);
1164+
add_assoc_zval_ex(retval, ZEND_STRS("firstBatch"), state.zchild);
1165+
} else {
1166+
add_assoc_null_ex(retval, ZEND_STRS("firstBatch"));
1167+
}
1168+
add_assoc_long_ex(retval, ZEND_STRS("hint"), result->hint);
1169+
add_assoc_bool_ex(retval, ZEND_STRS("is_command_cursor"), result->is_command_cursor);
1170+
1171+
} /* }}} */
1172+
1173+
10731174
void php_phongo_new_utcdatetime_from_epoch(zval *object, int64_t msec_since_epoch TSRMLS_DC) /* {{{ */
10741175
{
10751176
php_phongo_utcdatetime_t *intern;
@@ -1426,10 +1527,14 @@ PHP_MINIT_FUNCTION(phongo)
14261527

14271528
/* Prep default object handlers to be used when we register the classes */
14281529
memcpy(&phongo_std_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1429-
phongo_std_object_handlers.clone_obj = NULL;
1430-
phongo_std_object_handlers.read_property = NULL;
1431-
phongo_std_object_handlers.write_property = NULL;
1432-
phongo_std_object_handlers.get_debug_info = NULL;
1530+
phongo_std_object_handlers.clone_obj = NULL;
1531+
/*
1532+
phongo_std_object_handlers.get_debug_info = NULL;
1533+
phongo_std_object_handlers.compare_objects = NULL;
1534+
phongo_std_object_handlers.cast_object = NULL;
1535+
phongo_std_object_handlers.count_elements = NULL;
1536+
phongo_std_object_handlers.get_closure = NULL;
1537+
*/
14331538

14341539
PHP_MINIT(bson)(INIT_FUNC_ARGS_PASSTHRU);
14351540

php_phongo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_preference
117117
mongoc_write_concern_t* phongo_write_concern_from_zval (zval *zwrite_concern TSRMLS_DC);
118118
php_phongo_query_t* phongo_query_from_zval (zval *zquery TSRMLS_DC);
119119

120+
void php_phongo_read_preference_to_zval(zval *retval, mongoc_read_prefs_t *read_prefs);
121+
void php_phongo_result_to_zval(zval *retval, php_phongo_result_t *result);
120122

121123
void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS_DC);
122124
void php_phongo_cursor_new_from_result(zval *object, php_phongo_result_t *result TSRMLS_DC);

src/MongoDB/CommandResult.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
PHONGO_API zend_class_entry *php_phongo_commandresult_ce;
4646

47+
zend_object_handlers php_phongo_handler_commandresult;
48+
4749
/* {{{ proto MongoDB\Driver\CommandResult CommandResult::__construct(MongoDB\Driver\Server $server, array|object $responseDocument)
4850
Constructs a new CommandResult */
4951
PHP_METHOD(CommandResult, __construct)
@@ -182,16 +184,6 @@ PHP_METHOD(CommandResult, getServer)
182184
}
183185
/* }}} */
184186

185-
/**
186-
* Result returned by Server and Manager executeCommand() methods.
187-
*
188-
* This object wraps an OP_REPLY. It is constructed after a command is executed
189-
* on the server but before a Cursor is created in the driver (if applicable).
190-
* This allows the Cursor implementation to be customized.
191-
*
192-
* For commands that do not support cursors (i.e. most commands), getIterator()
193-
* should return a cursor consisting of a single document, the command result.
194-
*/
195187
/* {{{ MongoDB\Driver\CommandResult */
196188

197189
ZEND_BEGIN_ARG_INFO_EX(ai_CommandResult___construct, 0, 0, 2)
@@ -244,34 +236,51 @@ static void php_phongo_commandresult_free_object(void *object TSRMLS_DC) /* {{{
244236

245237
zend_object_value php_phongo_commandresult_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
246238
{
247-
zend_object_value retval;
239+
zend_object_value retval;
248240
php_phongo_commandresult_t *intern = NULL;
249241

250-
intern = (php_phongo_commandresult_t *)emalloc(sizeof(php_phongo_commandresult_t));
251-
memset(intern, 0, sizeof(php_phongo_commandresult_t));
242+
intern = (php_phongo_commandresult_t *)ecalloc(1, sizeof *intern);
252243

253244
zend_object_std_init(&intern->result.std, class_type TSRMLS_CC);
254245
object_properties_init(&intern->result.std, class_type);
255246

256247
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_commandresult_free_object, NULL TSRMLS_CC);
257-
retval.handlers = phongo_get_std_object_handlers();
248+
retval.handlers = &php_phongo_handler_commandresult;
258249

259250
return retval;
260251
} /* }}} */
252+
253+
HashTable *php_phongo_commandresult_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
254+
{
255+
php_phongo_commandresult_t *intern;
256+
zval retval = zval_used_for_init;
257+
258+
259+
*is_temp = 1;
260+
intern = (php_phongo_commandresult_t *)zend_object_store_get_object(object TSRMLS_CC);
261+
262+
php_phongo_result_to_zval(&retval, &intern->result);
263+
264+
return Z_ARRVAL(retval);
265+
266+
} /* }}} */
261267
/* }}} */
262268

263269
/* {{{ PHP_MINIT_FUNCTION */
264270
PHP_MINIT_FUNCTION(CommandResult)
265271
{
266-
(void)type; /* We don't care if we are loaded via dl() or extension= */
272+
(void)type; (void)module_number;
267273
zend_class_entry ce;
268274

269275
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "CommandResult", php_phongo_commandresult_me);
270-
ce.create_object = php_phongo_commandresult_create_object;
271276
php_phongo_commandresult_ce = zend_register_internal_class(&ce TSRMLS_CC);
277+
php_phongo_commandresult_ce->create_object = php_phongo_commandresult_create_object;
272278
php_phongo_commandresult_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
273279
php_phongo_commandresult_ce->get_iterator = phongo_result_get_iterator;
274280

281+
memcpy(&php_phongo_handler_commandresult, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
282+
php_phongo_handler_commandresult.get_debug_info = php_phongo_commandresult_get_debug_info;
283+
275284
zend_class_implements(php_phongo_commandresult_ce TSRMLS_CC, 1, zend_ce_aggregate);
276285

277286

src/MongoDB/Cursor.c

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <bson.h>
2929
#include <mongoc.h>
3030
#include <mongoc-cursor-private.h>
31-
#include <mongoc-read-prefs-private.h>
3231

3332
/* PHP Core stuff */
3433
#include <php.h>
@@ -446,104 +445,6 @@ zend_object_value php_phongo_cursor_create_object(zend_class_entry *class_type T
446445
return retval;
447446
} /* }}} */
448447

449-
void php_phongo_read_preference_to_zval(zval *retval, mongoc_read_prefs_t *read_prefs) /* {{{ */
450-
{
451-
452-
array_init_size(retval, 2);
453-
454-
add_assoc_long_ex(retval, ZEND_STRS("mode"), read_prefs->mode);
455-
if (read_prefs->tags.len) {
456-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
457-
458-
MAKE_STD_ZVAL(state.zchild);
459-
bson_to_zval(bson_get_data(&read_prefs->tags), read_prefs->tags.len, &state);
460-
add_assoc_zval_ex(retval, ZEND_STRS("tags"), state.zchild);
461-
} else {
462-
add_assoc_null_ex(retval, ZEND_STRS("tags"));
463-
}
464-
} /* }}} */
465-
466-
void php_phongo_result_to_zval(zval *retval, php_phongo_result_t *result) /* {{{ */
467-
{
468-
469-
array_init_size(retval, 4);
470-
471-
if (result->cursor) {
472-
zval *cursor = NULL;
473-
474-
MAKE_STD_ZVAL(cursor);
475-
array_init_size(cursor, 16);
476-
477-
add_assoc_long_ex(cursor, ZEND_STRS("stamp"), result->cursor->stamp);
478-
479-
#define _ADD_BOOL(z, field) add_assoc_bool_ex(z, ZEND_STRS(#field), result->cursor->field)
480-
_ADD_BOOL(cursor, is_command);
481-
_ADD_BOOL(cursor, sent);
482-
_ADD_BOOL(cursor, done);
483-
_ADD_BOOL(cursor, failed);
484-
_ADD_BOOL(cursor, end_of_event);
485-
_ADD_BOOL(cursor, in_exhaust);
486-
_ADD_BOOL(cursor, redir_primary);
487-
_ADD_BOOL(cursor, has_fields);
488-
#undef _ADD_BOOL
489-
490-
{
491-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
492-
493-
MAKE_STD_ZVAL(state.zchild);
494-
bson_to_zval(bson_get_data(&result->cursor->query), result->cursor->query.len, &state);
495-
add_assoc_zval_ex(cursor, ZEND_STRS("query"), state.zchild);
496-
}
497-
{
498-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
499-
500-
MAKE_STD_ZVAL(state.zchild);
501-
bson_to_zval(bson_get_data(&result->cursor->fields), result->cursor->fields.len, &state);
502-
add_assoc_zval_ex(cursor, ZEND_STRS("fields"), state.zchild);
503-
}
504-
{
505-
zval *read_preference = NULL;
506-
507-
MAKE_STD_ZVAL(read_preference);
508-
php_phongo_read_preference_to_zval(read_preference, result->cursor->read_prefs);
509-
add_assoc_zval_ex(cursor, ZEND_STRS("read_preference"), read_preference);
510-
}
511-
512-
#define _ADD_INT(z, field) add_assoc_long_ex(z, ZEND_STRS(#field), result->cursor->field)
513-
_ADD_INT(cursor, flags);
514-
_ADD_INT(cursor, skip);
515-
_ADD_INT(cursor, limit);
516-
_ADD_INT(cursor, count);
517-
_ADD_INT(cursor, batch_size);
518-
#undef _ADD_INT
519-
520-
add_assoc_string_ex(cursor, ZEND_STRS("ns"), result->cursor->ns, 1);
521-
{
522-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
523-
524-
MAKE_STD_ZVAL(state.zchild);
525-
bson_to_zval(bson_get_data(result->cursor->current), result->cursor->current->len, &state);
526-
add_assoc_zval_ex(cursor, ZEND_STRS("current_doc"), state.zchild);
527-
}
528-
add_assoc_zval_ex(retval, ZEND_STRS("cursor"), cursor);
529-
} else {
530-
add_assoc_null_ex(retval, ZEND_STRS("cursor"));
531-
}
532-
533-
if (result->firstBatch) {
534-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
535-
536-
MAKE_STD_ZVAL(state.zchild);
537-
bson_to_zval(bson_get_data(result->firstBatch), result->firstBatch->len, &state);
538-
add_assoc_zval_ex(retval, ZEND_STRS("firstBatch"), state.zchild);
539-
} else {
540-
add_assoc_null_ex(retval, ZEND_STRS("firstBatch"));
541-
}
542-
add_assoc_long_ex(retval, ZEND_STRS("hint"), result->hint);
543-
add_assoc_bool_ex(retval, ZEND_STRS("is_command_cursor"), result->is_command_cursor);
544-
545-
} /* }}} */
546-
547448
HashTable *php_phongo_cursor_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
548449
{
549450
php_phongo_cursor_t *intern;

0 commit comments

Comments
 (0)