Skip to content

Commit aec79b2

Browse files
committed
Refactor command and query execute functions
This corrects the function signatures to return a boolean indicating success instead of an integer (which was cast from a boolean). Additionally, it restructures phongo_execute_command() to use a cleanup label, in anticipation of implicit session changes.
1 parent 1dfc8df commit aec79b2

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

php_phongo.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ bool phongo_cursor_advance_and_check_for_error(mongoc_cursor_t *cursor TSRMLS_DC
749749
return true;
750750
} /* }}} */
751751

752-
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) /* {{{ */
752+
bool 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) /* {{{ */
753753
{
754754
const php_phongo_query_t *query;
755755
mongoc_cursor_t *cursor;
@@ -827,7 +827,7 @@ static bson_t *create_wrapped_command_envelope(const char *db, bson_t *reply)
827827
return tmp;
828828
}
829829

830-
int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t type, const char *db, zval *zcommand, zval *options, uint32_t server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
830+
bool phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t type, const char *db, zval *zcommand, zval *options, uint32_t server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
831831
{
832832
const php_phongo_command_t *command;
833833
bson_iter_t iter;
@@ -837,38 +837,34 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
837837
mongoc_cursor_t *cmd_cursor;
838838
zval *zreadPreference = NULL;
839839
zval *zsession = NULL;
840-
int result;
840+
bool result = false;
841+
bool free_reply = false;
841842

842843
command = Z_COMMAND_OBJ_P(zcommand);
843844

844845
if ((type & PHONGO_OPTION_READ_CONCERN) && !phongo_parse_read_concern(options, &opts TSRMLS_CC)) {
845846
/* Exception should already have been thrown */
846-
bson_destroy(&opts);
847-
return false;
847+
goto cleanup;
848848
}
849849

850850
if ((type & PHONGO_OPTION_READ_PREFERENCE) && !phongo_parse_read_preference(options, &zreadPreference TSRMLS_CC)) {
851851
/* Exception should already have been thrown */
852-
bson_destroy(&opts);
853-
return false;
852+
goto cleanup;
854853
}
855854

856855
if (!phongo_parse_session(options, client, &opts, &zsession TSRMLS_CC)) {
857856
/* Exception should already have been thrown */
858-
bson_destroy(&opts);
859-
return false;
857+
goto cleanup;
860858
}
861859

862860
if ((type & PHONGO_OPTION_WRITE_CONCERN) && !phongo_parse_write_concern(options, &opts, NULL TSRMLS_CC)) {
863861
/* Exception should already have been thrown */
864-
bson_destroy(&opts);
865-
return false;
862+
goto cleanup;
866863
}
867864

868865
if (!BSON_APPEND_INT32(&opts, "serverId", server_id)) {
869866
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error appending \"serverId\" option");
870-
bson_destroy(&opts);
871-
return false;
867+
goto cleanup;
872868
}
873869

874870
/* Although "opts" already always includes the serverId option, the read
@@ -891,21 +887,18 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
891887
default:
892888
/* Should never happen, but if it does: exception */
893889
phongo_throw_exception(PHONGO_ERROR_LOGIC TSRMLS_CC, "Type '%d' should never have been passed to phongo_execute_command, please file a bug report", type);
894-
bson_destroy(&opts);
895-
return false;
890+
goto cleanup;
896891
}
892+
893+
free_reply = true;
894+
897895
if (!result) {
898896
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
899-
bson_destroy(&reply);
900-
bson_destroy(&opts);
901-
return false;
897+
goto cleanup;
902898
}
903899

904-
bson_destroy(&opts);
905-
906900
if (!return_value_used) {
907-
bson_destroy(&reply);
908-
return true;
901+
goto cleanup;
909902
}
910903

911904
/* According to mongoc_cursor_new_from_command_reply(), the reply bson_t
@@ -926,16 +919,22 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
926919
}
927920

928921
cmd_cursor = mongoc_cursor_new_from_command_reply(client, &initial_reply, server_id);
929-
bson_destroy(&reply);
930922
} else {
931923
bson_t *wrapped_reply = create_wrapped_command_envelope(db, &reply);
932924

933925
cmd_cursor = mongoc_cursor_new_from_command_reply(client, wrapped_reply, server_id);
934-
bson_destroy(&reply);
935926
}
936927

937928
phongo_cursor_init_for_command(return_value, client, cmd_cursor, db, zcommand, zreadPreference, zsession TSRMLS_CC);
938-
return true;
929+
930+
cleanup:
931+
bson_destroy(&opts);
932+
933+
if (free_reply) {
934+
bson_destroy(&reply);
935+
}
936+
937+
return result;
939938
} /* }}} */
940939
/* }}} */
941940

php_phongo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ void phongo_readconcern_init (zval *return_value, const
134134
void phongo_readpreference_init (zval *return_value, const mongoc_read_prefs_t *read_prefs TSRMLS_DC);
135135
void phongo_writeconcern_init (zval *return_value, const mongoc_write_concern_t *write_concern TSRMLS_DC);
136136
bool phongo_execute_bulk_write (mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, zval *zwriteConcern, uint32_t server_id, zval *return_value, int return_value_used TSRMLS_DC);
137-
int phongo_execute_command (mongoc_client_t *client, php_phongo_command_type_t type, const char *db, zval *zcommand, zval *zreadPreference, uint32_t server_id, zval *return_value, int return_value_used TSRMLS_DC);
138-
int phongo_execute_query (mongoc_client_t *client, const char *namespace, zval *zquery, zval *zreadPreference, uint32_t server_id, zval *return_value, int return_value_used TSRMLS_DC);
137+
bool phongo_execute_command (mongoc_client_t *client, php_phongo_command_type_t type, const char *db, zval *zcommand, zval *zreadPreference, uint32_t server_id, zval *return_value, int return_value_used TSRMLS_DC);
138+
bool phongo_execute_query (mongoc_client_t *client, const char *namespace, zval *zquery, zval *zreadPreference, uint32_t server_id, zval *return_value, int return_value_used TSRMLS_DC);
139139

140140
bool phongo_cursor_advance_and_check_for_error(mongoc_cursor_t *cursor TSRMLS_DC);
141141

0 commit comments

Comments
 (0)