Files
GRDB.swift/GRDB/Core/DatabaseSchemaCache.swift
T

37 lines
1.1 KiB
Swift

/// A thread-unsafe database schema cache
class DatabaseSchemaCache: DatabaseSchemaCacheType {
private var primaryKeys: [String: PrimaryKey?] = [:]
func clear() {
primaryKeys = [:]
}
func primaryKey(tableName tableName: String) -> PrimaryKey? {
guard let pk = primaryKeys[tableName] else {
return nil
}
return pk
}
func setPrimaryKey(primaryKey: PrimaryKey?, forTableName tableName: String) {
primaryKeys[tableName] = primaryKey
}
}
/// A thread-safe database schema cache
struct SharedDatabaseSchemaCache: DatabaseSchemaCacheType {
private let cache = ReadWriteBox(DatabaseSchemaCache())
mutating func clear() {
cache.write { $0.clear() }
}
func primaryKey(tableName tableName: String) -> PrimaryKey? {
return cache.read { $0.primaryKey(tableName: tableName) }
}
mutating func setPrimaryKey(primaryKey: PrimaryKey?, forTableName tableName: String) {
cache.write { $0.setPrimaryKey(primaryKey, forTableName: tableName) }
}
}