diff --git a/GRDB/Core/Database+Statements.swift b/GRDB/Core/Database+Statements.swift index e0eefa72e..859882b91 100644 --- a/GRDB/Core/Database+Statements.swift +++ b/GRDB/Core/Database+Statements.swift @@ -291,7 +291,22 @@ extension Database { @inline(__always) func selectStatementWillExecute(_ statement: SelectStatement) { if _isRecordingSelectedRegion { - _selectedRegion.formUnion(statement.databaseRegion) + // Don't record schema introspection queries, which may be + // run, or not, depending on the state of the schema cache. + // + // This gives us a quick way to make sure that the observation + // below, which runs schema introspection queries as a side effect, + // only tracks the "player" table: + // + // let observation = ValueObservation.tracking { db in + // try Player.fetchOne(db, key: 1) + // } + // + // Strictly speaking, this prevents the recording of all schema + // queries. But we assume, until proven wrong, that such recording + // isn't needed by anyone. + let region = statement.databaseRegion.ignoringInternalSQLiteTables() + _selectedRegion.formUnion(region) } } diff --git a/GRDB/Core/DatabaseRegion.swift b/GRDB/Core/DatabaseRegion.swift index af22ad1e8..793c82f4b 100644 --- a/GRDB/Core/DatabaseRegion.swift +++ b/GRDB/Core/DatabaseRegion.swift @@ -154,6 +154,13 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable { let filteredRegions = tableRegions.filter { viewNames.contains($0.key) == false } return DatabaseRegion(tableRegions: filteredRegions) } + + /// Returns a region which doesn't contain any SQLite internal table. + func ignoringInternalSQLiteTables() -> DatabaseRegion { + guard let tableRegions = tableRegions else { return .fullDatabase } + let filteredRegions = tableRegions.filter { !$0.key.starts(with: "sqlite_") } + return DatabaseRegion(tableRegions: filteredRegions) + } } extension DatabaseRegion { diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 991ca93f1..66fdbb6d7 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -277,7 +277,7 @@ extension Statement { /// } public final class SelectStatement: Statement { /// The database region that the statement looks into. - public private(set) var databaseRegion = DatabaseRegion() + public internal(set) var databaseRegion = DatabaseRegion() /// Creates a prepared statement. Returns nil if the compiled string is /// blank or empty. diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 2694ba92c..d9b715ce8 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -86,7 +86,7 @@ extension QueryInterfaceRequest: FetchRequest { /// - parameter db: A database connection. /// :nodoc: public func databaseRegion(_ db: Database) throws -> DatabaseRegion { - var region = try SQLQueryGenerator(query).databaseRegion(db) + var region = try SQLQueryGenerator(query).makeSelectStatement(db).databaseRegion // Iterate all prefetched associations var fifo = query.relation.prefetchedAssociations @@ -105,7 +105,7 @@ extension QueryInterfaceRequest: FetchRequest { let prefetchedQuery = SQLQuery(relation: prefetchedRelation) // Union region - try region.formUnion(SQLQueryGenerator(prefetchedQuery).databaseRegion(db)) + try region.formUnion(SQLQueryGenerator(prefetchedQuery).makeSelectStatement(db).databaseRegion) // Append nested prefetched associations (support for // A.including(all: A.bs.including(all: B.cs)) diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 56b277f66..c3c5e1467 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -100,10 +100,7 @@ struct SQLQueryGenerator { return try (makeSelectStatement(db), rowAdapter(db)) } - func databaseRegion(_ db: Database) throws -> DatabaseRegion { - let statement = try makeSelectStatement(db) - let databaseRegion = statement.databaseRegion - + private func optimizedDatabaseRegion(_ db: Database, _ databaseRegion: DatabaseRegion) throws -> DatabaseRegion { // Can we intersect the region with rowIds? // // Give up unless request feeds from a single database table @@ -178,7 +175,7 @@ struct SQLQueryGenerator { } /// Returns a select statement - private func makeSelectStatement(_ db: Database) throws -> SelectStatement { + func makeSelectStatement(_ db: Database) throws -> SelectStatement { // Build an SQK generation context with all aliases found in the query, // so that we can disambiguate tables that are used several times with // SQL aliases. @@ -190,6 +187,9 @@ struct SQLQueryGenerator { // Compile & set arguments let statement = try db.makeSelectStatement(sql: sql) statement.arguments = context.arguments! // not nil for this kind of context + + // Optimize databaseRegion + statement.databaseRegion = try optimizedDatabaseRegion(db, statement.databaseRegion) return statement } diff --git a/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift b/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift index a10f90cd1..8d4d38f16 100644 --- a/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift @@ -87,7 +87,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -112,7 +112,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM \"a\" WHERE 0 ORDER BY \"cola1\" @@ -138,7 +138,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" \ @@ -196,7 +196,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "parent" ORDER BY "parentA", "parentB" @@ -219,7 +219,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "parent" WHERE 0 ORDER BY "parentA", "parentB" @@ -238,7 +238,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "parent" WHERE "parentA" = 'foo' ORDER BY "parentA", "parentB" @@ -268,7 +268,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -302,7 +302,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -354,7 +354,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * \ @@ -419,7 +419,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -448,7 +448,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -501,7 +501,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * \ @@ -543,7 +543,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -579,7 +579,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" \ @@ -623,7 +623,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "b" ORDER BY "colb1" @@ -658,7 +658,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "b" ORDER BY "colb1" @@ -696,7 +696,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "b" ORDER BY "colb1" @@ -738,7 +738,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "b" ORDER BY "colb1" @@ -772,7 +772,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -812,7 +812,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -854,7 +854,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -900,7 +900,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT * FROM "a" ORDER BY "cola1" @@ -941,7 +941,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT "b".*, "a".* \ @@ -993,7 +993,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT "b".*, "a1".*, "a2".* \ @@ -1051,7 +1051,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT "a".*, "c".* \ @@ -1103,7 +1103,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT "a".*, "c1".*, "c2".* \ @@ -1163,7 +1163,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT "d".*, "c".*, "a".* \ @@ -1201,7 +1201,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } XCTAssertEqual(selectQueries, [ """ SELECT "d".*, "a".* \ @@ -1239,7 +1239,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } // LEFT JOIN in the first query are useless but harmless. // And SQLite may well optimize them out. // So don't bother removing them. @@ -1294,7 +1294,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } // LEFT JOIN in the first query are useless but harmless. // And SQLite may well optimize them out. // So don't bother removing them. @@ -1355,7 +1355,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } // LEFT JOIN in the first query are useless but harmless. // And SQLite may well optimize them out. // So don't bother removing them. @@ -1410,7 +1410,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } // LEFT JOIN in the first query are useless but harmless. // And SQLite may well optimize them out. // So don't bother removing them. @@ -1473,7 +1473,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } // LEFT JOIN in the first query are useless but harmless. // And SQLite may well optimize them out. // So don't bother removing them. @@ -1514,7 +1514,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { sqlQueries.removeAll() _ = try Row.fetchAll(db, request) - let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + let selectQueries = sqlQueries.filter { $0.contains("SELECT") && !$0.contains("sqlite_") } // LEFT JOIN in the first query are useless but harmless. // And SQLite may well optimize them out. // So don't bother removing them.