From 2c49a69f43033628b5ee7892f69db438188a3fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 12 Oct 2024 15:41:36 +0200 Subject: [PATCH] Raise a fatal error when the users filters a database view by primary key Fixes #1648 --- GRDB/Core/Database+Schema.swift | 30 +++++++++++++++++++ .../Request/RequestProtocols.swift | 26 ++++------------ GRDB/Record/TableRecord.swift | 7 ++--- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/GRDB/Core/Database+Schema.swift b/GRDB/Core/Database+Schema.swift index beb671277..bbc2e9040 100644 --- a/GRDB/Core/Database+Schema.swift +++ b/GRDB/Core/Database+Schema.swift @@ -359,6 +359,36 @@ extension Database { throw DatabaseError.noSuchTable(tableName) } + /// Returns the name of the single-column primary key. + /// + /// A fatal error is raised if the primary key has several columns, or + /// if `tableName` is the name of a database view. + func filteringPrimaryKeyColumn(_ tableName: String) throws -> String { + do { + let primaryKey = try primaryKey(tableName) + GRDBPrecondition( + primaryKey.columns.count == 1, + "Filtering by primary key requires a single-column primary key in the table '\(tableName)'") + return primaryKey.columns[0] + } catch let error as DatabaseError { + // Maybe the user tries to filter a view by primary key, + // as in . + // In this case, raise a fatalError because this is a + // programmer error which is very likely to be detected + // during development. + if case .SQLITE_ERROR = error.resultCode, + (try? viewExists(tableName)) == true + { + fatalError(""" + Filtering by primary key is not available on the database view '\(tableName)'. \ + Use `filter(Column("...") == value)` instead. + """) + } else { + throw error + } + } + } + /// Returns nil if table does not exist private func primaryKey(_ table: TableIdentifier) throws -> PrimaryKeyInfo? { SchedulingWatchdog.preconditionValidQueue(self) diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index b725b8b4b..29d6a3a58 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -458,11 +458,7 @@ extension TableRequest where Self: FilteredRequest, Self: TypedRequest { } return filterWhenConnected(keys: { [databaseTableName] db in - let primaryKey = try db.primaryKey(databaseTableName) - GRDBPrecondition( - primaryKey.columns.count == 1, - "Requesting by key requires a single-column primary key in the table \(databaseTableName)") - let column = primaryKey.columns[0] + let column = try db.filteringPrimaryKeyColumn(databaseTableName) let strategy = recordType.databaseDataEncodingStrategy(for: column) let expressions = try datas.map { try strategy.encode($0).sqlExpression } return expressions @@ -475,11 +471,7 @@ extension TableRequest where Self: FilteredRequest, Self: TypedRequest { } return filterWhenConnected(keys: { [databaseTableName] db in - let primaryKey = try db.primaryKey(databaseTableName) - GRDBPrecondition( - primaryKey.columns.count == 1, - "Requesting by key requires a single-column primary key in the table \(databaseTableName)") - let column = primaryKey.columns[0] + let column = try db.filteringPrimaryKeyColumn(databaseTableName) let strategy = recordType.databaseDateEncodingStrategy(for: column) let expressions = dates.map { strategy.encode($0).sqlExpression } return expressions @@ -492,11 +484,7 @@ extension TableRequest where Self: FilteredRequest, Self: TypedRequest { } return filterWhenConnected(keys: { [databaseTableName] db in - let primaryKey = try db.primaryKey(databaseTableName) - GRDBPrecondition( - primaryKey.columns.count == 1, - "Requesting by key requires a single-column primary key in the table \(databaseTableName)") - let column = primaryKey.columns[0] + let column = try db.filteringPrimaryKeyColumn(databaseTableName) let strategy = recordType.databaseUUIDEncodingStrategy(for: column) let expressions = uuids.map { strategy.encode($0).sqlExpression } return expressions @@ -524,12 +512,8 @@ extension TableRequest where Self: FilteredRequest, Self: TypedRequest { // Don't bother removing NULLs. We'd lose CPU cycles, and this does not // change the SQLite results anyway. let expressions = try keys(db) - - let primaryKey = try db.primaryKey(databaseTableName) - GRDBPrecondition( - primaryKey.columns.count == 1, - "Requesting by key requires a single-column primary key in the table \(databaseTableName)") - return SQLCollection.array(expressions).contains(Column(primaryKey.columns[0]).sqlExpression) + let column = try db.filteringPrimaryKeyColumn(databaseTableName) + return SQLCollection.array(expressions).contains(Column(column).sqlExpression) } } diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index 2f29ec8fb..72719873c 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -744,13 +744,10 @@ extension TableRecord { /// any error that prevented the `RecordError` from being constructed. public static func recordNotFound(_ db: Database, key: some DatabaseValueConvertible) -> any Error { do { - let primaryKey = try db.primaryKey(databaseTableName) - GRDBPrecondition( - primaryKey.columns.count == 1, - "Requesting by key requires a single-column primary key in the table \(databaseTableName)") + let column = try db.filteringPrimaryKeyColumn(databaseTableName) return RecordError.recordNotFound( databaseTableName: databaseTableName, - key: [primaryKey.columns[0]: key.databaseValue]) + key: [column: key.databaseValue]) } catch { return error }