@@ -467,34 +467,18 @@ zend_bool phongo_writeerror_init(zval *return_value, bson_t *bson TSRMLS_DC) /*
467
467
return true;
468
468
} /* }}} */
469
469
470
- php_phongo_writeresult_t * phongo_writeresult_init (zval * return_value , mongoc_write_result_t * write_result , mongoc_client_t * client , int server_id TSRMLS_DC ) /* {{{ */
470
+ php_phongo_writeresult_t * phongo_writeresult_init (zval * return_value , bson_t * reply , mongoc_client_t * client , int server_id TSRMLS_DC ) /* {{{ */
471
471
{
472
472
php_phongo_writeresult_t * writeresult ;
473
473
474
474
object_init_ex (return_value , php_phongo_writeresult_ce );
475
475
476
476
writeresult = Z_WRITERESULT_OBJ_P (return_value );
477
+ writeresult -> reply = bson_copy (reply );
477
478
writeresult -> client = client ;
478
479
writeresult -> server_id = server_id ;
479
480
480
- /* Copy write_results or else it'll get destroyed with the bulk destruction */
481
- #define SCP (field ) writeresult->write_result.field = write_result->field
482
- SCP (omit_nModified );
483
- SCP (nInserted );
484
- SCP (nMatched );
485
- SCP (nModified );
486
- SCP (nRemoved );
487
- SCP (nUpserted );
488
-
489
- bson_copy_to (& write_result -> upserted , & writeresult -> write_result .upserted );
490
- SCP (n_writeConcernErrors );
491
- bson_copy_to (& write_result -> writeConcernErrors , & writeresult -> write_result .writeConcernErrors );
492
- bson_copy_to (& write_result -> writeErrors , & writeresult -> write_result .writeErrors );
493
- SCP (upsert_append_count );
494
- #undef SCP
495
-
496
481
return writeresult ;
497
-
498
482
} /* }}} */
499
483
/* }}} */
500
484
@@ -522,62 +506,13 @@ mongoc_bulk_operation_t *phongo_bulkwrite_init(zend_bool ordered) { /* {{{ */
522
506
return mongoc_bulk_operation_new (ordered );
523
507
} /* }}} */
524
508
525
- static void phongo_bulk_write_error_add_message (char * * tmp_msg , bson_t * errors )
526
- {
527
- bson_iter_t iter ;
528
-
529
- bson_iter_init (& iter , errors );
530
-
531
- while (bson_iter_next (& iter )) {
532
- bson_t cbson ;
533
- uint32_t len ;
534
- const uint8_t * data ;
535
- bson_iter_t inner_iter ;
536
-
537
- if (!BSON_ITER_HOLDS_DOCUMENT (& iter )) {
538
- continue ;
539
- }
540
-
541
- bson_iter_document (& iter , & len , & data );
542
-
543
- if (!bson_init_static (& cbson , data , len )) {
544
- continue ;
545
- }
546
-
547
- if (bson_iter_init_find (& inner_iter , & cbson , "errmsg" ) && BSON_ITER_HOLDS_UTF8 (& inner_iter )) {
548
- const char * tmp_errmsg = bson_iter_utf8 (& inner_iter , NULL );
549
- size_t tmp_errmsg_len = strlen (tmp_errmsg );
550
-
551
- * tmp_msg = erealloc (* tmp_msg , strlen (* tmp_msg ) + tmp_errmsg_len + 5 );
552
- strncpy (* tmp_msg + strlen (* tmp_msg ), " :: " , 5 );
553
- strncpy (* tmp_msg + strlen (* tmp_msg ), tmp_errmsg , tmp_errmsg_len + 1 );
554
- }
555
- }
556
- }
557
-
558
- static char * phongo_assemble_bulk_write_error (mongoc_write_result_t * write_result )
559
- {
560
- char * tmp_msg = emalloc (sizeof ("BulkWrite error" ));
561
-
562
- strncpy (tmp_msg , "BulkWrite error" , sizeof ("BulkWrite error" ));
563
-
564
- if (!bson_empty0 (& write_result -> writeErrors )) {
565
- phongo_bulk_write_error_add_message (& tmp_msg , & write_result -> writeErrors );
566
- }
567
-
568
- if (!bson_empty0 (& write_result -> writeConcernErrors )) {
569
- phongo_bulk_write_error_add_message (& tmp_msg , & write_result -> writeConcernErrors );
570
- }
571
-
572
- return tmp_msg ;
573
- }
574
-
575
509
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 ) /* {{{ */
576
510
{
577
511
bson_error_t error ;
578
512
char * dbname ;
579
513
char * collname ;
580
514
int success ;
515
+ bson_t reply = BSON_INITIALIZER ;
581
516
php_phongo_writeresult_t * writeresult ;
582
517
583
518
if (!phongo_split_namespace (namespace , & dbname , & collname )) {
@@ -604,38 +539,36 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc
604
539
mongoc_bulk_operation_set_hint (bulk , server_id );
605
540
}
606
541
607
- success = mongoc_bulk_operation_execute (bulk , NULL , & error );
542
+ success = mongoc_bulk_operation_execute (bulk , & reply , & error );
608
543
609
544
/* Write succeeded and the user doesn't care for the results */
610
545
if (success && !return_value_used ) {
546
+ bson_destroy (& reply );
611
547
return true;
612
548
}
613
549
614
550
/* Check for connection related exceptions */
615
551
if (EG (exception )) {
552
+ bson_destroy (& reply );
616
553
return false;
617
554
}
618
555
619
- writeresult = phongo_writeresult_init (return_value , & bulk -> result , client , bulk -> hint TSRMLS_CC );
556
+ writeresult = phongo_writeresult_init (return_value , & reply , client , bulk -> hint TSRMLS_CC );
620
557
writeresult -> write_concern = mongoc_write_concern_copy (write_concern );
621
558
622
559
/* The Write failed */
623
560
if (!success ) {
624
- /* The Command itself failed */
625
- if (bson_empty0 (& writeresult -> write_result .writeErrors ) && bson_empty0 (& writeresult -> write_result .writeConcernErrors )) {
626
- /* FIXME: Maybe we can look at write_result.error and not pass error at all? */
627
- phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
628
- } else {
629
- char * bulk_error_msg ;
630
-
631
- bulk_error_msg = phongo_assemble_bulk_write_error (& writeresult -> write_result );
632
- phongo_throw_exception (PHONGO_ERROR_WRITE_FAILED TSRMLS_CC , "%s" , bulk_error_msg );
633
- efree (bulk_error_msg );
561
+ if (error .domain == MONGOC_ERROR_COMMAND || error .domain == MONGOC_ERROR_WRITE_CONCERN ) {
562
+ phongo_throw_exception (PHONGO_ERROR_WRITE_FAILED TSRMLS_CC , "%s" , error .message );
634
563
phongo_add_exception_prop (ZEND_STRL ("writeResult" ), return_value TSRMLS_CC );
564
+ } else {
565
+ phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
635
566
}
636
- return false;
637
567
}
638
- return true;
568
+
569
+ bson_destroy (& reply );
570
+
571
+ return success ;
639
572
} /* }}} */
640
573
641
574
int phongo_execute_query (mongoc_client_t * client , const char * namespace , const php_phongo_query_t * query , const mongoc_read_prefs_t * read_preference , int server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
0 commit comments