@@ -512,9 +512,10 @@ php_phongo_writeresult_t *phongo_writeresult_init(zval *return_value, mongoc_wri
512
512
SCP (nRemoved );
513
513
SCP (nUpserted );
514
514
515
- bson_copy_to (& write_result -> upserted , & writeresult -> write_result .upserted );
516
- bson_copy_to (& write_result -> writeConcernError , & writeresult -> write_result .writeConcernError );
517
- bson_copy_to (& write_result -> writeErrors , & writeresult -> write_result .writeErrors );
515
+ bson_copy_to (& write_result -> upserted , & writeresult -> write_result .upserted );
516
+ SCP (n_writeConcernErrors );
517
+ bson_copy_to (& write_result -> writeConcernErrors , & writeresult -> write_result .writeConcernErrors );
518
+ bson_copy_to (& write_result -> writeErrors , & writeresult -> write_result .writeErrors );
518
519
SCP (upsert_append_count );
519
520
#undef SCP
520
521
@@ -684,10 +685,7 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc
684
685
/* The Write failed */
685
686
if (!success ) {
686
687
/* The Command itself failed */
687
- if (
688
- bson_empty0 (& writeresult -> write_result .writeErrors )
689
- && bson_empty0 (& writeresult -> write_result .writeConcernError )
690
- ) {
688
+ if (bson_empty0 (& writeresult -> write_result .writeErrors ) && bson_empty0 (& writeresult -> write_result .writeConcernErrors )) {
691
689
/* FIXME: Maybe we can look at write_result.error and not pass error at all? */
692
690
phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
693
691
} else {
@@ -726,7 +724,9 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, const p
726
724
return false;
727
725
}
728
726
729
- cursor -> hint = server_id ;
727
+ if (server_id > 0 ) {
728
+ cursor -> hint = server_id ;
729
+ }
730
730
if (!mongoc_cursor_next (cursor , & doc )) {
731
731
bson_error_t error ;
732
732
@@ -757,7 +757,9 @@ int phongo_execute_command(mongoc_client_t *client, const char *db, const bson_t
757
757
758
758
759
759
cursor = mongoc_client_command (client , db , MONGOC_QUERY_NONE , 0 , 1 , 0 , command , NULL , read_preference );
760
- cursor -> hint = server_id ;
760
+ if (server_id > 0 ) {
761
+ cursor -> hint = server_id ;
762
+ }
761
763
762
764
if (!mongoc_cursor_next (cursor , & doc )) {
763
765
bson_error_t error ;
@@ -821,7 +823,11 @@ void phongo_stream_destroy(mongoc_stream_t *stream_wrap) /* {{{ */
821
823
{
822
824
php_phongo_stream_socket * base_stream = (php_phongo_stream_socket * )stream_wrap ;
823
825
824
- MONGOC_DEBUG ("Not destroying RSRC#%d" , base_stream -> stream -> rsrc_id );
826
+ if (base_stream -> stream ) {
827
+ MONGOC_DEBUG ("Not destroying RSRC#%d" , base_stream -> stream -> rsrc_id );
828
+ } else {
829
+ MONGOC_DEBUG ("Wrapped stream already destroyed" );
830
+ }
825
831
/*
826
832
* DON'T DO ANYTHING TO THE INTERNAL base_stream->stream
827
833
* The stream should not be closed during normal dtor -- as we want it to
@@ -851,7 +857,14 @@ int phongo_stream_close(mongoc_stream_t *stream_wrap) /* {{{ */
851
857
php_phongo_stream_socket * base_stream = (php_phongo_stream_socket * )stream_wrap ;
852
858
853
859
MONGOC_DEBUG ("Closing RSRC#%d" , base_stream -> stream -> rsrc_id );
854
- phongo_stream_destroy (stream_wrap );
860
+ if (base_stream -> stream ) {
861
+ TSRMLS_FETCH_FROM_CTX (base_stream -> tsrm_ls );
862
+
863
+ MONGOC_DEBUG ("Destroying RSRC#%d" , base_stream -> stream -> rsrc_id );
864
+ php_stream_free (base_stream -> stream , PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR );
865
+ base_stream -> stream = NULL ;
866
+ }
867
+
855
868
return 0 ;
856
869
} /* }}} */
857
870
@@ -2007,25 +2020,30 @@ bool php_phongo_writeresult_get_write_errors(php_phongo_writeresult_t *writeresu
2007
2020
}
2008
2021
return false;
2009
2022
} /* }}} */
2023
+
2010
2024
bool php_phongo_writeresult_get_writeconcern_error (php_phongo_writeresult_t * writeresult , bson_error_t * error ) /* {{{ */
2011
2025
{
2012
2026
const char * err = NULL ;
2013
2027
uint32_t code = 0 ;
2028
+ bson_iter_t iter ;
2029
+ bson_iter_t citer ;
2014
2030
2015
- if (!bson_empty0 (& writeresult -> write_result .writeConcernError )) {
2016
- bson_iter_t iter ;
2017
-
2018
- if (bson_iter_init_find (& iter , & writeresult -> write_result .writeConcernError , "code" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
2019
- code = bson_iter_int32 (& iter );
2020
- }
2021
- if (bson_iter_init_find (& iter , & writeresult -> write_result .writeConcernError , "errmsg" ) && BSON_ITER_HOLDS_UTF8 (& iter )) {
2022
- err = bson_iter_utf8 (& iter , NULL );
2031
+ if (!bson_empty0 (& writeresult -> write_result .writeConcernErrors ) &&
2032
+ bson_iter_init (& iter , & writeresult -> write_result .writeConcernErrors ) &&
2033
+ bson_iter_next (& iter ) &&
2034
+ BSON_ITER_HOLDS_DOCUMENT (& iter ) &&
2035
+ bson_iter_recurse (& iter , & citer )) {
2036
+ while (bson_iter_next (& citer )) {
2037
+ if (BSON_ITER_IS_KEY (& citer , "errmsg" )) {
2038
+ err = bson_iter_utf8 (& citer , NULL );
2039
+ } else if (BSON_ITER_IS_KEY (& citer , "code" )) {
2040
+ code = bson_iter_int32 (& citer );
2041
+ }
2023
2042
}
2024
2043
2025
2044
bson_set_error (error , PHONGO_ERROR_WRITECONCERN_FAILED , code , "%s" , err );
2026
2045
return true;
2027
2046
}
2028
-
2029
2047
return false;
2030
2048
} /* }}} */
2031
2049
zval * php_phongo_throw_write_errors (php_phongo_writeresult_t * wr TSRMLS_DC ) /* {{{ */
0 commit comments