Skip to content

Commit f437f6d

Browse files
author
Christian Hergert
committed
database: change API for command_simple to match client.
1 parent 161367c commit f437f6d

File tree

3 files changed

+38
-39
lines changed

3 files changed

+38
-39
lines changed

mongoc/mongoc-database.c

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -171,48 +171,21 @@ mongoc_database_command (mongoc_database_t *database,
171171
*/
172172

173173
bson_bool_t
174-
mongoc_database_command_simple (mongoc_database_t *database,
175-
const bson_t *cmd,
176-
bson_error_t *error)
174+
mongoc_database_command_simple (mongoc_database_t *database,
175+
const bson_t *command,
176+
const mongoc_read_prefs_t *read_prefs,
177+
bson_t *reply,
178+
bson_error_t *error)
177179
{
178-
mongoc_cursor_t *cursor;
179-
const bson_t *b;
180-
bson_iter_t iter;
181-
bson_bool_t ret = FALSE;
182-
const char *errmsg = "unknown error";
183-
char ns[140];
184-
185-
bson_return_val_if_fail(database, FALSE);
186-
bson_return_val_if_fail(cmd, FALSE);
187-
188-
snprintf(ns, sizeof ns, "%s.$cmd", database->name);
189-
ns[sizeof ns-1] = '\0';
190-
191-
cursor = _mongoc_cursor_new(database->client, ns, MONGOC_QUERY_NONE, 0, 1,
192-
0, TRUE, cmd, NULL, database->read_prefs);
193-
194-
if (mongoc_cursor_next(cursor, &b) &&
195-
bson_iter_init_find(&iter, b, "ok") &&
196-
BSON_ITER_HOLDS_DOUBLE(&iter)) {
197-
if (bson_iter_double(&iter) == 1.0) {
198-
ret = TRUE;
199-
} else {
200-
if (bson_iter_init_find(&iter, b, "errmsg")) {
201-
errmsg = bson_iter_utf8(&iter, NULL);
202-
}
203-
}
204-
}
180+
BSON_ASSERT (database);
181+
BSON_ASSERT (command);
205182

206-
if (!ret) {
207-
bson_set_error(error,
208-
MONGOC_ERROR_QUERY,
209-
MONGOC_ERROR_QUERY_FAILURE,
210-
"%s", errmsg);
183+
if (!read_prefs) {
184+
read_prefs = database->read_prefs;
211185
}
212186

213-
mongoc_cursor_destroy(cursor);
214-
215-
return ret;
187+
return mongoc_client_command_simple (database->client, database->name,
188+
command, read_prefs, reply, error);
216189
}
217190

218191

@@ -246,7 +219,7 @@ mongoc_database_drop (mongoc_database_t *database,
246219

247220
bson_init(&cmd);
248221
bson_append_int32(&cmd, "dropDatabase", 12, 1);
249-
ret = mongoc_database_command_simple(database, &cmd, error);
222+
ret = mongoc_database_command_simple(database, &cmd, NULL, NULL, error);
250223
bson_destroy(&cmd);
251224

252225
return ret;

mongoc/mongoc-database.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ mongoc_cursor_t *mongoc_database_command (mongoc_database
4848
const mongoc_read_prefs_t *read_prefs);
4949
bson_bool_t mongoc_database_command_simple (mongoc_database_t *database,
5050
const bson_t *command,
51+
const mongoc_read_prefs_t *read_prefs,
52+
bson_t *reply,
5153
bson_error_t *error);
5254
bson_bool_t mongoc_database_drop (mongoc_database_t *database,
5355
bson_error_t *error);

tests/test-mongoc-database.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,29 @@ test_command (void)
8787
}
8888

8989

90+
static void
91+
test_drop (void)
92+
{
93+
mongoc_database_t *database;
94+
mongoc_client_t *client;
95+
bson_error_t error = { 0 };
96+
bson_bool_t r;
97+
98+
client = mongoc_client_new (gTestUri);
99+
assert (client);
100+
101+
database = mongoc_client_get_database (client, "some_random_database");
102+
103+
r = mongoc_database_drop (database, &error);
104+
assert (r);
105+
assert (!error.domain);
106+
assert (!error.code);
107+
108+
mongoc_database_destroy (database);
109+
mongoc_client_destroy (client);
110+
}
111+
112+
90113
static void
91114
log_handler (mongoc_log_level_t log_level,
92115
const char *domain,
@@ -109,6 +132,7 @@ main (int argc,
109132

110133
run_test("/mongoc/database/has_collection", test_has_collection);
111134
run_test("/mongoc/database/command", test_command);
135+
run_test("/mongoc/database/drop", test_drop);
112136

113137
bson_free(gTestUri);
114138

0 commit comments

Comments
 (0)