-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathRunCommandOperation.swift
62 lines (54 loc) · 2.27 KB
/
RunCommandOperation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import CLibMongoC
/// Options to use when running a command against a `MongoDatabase`.
public struct RunCommandOptions: Encodable {
/// An optional `ReadConcern` to use for this operation. This option should only be used when executing a command
/// that reads.
public var readConcern: ReadConcern?
/// An optional `ReadPreference` to use for this operation. This option should only be used when executing a
/// command that reads.
public var readPreference: ReadPreference?
/// An optional `WriteConcern` to use for this operation. This option should only be used when executing a command
/// that writes.
public var writeConcern: WriteConcern?
/// Convenience initializer allowing any/all parameters to be omitted or optional.
public init(
readConcern: ReadConcern? = nil,
readPreference: ReadPreference? = nil,
writeConcern: WriteConcern? = nil
) {
self.readConcern = readConcern
self.readPreference = readPreference
self.writeConcern = writeConcern
}
private enum CodingKeys: String, CodingKey {
case readConcern, writeConcern
}
}
/// An operation corresponding to a `runCommand` call.
internal struct RunCommandOperation: Operation {
private let database: MongoDatabase
private let command: BSONDocument
private let options: RunCommandOptions?
internal init(
database: MongoDatabase,
command: BSONDocument,
options: RunCommandOptions?
) {
self.database = database
self.command = command
self.options = options
}
internal func execute(using connection: Connection, session: ClientSession?) throws -> BSONDocument {
let opts = try encodeOptions(options: self.options, session: session)
return try self.database.withMongocDatabase(from: connection) { dbPtr in
try ReadPreference.withOptionalMongocReadPreference(from: self.options?.readPreference) { rpPtr in
try runMongocCommandWithReply(
command: self.command,
options: opts
) { cmdPtr, optsPtr, replyPtr, error in
mongoc_database_command_with_opts(dbPtr, cmdPtr, rpPtr, optsPtr, replyPtr, &error)
}
}
}
}
}