@@ -517,22 +517,32 @@ mongoc_bulk_operation_t *phongo_bulkwrite_init(zend_bool ordered) { /* {{{ */
517517 return mongoc_bulk_operation_new (ordered );
518518} /* }}} */
519519
520- bool phongo_execute_write (mongoc_client_t * client , const char * namespace , mongoc_bulk_operation_t * bulk , const mongoc_write_concern_t * write_concern , int server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
520+ bool phongo_execute_write (mongoc_client_t * client , const char * namespace , php_phongo_bulkwrite_t * bulk_write , const mongoc_write_concern_t * write_concern , int server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
521521{
522522 bson_error_t error ;
523- char * dbname ;
524- char * collname ;
525523 int success ;
526524 bson_t reply = BSON_INITIALIZER ;
525+ mongoc_bulk_operation_t * bulk = bulk_write -> bulk ;
527526 php_phongo_writeresult_t * writeresult ;
528527
529- if (!phongo_split_namespace (namespace , & dbname , & collname )) {
528+ /* Since BulkWrite objects can currently be executed multiple times, ensure
529+ * that the database and collection name are freed before we overwrite them.
530+ * This may be removed once PHPC-676 is implemented. */
531+ if (bulk_write -> database ) {
532+ efree (bulk_write -> database );
533+ }
534+
535+ if (bulk_write -> collection ) {
536+ efree (bulk_write -> collection );
537+ }
538+
539+ if (!phongo_split_namespace (namespace , & bulk_write -> database , & bulk_write -> collection )) {
530540 phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "%s: %s" , "Invalid namespace provided" , namespace );
531541 return false;
532542 }
533543
534- mongoc_bulk_operation_set_database (bulk , dbname );
535- mongoc_bulk_operation_set_collection (bulk , collname );
544+ mongoc_bulk_operation_set_database (bulk , bulk_write -> database );
545+ mongoc_bulk_operation_set_collection (bulk , bulk_write -> collection );
536546 mongoc_bulk_operation_set_client (bulk , client );
537547
538548 /* If a write concern was not specified, libmongoc will use the client's
@@ -543,14 +553,12 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc
543553 write_concern = mongoc_client_get_write_concern (client );
544554 }
545555
546- efree (dbname );
547- efree (collname );
548-
549556 if (server_id > 0 ) {
550557 mongoc_bulk_operation_set_hint (bulk , server_id );
551558 }
552559
553560 success = mongoc_bulk_operation_execute (bulk , & reply , & error );
561+ bulk_write -> executed = true;
554562
555563 /* Write succeeded and the user doesn't care for the results */
556564 if (success && !return_value_used ) {
0 commit comments