Skip to content

Commit 22c6856

Browse files
committed
refactor so collection options variant of drop calls the common _drop method
1 parent 59980a1 commit 22c6856

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

src/mongocxx/collection.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,8 @@ cursor collection::list_indexes(const client_session& session) const {
12781278
}
12791279

12801280
void collection::_drop(const client_session* session,
1281-
const stdx::optional<mongocxx::write_concern>& wc) {
1281+
const stdx::optional<mongocxx::write_concern>& wc,
1282+
bsoncxx::document::view_or_value collection_options) {
12821283
bson_error_t error;
12831284

12841285
bsoncxx::builder::basic::document opts_doc;
@@ -1290,6 +1291,10 @@ void collection::_drop(const client_session* session,
12901291
opts_doc.append(bsoncxx::builder::concatenate_doc{session->_get_impl().to_document()});
12911292
}
12921293

1294+
if (!collection_options.view().empty()) {
1295+
opts_doc.append(bsoncxx::builder::concatenate_doc{collection_options});
1296+
}
1297+
12931298
scoped_bson_t opts_bson{opts_doc.view()};
12941299
auto result =
12951300
libmongoc::collection_drop_with_opts(_get_impl().collection_t, opts_bson.bson(), &error);
@@ -1304,35 +1309,22 @@ void collection::_drop(const client_session* session,
13041309
}
13051310

13061311
void collection::drop(const stdx::optional<mongocxx::write_concern>& wc) {
1307-
return _drop(nullptr, wc);
1312+
return _drop(nullptr, wc, {});
13081313
}
13091314

13101315
void collection::drop(const client_session& session,
13111316
const stdx::optional<mongocxx::write_concern>& wc) {
1312-
return _drop(&session, wc);
1317+
return _drop(&session, wc, {});
13131318
}
13141319

1315-
// TODO: This was needed to get FLE 2 spec tests to pass.
1316-
// Should we allow users to specify options, or should there be a specialized
1317-
// drop for specifying encrypted fields?
1320+
// From the spec:
1321+
// https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.rst#user-facing-api
1322+
// > Drivers MUST support a BSON document option named encryptedFields for any
1323+
// > drop command helpers (e.g. Database.dropCollection(), Collection.drop()).
1324+
// > This option will only be interpreted by the helper method and MUST NOT be
1325+
// > passed to the drop command.
13181326
void collection::drop(bsoncxx::document::view_or_value collection_options) {
1319-
bson_error_t error;
1320-
1321-
bsoncxx::builder::basic::document opts_doc;
1322-
1323-
opts_doc.append(bsoncxx::builder::concatenate_doc{collection_options});
1324-
1325-
scoped_bson_t opts_bson{opts_doc.view()};
1326-
auto result =
1327-
libmongoc::collection_drop_with_opts(_get_impl().collection_t, opts_bson.bson(), &error);
1328-
1329-
// Throw an exception if the command failed, unless the failure was due to a non-existent
1330-
// collection. We check for this failure using 'code', but we fall back to checking 'message'
1331-
// for old server versions (3.0 and earlier) that do not send a code with the command response.
1332-
if (!result && !(error.code == ::MONGOC_ERROR_COLLECTION_DOES_NOT_EXIST ||
1333-
stdx::string_view{error.message} == stdx::string_view{"ns not found"})) {
1334-
throw_exception<operation_exception>(error);
1335-
}
1327+
return _drop(nullptr, {}, collection_options);
13361328
}
13371329

13381330
void collection::read_concern(class read_concern rc) {

src/mongocxx/collection.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,8 @@ class MONGOCXX_API collection {
18901890

18911891
MONGOCXX_PRIVATE void _drop(
18921892
const client_session* session,
1893-
const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern);
1893+
const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern,
1894+
bsoncxx::document::view_or_value collection_options);
18941895

18951896
MONGOCXX_PRIVATE cursor _find(const client_session* session,
18961897
bsoncxx::document::view_or_value filter,

0 commit comments

Comments
 (0)