@@ -512,9 +512,10 @@ php_phongo_writeresult_t *phongo_writeresult_init(zval *return_value, mongoc_wri
512512 SCP (nRemoved );
513513 SCP (nUpserted );
514514
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 );
518519 SCP (upsert_append_count );
519520#undef SCP
520521
@@ -684,10 +685,7 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc
684685 /* The Write failed */
685686 if (!success ) {
686687 /* 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 )) {
691689 /* FIXME: Maybe we can look at write_result.error and not pass error at all? */
692690 phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
693691 } else {
@@ -726,7 +724,9 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, const p
726724 return false;
727725 }
728726
729- cursor -> hint = server_id ;
727+ if (server_id > 0 ) {
728+ cursor -> hint = server_id ;
729+ }
730730 if (!mongoc_cursor_next (cursor , & doc )) {
731731 bson_error_t error ;
732732
@@ -757,7 +757,9 @@ int phongo_execute_command(mongoc_client_t *client, const char *db, const bson_t
757757
758758
759759 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+ }
761763
762764 if (!mongoc_cursor_next (cursor , & doc )) {
763765 bson_error_t error ;
@@ -821,7 +823,11 @@ void phongo_stream_destroy(mongoc_stream_t *stream_wrap) /* {{{ */
821823{
822824 php_phongo_stream_socket * base_stream = (php_phongo_stream_socket * )stream_wrap ;
823825
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+ }
825831 /*
826832 * DON'T DO ANYTHING TO THE INTERNAL base_stream->stream
827833 * 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) /* {{{ */
851857 php_phongo_stream_socket * base_stream = (php_phongo_stream_socket * )stream_wrap ;
852858
853859 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+
855868 return 0 ;
856869} /* }}} */
857870
@@ -2007,25 +2020,30 @@ bool php_phongo_writeresult_get_write_errors(php_phongo_writeresult_t *writeresu
20072020 }
20082021 return false;
20092022} /* }}} */
2023+
20102024bool php_phongo_writeresult_get_writeconcern_error (php_phongo_writeresult_t * writeresult , bson_error_t * error ) /* {{{ */
20112025{
20122026 const char * err = NULL ;
20132027 uint32_t code = 0 ;
2028+ bson_iter_t iter ;
2029+ bson_iter_t citer ;
20142030
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+ }
20232042 }
20242043
20252044 bson_set_error (error , PHONGO_ERROR_WRITECONCERN_FAILED , code , "%s" , err );
20262045 return true;
20272046 }
2028-
20292047 return false;
20302048} /* }}} */
20312049zval * php_phongo_throw_write_errors (php_phongo_writeresult_t * wr TSRMLS_DC ) /* {{{ */
0 commit comments