From 4841a47a290be63d9d89fa01d39cbe53c3cb2d8d Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Tue, 21 May 2019 16:50:47 -0400 Subject: [PATCH] support passing in sessions to db and collection drop --- Sources/MongoSwift/MongoCollection.swift | 4 ++-- Sources/MongoSwift/MongoDatabase.swift | 4 ++-- .../Operations/DropCollectionOperation.swift | 16 +++++++++++++--- .../Operations/DropDatabaseOperation.swift | 16 +++++++++++++--- Tests/MongoSwiftTests/ClientSessionTests.swift | 6 ++++-- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Sources/MongoSwift/MongoCollection.swift b/Sources/MongoSwift/MongoCollection.swift index bef04e689..6ad294ed6 100644 --- a/Sources/MongoSwift/MongoCollection.swift +++ b/Sources/MongoSwift/MongoCollection.swift @@ -87,8 +87,8 @@ public class MongoCollection { /// Drops this collection from its parent database. /// - Throws: /// - `ServerError.commandError` if an error occurs that prevents the command from executing. - public func drop() throws { - let operation = DropCollectionOperation(collection: self) + public func drop(session: ClientSession? = nil) throws { + let operation = DropCollectionOperation(collection: self, session: session) try operation.execute() } } diff --git a/Sources/MongoSwift/MongoDatabase.swift b/Sources/MongoSwift/MongoDatabase.swift index 5b5a4d0d5..5042fdfa5 100644 --- a/Sources/MongoSwift/MongoDatabase.swift +++ b/Sources/MongoSwift/MongoDatabase.swift @@ -132,8 +132,8 @@ public class MongoDatabase { /// Drops this database. /// - Throws: /// - `ServerError.commandError` if an error occurs that prevents the command from executing. - public func drop() throws { - let operation = DropDatabaseOperation(database: self) + public func drop(session: ClientSession? = nil) throws { + let operation = DropDatabaseOperation(database: self, session: session) try operation.execute() } diff --git a/Sources/MongoSwift/Operations/DropCollectionOperation.swift b/Sources/MongoSwift/Operations/DropCollectionOperation.swift index 707c25f1c..452fe0c71 100644 --- a/Sources/MongoSwift/Operations/DropCollectionOperation.swift +++ b/Sources/MongoSwift/Operations/DropCollectionOperation.swift @@ -3,15 +3,25 @@ import mongoc /// An operation corresponding to a "drop" command on a MongoCollection. internal struct DropCollectionOperation: Operation { private let collection: MongoCollection + private let session: ClientSession? - internal init(collection: MongoCollection) { + internal init(collection: MongoCollection, session: ClientSession?) { self.collection = collection + self.session = session } internal func execute() throws { + let command: Document = ["drop": self.collection.name] + let opts = try encodeOptions(options: Document(), session: self.session) + + var reply = Document() var error = bson_error_t() - guard mongoc_collection_drop(self.collection._collection, &error) else { - throw parseMongocError(error) + let success = withMutableBSONPointer(to: &reply) { replyPtr in + mongoc_collection_write_command_with_opts( + self.collection._collection, command._bson, opts?._bson, replyPtr, &error) + } + guard success else { + throw getErrorFromReply(bsonError: error, from: reply) } } } diff --git a/Sources/MongoSwift/Operations/DropDatabaseOperation.swift b/Sources/MongoSwift/Operations/DropDatabaseOperation.swift index 7797442be..a14de4426 100644 --- a/Sources/MongoSwift/Operations/DropDatabaseOperation.swift +++ b/Sources/MongoSwift/Operations/DropDatabaseOperation.swift @@ -3,15 +3,25 @@ import mongoc /// An operation corresponding to a "drop" command on a MongoDatabase. internal struct DropDatabaseOperation: Operation { private let database: MongoDatabase + private let session: ClientSession? - internal init(database: MongoDatabase) { + internal init(database: MongoDatabase, session: ClientSession?) { self.database = database + self.session = session } internal func execute() throws { + let command: Document = ["dropDatabase": 1] + let opts = try encodeOptions(options: Document(), session: self.session) + + var reply = Document() var error = bson_error_t() - guard mongoc_database_drop(self.database._database, &error) else { - throw parseMongocError(error) + let success = withMutableBSONPointer(to: &reply) { replyPtr in + mongoc_database_write_command_with_opts( + self.database._database, command._bson, opts?._bson, replyPtr, &error) + } + guard success else { + throw getErrorFromReply(bsonError: error, from: reply) } } } diff --git a/Tests/MongoSwiftTests/ClientSessionTests.swift b/Tests/MongoSwiftTests/ClientSessionTests.swift index 4517af2a5..057f9cc4a 100644 --- a/Tests/MongoSwiftTests/ClientSessionTests.swift +++ b/Tests/MongoSwiftTests/ClientSessionTests.swift @@ -51,14 +51,16 @@ final class ClientSessionTests: MongoSwiftTestCase { (name: "listIndexes", body: { _ = try $0.listIndexes(session: $1).next() }), (name: "findOneAndDelete", body: { _ = try $0.findOneAndDelete([:], session: $1) }), (name: "findOneAndReplace", body: { _ = try $0.findOneAndReplace(filter: [:], replacement: [:], session: $1) }), - (name: "findOneAndUpdate", body: { _ = try $0.findOneAndUpdate(filter: [:], update: [:], session: $1) }) + (name: "findOneAndUpdate", body: { _ = try $0.findOneAndUpdate(filter: [:], update: [:], session: $1) }), + (name: "drop", body: { _ = try $0.drop(session: $1) }) ] // list of operations on MongoDatabase that take in a session let databaseSessionOps: [DatabaseSessionOp] = [ (name: "runCommand", { try $0.runCommand(["isMaster": 0], session: $1) }), (name: "createCollection", body: { _ = try $0.createCollection("asdf", session: $1) }), - (name: "createCollection1", body: { _ = try $0.createCollection("asdf", withType: Document.self, session: $1) }) + (name: "createCollection1", body: { _ = try $0.createCollection("asf", withType: Document.self, session: $1) }), + (name: "drop", body: { _ = try $0.drop(session: $1) }) ] // list of operatoins on MongoClient that take in a session