Skip to content

Commit c6c078d

Browse files
author
Christian Hergert
committed
client: add mongoc_client_command() to API/ABI.
We should restructure the other wrappers to use this.
1 parent a6cb643 commit c6c078d

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

mongoc/libmongoc.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mongoc_client_command
12
mongoc_client_destroy
23
mongoc_client_get_collection
34
mongoc_client_get_database

mongoc/mongoc-client.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "mongoc-collection-private.h"
3535
#include "mongoc-config.h"
3636
#include "mongoc-counters-private.h"
37+
#include "mongoc-cursor-private.h"
3738
#include "mongoc-database-private.h"
3839
#include "mongoc-gridfs-private.h"
3940
#include "mongoc-error.h"
@@ -1076,3 +1077,31 @@ _mongoc_client_warm_up (mongoc_client_t *client,
10761077

10771078
return ret;
10781079
}
1080+
1081+
1082+
mongoc_cursor_t *
1083+
mongoc_client_command (mongoc_client_t *client,
1084+
const char *db_name,
1085+
mongoc_query_flags_t flags,
1086+
bson_uint32_t skip,
1087+
bson_uint32_t n_return,
1088+
const bson_t *query,
1089+
const bson_t *fields,
1090+
const mongoc_read_prefs_t *read_prefs)
1091+
{
1092+
char ns[MONGOC_NAMESPACE_MAX];
1093+
1094+
BSON_ASSERT (client);
1095+
BSON_ASSERT (db_name);
1096+
BSON_ASSERT (query);
1097+
1098+
if (!read_prefs) {
1099+
read_prefs = client->read_prefs;
1100+
}
1101+
1102+
snprintf (ns, sizeof ns, "%s.$cmd", db_name);
1103+
ns[sizeof ns - 1] = '\0';
1104+
1105+
return _mongoc_cursor_new (client, ns, flags, skip, n_return, 100, TRUE,
1106+
query, fields, read_prefs);
1107+
}

mongoc/mongoc-client.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "mongoc-collection.h"
2525
#include "mongoc-config.h"
26+
#include "mongoc-cursor.h"
2627
#include "mongoc-database.h"
2728
#include "mongoc-gridfs.h"
2829
#include "mongoc-index.h"
@@ -84,6 +85,16 @@ mongoc_client_set_stream_initiator (mongoc_client_t *client,
8485
mongoc_stream_initiator_t initiator,
8586
void *user_data);
8687

88+
mongoc_cursor_t *
89+
mongoc_client_command (mongoc_client_t *client,
90+
const char *db_name,
91+
mongoc_query_flags_t flags,
92+
bson_uint32_t skip,
93+
bson_uint32_t n_return,
94+
const bson_t *query,
95+
const bson_t *fields,
96+
const mongoc_read_prefs_t *read_prefs);
97+
8798
void
8899
mongoc_client_destroy (mongoc_client_t *client);
89100

tests/test-mongoc-client.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,35 @@ test_mongoc_client_read_prefs (void)
349349
}
350350

351351

352+
static void
353+
test_mongoc_client_command (void)
354+
{
355+
mongoc_client_t *client;
356+
mongoc_cursor_t *cursor;
357+
const bson_t *doc;
358+
bson_bool_t r;
359+
bson_t cmd = BSON_INITIALIZER;
360+
361+
client = mongoc_client_new (gTestUri);
362+
assert (client);
363+
364+
bson_append_int32 (&cmd, "ping", 4, 1);
365+
366+
cursor = mongoc_client_command (client, "admin", MONGOC_QUERY_NONE, 0, 1, &cmd, NULL, NULL);
367+
368+
r = mongoc_cursor_next (cursor, &doc);
369+
assert (r);
370+
assert (doc);
371+
372+
r = mongoc_cursor_next (cursor, &doc);
373+
assert (!r);
374+
assert (!doc);
375+
376+
mongoc_client_destroy (client);
377+
bson_destroy (&cmd);
378+
}
379+
380+
352381
static void
353382
log_handler (mongoc_log_level_t log_level,
354383
const char *domain,
@@ -395,6 +424,7 @@ main (int argc,
395424
run_test("/mongoc/client/authenticate", test_mongoc_client_authenticate);
396425
run_test("/mongoc/client/authenticate_failure", test_mongoc_client_authenticate_failure);
397426
run_test("/mongoc/client/read_prefs", test_mongoc_client_read_prefs);
427+
run_test("/mongoc/client/command", test_mongoc_client_command);
398428

399429
bson_free(gTestUri);
400430
bson_free(gTestUriWithPassword);

0 commit comments

Comments
 (0)