Skip to content

Commit 4a48529

Browse files
committed
PHPC-2494: Relocate error fields to BulkWriteCommandException
Revise the stubs to reflect that executeBulkWriteCommand() always returns a BulkWriteCommandResult. If the result is unacknowledged, that is reported via isAcknowledged() and other methods can throw, which is consistent with WriteResult for the legacy bulk write API. If the result is empty on error (i.e. no writes were successful), BulkWriteCommandException::$partialResult is left unset.
1 parent 1afeeff commit 4a48529

24 files changed

+461
-373
lines changed

src/MongoDB/BulkWriteCommandResult.c

+14-171
Original file line numberDiff line numberDiff line change
@@ -39,86 +39,6 @@
3939

4040
zend_class_entry* php_phongo_bulkwritecommandresult_ce;
4141

42-
/* Populates return_value with a list of WriteConcernError objects. Returns true
43-
* on success; otherwise, false is returned and an exception is thrown. */
44-
static bool phongo_bulkwritecommandresult_get_writeconcernerrors(php_phongo_bulkwritecommandresult_t* intern, zval* return_value)
45-
{
46-
bson_iter_t iter;
47-
48-
array_init(return_value);
49-
50-
if (intern->write_concern_errors && bson_iter_init(&iter, intern->write_concern_errors)) {
51-
while (bson_iter_next(&iter)) {
52-
bson_t bson;
53-
uint32_t len;
54-
const uint8_t* data;
55-
zval write_concern_error;
56-
57-
if (!BSON_ITER_HOLDS_DOCUMENT(&iter)) {
58-
continue;
59-
}
60-
61-
bson_iter_document(&iter, &len, &data);
62-
63-
if (!bson_init_static(&bson, data, len)) {
64-
continue;
65-
}
66-
67-
if (!phongo_writeconcernerror_init(&write_concern_error, &bson)) {
68-
/* Exception already thrown */
69-
zval_ptr_dtor(&write_concern_error);
70-
return false;
71-
}
72-
73-
add_next_index_zval(return_value, &write_concern_error);
74-
}
75-
}
76-
77-
return true;
78-
}
79-
80-
/* Populates return_value with a map of WriteError objects indexed by the offset
81-
* of the corresponding operation. Returns true on success; otherwise, false is
82-
* returned and an exception is thrown. */
83-
static bool phongo_bulkwritecommandresult_get_writeerrors(php_phongo_bulkwritecommandresult_t* intern, zval* return_value)
84-
{
85-
bson_iter_t iter;
86-
87-
array_init(return_value);
88-
89-
if (intern->write_errors && bson_iter_init(&iter, intern->write_errors)) {
90-
while (bson_iter_next(&iter)) {
91-
bson_t bson;
92-
uint32_t len;
93-
const uint8_t* data;
94-
zval write_error;
95-
zend_ulong index;
96-
97-
if (!BSON_ITER_HOLDS_DOCUMENT(&iter)) {
98-
continue;
99-
}
100-
101-
bson_iter_document(&iter, &len, &data);
102-
103-
if (!bson_init_static(&bson, data, len)) {
104-
continue;
105-
}
106-
107-
index = (zend_ulong) ZEND_STRTOUL(bson_iter_key(&iter), NULL, 10);
108-
109-
if (!phongo_writeerror_init_ex(&write_error, &bson, (int32_t) index)) {
110-
/* Exception already thrown */
111-
zval_ptr_dtor(&write_error);
112-
return false;
113-
}
114-
115-
add_index_zval(return_value, index, &write_error);
116-
}
117-
}
118-
119-
return true;
120-
}
121-
12242
PHONGO_DISABLED_CONSTRUCTOR(MongoDB_Driver_BulkWriteCommandResult)
12343

12444
/* Returns the number of documents that were inserted */
@@ -236,43 +156,6 @@ static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, getDeleteResults)
236156
}
237157
}
238158

239-
/* Return any write concern errors that occurred */
240-
static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, getWriteConcernErrors)
241-
{
242-
php_phongo_bulkwritecommandresult_t* intern;
243-
244-
intern = Z_BULKWRITECOMMANDRESULT_OBJ_P(getThis());
245-
246-
PHONGO_PARSE_PARAMETERS_NONE();
247-
248-
phongo_bulkwritecommandresult_get_writeconcernerrors(intern, return_value);
249-
}
250-
251-
/* Returns any write errors that occurred */
252-
static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, getWriteErrors)
253-
{
254-
php_phongo_bulkwritecommandresult_t* intern;
255-
256-
intern = Z_BULKWRITECOMMANDRESULT_OBJ_P(getThis());
257-
258-
PHONGO_PARSE_PARAMETERS_NONE();
259-
260-
phongo_bulkwritecommandresult_get_writeerrors(intern, return_value);
261-
}
262-
263-
static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, getErrorReply)
264-
{
265-
php_phongo_bulkwritecommandresult_t* intern;
266-
267-
intern = Z_BULKWRITECOMMANDRESULT_OBJ_P(getThis());
268-
269-
PHONGO_PARSE_PARAMETERS_NONE();
270-
271-
if (intern->error_reply) {
272-
phongo_document_new(return_value, intern->error_reply, true);
273-
}
274-
}
275-
276159
/* Returns whether the write operation was acknowledged (based on the write
277160
concern). */
278161
static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, isAcknowledged)
@@ -298,13 +181,6 @@ static void php_phongo_bulkwritecommandresult_free_object(zend_object* object)
298181
bson_destroy(intern->insert_results);
299182
bson_destroy(intern->update_results);
300183
bson_destroy(intern->delete_results);
301-
bson_destroy(intern->error_reply);
302-
bson_destroy(intern->write_errors);
303-
bson_destroy(intern->write_concern_errors);
304-
305-
if (!Z_ISUNDEF(intern->manager)) {
306-
zval_ptr_dtor(&intern->manager);
307-
}
308184
}
309185

310186
static zend_object* php_phongo_bulkwritecommandresult_create_object(zend_class_entry* class_type)
@@ -362,29 +238,6 @@ static HashTable* php_phongo_bulkwritecommandresult_get_debug_info(zend_object*
362238
ADD_ASSOC_NULL_EX(&retval, "deleteResults");
363239
}
364240

365-
{
366-
zval writeerrors;
367-
368-
phongo_bulkwritecommandresult_get_writeerrors(intern, &writeerrors);
369-
ADD_ASSOC_ZVAL_EX(&retval, "writeErrors", &writeerrors);
370-
}
371-
372-
{
373-
zval writeconcernerrors;
374-
375-
phongo_bulkwritecommandresult_get_writeconcernerrors(intern, &writeconcernerrors);
376-
ADD_ASSOC_ZVAL_EX(&retval, "writeConcernErrors", &writeconcernerrors);
377-
}
378-
379-
if (intern->error_reply) {
380-
zval error_reply;
381-
382-
phongo_document_new(&error_reply, intern->error_reply, true);
383-
ADD_ASSOC_ZVAL_EX(&retval, "errorReply", &error_reply);
384-
} else {
385-
ADD_ASSOC_NULL_EX(&retval, "errorReply");
386-
}
387-
388241
return Z_ARRVAL(retval);
389242
}
390243

@@ -404,38 +257,28 @@ static inline bson_t* _bson_copy_or_null(const bson_t* bson)
404257
return bson ? bson_copy(bson) : NULL;
405258
}
406259

407-
php_phongo_bulkwritecommandresult_t* phongo_bulkwritecommandresult_init(zval* return_value, mongoc_bulkwritereturn_t* bw_ret, zval* manager)
260+
php_phongo_bulkwritecommandresult_t* phongo_bulkwritecommandresult_init(zval* return_value, mongoc_bulkwriteresult_t* bw_res)
408261
{
409-
php_phongo_bulkwritecommandresult_t* bwcr;
262+
php_phongo_bulkwritecommandresult_t* intern;
410263

411264
object_init_ex(return_value, php_phongo_bulkwritecommandresult_ce);
412265

413-
bwcr = Z_BULKWRITECOMMANDRESULT_OBJ_P(return_value);
414-
bwcr->is_acknowledged = !!bw_ret->res;
266+
intern = Z_BULKWRITECOMMANDRESULT_OBJ_P(return_value);
267+
intern->is_acknowledged = (bw_res != NULL);
415268

416269
// Copy mongoc_bulkwriteresult_t fields
417-
if (bw_ret->res) {
418-
bwcr->inserted_count = mongoc_bulkwriteresult_insertedcount(bw_ret->res);
419-
bwcr->upserted_count = mongoc_bulkwriteresult_upsertedcount(bw_ret->res);
420-
bwcr->matched_count = mongoc_bulkwriteresult_matchedcount(bw_ret->res);
421-
bwcr->modified_count = mongoc_bulkwriteresult_modifiedcount(bw_ret->res);
422-
bwcr->deleted_count = mongoc_bulkwriteresult_deletedcount(bw_ret->res);
270+
if (bw_res) {
271+
intern->inserted_count = mongoc_bulkwriteresult_insertedcount(bw_res);
272+
intern->upserted_count = mongoc_bulkwriteresult_upsertedcount(bw_res);
273+
intern->matched_count = mongoc_bulkwriteresult_matchedcount(bw_res);
274+
intern->modified_count = mongoc_bulkwriteresult_modifiedcount(bw_res);
275+
intern->deleted_count = mongoc_bulkwriteresult_deletedcount(bw_res);
423276

424277
// Result documents will null if verboseResults=false
425-
bwcr->insert_results = _bson_copy_or_null(mongoc_bulkwriteresult_insertresults(bw_ret->res));
426-
bwcr->update_results = _bson_copy_or_null(mongoc_bulkwriteresult_updateresults(bw_ret->res));
427-
bwcr->delete_results = _bson_copy_or_null(mongoc_bulkwriteresult_deleteresults(bw_ret->res));
428-
}
429-
430-
/* If any error(s) occurred, mongoc_bulkwriteexception_t will be non-null.
431-
* Copy its fields into the result object. */
432-
if (bw_ret->exc) {
433-
bwcr->error_reply = bson_copy(mongoc_bulkwriteexception_errorreply(bw_ret->exc));
434-
bwcr->write_errors = bson_copy(mongoc_bulkwriteexception_writeerrors(bw_ret->exc));
435-
bwcr->write_concern_errors = bson_copy(mongoc_bulkwriteexception_writeconcernerrors(bw_ret->exc));
278+
intern->insert_results = _bson_copy_or_null(mongoc_bulkwriteresult_insertresults(bw_res));
279+
intern->update_results = _bson_copy_or_null(mongoc_bulkwriteresult_updateresults(bw_res));
280+
intern->delete_results = _bson_copy_or_null(mongoc_bulkwriteresult_deleteresults(bw_res));
436281
}
437282

438-
ZVAL_ZVAL(&bwcr->manager, manager, 1, 0);
439-
440-
return bwcr;
283+
return intern;
441284
}

src/MongoDB/BulkWriteCommandResult.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323

2424
#include "phongo_structs.h"
2525

26-
php_phongo_bulkwritecommandresult_t* phongo_bulkwritecommandresult_init(zval* return_value, mongoc_bulkwritereturn_t* bw_ret, zval* manager);
26+
php_phongo_bulkwritecommandresult_t* phongo_bulkwritecommandresult_init(zval* return_value, mongoc_bulkwriteresult_t* bw_res);
2727

2828
#endif /* PHONGO_BULKWRITECOMMANDRESULT_H */

src/MongoDB/BulkWriteCommandResult.stub.php

-6
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,5 @@ final public function getUpdateResults(): ?\MongoDB\BSON\Document {}
2828

2929
final public function getDeleteResults(): ?\MongoDB\BSON\Document {}
3030

31-
final public function getWriteErrors(): array {}
32-
33-
final public function getWriteConcernErrors(): array {}
34-
35-
final public function getErrorReply(): ?\MongoDB\BSON\Document {}
36-
3731
final public function isAcknowledged(): bool {}
3832
}

src/MongoDB/BulkWriteCommandResult_arginfo.h

+1-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)