From 5a942d72135b6744058b0b3ec58a2c6dfc7c1a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 2 Feb 2019 16:20:43 +0100 Subject: [PATCH] More trimming: QueryInterfaceQuery now embeds a SQLRelation --- GRDB/QueryInterface/QueryInterfaceQuery.swift | 202 +++++------------- .../QueryInterfaceRequest.swift | 13 +- GRDB/QueryInterface/SQLRelation.swift | 26 ++- 3 files changed, 84 insertions(+), 157 deletions(-) diff --git a/GRDB/QueryInterface/QueryInterfaceQuery.swift b/GRDB/QueryInterface/QueryInterfaceQuery.swift index d68b59a45..5555db9d9 100644 --- a/GRDB/QueryInterface/QueryInterfaceQuery.swift +++ b/GRDB/QueryInterface/QueryInterfaceQuery.swift @@ -17,63 +17,42 @@ struct DatabasePromise { } struct QueryInterfaceQuery { - var source: SQLSource - var selection: [SQLSelectable] + var relation: SQLRelation var isDistinct: Bool - var filterPromise: DatabasePromise var groupPromise: DatabasePromise<[SQLExpression]>? - var ordering: QueryOrdering var havingExpression: SQLExpression? var limit: SQLLimit? - var joins: OrderedDictionary init( - source: SQLSource, - selection: [SQLSelectable] = [], + relation: SQLRelation, isDistinct: Bool = false, - filterPromise: DatabasePromise = DatabasePromise(value: nil), groupPromise: DatabasePromise<[SQLExpression]>? = nil, - ordering: QueryOrdering = QueryOrdering(), havingExpression: SQLExpression? = nil, - limit: SQLLimit? = nil, - joins: OrderedDictionary = [:]) + limit: SQLLimit? = nil) { - self.source = source - self.selection = selection + self.relation = relation self.isDistinct = isDistinct - self.filterPromise = filterPromise self.groupPromise = groupPromise - self.ordering = ordering self.havingExpression = havingExpression self.limit = limit - self.joins = joins - } - - init(_ relation: SQLRelation) { - self.init( - source: relation.source, - selection: relation.selection, - filterPromise: relation.filterPromise, - ordering: relation.ordering, - joins: relation.joins) } var alias: TableAlias? { - return source.alias + return relation.alias } } -extension QueryInterfaceQuery { +extension QueryInterfaceQuery: SelectionRequest, FilteredRequest, OrderedRequest { func select(_ selection: [SQLSelectable]) -> QueryInterfaceQuery { - var query = self - query.selection = selection - return query + return mapRelation { $0.select(selection) } } func annotated(with selection: [SQLSelectable]) -> QueryInterfaceQuery { - var query = self - query.selection.append(contentsOf: selection) - return query + return mapRelation { + var relation = $0 + relation.selection.append(contentsOf: selection) + return relation + } } func distinct() -> QueryInterfaceQuery { @@ -83,15 +62,7 @@ extension QueryInterfaceQuery { } 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 + return mapRelation { $0.filter(predicate) } } func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> QueryInterfaceQuery { @@ -111,23 +82,15 @@ extension QueryInterfaceQuery { } func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> QueryInterfaceQuery { - return order(QueryOrdering(orderings: orderings)) + return mapRelation { $0.order(orderings) } } func reversed() -> QueryInterfaceQuery { - return order(ordering.reversed) - } - - private func order(_ ordering: QueryOrdering) -> QueryInterfaceQuery { - var query = self - query.ordering = ordering - return query + return mapRelation { $0.reversed() } } func unordered() -> QueryInterfaceQuery { - var query = self - query.ordering = QueryOrdering() - return query + return mapRelation { $0.unordered() } } func limit(_ limit: Int, offset: Int? = nil) -> QueryInterfaceQuery { @@ -137,22 +100,16 @@ extension QueryInterfaceQuery { } func appendingJoin(_ join: Join, 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 + return mapRelation { $0.appendingJoin(join, forKey: key) } } func qualified(with alias: TableAlias) -> QueryInterfaceQuery { + return mapRelation { $0.qualified(with: alias) } + } + + private func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> QueryInterfaceQuery { var query = self - query.source = source.qualified(with: alias) + query.relation = transform(relation) return query } } @@ -162,74 +119,31 @@ extension QueryInterfaceQuery { 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.relation = query.relation.finalizedRelation + let alias = query.relation.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 + /// - precondition: self is the result of finalizedQuery private func finalizedRowAdapter(_ db: Database) throws -> RowAdapter? { - if joins.isEmpty { + // No join => no adapter + if relation.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 { + guard let (adapter, _) = try relation.finalizedRowAdapter(db, fromIndex: 0, forKeyPath: []) else { return nil } - let adapter = RangeRowAdapter(0 ..< (0 + selectionWidth)) - return adapter.addingScopes(scopes) + return adapter } } extension QueryInterfaceQuery { - /// precondition: self is the result of finalizedQuery + /// - precondition: self is the result of finalizedQuery func sql(_ db: Database, _ context: inout SQLGenerationContext) throws -> String { var sql = "SELECT" @@ -237,17 +151,17 @@ extension QueryInterfaceQuery { sql += " DISTINCT" } - let selection = finalizedSelection + let selection = relation.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) + sql += try " FROM " + relation.source.sourceSQL(db, &context) - for (_, join) in joins { + for (_, join) in relation.joins { sql += try " " + join.joinSQL(db, &context, leftAlias: alias!, isRequiredAllowed: true) } - if let filter = try filterPromise.resolve(db) { + if let filter = try relation.filterPromise.resolve(db) { sql += " WHERE " + filter.expressionSQL(&context) } @@ -261,7 +175,7 @@ extension QueryInterfaceQuery { sql += " HAVING " + havingExpression.expressionSQL(&context) } - let orderings = try finalizedOrdering.resolve(db) + let orderings = try relation.finalizedOrdering.resolve(db) if !orderings.isEmpty { sql += " ORDER BY " + orderings.map { $0.orderingTermSQL(&context) }.joined(separator: ", ") } @@ -273,16 +187,16 @@ extension QueryInterfaceQuery { return sql } - /// precondition: self is the result of finalizedQuery + /// - precondition: self is the result of finalizedQuery private func makeSelectStatement(_ db: Database) throws -> SelectStatement { - var context = SQLGenerationContext.queryGenerationContext(aliases: finalizedAliases) + var context = SQLGenerationContext.queryGenerationContext(aliases: relation.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 + /// - precondition: self is the result of finalizedQuery func makeDeleteStatement(_ db: Database) throws -> UpdateStatement { if let groupExpressions = try groupPromise?.resolve(db), !groupExpressions.isEmpty { // Programmer error @@ -294,26 +208,26 @@ extension QueryInterfaceQuery { fatalError("Can't delete query with HAVING clause") } - guard joins.isEmpty else { + guard relation.joins.isEmpty else { // Programmer error fatalError("Can't delete query with JOIN clause") } - guard case .table = source else { + guard case .table = relation.source else { // Programmer error fatalError("Can't delete without any database table") } - var context = SQLGenerationContext.queryGenerationContext(aliases: finalizedAliases) + var context = SQLGenerationContext.queryGenerationContext(aliases: relation.finalizedAliases) - var sql = try "DELETE FROM " + source.sourceSQL(db, &context) + var sql = try "DELETE FROM " + relation.source.sourceSQL(db, &context) - if let filter = try filterPromise.resolve(db) { + if let filter = try relation.filterPromise.resolve(db) { sql += " WHERE " + filter.expressionSQL(&context) } if let limit = limit { - let orderings = try finalizedOrdering.resolve(db) + let orderings = try relation.finalizedOrdering.resolve(db) if !orderings.isEmpty { sql += " ORDER BY " + orderings.map { $0.orderingTermSQL(&context) }.joined(separator: ", ") } @@ -330,7 +244,7 @@ extension QueryInterfaceQuery { return statement } - /// precondition: self is the result of finalizedQuery + /// - precondition: self is the result of finalizedQuery func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) { return try (makeSelectStatement(db), finalizedRowAdapter(db)) } @@ -341,7 +255,7 @@ extension QueryInterfaceQuery { } /// The database region that the request looks into. - /// precondition: self is the result of finalizedQuery + /// - precondition: self is the result of finalizedQuery func databaseRegion(_ db: Database) throws -> DatabaseRegion { let statement = try makeSelectStatement(db) let databaseRegion = statement.databaseRegion @@ -349,7 +263,7 @@ extension QueryInterfaceQuery { // 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 { + guard case .table(tableName: let tableName, alias: _) = relation.source else { // TODO: try harder return databaseRegion } @@ -361,7 +275,7 @@ extension QueryInterfaceQuery { } // Give up unless there is a where clause - guard let filter = try filterPromise.resolve(db) else { + guard let filter = try relation.filterPromise.resolve(db) else { return databaseRegion } @@ -382,19 +296,18 @@ extension QueryInterfaceQuery { return trivialCountQuery } - guard joins.isEmpty, case .table = source else { + guard relation.joins.isEmpty, case .table = relation.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 { + 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 { return trivialCountQuery } - var countQuery = self.unordered() + var countQuery = self.unordered().select(count.sqlSelectable) countQuery.isDistinct = false - countQuery.selection = [count.sqlSelectable] return countQuery } else { // SELECT [DISTINCT] expr1, expr2, ... FROM tableName ... @@ -406,17 +319,16 @@ extension QueryInterfaceQuery { // SELECT expr1, expr2, ... FROM tableName ... // -> // SELECT COUNT(*) FROM tableName ... - var countQuery = self.unordered() - countQuery.selection = [SQLExpressionCount(AllColumns())] - return countQuery + return self.unordered().select(SQLExpressionCount(AllColumns())) } } // SELECT COUNT(*) FROM (self) private var trivialCountQuery: QueryInterfaceQuery { - return QueryInterfaceQuery( + let relation = SQLRelation( source: .query(unordered()), selection: [SQLExpressionCount(AllColumns())]) + return QueryInterfaceQuery(relation: relation) } } @@ -577,7 +489,7 @@ struct Join { return try relation.finalizedRowAdapter(db, fromIndex: startIndex, forKeyPath: keyPath) } - /// precondition: relation is the result of finalizedRelation + /// - precondition: relation is the result of finalizedRelation func joinSQL(_ db: Database,_ context: inout SQLGenerationContext, leftAlias: TableAlias, isRequiredAllowed: Bool) throws -> String { var isRequiredAllowed = isRequiredAllowed var sql = "" diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 447a9990a..08a9c44fd 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -9,7 +9,7 @@ public struct QueryInterfaceRequest { } init(relation: SQLRelation) { - self.query = QueryInterfaceQuery(relation) + self.query = QueryInterfaceQuery(relation: relation) } } @@ -238,17 +238,13 @@ extension QueryInterfaceRequest { var relation: SQLRelation { let query = self.query + // Prevent information loss GRDBPrecondition(!query.isDistinct, "Not implemented: join distinct queries") GRDBPrecondition(query.groupPromise == nil, "Can't join aggregated queries") GRDBPrecondition(query.havingExpression == nil, "Can't join aggregated queries") GRDBPrecondition(query.limit == nil, "Can't join limited queries") - return SQLRelation( - source: query.source, - selection: query.selection, - filterPromise: query.filterPromise, - ordering: query.ordering, - joins: query.joins) + return query.relation } } @@ -294,9 +290,10 @@ extension TableRecord { /// all requests by the `TableRecord.databaseSelection` property, or /// for individual requests with the `TableRecord.select` method. public static func all() -> QueryInterfaceRequest { - let query = QueryInterfaceQuery( + let relation = SQLRelation( source: .table(tableName: databaseTableName, alias: nil), selection: databaseSelection) + let query = QueryInterfaceQuery(relation: relation) return QueryInterfaceRequest(query: query) } diff --git a/GRDB/QueryInterface/SQLRelation.swift b/GRDB/QueryInterface/SQLRelation.swift index 908330050..1174b111c 100644 --- a/GRDB/QueryInterface/SQLRelation.swift +++ b/GRDB/QueryInterface/SQLRelation.swift @@ -15,6 +15,20 @@ public /* TODO: make internal when possible */ struct SQLRelation { var alias: TableAlias? { return source.alias } + + init( + source: SQLSource, + selection: [SQLSelectable] = [], + filterPromise: DatabasePromise = DatabasePromise(value: nil), + ordering: QueryOrdering = QueryOrdering(), + joins: OrderedDictionary = [:]) + { + self.source = source + self.selection = selection + self.filterPromise = filterPromise + self.ordering = ordering + self.joins = joins + } } extension SQLRelation { @@ -50,6 +64,10 @@ extension SQLRelation { return relation } + func unordered() -> SQLRelation { + return order(QueryOrdering()) + } + func appendingJoin(_ join: Join, forKey key: String) -> SQLRelation { var relation = self if let existingJoin = relation.joins.removeValue(forKey: key) { @@ -86,7 +104,7 @@ extension SQLRelation { return relation } - /// precondition: self is the result of finalizedRelation + /// - precondition: self is the result of finalizedRelation var finalizedAliases: [TableAlias] { var aliases: [TableAlias] = [] if let alias = alias { @@ -97,21 +115,21 @@ extension SQLRelation { } } - /// precondition: self is the result of finalizedRelation + /// - precondition: self is the result of finalizedRelation var finalizedSelection: [SQLSelectable] { return joins.reduce(into: selection) { $0.append(contentsOf: $1.value.finalizedSelection) } } - /// precondition: self is the result of finalizedRelation + /// - precondition: self is the result of finalizedRelation var finalizedOrdering: QueryOrdering { return joins.reduce(ordering) { $0.appending($1.value.finalizedOrdering) } } - /// precondition: self is the result of finalizedRelation + /// - precondition: self is the result of finalizedRelation func finalizedRowAdapter(_ db: Database, fromIndex startIndex: Int, forKeyPath keyPath: [String]) throws -> (adapter: RowAdapter, endIndex: Int)? { let selectionWidth = try selection .map { try $0.columnCount(db) }