Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/MongoSwift/MongoCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public class MongoCollection<T: Codable> {
/// 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()
}
}
4 changes: 2 additions & 2 deletions Sources/MongoSwift/MongoDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
16 changes: 13 additions & 3 deletions Sources/MongoSwift/Operations/DropCollectionOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ import mongoc
/// An operation corresponding to a "drop" command on a MongoCollection.
internal struct DropCollectionOperation<T: Codable>: Operation {
private let collection: MongoCollection<T>
private let session: ClientSession?

internal init(collection: MongoCollection<T>) {
internal init(collection: MongoCollection<T>, 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)
}
}
}
16 changes: 13 additions & 3 deletions Sources/MongoSwift/Operations/DropDatabaseOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
6 changes: 4 additions & 2 deletions Tests/MongoSwiftTests/ClientSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down