mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Turn selection and having clauses into promises. Give association aggregates access to the actual primary key, for better support for WITHOUT ROW ID tables.
This commit is contained in:
1 parent
b38ccb5f40
commit
d0ba9e342c
17 files changed
+310
-409
No files matched your search
@@ -41,10 +41,4 @@ struct DatabasePromise<T> {
|
||||
try transform(self.resolve(db))
|
||||
}
|
||||
}
|
||||
|
||||
func flatMap<U>(_ transform: @escaping (T) -> DatabasePromise<U>) -> DatabasePromise<U> {
|
||||
DatabasePromise<U> { db in
|
||||
try transform(self.resolve(db)).resolve(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ struct SQLQuery {
|
||||
// Having clause is an array of expressions that we'll join with
|
||||
// the AND operator. This gives nicer output in generated SQL:
|
||||
// `(a AND b AND c)` instead of `((a AND b) AND c)`.
|
||||
var havingExpressions: [SQLExpression] = []
|
||||
var havingExpressionsPromise: DatabasePromise<[SQLExpression]> = DatabasePromise(value: [])
|
||||
var limit: SQLLimit?
|
||||
}
|
||||
|
||||
@@ -34,11 +34,11 @@ extension SQLQuery: Refinable {
|
||||
}
|
||||
|
||||
extension SQLQuery: SelectionRequest {
|
||||
func select(_ selection: [SQLSelectable]) -> Self {
|
||||
func select(_ selection: @escaping (Database) throws -> [SQLSelectable]) -> Self {
|
||||
map(\.relation) { $0.select(selection) }
|
||||
}
|
||||
|
||||
func annotated(with selection: [SQLSelectable]) -> Self {
|
||||
func annotated(with selection: @escaping (Database) throws -> [SQLSelectable]) -> Self {
|
||||
map(\.relation) { $0.annotated(with: selection) }
|
||||
}
|
||||
}
|
||||
@@ -68,8 +68,12 @@ extension SQLQuery: AggregatingRequest {
|
||||
with(\.groupPromise, DatabasePromise { db in try expressions(db).map(\.sqlExpression) })
|
||||
}
|
||||
|
||||
func having(_ predicate: SQLExpressible) -> Self {
|
||||
mapInto(\.havingExpressions) { $0.append(predicate.sqlExpression) }
|
||||
func having(_ predicate: @escaping (Database) throws -> SQLExpressible) -> Self {
|
||||
map(\.havingExpressionsPromise) { havingExpressionsPromise in
|
||||
DatabasePromise { db in
|
||||
try havingExpressionsPromise.resolve(db) + [predicate(db).sqlExpression]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,11 +101,11 @@ extension SQLQuery: _JoinableRequest {
|
||||
|
||||
extension SQLQuery {
|
||||
func fetchCount(_ db: Database) throws -> Int {
|
||||
let (statement, adapter) = try SQLQueryGenerator(countQuery).prepare(db)
|
||||
let (statement, adapter) = try SQLQueryGenerator(countQuery(db)).prepare(db)
|
||||
return try Int.fetchOne(statement, adapter: adapter)!
|
||||
}
|
||||
|
||||
private var countQuery: SQLQuery {
|
||||
private func countQuery(_ db: Database) throws -> SQLQuery {
|
||||
guard groupPromise == nil && limit == nil else {
|
||||
// SELECT ... GROUP BY ...
|
||||
// SELECT ... LIMIT ...
|
||||
@@ -118,9 +122,10 @@ extension SQLQuery {
|
||||
return trivialCountQuery
|
||||
}
|
||||
|
||||
GRDBPrecondition(!relation.selection.isEmpty, "Can't generate SQL with empty selection")
|
||||
if relation.selection.count == 1 {
|
||||
guard let count = relation.selection[0].count(distinct: isDistinct) else {
|
||||
let selection = try relation.selectionPromise.resolve(db)
|
||||
GRDBPrecondition(!selection.isEmpty, "Can't generate SQL with empty selection")
|
||||
if selection.count == 1 {
|
||||
guard let count = selection[0].count(distinct: isDistinct) else {
|
||||
return trivialCountQuery
|
||||
}
|
||||
var countQuery = self.unordered()
|
||||
@@ -150,7 +155,7 @@ extension SQLQuery {
|
||||
private var trivialCountQuery: SQLQuery {
|
||||
let relation = SQLRelation(
|
||||
source: .query(unordered()),
|
||||
selection: [SQLExpressionCount(AllColumns())])
|
||||
selectionPromise: DatabasePromise(value: [SQLExpressionCount(AllColumns())]))
|
||||
return SQLQuery(relation: relation)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ struct SQLRelation {
|
||||
}
|
||||
|
||||
var source: SQLSource
|
||||
var selection: [SQLSelectable] = []
|
||||
var selectionPromise: DatabasePromise<[SQLSelectable]> = DatabasePromise(value: [])
|
||||
// Filter is an array of expressions that we'll join with the AND operator.
|
||||
// This gives nicer output in generated SQL: `(a AND b AND c)` instead of
|
||||
// `((a AND b) AND c)`.
|
||||
@@ -199,24 +199,37 @@ struct SQLRelation {
|
||||
}
|
||||
|
||||
extension SQLRelation: Refinable {
|
||||
func select(_ selection: @escaping (Database) throws -> [SQLSelectable]) -> Self {
|
||||
with(\.selectionPromise, DatabasePromise(selection))
|
||||
}
|
||||
|
||||
// Convenience
|
||||
func select(_ selection: [SQLSelectable]) -> Self {
|
||||
with(\.selection, selection)
|
||||
select { _ in selection }
|
||||
}
|
||||
|
||||
/// Removes all selections from chidren
|
||||
func selectOnly(_ selection: [SQLSelectable]) -> Self {
|
||||
self.with(\.selection, selection)
|
||||
.map(\.children, { $0.mapValues { $0.map(\.relation, { $0.selectOnly([]) }) } })
|
||||
select(selection).map(\.children, { $0.mapValues { $0.map(\.relation, { $0.selectOnly([]) }) } })
|
||||
}
|
||||
|
||||
func annotated(with selection: @escaping (Database) throws -> [SQLSelectable]) -> Self {
|
||||
map(\.selectionPromise) { selectionPromise in
|
||||
DatabasePromise { db in
|
||||
try selectionPromise.resolve(db) + selection(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convenience
|
||||
func annotated(with selection: [SQLSelectable]) -> Self {
|
||||
mapInto(\.selection) { $0.append(contentsOf: selection) }
|
||||
annotated(with: { _ in selection })
|
||||
}
|
||||
|
||||
func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> Self {
|
||||
map(\.filtersPromise) { filtersPromise in
|
||||
filtersPromise.flatMap { filters in
|
||||
DatabasePromise { try filters + [predicate($0).sqlExpression] }
|
||||
DatabasePromise { db in
|
||||
try filtersPromise.resolve(db) + [predicate(db).sqlExpression]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -653,8 +666,8 @@ extension SQLRelation {
|
||||
return nil
|
||||
}
|
||||
|
||||
let mergedFiltersPromise: DatabasePromise<[SQLExpression]> = filtersPromise.flatMap { filters in
|
||||
DatabasePromise { try filters + other.filtersPromise.resolve($0) }
|
||||
let mergedFiltersPromise = DatabasePromise<[SQLExpression]> { db in
|
||||
try self.filtersPromise.resolve(db) + other.filtersPromise.resolve(db)
|
||||
}
|
||||
|
||||
var mergedChildren: OrderedDictionary<String, SQLRelation.Child> = [:]
|
||||
@@ -674,14 +687,21 @@ extension SQLRelation {
|
||||
}
|
||||
|
||||
// replace selection unless empty
|
||||
let mergedSelection = other.selection.isEmpty ? selection : other.selection
|
||||
let mergedSelectionPromise = DatabasePromise { db -> [SQLSelectable] in
|
||||
let otherSelection = try other.selectionPromise.resolve(db)
|
||||
if otherSelection.isEmpty {
|
||||
return try self.selectionPromise.resolve(db)
|
||||
} else {
|
||||
return otherSelection
|
||||
}
|
||||
}
|
||||
|
||||
// replace ordering unless empty
|
||||
let mergedOrdering = other.ordering.isEmpty ? ordering : other.ordering
|
||||
|
||||
return SQLRelation(
|
||||
source: mergedSource,
|
||||
selection: mergedSelection,
|
||||
selectionPromise: mergedSelectionPromise,
|
||||
filtersPromise: mergedFiltersPromise,
|
||||
ordering: mergedOrdering,
|
||||
children: mergedChildren)
|
||||
|
||||
Reference in new issue
Block a user