@@ -248,6 +248,7 @@ static void phongo_cursor_init(zval *return_value, mongoc_client_t *client, mong
248248 intern -> cursor = cursor ;
249249 intern -> server_id = mongoc_cursor_get_hint (cursor );
250250 intern -> client = client ;
251+ intern -> advanced = false;
251252
252253 if (readPreference ) {
253254#if PHP_VERSION_ID >= 70000
@@ -286,6 +287,10 @@ static void phongo_cursor_init_for_query(zval *return_value, mongoc_client_t *cl
286287 /* namespace has already been validated by phongo_execute_query() */
287288 phongo_split_namespace (namespace , & intern -> database , & intern -> collection );
288289
290+ /* cursor has already been advanced by phongo_execute_query() calling
291+ * phongo_cursor_advance_and_check_for_error() */
292+ intern -> advanced = true;
293+
289294#if PHP_VERSION_ID >= 70000
290295 ZVAL_ZVAL (& intern -> query , query , 1 , 0 );
291296#else
@@ -709,9 +714,9 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
709714 return success ;
710715} /* }}} */
711716
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 ) /* {{{ */
715720{
716721 const bson_t * doc ;
717722
@@ -720,20 +725,18 @@ static bool phongo_advance_cursor_and_check_for_error(mongoc_cursor_t *cursor TS
720725
721726 /* Check for connection related exceptions */
722727 if (EG (exception )) {
723- mongoc_cursor_destroy (cursor );
724728 return false;
725729 }
726730
727731 /* Could simply be no docs, which is not an error */
728732 if (mongoc_cursor_error (cursor , & error )) {
729733 phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
730- mongoc_cursor_destroy (cursor );
731734 return false;
732735 }
733736 }
734737
735738 return true;
736- }
739+ } /* }}} */
737740
738741int 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 ) /* {{{ */
739742{
@@ -784,7 +787,8 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
784787 mongoc_cursor_set_max_await_time_ms (cursor , query -> max_await_time_ms );
785788 }
786789
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 );
788792 return false;
789793 }
790794
@@ -912,15 +916,9 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
912916 bson_destroy (& reply );
913917 }
914918
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-
920919 phongo_cursor_init_for_command (return_value , client , cmd_cursor , db , zcommand , zreadPreference TSRMLS_CC );
921920 return true;
922921} /* }}} */
923-
924922/* }}} */
925923
926924/* {{{ mongoc types from from_zval */
0 commit comments