-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDbParent.swift
563 lines (428 loc) · 17.5 KB
/
DbParent.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
559
560
561
562
563
// // This file is generated, do not edit
import Foundation
import GRDB
// Mapped table to struct
public struct DbParent: FetchableRecord, PersistableRecord, Codable, Equatable, Hashable, GenDbTable, GenDbTableWithSelf {
// Static queries
public static let insertUniqueQuery = "insert into Parent (parentUuid, userUuid) values (?, ?)"
public static let replaceUniqueQuery = "replace into Parent (parentUuid, userUuid) values (?, ?)"
public static let insertOrIgnoreUniqueQuery = "insert or ignore into Parent (parentUuid, userUuid) values (?, ?)"
public static let deleteAllQuery = "delete from Parent"
public static let selectAllQuery = "select parentUuid, userUuid from Parent"
public static let selectCountQuery = "select count(*) from Parent"
public static let updateUniqueQuery = "update Parent set userUuid = ? where parentUuid = ?"
public static let upsertUserUuidQuery = "insert into Parent (parentUuid, userUuid) values (?, ?) on conflict (parentUuid) do update set userUuid=excluded.userUuid"
// Mapped columns to properties
public var parentUuid: UUID
public var userUuid: UUID?
// Default initializer
public init(parentUuid: UUID,
userUuid: UUID?) {
self.parentUuid = parentUuid
self.userUuid = userUuid
}
// Row initializer
public init(row: Row, startingIndex: Int) {
parentUuid = row[0 + startingIndex]
userUuid = row[1 + startingIndex]
}
// The initializer defined by the protocol
public init(row: Row) {
self.init(row: row, startingIndex: 0)
}
// Easy way to get the PrimaryKey from the table
public func primaryKey() -> PrimaryKey {
.init(parentUuid: parentUuid)
}
public func hash(into hasher: inout Hasher) {
hasher.combine(parentUuid)
}
public func genInsert(db: Database, assertOneRowAffected: Bool = true) throws {
let statement = try db.cachedStatement(sql: Self.insertUniqueQuery)
let arguments: StatementArguments = try [
parentUuid.uuidString,
userUuid?.uuidString
]
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
Logging.log(Self.insertUniqueQuery, statementArguments: arguments)
try statement.execute()
if assertOneRowAffected {
// Only 1 row should be affected
assert(db.changesCount == 1)
}
}
public func genInsertOrIgnore(db: Database) throws {
let statement = try db.cachedStatement(sql: Self.insertOrIgnoreUniqueQuery)
let arguments: StatementArguments = try [
parentUuid.uuidString,
userUuid?.uuidString
]
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
Logging.log(Self.insertOrIgnoreUniqueQuery, statementArguments: arguments)
try statement.execute()
}
public func genReplace(db: Database) throws {
let statement = try db.cachedStatement(sql: Self.replaceUniqueQuery)
let arguments: StatementArguments = try [
parentUuid.uuidString,
userUuid?.uuidString
]
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
Logging.log(Self.replaceUniqueQuery, statementArguments: arguments)
try statement.execute()
}
public func genUpsertUserUuid(db: Database) throws {
let statement = try db.cachedStatement(sql: Self.upsertUserUuidQuery)
let arguments: StatementArguments = try [
parentUuid.uuidString,
userUuid?.uuidString
]
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
Logging.log(Self.upsertUserUuidQuery, statementArguments: arguments)
try statement.execute()
}
public func genInsertOrDelete(db: Database, insert: Bool, assertOneRowAffected: Bool = true) throws {
if insert {
try genInsert(db: db, assertOneRowAffected: assertOneRowAffected)
} else {
try primaryKey().genDelete(db: db, assertOneRowAffected: assertOneRowAffected)
}
}
public static func genDeleteAll(db: Database) throws {
let statement = try db.cachedStatement(sql: Self.deleteAllQuery)
Logging.log(Self.deleteAllQuery, statementArguments: .init())
try statement.execute()
}
public
static func genDeleteByParentUuid(db: Database, parentUuid: UUID) throws {
let arguments: StatementArguments = try [
parentUuid.uuidString
]
Logging.log("delete from Parent where parentUuid = ?", statementArguments: arguments)
let statement = try db.cachedStatement(sql: "delete from Parent where parentUuid = ?")
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
try statement.execute()
}
public
static func genDeleteByUserUuid(db: Database, userUuid: UUID) throws {
let arguments: StatementArguments = try [
userUuid.uuidString
]
Logging.log("delete from Parent where userUuid = ?", statementArguments: arguments)
let statement = try db.cachedStatement(sql: "delete from Parent where userUuid = ?")
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
try statement.execute()
}
public func genUpdate(db: Database, assertOneRowAffected: Bool = true) throws {
let statement = try db.cachedStatement(sql: Self.updateUniqueQuery)
let arguments: StatementArguments = try [
userUuid?.uuidString,
parentUuid.uuidString
]
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
Logging.log(Self.updateUniqueQuery, statementArguments: arguments)
try statement.execute()
if assertOneRowAffected {
// Only 1 row should be affected
assert(db.changesCount == 1)
}
}
public enum UpdatableColumn: String {
case parentUuid, userUuid
public static let updateParentUuidQuery = "update Parent set parentUuid = ? where parentUuid = ?"
public static let updateUserUuidQuery = "update Parent set userUuid = ? where parentUuid = ?"
}
public enum UpdatableColumnWithValue {
case parentUuid(UUID), userUuid(UUID?)
var columnName: String {
switch self {
case .parentUuid: return "parentUuid"
case .userUuid: return "userUuid"
}
}
public func toUpdatableColumn() -> UpdatableColumn {
switch self {
case .parentUuid: return .parentUuid
case .userUuid: return .userUuid
}
}
public func update(entity: inout DbParent) {
switch self {
case let .parentUuid(value): entity.parentUuid = value
case let .userUuid(value): entity.userUuid = value
}
}
}
public
func createColumnParentUuid() -> Self.UpdatableColumnWithValue {
return .parentUuid(parentUuid)
}
public
func createColumnUserUuid() -> Self.UpdatableColumnWithValue {
return .userUuid(userUuid)
}
public func genUpsertDynamic(db: Database, columns: [UpdatableColumn]) throws {
// Check for duplicates
assert(Set(columns).count == columns.count)
if columns.isEmpty {
return
}
var upsertQuery = DbParent.insertUniqueQuery + "on conflict (parentUuid) do update set "
var processedAtLeastOneColumns = false
for column in columns {
switch column {
case .parentUuid:
if processedAtLeastOneColumns {
upsertQuery += ", "
}
upsertQuery += "parentUuid=excluded.parentUuid"
case .userUuid:
if processedAtLeastOneColumns {
upsertQuery += ", "
}
upsertQuery += "userUuid=excluded.userUuid"
}
processedAtLeastOneColumns = true
}
let arguments: StatementArguments = try [
parentUuid.uuidString,
userUuid?.uuidString
]
Logging.log(upsertQuery, statementArguments: arguments)
let statement = try db.cachedStatement(sql: upsertQuery)
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
try statement.execute()
}
public mutating func genUpsertDynamicMutate(db: Database, columns: [UpdatableColumnWithValue]) throws {
for column in columns {
column.update(entity: &self)
}
try genUpsertDynamic(db: db, columns: columns.map { $0.toUpdatableColumn() })
}
public
static func genUpdateParentUuidAllRows(db: Database, parentUuid: UUID) throws {
let arguments: StatementArguments = try [
parentUuid.uuidString
]
Logging.log("update Parent set parentUuid = ?", statementArguments: arguments)
let statement = try db.cachedStatement(sql: "update Parent set parentUuid = ?")
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
try statement.execute()
}
public
static func genUpdateUserUuidAllRows(db: Database, userUuid: UUID?) throws {
let arguments: StatementArguments = try [
userUuid?.uuidString
]
Logging.log("update Parent set userUuid = ?", statementArguments: arguments)
let statement = try db.cachedStatement(sql: "update Parent set userUuid = ?")
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
try statement.execute()
}
public
static func genSelectAll(db: Database) throws -> [DbParent] {
Logging.log(selectAllQuery, statementArguments: .init())
let statement = try db.cachedStatement(sql: selectAllQuery)
return try DbParent.fetchAll(statement)
}
public
static func genSelectCount(db: Database) throws -> Int {
Logging.log(selectCountQuery, statementArguments: .init())
let statement = try db.cachedStatement(sql: selectCountQuery)
return try Int.fetchOne(statement)!
}
// Write the primary key struct, useful for selecting or deleting a unique row
public struct PrimaryKey: Equatable, Hashable {
// Static queries
public static let selectQuery = "select * from Parent where parentUuid = ?"
public static let selectExistsQuery = "select exists(select 1 from Parent where parentUuid = ?)"
public static let deleteQuery = "delete from Parent where parentUuid = ?"
// Mapped columns to properties
public var parentUuid: UUID
// Default initializer
public init(parentUuid: UUID) {
self.parentUuid = parentUuid
}
// Queries a unique row in the database, the row may or may not exist
public func genSelect(db: Database) throws -> DbParent? {
let arguments: StatementArguments = try [
parentUuid.uuidString
]
Logging.log(Self.selectQuery, statementArguments: arguments)
let statement = try db.cachedStatement(sql: Self.selectQuery)
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
return try DbParent.fetchOne(statement)
}
// Same as function 'genSelectUnique', but throws an error when no record has been found
public func genSelectExpect(db: Database) throws -> DbParent {
if let instance = try genSelect(db: db) {
return instance
} else {
throw DatabaseError(message: "Didn't found a record for \(self)")
}
}
// Checks if a row exists
public func genSelectExists(db: Database) throws -> Bool {
let arguments: StatementArguments = try [
parentUuid.uuidString
]
Logging.log(Self.selectExistsQuery, statementArguments: arguments)
let statement = try db.cachedStatement(sql: Self.selectExistsQuery)
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
// This always returns a row
return try Bool.fetchOne(statement)!
}
// Deletes a unique row, asserts that the row actually existed
public func genDelete(db: Database, assertOneRowAffected: Bool = true) throws {
let arguments: StatementArguments = try [
parentUuid.uuidString
]
let statement = try db.cachedStatement(sql: Self.deleteQuery)
Logging.log(Self.deleteQuery, statementArguments: arguments)
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
try statement.execute()
if assertOneRowAffected {
assert(db.changesCount == 1)
}
}
public func genUpdateParentUuid(db: Database, parentUuid: UUID, assertOneRowAffected: Bool = true) throws {
let arguments: StatementArguments = try [
parentUuid.uuidString,
self.parentUuid.uuidString
]
let statement = try db.cachedStatement(sql: DbParent.UpdatableColumn.updateParentUuidQuery)
Logging.log(DbParent.UpdatableColumn.updateParentUuidQuery, statementArguments: arguments)
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
try statement.execute()
if assertOneRowAffected {
assert(db.changesCount == 1)
}
}
public func genUpdateUserUuid(db: Database, userUuid: UUID?, assertOneRowAffected: Bool = true) throws {
let arguments: StatementArguments = try [
userUuid?.uuidString,
parentUuid.uuidString
]
let statement = try db.cachedStatement(sql: DbParent.UpdatableColumn.updateUserUuidQuery)
Logging.log(DbParent.UpdatableColumn.updateUserUuidQuery, statementArguments: arguments)
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
try statement.execute()
if assertOneRowAffected {
assert(db.changesCount == 1)
}
}
public
func genUpdateDynamic(db: Database, columns: [DbParent.UpdatableColumnWithValue], assertOneRowAffected: Bool = true, assertAtLeastOneUpdate: Bool = true) throws {
assert(!assertAtLeastOneUpdate || !columns.isEmpty)
// Check for duplicates
assert(Set(columns.map { $0.columnName }).count == columns.count)
if columns.isEmpty {
return
}
let pkQuery = "where parentUuid = ?"
var updateQuery = "update Parent set "
var arguments = StatementArguments()
for column in columns {
switch column {
case let .parentUuid(value):
if !arguments.isEmpty {
updateQuery += ", "
}
arguments += [value.uuidString]
updateQuery += "parentUuid = ?"
case let .userUuid(value):
if !arguments.isEmpty {
updateQuery += ", "
}
arguments += [value?.uuidString]
updateQuery += "userUuid = ?"
}
}
arguments += [parentUuid.uuidString]
let finalQuery = updateQuery + " " + pkQuery
Logging.log(finalQuery, statementArguments: arguments)
let statement = try db.cachedStatement(sql: finalQuery)
#if DEBUG
try statement.setArguments(arguments)
#else
statement.setUncheckedArguments(arguments)
#endif
try statement.execute()
if assertOneRowAffected {
assert(db.changesCount == 1)
}
}
public
func genUpdate(db: Database, column: UpdatableColumnWithValue, assertOneRowAffected: Bool = true) throws {
switch column {
case let .parentUuid(val): try genUpdateParentUuid(db: db, parentUuid: val, assertOneRowAffected: assertOneRowAffected)
case let .userUuid(val): try genUpdateUserUuid(db: db, userUuid: val, assertOneRowAffected: assertOneRowAffected)
}
}
}
}
extension DbParent: Identifiable {
public var id: UUID {
parentUuid
}
}