-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathMySQLData.swift
473 lines (428 loc) · 14.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import NIOCore
import Foundation
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 = .tiny
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(decimal: Decimal) {
let string = decimal.description
self.format = .binary
self.type = .newdecimal
var buffer = ByteBufferAllocator().buffer(capacity: string.utf8.count)
buffer.writeString(string)
self.buffer = buffer
self.isUnsigned = false
}
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(uuid: UUID) {
self.format = .binary
self.type = .blob
var buffer = ByteBufferAllocator().buffer(capacity: 16)
var cuuid = uuid.uuid
_ = Swift.withUnsafeBytes(of: &cuuid) { 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: 12)
var type: MySQLProtocol.DataType = .datetime
buffer.writeMySQLTime(time, as: &type)
self.init(type: type, format: .binary, buffer: buffer, isUnsigned: false)
}
private struct Wrapper: Encodable {
let encodable: Encodable
init(_ encodable: Encodable) {
self.encodable = encodable
}
func encode(to encoder: Encoder) throws {
try self.encodable.encode(to: encoder)
}
}
public init(json value: Encodable) throws {
let json = JSONEncoder()
let data = try json.encode(Wrapper(value))
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeBytes(data)
self.init(
type: .string,
format: .text,
buffer: buffer,
isUnsigned: true
)
}
// 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 func json<Value>(as type: Value.Type) throws -> Value?
where Value: Decodable
{
guard var buffer = self.buffer else {
return nil
}
guard let data = buffer.readBytes(length: buffer.readableBytes) else {
return nil
}
return try JSONDecoder().decode(Value.self, from: Data(data))
}
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, .tinyBlob, .mediumBlob, .longBlob, .null:
return buffer.readString(length: buffer.readableBytes)
case .longlong, .long, .int24, .short, .tiny, .bit:
return self.int?.description
default:
return nil
}
}
}
public var double: Double? {
switch self.format {
case .binary:
switch self.type {
case .double:
guard var buffer = self.buffer else {
return nil
}
guard let bytes = buffer.readBytes(length: MemoryLayout<Double>.size) else {
return nil
}
var double: Double = 0
Swift.withUnsafeMutableBytes(of: &double) { buffer in
buffer.copyBytes(from: bytes)
}
return double
case .float:
return self.float.flatMap(Double.init)
case .newdecimal:
guard var buffer = self.buffer else {
return nil
}
return buffer.readString(length: buffer.readableBytes)
.flatMap(Double.init)
default:
return nil
}
case .text:
return self.string.flatMap(Double.init)
}
}
public var decimal: Decimal? {
switch self.format {
case .binary:
switch self.type {
case .double:
guard var buffer = self.buffer else {
return nil
}
guard let bytes = buffer.readBytes(length: MemoryLayout<Double>.size) else {
return nil
}
var double: Double = 0
Swift.withUnsafeMutableBytes(of: &double) { buffer in
buffer.copyBytes(from: bytes)
}
return Decimal(floatLiteral: double)
case .float:
return self.float.flatMap { Decimal(Double($0)) }
case .newdecimal:
guard var buffer = self.buffer else {
return nil
}
return buffer.readString(length: buffer.readableBytes)
.flatMap{ Decimal(string: $0) }
case .varchar, .varString, .string, .blob, .tinyBlob, .mediumBlob, .longBlob:
guard var buffer = self.buffer else {
return nil
}
return buffer.readString(length: buffer.readableBytes)
.flatMap{ Decimal(string: $0) }
default:
return nil
}
case .text:
return self.string.flatMap{ Decimal(string: $0) }
}
}
public var float: Float? {
switch self.format {
case .binary:
switch self.type {
case .float:
guard var buffer = self.buffer else {
return nil
}
guard let bytes = buffer.readBytes(length: MemoryLayout<Float>.size) else {
return nil
}
var float: Float = 0
Swift.withUnsafeMutableBytes(of: &float) { buffer in
buffer.copyBytes(from: bytes)
}
return float
case .double:
return self.double.flatMap(Float.init)
case .newdecimal:
guard var buffer = self.buffer else {
return nil
}
return buffer.readString(length: buffer.readableBytes)
.flatMap(Float.init)
default:
return nil
}
case .text:
return self.string.flatMap(Float.init)
}
}
public var uuid: UUID? {
switch self.format {
case .binary:
guard var buffer = self.buffer else {
return nil
}
guard buffer.readableBytes == 16 else {
return nil
}
guard let bytes = buffer.readBytes(length: 16) else {
return nil
}
return UUID(uuid: (
bytes[0], bytes[1], bytes[2], bytes[3],
bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9], bytes[10], bytes[11],
bytes[12], bytes[13], bytes[14], bytes[15]
))
case .text:
return self.string.flatMap(UUID.init)
}
}
public var bool: Bool? {
switch self.string {
case "true", "1": return true
case "false", "0": return false
default: return nil
}
}
public var int: Int? {
self.fwi()
}
public var int8: Int8? {
self.fwi()
}
public var int16: Int16? {
self.fwi()
}
public var int32: Int32? {
self.fwi()
}
public var int64: Int64? {
self.fwi()
}
public var uint: UInt? {
self.fwi()
}
public var uint8: UInt8? {
self.fwi()
}
public var uint16: UInt16? {
self.fwi()
}
public var uint32: UInt32? {
self.fwi()
}
public var uint64: UInt64? {
self.fwi()
}
private func fwi<I>(_ type: I.Type = I.self) -> I?
where I: FixedWidthInteger
{
guard var buffer = self.buffer else {
return nil
}
switch format {
case .text:
return buffer.readString(length: buffer.readableBytes).flatMap(I.init)
default:
switch self.type {
case .varchar, .varString:
return buffer.readString(length: buffer.readableBytes).flatMap(I.init)
case .longlong:
if self.isUnsigned {
return buffer.readInteger(endianness: .little, as: UInt64.self)
.flatMap(I.init)
} else {
return buffer.readInteger(endianness: .little, as: Int64.self)
.flatMap(I.init)
}
case .long, .int24:
if self.isUnsigned {
return buffer.readInteger(endianness: .little, as: UInt32.self)
.flatMap(I.init)
} else {
return buffer.readInteger(endianness: .little, as: Int32.self)
.flatMap(I.init)
}
case .short:
if self.isUnsigned {
return buffer.readInteger(endianness: .little, as: UInt16.self)
.flatMap(I.init)
} else {
return buffer.readInteger(endianness: .little, as: Int16.self)
.flatMap(I.init)
}
case .tiny, .bit:
if self.isUnsigned {
return buffer.readInteger(endianness: .little, as: UInt8.self)
.flatMap(I.init)
} else {
return buffer.readInteger(endianness: .little, as: Int8.self)
.flatMap(I.init)
}
case .newdecimal:
return buffer.readString(length: buffer.readableBytes)
.flatMap(I.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.format {
case .binary:
switch self.type {
case .timestamp, .datetime, .date, .time:
return buffer.readMySQLTime()
default: return nil
}
case .text:
guard let s = buffer.readString(length: buffer.readableBytes) else { return nil }
return MySQLTime(s)
}
}
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.time!.date ?? Date(timeIntervalSince1970: 0)).description
case .varchar, .varString, .string:
return self.string!.debugDescription
case .double:
return self.double!.description
case .float:
return self.float!.description
case .blob:
return Data(self.buffer!.readableBytesView).description
default:
return "<\(self.type)>"
}
}
}
}