Skip to content

Commit 31bcbd3

Browse files
committed
All tests passed
1 parent 19c16d3 commit 31bcbd3

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/mongocxx/collection.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,26 @@ void collection::drop(const client_session& session,
13121312
return _drop(&session, wc);
13131313
}
13141314

1315+
void collection::drop(bsoncxx::document::view_or_value collection_options) {
1316+
bson_error_t error;
1317+
1318+
bsoncxx::builder::basic::document opts_doc;
1319+
1320+
opts_doc.append(bsoncxx::builder::concatenate_doc{collection_options});
1321+
1322+
scoped_bson_t opts_bson{opts_doc.view()};
1323+
auto result =
1324+
libmongoc::collection_drop_with_opts(_get_impl().collection_t, opts_bson.bson(), &error);
1325+
1326+
// Throw an exception if the command failed, unless the failure was due to a non-existent
1327+
// collection. We check for this failure using 'code', but we fall back to checking 'message'
1328+
// for old server versions (3.0 and earlier) that do not send a code with the command response.
1329+
if (!result && !(error.code == ::MONGOC_ERROR_COLLECTION_DOES_NOT_EXIST ||
1330+
stdx::string_view{error.message} == stdx::string_view{"ns not found"})) {
1331+
throw_exception<operation_exception>(error);
1332+
}
1333+
}
1334+
13151335
void collection::read_concern(class read_concern rc) {
13161336
libmongoc::collection_set_read_concern(_get_impl().collection_t, rc._impl->read_concern_t);
13171337
}

src/mongocxx/collection.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ class MONGOCXX_API collection {
722722
void drop(const client_session& session,
723723
const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern = {});
724724

725+
void drop(bsoncxx::document::view_or_value collection_options);
725726
///
726727
/// @}
727728
///

src/mongocxx/test/spec/operation.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,18 +1282,43 @@ document::value operation_runner::_run_create_collection(document::view operatio
12821282
auto arguments = operation["arguments"].get_document().value;
12831283
auto collection_name = arguments["collection"].get_string().value;
12841284
auto session = _lookup_session(arguments);
1285+
1286+
std::cerr << "CREATE COLLECTION: " << bsoncxx::to_json(operation) << std::endl;
12851287
if (session) {
12861288
_db->create_collection(*session, collection_name);
12871289
} else if (arguments.find("encryptedFields") != arguments.end()) {
12881290
auto encrypted_fields = arguments["encryptedFields"].get_document().value;
12891291
auto encrypted_fields_map = make_document(kvp("encryptedFields", encrypted_fields));
12901292
_db->create_collection(collection_name, std::move(encrypted_fields_map));
1293+
std::cerr << "CREATED COLLECTION WITH ENCRYPTED FIELDS" << std::endl;
12911294
} else {
12921295
_db->create_collection(collection_name);
12931296
}
12941297
return empty_document;
12951298
}
12961299

1300+
document::value operation_runner::_run_drop_collection(document::view operation) {
1301+
using bsoncxx::builder::basic::make_document;
1302+
using bsoncxx::builder::basic::kvp;
1303+
1304+
bsoncxx::document::value empty_document({});
1305+
1306+
auto arguments = operation["arguments"].get_document().value;
1307+
1308+
std::cerr << "DROP COLLECTION: " << bsoncxx::to_json(operation) << std::endl;
1309+
auto collection_name = operation["arguments"]["collection"].get_string().value;
1310+
1311+
if (arguments.find("encryptedFields") != arguments.end()) {
1312+
auto encrypted_fields = arguments["encryptedFields"].get_document().value;
1313+
auto encrypted_fields_map = make_document(kvp("encryptedFields", encrypted_fields));
1314+
_db->collection(collection_name).drop(std::move(encrypted_fields_map));
1315+
std::cerr << "CREATED COLLECTION WITH ENCRYPTED FIELDS" << std::endl;
1316+
} else {
1317+
_db->collection(collection_name).drop();
1318+
}
1319+
return empty_document;
1320+
}
1321+
12971322
operation_runner::operation_runner(collection* coll) : operation_runner(nullptr, coll) {}
12981323
operation_runner::operation_runner(database* db,
12991324
collection* coll,
@@ -1387,9 +1412,7 @@ document::value operation_runner::run(document::view operation) {
13871412
_coll->drop();
13881413
return empty_document;
13891414
} else if (key.compare("dropCollection") == 0) {
1390-
auto collection_name = operation["arguments"]["collection"].get_string().value;
1391-
_db->collection(collection_name).drop();
1392-
return empty_document;
1415+
return _run_drop_collection(operation);
13931416
} else if (key.compare("listCollectionNames") == 0) {
13941417
_db->list_collection_names();
13951418
return empty_document;

src/mongocxx/test/spec/operation.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class operation_runner {
5959
document::value _run_aggregate(document::view operation);
6060
document::value _run_count(document::view operation);
6161
document::value _run_create_collection(document::view operation);
62+
document::value _run_drop_collection(document::view operation);
6263
document::value _run_distinct(document::view operation);
6364
document::value _run_find(document::view operation);
6465
document::value _run_delete_many(document::view operation);

0 commit comments

Comments
 (0)