forked from vapor/mysql-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQLData.swift
236 lines (210 loc) · 7.13 KB
/
MySQLData.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
@_exported import struct Foundation.Date
public struct MySQLData: CustomStringConvertible, ExpressibleByStringLiteral, ExpressibleByIntegerLiteral, ExpressibleByBooleanLiteral, MySQLDataConvertible {
public enum Format {
case binary
case text
}
public static var null: MySQLData {
return .init(type: .null, buffer: nil)
}
public let type: MySQLProtocol.DataType
public let format: Format
public let buffer: ByteBuffer?
/// If `true`, this value is unsigned.
public var isUnsigned: Bool
// MARK: Initializers
public init(
type: MySQLProtocol.DataType,
format: Format = .binary,
buffer: ByteBuffer?,
isUnsigned: Bool = false
) {
self.type = type
self.format = format
self.buffer = buffer
self.isUnsigned = isUnsigned
}
public init(string: String) {
self.format = .binary
self.type = .varString
var buffer = ByteBufferAllocator().buffer(capacity: string.utf8.count)
buffer.writeString(string)
self.buffer = buffer
self.isUnsigned = false
}
public init(int: Int) {
self.format = .binary
self.type = .longlong
assert(Int.bitWidth == 64)
var buffer = ByteBufferAllocator().buffer(capacity: 8)
buffer.writeInteger(int, endianness: .little)
self.isUnsigned = false
self.buffer = buffer
}
public init(bool: Bool) {
self.format = .binary
self.type = .bit
var buffer = ByteBufferAllocator().buffer(capacity: 1)
buffer.writeInteger(bool ? 1 : 0, endianness: .little, as: UInt8.self)
self.isUnsigned = true
self.buffer = buffer
}
public init(double: Double) {
var double = double
self.format = .binary
self.type = .double
var buffer = ByteBufferAllocator().buffer(capacity: MemoryLayout<Double>.size)
_ = Swift.withUnsafeBytes(of: &double) { ptr in
buffer.writeBytes(ptr)
}
self.isUnsigned = false
self.buffer = buffer
}
public init(float: Float) {
var float = float
self.format = .binary
self.type = .float
var buffer = ByteBufferAllocator().buffer(capacity: MemoryLayout<Float>.size)
_ = Swift.withUnsafeBytes(of: &float) { ptr in
buffer.writeBytes(ptr)
}
self.isUnsigned = false
self.buffer = buffer
}
public init(date: Date) {
self.init(time: .init(date: date))
}
public init(time: MySQLTime) {
var buffer = ByteBufferAllocator().buffer(capacity: 11)
buffer.writeMySQLTime(time)
self.init(type: .datetime, format: .binary, buffer: buffer, isUnsigned: false)
}
// MARK: Literal Initializers
public init(booleanLiteral value: Bool) {
self.init(bool: value)
}
public init(stringLiteral value: String) {
self.init(string: value)
}
public init(integerLiteral value: Int) {
self.init(int: value)
}
public init?(mysqlData: MySQLData) {
self = mysqlData
}
// MARK: Converters
public var string: String? {
guard var buffer = self.buffer else {
return nil
}
switch format {
case .text:
return buffer.readString(length: buffer.readableBytes)
default:
switch self.type {
case .varchar, .varString, .string, .blob:
return buffer.readString(length: buffer.readableBytes)
case .longlong, .long, .int24, .short, .tiny, .bit:
return self.int?.description
default:
return nil
}
}
}
public var bool: Bool? {
switch self.string {
case "true", "1": return true
case "false", "0": return false
default: return nil
}
}
public var int: Int? {
guard var buffer = self.buffer else {
return nil
}
switch format {
case .text:
return buffer.readString(length: buffer.readableBytes).flatMap(Int.init)
default:
switch self.type {
case .varchar, .varString:
return buffer.readString(length: buffer.readableBytes).flatMap(Int.init)
case .longlong:
#warning("TODO: consider throwing on overflow")
if self.isUnsigned {
return buffer.readInteger(endianness: .little, as: UInt64.self)
.flatMap(Int.init)
} else {
return buffer.readInteger(endianness: .little, as: Int64.self)
.flatMap(Int.init)
}
case .long, .int24:
#warning("TODO: consider throwing on overflow")
if self.isUnsigned {
return buffer.readInteger(endianness: .little, as: UInt32.self)
.flatMap(Int.init)
} else {
return buffer.readInteger(endianness: .little, as: Int32.self)
.flatMap(Int.init)
}
case .short:
#warning("TODO: consider throwing on overflow")
if self.isUnsigned {
return buffer.readInteger(endianness: .little, as: UInt16.self)
.flatMap(Int.init)
} else {
return buffer.readInteger(endianness: .little, as: Int16.self)
.flatMap(Int.init)
}
case .tiny, .bit:
#warning("TODO: consider throwing on overflow")
if self.isUnsigned {
return buffer.readInteger(endianness: .little, as: UInt8.self)
.flatMap(Int.init)
} else {
return buffer.readInteger(endianness: .little, as: Int8.self)
.flatMap(Int.init)
}
default:
return nil
}
}
}
public var date: Date? {
guard let time = self.time else {
return nil
}
return time.date
}
public var time: MySQLTime? {
guard var buffer = self.buffer else {
return nil
}
switch self.type {
case .timestamp, .datetime:
return buffer.readMySQLTime()
default: return nil
}
}
public var mysqlData: MySQLData? {
return self
}
public var description: String {
if self.buffer == nil {
return "nil"
} else {
switch self.type {
case .longlong, .long, .int24, .short, .tiny:
return self.int!.description
case .bit:
return self.bool!.description
case .datetime, .timestamp:
return self.date!.description
case .varchar, .varString, .string, .blob:
return self.string!.debugDescription
default:
return "<\(self.type)>"
}
}
}
}