mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Database.table(_:hasUniqueKey:) now accept views
This commit is contained in:
1 parent
2b19803644
commit
38f5226e95
2 files changed
+49
-15
No files matched your search
@@ -1067,6 +1067,9 @@ extension Database {
|
||||
/// returns the columns of the unique key, ordered as the matching index (or
|
||||
/// primary key). The case of returned columns is not guaranteed to match
|
||||
/// the case of input columns.
|
||||
///
|
||||
/// This method accepts both tables and views. For views, the primary
|
||||
/// key returned by the schemaSource is considered as a unique key.
|
||||
func columnsForUniqueKey(
|
||||
_ columns: some Collection<String>,
|
||||
in tableName: String
|
||||
@@ -1077,23 +1080,26 @@ extension Database {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check rowid
|
||||
let primaryKey = try self.primaryKey(tableName)
|
||||
if primaryKey.tableHasRowID && lowercasedColumns == ["rowid"] {
|
||||
return ["rowid"]
|
||||
// Check primaryKey (ignoring views if the schemaSource does not customize them).
|
||||
if let primaryKey = try? self.primaryKey(tableName) {
|
||||
if primaryKey.tableHasRowID && lowercasedColumns == ["rowid"] {
|
||||
return ["rowid"]
|
||||
}
|
||||
|
||||
// Check primaryKey
|
||||
if Set(primaryKey.columns.map { $0.lowercased() }).isSubset(of: lowercasedColumns) {
|
||||
return primaryKey.columns
|
||||
}
|
||||
}
|
||||
|
||||
// Check primaryKey
|
||||
if Set(primaryKey.columns.map { $0.lowercased() }).isSubset(of: lowercasedColumns) {
|
||||
return primaryKey.columns
|
||||
}
|
||||
|
||||
// Check unique indexes
|
||||
let matchingIndex = try indexes(on: tableName).first { index in
|
||||
index.isUnique && Set(index.columns.map { $0.lowercased() }).isSubset(of: lowercasedColumns)
|
||||
}
|
||||
if let matchingIndex {
|
||||
return matchingIndex.columns
|
||||
// Check unique indexes (ignoring views)
|
||||
if try tableExists(tableName) {
|
||||
let matchingIndex = try indexes(on: tableName).first { index in
|
||||
index.isUnique && Set(index.columns.map { $0.lowercased() }).isSubset(of: lowercasedColumns)
|
||||
}
|
||||
if let matchingIndex {
|
||||
return matchingIndex.columns
|
||||
}
|
||||
}
|
||||
|
||||
// No matching unique key found
|
||||
|
||||
@@ -71,9 +71,37 @@ class IndexInfoTests: GRDBTestCase {
|
||||
try XCTAssertFalse(db.table("citizenships", hasUniqueKey: ["year"]))
|
||||
try XCTAssertFalse(db.table("citizenships", hasUniqueKey: ["personId"]))
|
||||
try XCTAssertFalse(db.table("citizenships", hasUniqueKey: ["countryIsoCode"]))
|
||||
|
||||
try db.execute(sql: "CREATE VIEW personView AS SELECT * FROM persons")
|
||||
try XCTAssertFalse(db.table("personView", hasUniqueKey: ["rowid"]))
|
||||
try XCTAssertFalse(db.table("personView", hasUniqueKey: ["id"]))
|
||||
try XCTAssertFalse(db.table("personView", hasUniqueKey: ["email"]))
|
||||
try XCTAssertFalse(db.table("personView", hasUniqueKey: []))
|
||||
try XCTAssertFalse(db.table("personView", hasUniqueKey: ["name"]))
|
||||
try XCTAssertFalse(db.table("personView", hasUniqueKey: ["id", "email"]))
|
||||
}
|
||||
}
|
||||
|
||||
func testColumnsThatUniquelyIdentityRows_with_view_and_schema_source() throws {
|
||||
struct MySchemaSource: DatabaseSchemaSource {
|
||||
func columnsForPrimaryKey(_ db: Database, inView view: DatabaseObjectID) throws -> [String]? {
|
||||
["id"]
|
||||
}
|
||||
}
|
||||
|
||||
dbConfiguration.schemaSource = MySchemaSource()
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE persons (id INTEGER PRIMARY KEY, name TEXT, email TEXT UNIQUE)")
|
||||
try db.execute(sql: "CREATE VIEW personView AS SELECT * FROM persons")
|
||||
|
||||
try XCTAssertTrue(db.table("personView", hasUniqueKey: ["id"]))
|
||||
try XCTAssertFalse(db.table("personView", hasUniqueKey: []))
|
||||
try XCTAssertFalse(db.table("personView", hasUniqueKey: ["name"]))
|
||||
try XCTAssertTrue(db.table("personView", hasUniqueKey: ["id", "email"]))
|
||||
}
|
||||
}
|
||||
|
||||
// Regression test for https://github.com/groue/GRDB.swift/issues/840
|
||||
func testIndexOnExpressionIsExcluded() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
Reference in new issue
Block a user