Skip to content

Commit 7626368

Browse files
committed
PHPC-139: var_dump()ing WriteBatch
1 parent 4b8ef30 commit 7626368

File tree

6 files changed

+200
-23
lines changed

6 files changed

+200
-23
lines changed

php_phongo.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,32 @@ void php_phongo_read_preference_to_zval(zval *retval, mongoc_read_prefs_t *read_
10891089
}
10901090
} /* }}} */
10911091

1092+
void php_phongo_write_concern_to_zval(zval *retval, mongoc_write_concern_t *write_concern) /* {{{ */
1093+
{
1094+
char *wtag;
1095+
1096+
array_init_size(retval, 5);
1097+
1098+
if (mongoc_write_concern_get_w(write_concern) == MONGOC_WRITE_CONCERN_W_DEFAULT) {
1099+
return;
1100+
}
1101+
1102+
wtag = (char *)mongoc_write_concern_get_wtag(write_concern);
1103+
if (wtag) {
1104+
add_assoc_string_ex(retval, ZEND_STRS("w"), wtag, 1);
1105+
} else {
1106+
if (!mongoc_write_concern_get_wmajority(write_concern)) {
1107+
add_assoc_long_ex(retval, ZEND_STRS("w"), mongoc_write_concern_get_w(write_concern));
1108+
}
1109+
}
1110+
1111+
add_assoc_bool_ex(retval, ZEND_STRS("wmajority"), mongoc_write_concern_get_wmajority(write_concern));
1112+
add_assoc_long_ex(retval, ZEND_STRS("wtimeout"), mongoc_write_concern_get_wtimeout(write_concern));
1113+
add_assoc_bool_ex(retval, ZEND_STRS("fsync"), mongoc_write_concern_get_fsync(write_concern));
1114+
add_assoc_bool_ex(retval, ZEND_STRS("journal"), mongoc_write_concern_get_journal(write_concern));
1115+
} /* }}} */
1116+
1117+
10921118
void php_phongo_result_to_zval(zval *retval, php_phongo_result_t *result) /* {{{ */
10931119
{
10941120

@@ -1171,6 +1197,7 @@ void php_phongo_result_to_zval(zval *retval, php_phongo_result_t *result) /* {{{
11711197
} /* }}} */
11721198

11731199

1200+
11741201
void php_phongo_new_utcdatetime_from_epoch(zval *object, int64_t msec_since_epoch TSRMLS_DC) /* {{{ */
11751202
{
11761203
php_phongo_utcdatetime_t *intern;

php_phongo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ mongoc_write_concern_t* phongo_write_concern_from_zval (zval *zwrite_concern T
118118
php_phongo_query_t* phongo_query_from_zval (zval *zquery TSRMLS_DC);
119119

120120
void php_phongo_read_preference_to_zval(zval *retval, mongoc_read_prefs_t *read_prefs);
121+
void php_phongo_write_concern_to_zval(zval *retval, mongoc_write_concern_t *write_concern);
121122
void php_phongo_result_to_zval(zval *retval, php_phongo_result_t *result);
122123

123124
void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS_DC);

src/MongoDB/WriteBatch.c

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
PHONGO_API zend_class_entry *php_phongo_writebatch_ce;
5252

53+
zend_object_handlers php_phongo_handler_writebatch;
54+
5355
/* {{{ proto MongoDB\Driver\WriteBatch WriteBatch::__construct(boolean $ordered)
5456
Constructs a new WriteBatch */
5557
PHP_METHOD(WriteBatch, __construct)
@@ -292,29 +294,72 @@ zend_object_value php_phongo_writebatch_create_object(zend_class_entry *class_ty
292294
zend_object_value retval;
293295
php_phongo_writebatch_t *intern = NULL;
294296

295-
intern = (php_phongo_writebatch_t *)emalloc(sizeof(php_phongo_writebatch_t));
296-
memset(intern, 0, sizeof(php_phongo_writebatch_t));
297+
intern = (php_phongo_writebatch_t *)ecalloc(1, sizeof *intern);
297298

298299
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
299300
object_properties_init(&intern->std, class_type);
300301

301302
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_writebatch_free_object, NULL TSRMLS_CC);
302-
retval.handlers = phongo_get_std_object_handlers();
303+
retval.handlers = &php_phongo_handler_writebatch;
303304

304305
return retval;
305306
} /* }}} */
307+
308+
HashTable *php_phongo_writebatch_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
309+
{
310+
zval retval = zval_used_for_init;
311+
php_phongo_writebatch_t *intern = NULL;
312+
313+
314+
*is_temp = 1;
315+
intern = (php_phongo_writebatch_t *)zend_object_store_get_object(object TSRMLS_CC);
316+
array_init(&retval);
317+
318+
if (intern->batch->database) {
319+
add_assoc_string_ex(&retval, ZEND_STRS("database"), intern->batch->database, 1);
320+
} else {
321+
add_assoc_null_ex(&retval, ZEND_STRS("database"));
322+
}
323+
324+
if (intern->batch->collection) {
325+
add_assoc_string_ex(&retval, ZEND_STRS("collection"), intern->batch->collection, 1);
326+
} else {
327+
add_assoc_null_ex(&retval, ZEND_STRS("collection"));
328+
}
329+
330+
add_assoc_bool_ex(&retval, ZEND_STRS("ordered"), intern->batch->ordered);
331+
add_assoc_bool_ex(&retval, ZEND_STRS("executed"), intern->batch->executed);
332+
add_assoc_long_ex(&retval, ZEND_STRS("hint"), intern->batch->hint);
333+
334+
if (intern->batch->write_concern) {
335+
zval *write_concern = NULL;
336+
MAKE_STD_ZVAL(write_concern);
337+
338+
php_phongo_write_concern_to_zval(write_concern, intern->batch->write_concern);
339+
add_assoc_zval_ex(&retval, ZEND_STRS("write_concern"), write_concern);
340+
} else {
341+
add_assoc_null_ex(&retval, ZEND_STRS("write_concern"));
342+
}
343+
344+
345+
346+
return Z_ARRVAL(retval);
347+
} /* }}} */
306348
/* }}} */
307349

308350
/* {{{ PHP_MINIT_FUNCTION */
309351
PHP_MINIT_FUNCTION(WriteBatch)
310352
{
311-
(void)type; /* We don't care if we are loaded via dl() or extension= */
312-
(void)module_number; /* We don't care if we are loaded via dl() or extension= */
353+
(void)type; (void)module_number;
313354
zend_class_entry ce;
314355

315356
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "WriteBatch", php_phongo_writebatch_me);
316-
ce.create_object = php_phongo_writebatch_create_object;
317357
php_phongo_writebatch_ce = zend_register_internal_class(&ce TSRMLS_CC);
358+
php_phongo_writebatch_ce->create_object = php_phongo_writebatch_create_object;
359+
php_phongo_writebatch_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
360+
361+
memcpy(&php_phongo_handler_writebatch, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
362+
php_phongo_handler_writebatch.get_debug_info = php_phongo_writebatch_get_debug_info;
318363

319364
zend_class_implements(php_phongo_writebatch_ce TSRMLS_CC, 1, spl_ce_Countable);
320365

src/MongoDB/WriteConcern.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,25 +158,10 @@ HashTable *php_phongo_writeconcern_get_debug_info(zval *object, int *is_temp TSR
158158
{
159159
zval retval = zval_used_for_init;
160160
mongoc_write_concern_t *write_concern = phongo_write_concern_from_zval(object TSRMLS_CC);
161-
char *wtag;
162161

163162

164163
*is_temp = 1;
165-
array_init(&retval);
166-
167-
wtag = (char *)mongoc_write_concern_get_wtag(write_concern);
168-
if (wtag) {
169-
add_assoc_string_ex(&retval, ZEND_STRS("w"), wtag, 1);
170-
} else {
171-
if (!mongoc_write_concern_get_wmajority(write_concern)) {
172-
add_assoc_long_ex(&retval, ZEND_STRS("w"), mongoc_write_concern_get_w(write_concern));
173-
}
174-
}
175-
add_assoc_bool_ex(&retval, ZEND_STRS("wmajority"), mongoc_write_concern_get_wmajority(write_concern));
176-
add_assoc_long_ex(&retval, ZEND_STRS("wtimeout"), mongoc_write_concern_get_wtimeout(write_concern));
177-
add_assoc_bool_ex(&retval, ZEND_STRS("fsync"), mongoc_write_concern_get_fsync(write_concern));
178-
add_assoc_bool_ex(&retval, ZEND_STRS("journal"), mongoc_write_concern_get_journal(write_concern));
179-
164+
php_phongo_write_concern_to_zval(&retval, write_concern);
180165

181166
return Z_ARRVAL(retval);
182167
} /* }}} */

tests/batch/write-0001.phpt

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@ require_once "tests/utils/basic.inc";
99
$mc = new MongoDB\Driver\Manager(MONGODB_URI);
1010

1111
$batch = new MongoDB\Driver\WriteBatch;
12+
var_dump($batch);
1213

1314
$batch->insert(array("my" => "value"));
1415
$batch->insert(array("my" => "value", "foo" => "bar"));
1516
$batch->insert(array("my" => "value", "foo" => "bar"));
17+
var_dump($batch);
1618

1719
$batch->delete(array("my" => "value", "foo" => "bar"), array("limit" => 1));
20+
var_dump($batch);
1821

1922
$batch->update(array("foo" => "bar"), array('$set' => array("foo" => "baz")), array("limit" => 1, "upsert" => 0));
2023

24+
var_dump($batch);
25+
2126
$retval = $mc->executeWriteBatch(NS, $batch);
2227

28+
var_dump($batch);
29+
2330
printf("Inserted: %d\n", getInsertCount($retval));
2431
printf("Deleted: %d\n", getDeletedCount($retval));
2532
printf("Updated: %d\n", getModifiedCount($retval));
@@ -30,7 +37,78 @@ foreach(getWriteErrors($retval) as $error) {
3037
?>
3138
===DONE===
3239
<?php exit(0); ?>
33-
--EXPECT--
40+
--EXPECTF--
41+
object(MongoDB\Driver\WriteBatch)#%d (%d) {
42+
["database"]=>
43+
NULL
44+
["collection"]=>
45+
NULL
46+
["ordered"]=>
47+
bool(true)
48+
["executed"]=>
49+
bool(false)
50+
["hint"]=>
51+
int(0)
52+
["write_concern"]=>
53+
NULL
54+
}
55+
object(MongoDB\Driver\WriteBatch)#%d (%d) {
56+
["database"]=>
57+
NULL
58+
["collection"]=>
59+
NULL
60+
["ordered"]=>
61+
bool(true)
62+
["executed"]=>
63+
bool(false)
64+
["hint"]=>
65+
int(0)
66+
["write_concern"]=>
67+
NULL
68+
}
69+
object(MongoDB\Driver\WriteBatch)#%d (%d) {
70+
["database"]=>
71+
NULL
72+
["collection"]=>
73+
NULL
74+
["ordered"]=>
75+
bool(true)
76+
["executed"]=>
77+
bool(false)
78+
["hint"]=>
79+
int(0)
80+
["write_concern"]=>
81+
NULL
82+
}
83+
object(MongoDB\Driver\WriteBatch)#%d (%d) {
84+
["database"]=>
85+
NULL
86+
["collection"]=>
87+
NULL
88+
["ordered"]=>
89+
bool(true)
90+
["executed"]=>
91+
bool(false)
92+
["hint"]=>
93+
int(0)
94+
["write_concern"]=>
95+
NULL
96+
}
97+
object(MongoDB\Driver\WriteBatch)#%d (%d) {
98+
["database"]=>
99+
string(6) "phongo"
100+
["collection"]=>
101+
string(16) "batch_write_0001"
102+
["ordered"]=>
103+
bool(true)
104+
["executed"]=>
105+
bool(true)
106+
["hint"]=>
107+
int(0)
108+
["write_concern"]=>
109+
array(0) {
110+
}
111+
}
34112
Inserted: 3
35113
Deleted: 1
36114
Updated: 1

tests/batch/write-0002.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ $hayley_id = $insertBatch->insert($hayley);
1919
$w = 1;
2020
$wtimeout = 1000;
2121
$writeConcern = new \MongoDB\Driver\WriteConcern($w, $wtimeout);
22+
var_dump($insertBatch);
2223
$result = $mm->executeWriteBatch("db.collection", $insertBatch, $writeConcern);
24+
var_dump($insertBatch);
2325

2426
assert($result instanceof \MongoDB\Driver\WriteResult);
2527

@@ -33,6 +35,45 @@ printf("hannes: %s\nhayley: %s\n", $hannes_id, $hayley_id);
3335
===DONE===
3436
<?php exit(0); ?>
3537
--EXPECTF--
38+
object(MongoDB\Driver\WriteBatch)#%d (%d) {
39+
["database"]=>
40+
NULL
41+
["collection"]=>
42+
NULL
43+
["ordered"]=>
44+
bool(true)
45+
["executed"]=>
46+
bool(false)
47+
["hint"]=>
48+
int(0)
49+
["write_concern"]=>
50+
NULL
51+
}
52+
object(MongoDB\Driver\WriteBatch)#%d (%d) {
53+
["database"]=>
54+
string(2) "db"
55+
["collection"]=>
56+
string(10) "collection"
57+
["ordered"]=>
58+
bool(true)
59+
["executed"]=>
60+
bool(true)
61+
["hint"]=>
62+
int(0)
63+
["write_concern"]=>
64+
array(5) {
65+
["w"]=>
66+
int(1)
67+
["wmajority"]=>
68+
bool(false)
69+
["wtimeout"]=>
70+
int(1000)
71+
["fsync"]=>
72+
bool(false)
73+
["journal"]=>
74+
bool(false)
75+
}
76+
}
3677
Inserted 2 documents to %s
3778
hannes: %s
3879
hayley: %s

0 commit comments

Comments
 (0)