Skip to content

Commit d5c721c

Browse files
committed
PHPC-134: Add debug info for WriteResult
1 parent 5657ec7 commit d5c721c

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

src/MongoDB/WriteResult.c

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

4545
PHONGO_API zend_class_entry *php_phongo_writeresult_ce;
4646

47+
zend_object_handlers php_phongo_handler_writeresult;
48+
4749
/* {{{ proto integer WriteResult::getInsertedCount()
4850
Returns the number of documents that were inserted */
4951
PHP_METHOD(WriteResult, getInsertedCount)
@@ -369,17 +371,62 @@ zend_object_value php_phongo_writeresult_create_object(zend_class_entry *class_t
369371
zend_object_value retval;
370372
php_phongo_writeresult_t *intern = NULL;
371373

372-
intern = (php_phongo_writeresult_t *)emalloc(sizeof(php_phongo_writeresult_t));
373-
memset(intern, 0, sizeof(php_phongo_writeresult_t));
374+
intern = (php_phongo_writeresult_t *)ecalloc(1, sizeof *intern);
374375

375376
zend_object_std_init(&intern->result.std, class_type TSRMLS_CC);
376377
object_properties_init(&intern->result.std, class_type);
377378

378379
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_writeresult_free_object, NULL TSRMLS_CC);
379-
retval.handlers = phongo_get_std_object_handlers();
380+
retval.handlers = &php_phongo_handler_writeresult;
380381

381382
return retval;
382383
} /* }}} */
384+
385+
HashTable *php_phongo_writeresult_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
386+
{
387+
php_phongo_writeresult_t *intern;
388+
zval retval = zval_used_for_init;
389+
390+
intern = (php_phongo_writeresult_t *)zend_object_store_get_object(object TSRMLS_CC);
391+
*is_temp = 1;
392+
array_init_size(&retval, 9);
393+
394+
add_assoc_long_ex(&retval, ZEND_STRS("nInserted"), intern->nInserted);
395+
add_assoc_long_ex(&retval, ZEND_STRS("nMatched"), intern->nMatched);
396+
add_assoc_long_ex(&retval, ZEND_STRS("nModified"), intern->nModified);
397+
add_assoc_long_ex(&retval, ZEND_STRS("nRemoved"), intern->nRemoved);
398+
add_assoc_long_ex(&retval, ZEND_STRS("nUpserted"), intern->nUpserted);
399+
400+
if (intern->info) {
401+
zval_add_ref(&intern->info);
402+
add_assoc_zval_ex(&retval, ZEND_STRS("info"), intern->info);
403+
} else {
404+
add_assoc_null_ex(&retval, ZEND_STRS("info"));
405+
}
406+
407+
if (intern->upsertedIds) {
408+
zval_add_ref(&intern->upsertedIds);
409+
add_assoc_zval_ex(&retval, ZEND_STRS("upsertedIds"), intern->upsertedIds);
410+
} else {
411+
add_assoc_null_ex(&retval, ZEND_STRS("upsertedIds"));
412+
}
413+
414+
if (intern->writeErrors) {
415+
zval_add_ref(&intern->writeErrors);
416+
add_assoc_zval_ex(&retval, ZEND_STRS("writeErrors"), intern->writeErrors);
417+
} else {
418+
add_assoc_null_ex(&retval, ZEND_STRS("writeErrors"));
419+
}
420+
421+
if (intern->writeConcernError) {
422+
zval_add_ref(&intern->writeConcernError);
423+
add_assoc_zval_ex(&retval, ZEND_STRS("writeConcernError"), intern->writeConcernError);
424+
} else {
425+
add_assoc_null_ex(&retval, ZEND_STRS("writeConcernError"));
426+
}
427+
428+
return Z_ARRVAL(retval);
429+
} /* }}} */
383430
/* }}} */
384431

385432
/* {{{ PHP_MINIT_FUNCTION */
@@ -390,10 +437,12 @@ PHP_MINIT_FUNCTION(WriteResult)
390437
zend_class_entry ce;
391438

392439
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "WriteResult", php_phongo_writeresult_me);
393-
ce.create_object = php_phongo_writeresult_create_object;
394440
php_phongo_writeresult_ce = zend_register_internal_class(&ce TSRMLS_CC);
441+
php_phongo_writeresult_ce->create_object = php_phongo_writeresult_create_object;
395442
php_phongo_writeresult_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
396443

444+
memcpy(&php_phongo_handler_writeresult, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
445+
php_phongo_handler_writeresult.get_debug_info = php_phongo_writeresult_get_debug_info;
397446

398447
return SUCCESS;
399448
}

tests/standalone/server-executeWriteBatch-001.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ printf("WriteResult.server is the same: %s\n", $server == $result->getServer() ?
2222

2323
echo "\n===> WriteResult\n";
2424
printWriteResult($result);
25+
var_dump($result);
2526

2627
echo "\n===> Collection\n";
2728
$cursor = $server->executeQuery(NS, new MongoDB\Driver\Query(array()));
@@ -41,6 +42,30 @@ modifiedCount: 1
4142
upsertedCount: 1
4243
deletedCount: 1
4344
upsertedId[3]: int(3)
45+
object(MongoDB\Driver\WriteResult)#%d (9) {
46+
["nInserted"]=>
47+
int(2)
48+
["nMatched"]=>
49+
int(1)
50+
["nModified"]=>
51+
int(1)
52+
["nRemoved"]=>
53+
int(1)
54+
["nUpserted"]=>
55+
int(1)
56+
["info"]=>
57+
NULL
58+
["upsertedIds"]=>
59+
array(1) {
60+
[3]=>
61+
int(3)
62+
}
63+
["writeErrors"]=>
64+
array(0) {
65+
}
66+
["writeConcernError"]=>
67+
NULL
68+
}
4469

4570
===> Collection
4671
array(2) {

0 commit comments

Comments
 (0)