-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathMySQLDatabase.swift
71 lines (58 loc) · 1.97 KB
/
MySQLDatabase.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
63
64
65
66
67
68
69
70
71
import Logging
import NIOCore
public protocol MySQLDatabase {
var eventLoop: any EventLoop { get }
var logger: Logger { get }
func send(_ command: any MySQLCommand, logger: Logger) -> EventLoopFuture<Void>
func withConnection<T>(_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T>
}
public struct MySQLCommandState {
static var noResponse: MySQLCommandState {
return .init()
}
static var done: MySQLCommandState {
return .init(done: true)
}
static func response(_ packets: [MySQLPacket]) -> MySQLCommandState {
return .init(response: packets)
}
let response: [MySQLPacket]
let done: Bool
let resetSequence: Bool
var error: Error?
public init(
response: [MySQLPacket] = [],
done: Bool = false,
resetSequence: Bool = false,
error: Error? = nil
) {
self.response = response
self.done = done
self.resetSequence = resetSequence
self.error = error
}
}
public protocol MySQLCommand {
func handle(packet: inout MySQLPacket, capabilities: MySQLProtocol.CapabilityFlags) throws -> MySQLCommandState
func activate(capabilities: MySQLProtocol.CapabilityFlags) throws -> MySQLCommandState
}
extension MySQLDatabase {
public func logging(to logger: Logger) -> any MySQLDatabase {
_MySQLDatabaseWithLogger(database: self, logger: logger)
}
}
private struct _MySQLDatabaseWithLogger {
let database: any MySQLDatabase
let logger: Logger
}
extension _MySQLDatabaseWithLogger: MySQLDatabase {
var eventLoop: any EventLoop {
self.database.eventLoop
}
func send(_ command: any MySQLCommand, logger: Logger) -> EventLoopFuture<Void> {
self.database.send(command, logger: logger)
}
func withConnection<T>(_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
self.database.withConnection(closure)
}
}