@@ -248,6 +248,7 @@ static void phongo_cursor_init(zval *return_value, mongoc_client_t *client, mong
248
248
intern -> cursor = cursor ;
249
249
intern -> server_id = mongoc_cursor_get_hint (cursor );
250
250
intern -> client = client ;
251
+ intern -> advanced = false;
251
252
252
253
if (readPreference ) {
253
254
#if PHP_VERSION_ID >= 70000
@@ -286,6 +287,10 @@ static void phongo_cursor_init_for_query(zval *return_value, mongoc_client_t *cl
286
287
/* namespace has already been validated by phongo_execute_query() */
287
288
phongo_split_namespace (namespace , & intern -> database , & intern -> collection );
288
289
290
+ /* cursor has already been advanced by phongo_execute_query() calling
291
+ * phongo_cursor_advance_and_check_for_error() */
292
+ intern -> advanced = true;
293
+
289
294
#if PHP_VERSION_ID >= 70000
290
295
ZVAL_ZVAL (& intern -> query , query , 1 , 0 );
291
296
#else
@@ -709,9 +714,9 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
709
714
return success ;
710
715
} /* }}} */
711
716
712
- /* Advance the cursor and return whether there is an error. On error, the cursor
713
- * will be destroyed and an exception will be thrown. */
714
- static bool phongo_advance_cursor_and_check_for_error (mongoc_cursor_t * cursor TSRMLS_DC )
717
+ /* Advance the cursor and return whether there is an error. On error, false is
718
+ * returned and an exception is thrown. */
719
+ bool phongo_cursor_advance_and_check_for_error (mongoc_cursor_t * cursor TSRMLS_DC ) /* {{{ */
715
720
{
716
721
const bson_t * doc ;
717
722
@@ -720,20 +725,18 @@ static bool phongo_advance_cursor_and_check_for_error(mongoc_cursor_t *cursor TS
720
725
721
726
/* Check for connection related exceptions */
722
727
if (EG (exception )) {
723
- mongoc_cursor_destroy (cursor );
724
728
return false;
725
729
}
726
730
727
731
/* Could simply be no docs, which is not an error */
728
732
if (mongoc_cursor_error (cursor , & error )) {
729
733
phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
730
- mongoc_cursor_destroy (cursor );
731
734
return false;
732
735
}
733
736
}
734
737
735
738
return true;
736
- }
739
+ } /* }}} */
737
740
738
741
int phongo_execute_query (mongoc_client_t * client , const char * namespace , zval * zquery , zval * options , uint32_t server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
739
742
{
@@ -784,7 +787,8 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
784
787
mongoc_cursor_set_max_await_time_ms (cursor , query -> max_await_time_ms );
785
788
}
786
789
787
- if (!phongo_advance_cursor_and_check_for_error (cursor TSRMLS_CC )) {
790
+ if (!phongo_cursor_advance_and_check_for_error (cursor TSRMLS_CC )) {
791
+ mongoc_cursor_destroy (cursor );
788
792
return false;
789
793
}
790
794
@@ -912,15 +916,9 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
912
916
bson_destroy (& reply );
913
917
}
914
918
915
- if (!phongo_advance_cursor_and_check_for_error (cmd_cursor TSRMLS_CC )) {
916
- /* If an error is found, the cmd_cursor is destroyed already */
917
- return false;
918
- }
919
-
920
919
phongo_cursor_init_for_command (return_value , client , cmd_cursor , db , zcommand , zreadPreference TSRMLS_CC );
921
920
return true;
922
921
} /* }}} */
923
-
924
922
/* }}} */
925
923
926
924
/* {{{ mongoc types from from_zval */
0 commit comments