-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathMySQLCRUD.swift
558 lines (527 loc) · 15.9 KB
/
MySQLCRUD.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//
// MySQLCRUD.swift
// PerfectMySQL
//
// Created by Kyle Jessup on 2018-03-06.
//
import Foundation
import PerfectCRUD
public struct MySQLCRUDError: Error, CustomStringConvertible {
public let description: String
public init(_ msg: String) {
description = msg
CRUDLogging.log(.error, msg)
}
}
// maps column name to position which must be computed once before row reading action
typealias MySQLCRUDColumnMap = [String:Int]
class MySQLCRUDRowReader<K : CodingKey>: KeyedDecodingContainerProtocol {
typealias Key = K
var codingPath: [CodingKey] = []
var allKeys: [Key] = []
let database: MySQL
let statement: MySQLStmt
let columns: MySQLCRUDColumnMap
let row: MySQLStmt.Results.Element
init(_ db: MySQL,
stat: MySQLStmt,
columns cols: MySQLCRUDColumnMap,
row r: MySQLStmt.Results.Element) {
database = db
statement = stat
columns = cols
row = r
}
func column(_ key: Key) -> Any? {
guard let idx = columns[key.stringValue],
idx >= 0,
idx < row.count else {
return nil
}
return row[idx]
}
func contains(_ key: Key) -> Bool {
return nil != columns[key.stringValue]
}
func decodeNil(forKey key: Key) throws -> Bool {
return nil == column(key)
}
func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
return ((column(key) as? Int8) ?? 0) != 0
}
func decode(_ type: Int.Type, forKey key: Key) throws -> Int {
let a = column(key)
switch a {
case let i as Int64:
return Int(i)
case let i as Int32:
return Int(i)
case let i as Int16:
return Int(i)
case let i as Int8:
return Int(i)
case let i as Int:
return i
default:
throw MySQLCRUDError("Could not convert \(String(describing: a)) into an Int for key: \(key.stringValue)")
}
}
func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 {
return (column(key) as? Int8) ?? 0
}
func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 {
return (column(key) as? Int16) ?? 0
}
func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 {
return (column(key) as? Int32) ?? 0
}
func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 {
return (column(key) as? Int64) ?? 0
}
func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt {
let a = column(key)
switch a {
case let i as UInt64:
return UInt(i)
case let i as UInt32:
return UInt(i)
case let i as UInt16:
return UInt(i)
case let i as UInt8:
return UInt(i)
case let i as UInt:
return i
default:
throw MySQLCRUDError("Could not convert \(String(describing: a)) into an UInt for key: \(key.stringValue)")
}
}
func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 {
return (column(key) as? UInt8) ?? 0
}
func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 {
return (column(key) as? UInt16) ?? 0
}
func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 {
return (column(key) as? UInt32) ?? 0
}
func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 {
return (column(key) as? UInt64) ?? 0
}
func decode(_ type: Float.Type, forKey key: Key) throws -> Float {
return (column(key) as? Float) ?? 0
}
func decode(_ type: Double.Type, forKey key: Key) throws -> Double {
return (column(key) as? Double) ?? 0
}
func decode(_ type: String.Type, forKey key: Key) throws -> String {
return (column(key) as? String) ?? ""
}
func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T : Decodable {
guard let special = SpecialType(type) else {
throw CRUDDecoderError("Unsupported type: \(type) for key: \(key.stringValue)")
}
let val = column(key)
switch special {
case .uint8Array:
let ret: [UInt8] = (val as? [UInt8]) ?? []
return ret as! T
case .int8Array:
let ret: [Int8] = ((val as? [UInt8]) ?? []).map { Int8($0) }
return ret as! T
case .data:
let bytes: [UInt8] = (val as? [UInt8]) ?? []
return Data(bytes: bytes) as! T
case .uuid:
guard let str = val as? String, let uuid = UUID(uuidString: str) else {
throw CRUDDecoderError("Invalid UUID string \(String(describing: val)).")
}
return uuid as! T
case .date:
guard let str = val as? String, let date = Date(fromMysqlFormatted: str) else {
throw CRUDDecoderError("Invalid Date string \(String(describing: val)).")
}
return date as! T
case .url:
guard let str = val as? String, let url = URL(string: str) else {
throw CRUDDecoderError("Invalid URL string \(String(describing: val)).")
}
return url as! T
case .codable:
guard let data = (val as? String)?.data(using: .utf8) else {
throw CRUDDecoderError("Unsupported type: \(type) for key: \(key.stringValue)")
}
return try JSONDecoder().decode(type, from: data)
}
}
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
throw CRUDDecoderError("Unimplimented nestedContainer")
}
func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer {
throw CRUDDecoderError("Unimplimented nestedUnkeyedContainer")
}
func superDecoder() throws -> Decoder {
throw CRUDDecoderError("Unimplimented superDecoder")
}
func superDecoder(forKey key: Key) throws -> Decoder {
throw CRUDDecoderError("Unimplimented superDecoder")
}
}
struct MySQLColumnInfo: Codable {
enum CodingKeys: String, CodingKey {
case field = "Field", type = "Type", null = "Null", key = "Key"
}
let field: String
let type: String
private let null: String
private let key: String
var isNull: Bool {
return null == "YES"
}
var isPrimaryKey: Bool {
return key == "PRI"
}
}
class MySQLGenDelegate: SQLGenDelegate {
let database: MySQL
var parentTableStack: [TableStructure] = []
var bindings: Bindings = []
init(connection db: MySQL) {
database = db
}
func getEmptyInsertSnippet() -> String {
return "() VALUES ()"
}
func getBinding(for expr: Expression) throws -> String {
bindings.append(("?", expr))
return "?"
}
func quote(identifier: String) throws -> String {
return "`\(identifier)`"
}
func getCreateTableSQL(forTable: TableStructure, policy: TableCreatePolicy) throws -> [String] {
parentTableStack.append(forTable)
defer {
parentTableStack.removeLast()
}
var sub: [String]
if !policy.contains(.shallow) {
sub = try forTable.subTables.flatMap { try getCreateTableSQL(forTable: $0, policy: policy) }
} else {
sub = []
}
if policy.contains(.dropTable) {
sub += ["DROP TABLE IF EXISTS \(try quote(identifier: forTable.tableName))"]
}
if !policy.contains(.dropTable),
policy.contains(.reconcileTable),
let existingColumns = getExistingColumnData(forTable: forTable.tableName) {
let existingColumnMap: [String:MySQLColumnInfo] = .init(uniqueKeysWithValues: existingColumns.map { ($0.field, $0) })
let newColumnMap: [String:TableStructure.Column] = .init(uniqueKeysWithValues: forTable.columns.map { ($0.name.lowercased(), $0) })
let addColumns = newColumnMap.keys.filter { existingColumnMap[$0] == nil }
let removeColumns: [String] = existingColumnMap.keys.filter { newColumnMap[$0] == nil }
sub += try removeColumns.map {
return """
ALTER TABLE \(try quote(identifier: forTable.tableName)) DROP COLUMN \(try quote(identifier: $0))
"""
}
sub += try addColumns.compactMap { newColumnMap[$0] }.map {
let nameType = try getColumnDefinition($0)
return """
ALTER TABLE \(try quote(identifier: forTable.tableName)) ADD COLUMN \(nameType)
"""
}
return sub
} else {
sub += [
"""
CREATE TABLE IF NOT EXISTS \(try quote(identifier: forTable.tableName)) (
\(try forTable.columns.map { try getColumnDefinition($0) }.joined(separator: ",\n\t"))
)
"""]
}
return sub
}
func getCreateIndexSQL(forTable name: String, on columns: [String], unique: Bool) throws -> [String] {
let stat =
"""
CREATE \(unique ? "UNIQUE " : "")INDEX \(try quote(identifier: "index_\(columns.joined(separator: "_"))"))
ON \(try quote(identifier: name)) (\(try columns.map{try quote(identifier: $0)}.joined(separator: ",")))
"""
return [stat]
}
func getExistingColumnData(forTable: String) -> [MySQLColumnInfo]? {
do {
let statement = "SHOW COLUMNS FROM \(try quote(identifier: forTable))"
let stat = MySQLStmt(database)
guard stat.prepare(statement: statement) else {
return nil
}
let exeDelegate = MySQLStmtExeDelegate(connection: database, stat: stat)
var ret: [MySQLColumnInfo] = []
while try exeDelegate.hasNext() {
let rowDecoder: CRUDRowDecoder<ColumnKey> = CRUDRowDecoder(delegate: exeDelegate)
ret.append(try MySQLColumnInfo(from: rowDecoder))
}
guard !ret.isEmpty else {
return nil
}
return ret
} catch {
return nil
}
}
func getColumnDefinition(_ column: TableStructure.Column) throws -> String {
let name = column.name
let type = column.type
let typeName: String
switch type {
case is Int.Type:
typeName = "bigint"
case is Int8.Type:
typeName = "tinyint"
case is Int16.Type:
typeName = "smallint"
case is Int32.Type:
typeName = "int"
case is Int64.Type:
typeName = "bigint"
case is UInt.Type:
typeName = "bigint unsigned"
case is UInt8.Type:
typeName = "tinyint unsigned"
case is UInt16.Type:
typeName = "smallint unsigned"
case is UInt32.Type:
typeName = "int unsigned"
case is UInt64.Type:
typeName = "bigint unsigned"
case is Double.Type:
typeName = "double"
case is Float.Type:
typeName = "float"
case is Bool.Type:
typeName = "tinyint"
case is String.Type:
typeName = "longtext"
default:
guard let special = SpecialType(type) else {
throw MySQLCRUDError("Unsupported SQL column type \(type)")
}
switch special {
case .uint8Array:
typeName = "longblob"
case .int8Array:
typeName = "longblob"
case .data:
typeName = "longblob"
case .uuid:
typeName = "varchar(36)"
case .date:
typeName = "datetime"
case .url:
typeName = "longtext"
case .codable:
typeName = "json"
}
}
let addendum: String
if column.properties.contains(.primaryKey) {
addendum = " PRIMARY KEY"
} else if !column.optional {
addendum = " NOT NULL"
} else {
addendum = ""
}
return "\(try quote(identifier: name)) \(typeName)\(addendum)"
}
}
typealias MySQLColumnMap = [String:Int]
struct MySQLDirectExeDelegate: SQLExeDelegate {
let connection: MySQL
let sql: String
func bind(_ bindings: Bindings, skip: Int) throws {
guard bindings.isEmpty else {
throw MySQLCRUDError("Binds are not permitted for this type of statement.")
}
}
func hasNext() throws -> Bool {
guard connection.query(statement: sql) else {
throw MySQLCRUDError("Error executing statement. \(connection.errorMessage())")
}
return false
}
func next<A>() throws -> KeyedDecodingContainer<A>? where A : CodingKey {
return nil
}
}
class MySQLStmtExeDelegate: SQLExeDelegate {
let connection: MySQL
let statement: MySQLStmt
var results: MySQLStmt.Results?
let columnMap: MySQLColumnMap
init(connection c: MySQL, stat: MySQLStmt) {
connection = c
statement = stat
var m = MySQLColumnMap()
let inv = stat.fieldNames()
inv.forEach {
let (pos, name) = $0
m[name] = pos
}
columnMap = m
}
func bind(_ bindings: Bindings, skip: Int) throws {
try bindings[skip...].forEach {
let (_, expr) = $0
try bindOne(expr: expr)
}
}
func hasNext() throws -> Bool {
if nil == results {
guard statement.execute() else {
throw MySQLCRUDError("Error executing statement. \(statement.errorMessage())")
}
results = statement.results()
}
if results?.fetchRow() ?? false {
return true
}
statement.reset()
results = nil
return false
}
func next<A>() throws -> KeyedDecodingContainer<A>? where A : CodingKey {
guard let row = results?.currentRow() else {
return nil
}
return KeyedDecodingContainer(MySQLCRUDRowReader<A>(connection,
stat: statement,
columns: columnMap,
row: row))
}
private func bindOne(expr: CRUDExpression) throws {
switch expr {
case .lazy(let e):
try bindOne(expr: e())
case .integer(let i):
statement.bindParam(i)
case .decimal(let d):
statement.bindParam(d)
case .string(let s):
statement.bindParam(s)
case .blob(let b):
statement.bindParam(b)
case .bool(let b):
statement.bindParam(b ? 1 : 0)
case .date(let d):
statement.bindParam(d.mysqlFormatted())
case .url(let u):
statement.bindParam(u.absoluteString)
case .uuid(let u):
statement.bindParam(u.uuidString)
case .null:
statement.bindParam()
case .column(_), .and(_, _), .or(_, _),
.equality(_, _), .inequality(_, _),
.not(_), .lessThan(_, _), .lessThanEqual(_, _),
.greaterThan(_, _), .greaterThanEqual(_, _),
.keyPath(_), .in(_, _), .like(_, _, _, _):
throw MySQLCRUDError("Asked to bind unsupported expression type: \(expr)")
case .uinteger(let i):
statement.bindParam(i)
case .integer64(let i):
statement.bindParam(i)
case .uinteger64(let i):
statement.bindParam(i)
case .integer32(let i):
statement.bindParam(i)
case .uinteger32(let i):
statement.bindParam(i)
case .integer16(let i):
statement.bindParam(i)
case .uinteger16(let i):
statement.bindParam(i)
case .integer8(let i):
statement.bindParam(i)
case .uinteger8(let i):
statement.bindParam(i)
case .float(let d):
statement.bindParam(d)
case .sblob(let b):
statement.bindParam(b)
}
}
}
public struct MySQLDatabaseConfiguration: DatabaseConfigurationProtocol {
let connection: MySQL
public init(url: String?,
name: String?,
host: String?,
port: Int?,
user: String?,
pass: String?) throws {
guard let database = name, let host = host else {
throw MySQLCRUDError("Database name and host must be provided.")
}
try self.init(database: database, host: host, port: port, username: user, password: pass)
}
public init(database: String,
host: String,
port: Int? = nil,
username: String? = nil,
password: String? = nil) throws {
connection = MySQL()
_ = connection.setOption(.MYSQL_SET_CHARSET_NAME, "utf8mb4")
guard connection.connect(host: host, user: username, password: password, db: database, port: UInt32(port ?? 0), socket: nil, flag: 0) else {
throw MySQLCRUDError("Could not connect. \(connection.errorMessage())")
}
}
public init(connection c: MySQL) {
connection = c
}
public var sqlGenDelegate: SQLGenDelegate {
return MySQLGenDelegate(connection: connection)
}
public func sqlExeDelegate(forSQL: String) throws -> SQLExeDelegate {
let noPrepCommands = ["CREATE", "DROP", "ALTER", "BEGIN", "COMMIT", "ROLLBACK", "LOCK", "UNLOCK"]
if nil != noPrepCommands.first(where: { forSQL.hasPrefix($0) }) {
return MySQLDirectExeDelegate(connection: connection, sql: forSQL)
}
let stat = MySQLStmt(connection)
guard stat.prepare(statement: forSQL) else {
throw MySQLCRUDError("Could not prepare statement. \(stat.errorMessage())")
}
return MySQLStmtExeDelegate(connection: connection, stat: stat)
}
}
extension Date {
func mysqlFormatted() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let ret = dateFormatter.string(from: self)
return ret
}
init?(fromMysqlFormatted string: String) {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let d = dateFormatter.date(from: string) {
self = d
return
}
return nil
}
}
public extension Insert {
func lastInsertId() throws -> UInt64? {
let exeDelegate = try databaseConfiguration.sqlExeDelegate(forSQL: "SELECT LAST_INSERT_ID()")
guard try exeDelegate.hasNext(), let next: KeyedDecodingContainer<ColumnKey> = try exeDelegate.next() else {
throw CRUDSQLGenError("Did not get return value from statement \"SELECT LAST_INSERT_ID()\".")
}
let value = try next.decode(UInt64.self, forKey: ColumnKey(stringValue: "LAST_INSERT_ID()")!)
return value
}
}