-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
QueryInterfaceQuery.swift
652 lines (558 loc) · 21.8 KB
/
QueryInterfaceQuery.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
struct DatabasePromise<T> {
let resolve: (Database) throws -> T
init(value: T) {
self.resolve = { _ in value }
}
init(_ resolve: @escaping (Database) throws -> T) {
self.resolve = resolve
}
func map(_ transform: @escaping (Database, T) throws -> T) -> DatabasePromise {
return DatabasePromise { db in
try transform(db, self.resolve(db))
}
}
}
struct QueryInterfaceQuery {
var source: SQLSource
var selection: [SQLSelectable]
var isDistinct: Bool
var filterPromise: DatabasePromise<SQLExpression?>
var groupPromise: DatabasePromise<[SQLExpression]>?
var ordering: QueryOrdering
var havingExpression: SQLExpression?
var limit: SQLLimit?
var joins: OrderedDictionary<String, AssociationJoin>
init(
source: SQLSource,
selection: [SQLSelectable] = [],
isDistinct: Bool = false,
filterPromise: DatabasePromise<SQLExpression?> = DatabasePromise(value: nil),
groupPromise: DatabasePromise<[SQLExpression]>? = nil,
ordering: QueryOrdering = QueryOrdering(),
havingExpression: SQLExpression? = nil,
limit: SQLLimit? = nil,
joins: OrderedDictionary<String, AssociationJoin> = [:])
{
self.source = source
self.selection = selection
self.isDistinct = isDistinct
self.filterPromise = filterPromise
self.groupPromise = groupPromise
self.ordering = ordering
self.havingExpression = havingExpression
self.limit = limit
self.joins = joins
}
init(_ query: AssociationQuery) {
self.init(
source: query.source,
selection: query.selection,
filterPromise: query.filterPromise,
ordering: query.ordering,
joins: query.joins)
}
var alias: TableAlias? {
return source.alias
}
}
extension QueryInterfaceQuery {
func select(_ selection: [SQLSelectable]) -> QueryInterfaceQuery {
var query = self
query.selection = selection
return query
}
func annotated(with selection: [SQLSelectable]) -> QueryInterfaceQuery {
var query = self
query.selection.append(contentsOf: selection)
return query
}
func distinct() -> QueryInterfaceQuery {
var query = self
query.isDistinct = true
return query
}
func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> QueryInterfaceQuery {
var query = self
query.filterPromise = query.filterPromise.map { (db, filter) in
if let filter = filter {
return try filter && predicate(db)
} else {
return try predicate(db).sqlExpression
}
}
return query
}
func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> QueryInterfaceQuery {
var query = self
query.groupPromise = DatabasePromise { db in try expressions(db).map { $0.sqlExpression } }
return query
}
func having(_ predicate: SQLExpressible) -> QueryInterfaceQuery {
var query = self
if let havingExpression = query.havingExpression {
query.havingExpression = (havingExpression && predicate).sqlExpression
} else {
query.havingExpression = predicate.sqlExpression
}
return query
}
func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> QueryInterfaceQuery {
return order(QueryOrdering(orderings: orderings))
}
func reversed() -> QueryInterfaceQuery {
return order(ordering.reversed)
}
private func order(_ ordering: QueryOrdering) -> QueryInterfaceQuery {
var query = self
query.ordering = ordering
return query
}
func unordered() -> QueryInterfaceQuery {
var query = self
query.ordering = QueryOrdering()
return query
}
func limit(_ limit: Int, offset: Int? = nil) -> QueryInterfaceQuery {
var query = self
query.limit = SQLLimit(limit: limit, offset: offset)
return query
}
func appendingJoin(_ join: AssociationJoin, forKey key: String) -> QueryInterfaceQuery {
var query = self
if let existingJoin = query.joins.removeValue(forKey: key) {
guard let mergedJoin = existingJoin.merged(with: join) else {
// can't merge
fatalError("The association key \"\(key)\" is ambiguous. Use the Association.forKey(_:) method is order to disambiguate.")
}
query.joins.append(value: mergedJoin, forKey: key)
} else {
query.joins.append(value: join, forKey: key)
}
return query
}
func qualified(with alias: TableAlias) -> QueryInterfaceQuery {
var query = self
query.source = source.qualified(with: alias)
return query
}
}
extension QueryInterfaceQuery {
/// A finalized query is ready for SQL generation
var finalizedQuery: QueryInterfaceQuery {
var query = self
let alias = TableAlias()
query.source = source.qualified(with: alias)
query.selection = query.selection.map { $0.qualifiedSelectable(with: alias) }
query.filterPromise = query.filterPromise.map { [alias] (_, expr) in expr?.qualifiedExpression(with: alias) }
query.groupPromise = query.groupPromise?.map { [alias] (_, exprs) in exprs.map { $0.qualifiedExpression(with: alias) } }
query.ordering = query.ordering.qualified(with: alias)
query.havingExpression = query.havingExpression?.qualifiedExpression(with: alias)
query.joins = query.joins.mapValues { $0.finalizedJoin }
return query
}
/// precondition: self is the result of finalizedQuery
var finalizedAliases: [TableAlias] {
var aliases: [TableAlias] = []
if let alias = alias {
aliases.append(alias)
}
return joins.reduce(into: aliases) {
$0.append(contentsOf: $1.value.finalizedAliases)
}
}
/// precondition: self is the result of finalizedQuery
var finalizedSelection: [SQLSelectable] {
return joins.reduce(into: selection) {
$0.append(contentsOf: $1.value.finalizedSelection)
}
}
/// precondition: self is the result of finalizedQuery
var finalizedOrdering: QueryOrdering {
return joins.reduce(ordering) {
$0.appending($1.value.finalizedOrdering)
}
}
/// precondition: self is the result of finalizedQuery
private func finalizedRowAdapter(_ db: Database) throws -> RowAdapter? {
if joins.isEmpty {
return nil
}
let selectionWidth = try selection
.map { try $0.columnCount(db) }
.reduce(0, +)
var endIndex = selectionWidth
var scopes: [String: RowAdapter] = [:]
for (key, join) in joins {
if let (joinAdapter, joinEndIndex) = try join.finalizedRowAdapter(db, fromIndex: endIndex, forKeyPath: [key]) {
scopes[key] = joinAdapter
endIndex = joinEndIndex
}
}
if selectionWidth == 0 && scopes.isEmpty {
return nil
}
let adapter = RangeRowAdapter(0 ..< (0 + selectionWidth))
return adapter.addingScopes(scopes)
}
}
extension QueryInterfaceQuery {
/// precondition: self is the result of finalizedQuery
func sql(_ db: Database, _ context: inout SQLGenerationContext) throws -> String {
var sql = "SELECT"
if isDistinct {
sql += " DISTINCT"
}
let selection = finalizedSelection
GRDBPrecondition(!selection.isEmpty, "Can't generate SQL with empty selection")
sql += " " + selection.map { $0.resultColumnSQL(&context) }.joined(separator: ", ")
sql += try " FROM " + source.sourceSQL(db, &context)
for (_, join) in joins {
sql += try " " + join.joinSQL(db, &context, leftAlias: alias!, isRequiredAllowed: true)
}
if let filter = try filterPromise.resolve(db) {
sql += " WHERE " + filter.expressionSQL(&context)
}
if let groupExpressions = try groupPromise?.resolve(db), !groupExpressions.isEmpty {
sql += " GROUP BY "
sql += groupExpressions.map { $0.expressionSQL(&context) }
.joined(separator: ", ")
}
if let havingExpression = havingExpression {
sql += " HAVING " + havingExpression.expressionSQL(&context)
}
let orderings = try finalizedOrdering.resolve(db)
if !orderings.isEmpty {
sql += " ORDER BY " + orderings.map { $0.orderingTermSQL(&context) }.joined(separator: ", ")
}
if let limit = limit {
sql += " LIMIT " + limit.sql
}
return sql
}
/// precondition: self is the result of finalizedQuery
private func makeSelectStatement(_ db: Database) throws -> SelectStatement {
var context = SQLGenerationContext.queryGenerationContext(aliases: finalizedAliases)
let sql = try self.sql(db, &context)
let statement = try db.makeSelectStatement(sql)
statement.arguments = context.arguments!
return statement
}
/// precondition: self is the result of finalizedQuery
func makeDeleteStatement(_ db: Database) throws -> UpdateStatement {
if let groupExpressions = try groupPromise?.resolve(db), !groupExpressions.isEmpty {
// Programmer error
fatalError("Can't delete query with GROUP BY clause")
}
guard havingExpression == nil else {
// Programmer error
fatalError("Can't delete query with HAVING clause")
}
guard joins.isEmpty else {
// Programmer error
fatalError("Can't delete query with JOIN clause")
}
guard case .table = source else {
// Programmer error
fatalError("Can't delete without any database table")
}
var context = SQLGenerationContext.queryGenerationContext(aliases: finalizedAliases)
var sql = try "DELETE FROM " + source.sourceSQL(db, &context)
if let filter = try filterPromise.resolve(db) {
sql += " WHERE " + filter.expressionSQL(&context)
}
if let limit = limit {
let orderings = try finalizedOrdering.resolve(db)
if !orderings.isEmpty {
sql += " ORDER BY " + orderings.map { $0.orderingTermSQL(&context) }.joined(separator: ", ")
}
if Database.sqliteCompileOptions.contains("ENABLE_UPDATE_DELETE_LIMIT") {
sql += " LIMIT " + limit.sql
} else {
fatalError("Can't delete query with limit")
}
}
let statement = try db.makeUpdateStatement(sql)
statement.arguments = context.arguments!
return statement
}
/// precondition: self is the result of finalizedQuery
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
return try (makeSelectStatement(db), finalizedRowAdapter(db))
}
func fetchCount(_ db: Database) throws -> Int {
let (statement, adapter) = try countQuery.prepare(db)
return try Int.fetchOne(statement, adapter: adapter)!
}
/// The database region that the request looks into.
/// precondition: self is the result of finalizedQuery
func databaseRegion(_ db: Database) throws -> DatabaseRegion {
let statement = try makeSelectStatement(db)
let databaseRegion = statement.databaseRegion
// Can we intersect the region with rowIds?
//
// Give up unless request feeds from a single database table
guard case .table(tableName: let tableName, alias: _) = source else {
// TODO: try harder
return databaseRegion
}
// Give up unless primary key is rowId
let primaryKeyInfo = try db.primaryKey(tableName)
guard primaryKeyInfo.isRowID else {
return databaseRegion
}
// Give up unless there is a where clause
guard let filter = try filterPromise.resolve(db) else {
return databaseRegion
}
// The filter knows better
guard let rowIds = filter.matchedRowIds(rowIdName: primaryKeyInfo.rowIDColumn) else {
return databaseRegion
}
// Database regions are case-insensitive: use the canonical table name
let canonicalTableName = try db.canonicalTableName(tableName)
return databaseRegion.tableIntersection(canonicalTableName, rowIds: rowIds)
}
private var countQuery: QueryInterfaceQuery {
guard groupPromise == nil && limit == nil else {
// SELECT ... GROUP BY ...
// SELECT ... LIMIT ...
return trivialCountQuery
}
guard joins.isEmpty, case .table = source else {
// SELECT ... FROM (something which is not a plain table)
return trivialCountQuery
}
GRDBPrecondition(!selection.isEmpty, "Can't generate SQL with empty selection")
if selection.count == 1 {
guard let count = self.selection[0].count(distinct: isDistinct) else {
return trivialCountQuery
}
var countQuery = self.unordered()
countQuery.isDistinct = false
countQuery.selection = [count.sqlSelectable]
return countQuery
} else {
// SELECT [DISTINCT] expr1, expr2, ... FROM tableName ...
guard !isDistinct else {
return trivialCountQuery
}
// SELECT expr1, expr2, ... FROM tableName ...
// ->
// SELECT COUNT(*) FROM tableName ...
var countQuery = self.unordered()
countQuery.selection = [SQLExpressionCount(AllColumns())]
return countQuery
}
}
// SELECT COUNT(*) FROM (self)
private var trivialCountQuery: QueryInterfaceQuery {
return QueryInterfaceQuery(
source: .query(unordered()),
selection: [SQLExpressionCount(AllColumns())])
}
}
// MARK: - AssociationJoin
/// Not to be mismatched with SQL join operators (inner join, left join).
///
/// AssociationJoinOperator is designed to be hierarchically nested, unlike
/// SQL join operators.
///
/// Consider the following request for (A, B, C) tuples:
///
/// let r = A.including(optional: A.b.including(required: B.c))
///
/// It chains three associations, the first optional, the second required.
///
/// It looks like it means: "Give me all As, along with their Bs, granted those
/// Bs have their Cs. For As whose B has no C, give me a nil B".
///
/// It can not be expressed as one left join, and a regular join, as below,
/// Because this would not honor the first optional:
///
/// -- dubious
/// SELECT a.*, b.*, c.*
/// FROM a
/// LEFT JOIN b ON ...
/// JOIN c ON ...
///
/// Instead, it should:
/// - allow (A + missing (B + C))
/// - prevent (A + (B + missing C)).
///
/// This can be expressed in SQL with two left joins, and an extra condition:
///
/// -- likely correct
/// SELECT a.*, b.*, c.*
/// FROM a
/// LEFT JOIN b ON ...
/// LEFT JOIN c ON ...
/// WHERE NOT((b.id IS NOT NULL) AND (c.id IS NULL)) -- no B without C
///
/// This is currently not implemented, and requires a little more thought.
/// I don't even know if inventing a whole new way to perform joins should even
/// be on the table. But we have a hierarchical way to express joined queries,
/// and they have a meaning:
///
/// // what is my meaning?
/// A.including(optional: A.b.including(required: B.c))
enum AssociationJoinOperator {
case required, optional
}
struct AssociationJoin {
var joinOperator: AssociationJoinOperator
var joinCondition: JoinCondition
var query: AssociationQuery
var finalizedJoin: AssociationJoin {
var join = self
join.query = query.finalizedQuery
return join
}
var finalizedAliases: [TableAlias] {
return query.finalizedAliases
}
var finalizedSelection: [SQLSelectable] {
return query.finalizedSelection
}
var finalizedOrdering: QueryOrdering {
return query.finalizedOrdering
}
func finalizedRowAdapter(_ db: Database, fromIndex startIndex: Int, forKeyPath keyPath: [String]) throws -> (adapter: RowAdapter, endIndex: Int)? {
return try query.finalizedRowAdapter(db, fromIndex: startIndex, forKeyPath: keyPath)
}
/// precondition: query is the result of finalizedQuery
func joinSQL(_ db: Database,_ context: inout SQLGenerationContext, leftAlias: TableAlias, isRequiredAllowed: Bool) throws -> String {
var isRequiredAllowed = isRequiredAllowed
var sql = ""
switch joinOperator {
case .optional:
isRequiredAllowed = false
sql += "LEFT JOIN"
case .required:
guard isRequiredAllowed else {
// TODO: chainOptionalRequired
fatalError("Not implemented: chaining a required association behind an optional association")
}
sql += "JOIN"
}
sql += try " " + query.source.sourceSQL(db, &context)
let rightAlias = query.alias!
let filters = try [
joinCondition.sqlExpression(db, leftAlias: leftAlias, rightAlias: rightAlias),
query.filterPromise.resolve(db)
].compactMap { $0 }
if !filters.isEmpty {
sql += " ON " + filters.joined(operator: .and).expressionSQL(&context)
}
for (_, join) in query.joins {
sql += try " " + join.joinSQL(db, &context, leftAlias: rightAlias, isRequiredAllowed: isRequiredAllowed)
}
return sql
}
/// Returns nil if joins can't be merged (conflict in condition, query...)
func merged(with other: AssociationJoin) -> AssociationJoin? {
guard joinCondition == other.joinCondition else {
// can't merge
return nil
}
guard let mergedQuery = query.merged(with: other.query) else {
// can't merge
return nil
}
let mergedJoinOperator: AssociationJoinOperator
switch (joinOperator, other.joinOperator) {
case (.required, _), (_, .required):
mergedJoinOperator = .required
default:
mergedJoinOperator = .optional
}
return AssociationJoin(
joinOperator: mergedJoinOperator,
joinCondition: joinCondition,
query: mergedQuery)
}
}
// MARK: - SQLSource
enum SQLSource {
case table(tableName: String, alias: TableAlias?)
indirect case query(QueryInterfaceQuery)
var alias: TableAlias? {
switch self {
case .table(_, let alias):
return alias
case .query(let query):
return query.alias
}
}
func sourceSQL(_ db: Database, _ context: inout SQLGenerationContext) throws -> String {
switch self {
case .table(let tableName, let alias):
if let alias = alias, let aliasName = context.aliasName(for: alias) {
return "\(tableName.quotedDatabaseIdentifier) \(aliasName.quotedDatabaseIdentifier)"
} else {
return "\(tableName.quotedDatabaseIdentifier)"
}
case .query(let query):
return try "(\(query.sql(db, &context)))"
}
}
func qualified(with alias: TableAlias) -> SQLSource {
switch self {
case .table(let tableName, let sourceAlias):
if let sourceAlias = sourceAlias {
alias.becomeProxy(of: sourceAlias)
return self
} else {
alias.setTableName(tableName)
return .table(tableName: tableName, alias: alias)
}
case .query(let query):
return .query(query.qualified(with: alias))
}
}
/// Returns nil if sources can't be merged (conflict in tables, aliases...)
func merged(with other: SQLSource) -> SQLSource? {
switch (self, other) {
case let (.table(tableName: tableName, alias: alias), .table(tableName: otherTableName, alias: otherAlias)):
guard tableName == otherTableName else {
// can't merge
return nil
}
switch (alias, otherAlias) {
case (nil, nil):
return .table(tableName: tableName, alias: nil)
case let (alias?, nil), let (nil, alias?):
return .table(tableName: tableName, alias: alias)
case let (alias?, otherAlias?):
guard let mergedAlias = alias.merge(with: otherAlias) else {
// can't merge
return nil
}
return .table(tableName: tableName, alias: mergedAlias)
}
default:
// can't merge
return nil
}
}
}
struct SQLLimit {
let limit: Int
let offset: Int?
var sql: String {
if let offset = offset {
return "\(limit) OFFSET \(offset)"
} else {
return "\(limit)"
}
}
}
extension SQLCount {
var sqlSelectable: SQLSelectable {
switch self {
case .all:
return SQLExpressionCount(AllColumns())
case .distinct(let expression):
return SQLExpressionCountDistinct(expression)
}
}
}