From 4c2042c3cea7c2980771de6897a265343b752855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 25 May 2020 13:04:10 +0200 Subject: [PATCH] Cleanup 2bb53026ef1a38d4b30f51006913d6e8bf431ac9 --- GRDB/Core/Database+Statements.swift | 4 +- GRDB/Core/FetchRequest.swift | 2 +- GRDB/Core/SQLRequest.swift | 2 +- GRDB/Core/Statement.swift | 14 +++--- .../Request/QueryInterfaceRequest.swift | 30 ++++++++++++- .../SQLGeneration/SQLQueryGenerator.swift | 43 +++++-------------- 6 files changed, 48 insertions(+), 47 deletions(-) diff --git a/GRDB/Core/Database+Statements.swift b/GRDB/Core/Database+Statements.swift index be75a9d98..e1275308c 100644 --- a/GRDB/Core/Database+Statements.swift +++ b/GRDB/Core/Database+Statements.swift @@ -240,7 +240,7 @@ extension Database { try checkForSuspensionViolation(from: statement) if _isRecordingSelectedRegion { - _selectedRegion.formUnion(statement.selectedRegion) + _selectedRegion.formUnion(statement.databaseRegion) } let authorizer = observationBroker.updateStatementWillExecute(statement) @@ -317,7 +317,7 @@ extension Database { try checkForSuspensionViolation(from: statement) if _isRecordingSelectedRegion { - _selectedRegion.formUnion(statement.selectedRegion) + _selectedRegion.formUnion(statement.databaseRegion) } } diff --git a/GRDB/Core/FetchRequest.swift b/GRDB/Core/FetchRequest.swift index db259452e..06a71330d 100644 --- a/GRDB/Core/FetchRequest.swift +++ b/GRDB/Core/FetchRequest.swift @@ -129,7 +129,7 @@ extension FetchRequest { let context = SQLGenerationContext(db) let sql = try requestSQL(context, forSingleResult: false) let statement = try db.makeSelectStatement(sql: sql) - return statement.selectedRegion + return statement.databaseRegion } /// Returns a PreparedRequest that is ready to be executed. diff --git a/GRDB/Core/SQLRequest.swift b/GRDB/Core/SQLRequest.swift index 0fdf988f0..a46a2d1b4 100644 --- a/GRDB/Core/SQLRequest.swift +++ b/GRDB/Core/SQLRequest.swift @@ -111,7 +111,7 @@ extension SQLRequest: SQLRequestProtocol { extension SQLRequest: DatabaseRegionConvertible { public func databaseRegion(_ db: Database) throws -> DatabaseRegion { - try makePreparedRequest(db, forSingleResult: false).statement.selectedRegion + try makePreparedRequest(db, forSingleResult: false).statement.databaseRegion } } diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index f9aad76b9..85e743bf8 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -24,11 +24,12 @@ public class Statement { .trimmingCharacters(in: .sqlStatementSeparators) } - // Selected region is computed during statement compilation, and maybe - // optimized for select statements compiled by QueryInterfaceRequest, in + // Database region is computed during statement compilation, and maybe + // extended for select statements compiled by QueryInterfaceRequest, in // order to perform focused database observation. See - // SQLQueryGenerator.optimizedSelectedRegion(_:_:) - var selectedRegion = DatabaseRegion() + // SQLQueryGenerator.makeSelectStatement(_:) + /// The database region that the statement looks into. + public internal(set) var databaseRegion = DatabaseRegion() var isReadonly: Bool { sqlite3_stmt_readonly(sqliteStatement) != 0 @@ -87,7 +88,7 @@ public class Statement { self.database = database self.sqliteStatement = statement - self.selectedRegion = authorizer.selectedRegion + self.databaseRegion = authorizer.selectedRegion } deinit { @@ -356,9 +357,6 @@ public final class SelectStatement: Statement { "Invalid statement type for query \(String(reflecting: sql)): use UpdateStatement instead.") } - /// The database region that the statement looks into. - public var databaseRegion: DatabaseRegion { selectedRegion } - /// The number of columns in the resulting rows. public var columnCount: Int { Int(sqlite3_column_count(self.sqliteStatement)) diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index cc0449967..b93b3d105 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -46,7 +46,7 @@ extension QueryInterfaceRequest: DatabaseRegionConvertible { public func databaseRegion(_ db: Database) throws -> DatabaseRegion { try SQLQueryGenerator(query: query) .makeSelectStatement(db) - .selectedRegion + .databaseRegion } } @@ -473,7 +473,7 @@ private func prefetch(_ db: Database, associations: [SQLAssociation], in rows: [ return } - // CAUTION: Keep this code in sync with QueryInterfaceRequest.databaseRegion(_:) + // CAUTION: Keep this code in sync with prefetchedRegion(_:_:) for association in associations { let pivotMappings = try association.pivot.condition.columnMappings(db) @@ -521,6 +521,32 @@ private func prefetch(_ db: Database, associations: [SQLAssociation], in rows: [ } } +// Returns the region of prefetched associations +func prefetchedRegion(_ db: Database, associations: [SQLAssociation]) throws -> DatabaseRegion { + try associations.reduce(into: DatabaseRegion()) { (region, association) in + // CAUTION: Keep this code in sync with prefetch(_:associations:in:) + let pivotMappings = try association.pivot.condition.columnMappings(db) + let pivotColumns = pivotMappings.map(\.right) + let pivotAlias = TableAlias() + let prefetchedRelation = association + .map(\.pivot.relation, { $0.qualified(with: pivotAlias) }) + // Use a `NullRow` in order to make sure all join condition + // columns are made visible to SQLite, and present in the + // selected region: + // ... JOIN right ON right.leftId IS NULL + // ^ content of the NullRow + .destinationRelation(fromOriginRows: { _ in [NullRow()] }) + .annotated(with: pivotColumns.map { pivotAlias[Column($0)].forKey("grdb_\($0)") }) + let prefetchedQuery = SQLQuery(relation: prefetchedRelation) + + // Union prefetched region + let prefetchedRegion = try SQLQueryGenerator(query: prefetchedQuery) + .makeSelectStatement(db) + .databaseRegion // contains region of nested associations + region.formUnion(prefetchedRegion) + } +} + extension Array where Element == Row { /// - precondition: Columns all exist in all rows. All rows have the same /// columnns, in the same order. diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index e0572cf80..28c55b80a 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -156,40 +156,17 @@ struct SQLQueryGenerator: Refinable { let statement = try db.makeSelectStatement(sql: sql) statement.arguments = context.arguments - // Optimize databaseRegion - statement.selectedRegion = try optimizedSelectedRegion(db, statement.selectedRegion) + // Optimize statement region. This allows us to track individual rowids. + statement.databaseRegion = try optimizedSelectedRegion(db, statement.databaseRegion) - // Append region of prefetched associations - var fifo = prefetchedAssociations - while !fifo.isEmpty { - let association = fifo.removeFirst() - - // Build the query for prefetched rows. - // CAUTION: Keep this code in sync with prefetch(_:associations:in:) - let pivotMappings = try association.pivot.condition.columnMappings(db) - let pivotColumns = pivotMappings.map(\.right) - let pivotAlias = TableAlias() - let prefetchedRelation = association - .map(\.pivot.relation, { $0.qualified(with: pivotAlias) }) - // Use a `NullRow` in order to make sure all join condition - // columns are made visible to SQLite, and present in the - // selected region: - // ... JOIN right ON right.leftId IS NULL - // ^ content of the NullRow - .destinationRelation(fromOriginRows: { _ in [NullRow()] }) - .annotated(with: pivotColumns.map { pivotAlias[Column($0)].forKey("grdb_\($0)") }) - let prefetchedQuery = SQLQuery(relation: prefetchedRelation) - - // Union prefetched region - let prefetchedRegion = try SQLQueryGenerator(query: prefetchedQuery) - .makeSelectStatement(db) - .selectedRegion - statement.selectedRegion.formUnion(prefetchedRegion) - - // Append nested prefetched associations (support for - // A.including(all: A.bs.including(all: B.cs)) - fifo.append(contentsOf: prefetchedRelation.prefetchedAssociations) - } + // Also append the prefetched region. This makes sure we observe the + // correct database region when the statement is executed, even if we + // don't execute all request statements due to lacking database content. + // For example, fetching `parent.including(all: children)` will select + // parents, but won't attempt to select any children if there is no + // parent in the database. And yet we need to observe the table for + // children. This is why we include the prefetched region. + try statement.databaseRegion.formUnion(prefetchedRegion(db, associations: prefetchedAssociations)) return statement }