Skip to content

Commit 161367c

Browse files
author
Christian Hergert
committed
client: add mongoc_client_command_simple() to API/ABI.
Add simple command execution helper that can be used by database and collection.
1 parent 2fce142 commit 161367c

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

mongoc/libmongoc.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mongoc_client_command
2+
mongoc_client_command_simple
23
mongoc_client_destroy
34
mongoc_client_get_collection
45
mongoc_client_get_database

mongoc/mongoc-client.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,3 +1105,66 @@ mongoc_client_command (mongoc_client_t *client,
11051105
return _mongoc_cursor_new (client, ns, flags, skip, n_return, 100, TRUE,
11061106
query, fields, read_prefs);
11071107
}
1108+
1109+
1110+
/**
1111+
* mongoc_client_command_simple:
1112+
* @client: A mongoc_client_t.
1113+
* @db_name: The namespace, such as "admin".
1114+
* @command: The command to execute.
1115+
* @read_prefs: The read preferences or NULL.
1116+
* @reply: A location for the reply document or NULL.
1117+
* @error: A location for the error, or NULL.
1118+
*
1119+
* This wrapper around mongoc_client_command() aims to make it simpler to
1120+
* run a command and check the output result.
1121+
*
1122+
* FALSE is returned if the command failed to be delivered or if the execution
1123+
* of the command failed. For example, a command that returns {'ok': 0} will
1124+
* result in this function returning FALSE.
1125+
*
1126+
* To allow the caller to disambiguate between command execution failure and
1127+
* failure to send the command, reply will always be set if non-NULL. The
1128+
* caller should release this with bson_destroy().
1129+
*
1130+
* Returns: TRUE if the command executed and resulted in success. Otherwise
1131+
* FALSE and @error is set. @reply is always set, either to the resulting
1132+
* document or an empty bson document upon failure.
1133+
*/
1134+
bson_bool_t
1135+
mongoc_client_command_simple (mongoc_client_t *client,
1136+
const char *db_name,
1137+
const bson_t *command,
1138+
const mongoc_read_prefs_t *read_prefs,
1139+
bson_t *reply,
1140+
bson_error_t *error)
1141+
{
1142+
mongoc_cursor_t *cursor;
1143+
const bson_t *doc;
1144+
bson_bool_t ret;
1145+
1146+
BSON_ASSERT (client);
1147+
BSON_ASSERT (db_name);
1148+
BSON_ASSERT (command);
1149+
1150+
cursor = mongoc_client_command (client, db_name, MONGOC_QUERY_NONE, 0, 1,
1151+
command, NULL, read_prefs);
1152+
1153+
ret = mongoc_cursor_next (cursor, &doc);
1154+
1155+
if (reply) {
1156+
if (ret) {
1157+
bson_copy_to (doc, reply);
1158+
} else {
1159+
bson_init (reply);
1160+
}
1161+
}
1162+
1163+
if (!ret) {
1164+
mongoc_cursor_error (cursor, error);
1165+
}
1166+
1167+
mongoc_cursor_destroy (cursor);
1168+
1169+
return ret;
1170+
}

mongoc/mongoc-client.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ mongoc_client_command (mongoc_client_t *client,
9595
const bson_t *fields,
9696
const mongoc_read_prefs_t *read_prefs);
9797

98+
bson_bool_t
99+
mongoc_client_command_simple (mongoc_client_t *client,
100+
const char *db_name,
101+
const bson_t *command,
102+
const mongoc_read_prefs_t *read_prefs,
103+
bson_t *reply,
104+
bson_error_t *error);
105+
98106
void
99107
mongoc_client_destroy (mongoc_client_t *client);
100108

0 commit comments

Comments
 (0)