Files
GRDB.swift/GRDB/Core/DatabaseSchemaCache.swift
T
Gwendal Roué bcd6e6767a Merge branch 'master' of github.com:groue/GRDB.swift into Swift3
Conflicts:
	GRDB.xcodeproj/project.pbxproj
	GRDB/Core/Database.swift
	GRDB/Core/DatabaseSchemaCache.swift
	GRDB/QueryInterface/SQLCollation.swift
	README.md
	TODO.md
	Tests/Public/Core/DatabaseValueConvertible/DatabaseValueConversionTests.swift
	Tests/Public/Core/StatementColumnConvertible/StatementColumnConvertibleTests.swift
	Tests/Public/QueryInterfaceRequest/QueryInterfaceRequestTests.swift
2016-08-07 10:11:31 +02:00

89 lines
2.8 KiB
Swift

/// The protocol for schema cache.
///
/// This protocol must not contain values that are valid for a single connection
/// only, because several connections can share the same schema cache.
///
/// Statements can't be cached here, for example.
protocol DatabaseSchemaCache {
mutating func clear()
func primaryKey(_ tableName: String) -> PrimaryKeyInfo??
mutating func set(primaryKey: PrimaryKeyInfo?, for tableName: String)
func columns(in tableName: String) -> [ColumnInfo]?
mutating func set(columns: [ColumnInfo], forTableName tableName: String)
func indexes(on tableName: String) -> [IndexInfo]?
mutating func set(indexes: [IndexInfo], forTableName tableName: String)
}
/// A thread-unsafe database schema cache
final class SimpleDatabaseSchemaCache: DatabaseSchemaCache {
private var primaryKeys: [String: PrimaryKeyInfo?] = [:]
private var columns: [String: [ColumnInfo]] = [:]
private var indexes: [String: [IndexInfo]] = [:]
func clear() {
primaryKeys = [:]
columns = [:]
indexes = [:]
}
func primaryKey(_ tableName: String) -> PrimaryKeyInfo?? {
return primaryKeys[tableName]
}
func set(primaryKey: PrimaryKeyInfo?, for tableName: String) {
primaryKeys[tableName] = primaryKey
}
func columns(in tableName: String) -> [ColumnInfo]? {
return columns[tableName]
}
func set(columns: [ColumnInfo], forTableName tableName: String) {
self.columns[tableName] = columns
}
func indexes(on tableName: String) -> [IndexInfo]? {
return indexes[tableName]
}
func set(indexes: [IndexInfo], forTableName tableName: String) {
self.indexes[tableName] = indexes
}
}
/// A thread-safe database schema cache
final class SharedDatabaseSchemaCache: DatabaseSchemaCache {
private let cache = ReadWriteBox(SimpleDatabaseSchemaCache())
func clear() {
cache.write { $0.clear() }
}
func primaryKey(_ tableName: String) -> PrimaryKeyInfo?? {
return cache.read { $0.primaryKey(tableName) }
}
func set(primaryKey: PrimaryKeyInfo?, for tableName: String) {
cache.write { $0.set(primaryKey: primaryKey, for: tableName) }
}
func columns(in tableName: String) -> [ColumnInfo]? {
return cache.read { $0.columns(in: tableName) }
}
func set(columns: [ColumnInfo], forTableName tableName: String) {
cache.write { $0.set(columns: columns, forTableName: tableName) }
}
func indexes(on tableName: String) -> [IndexInfo]? {
return cache.read { $0.indexes(on: tableName) }
}
func set(indexes: [IndexInfo], forTableName tableName: String) {
cache.write { $0.set(indexes: indexes, forTableName: tableName) }
}
}