mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Raise a fatal error when the users filters a database view by primary key
Fixes #1648
This commit is contained in:
1 parent
d6b50b2c54
commit
2c49a69f43
3 files changed
+37
-26
No files matched your search
@@ -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 <https://github.com/groue/GRDB.swift/issues/1648>.
|
||||
// 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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user