mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Use SchemaInfo more
This commit is contained in:
1 parent
3b7801efff
commit
0ea9a1ce0d
3 files changed
+22
-52
No files matched your search
@@ -65,18 +65,9 @@ extension Database {
|
||||
// SQlite identifiers are case-insensitive, case-preserving:
|
||||
// http://www.alberton.info/dbms_identifiers_and_case_sensitivity.html
|
||||
let name = name.lowercased()
|
||||
|
||||
if try makeSelectStatement("SELECT 1 FROM sqlite_master WHERE type = ? AND LOWER(name) = ?")
|
||||
.makeCursor(arguments: [type.rawValue, name])
|
||||
.isEmpty() == false
|
||||
{ return true }
|
||||
|
||||
if try makeSelectStatement("SELECT 1 FROM sqlite_temp_master WHERE type = ? AND LOWER(name) = ?")
|
||||
.makeCursor(arguments: [type.rawValue, name])
|
||||
.isEmpty() == false
|
||||
{ return true }
|
||||
|
||||
return false
|
||||
return try schema()
|
||||
.names(ofType: type)
|
||||
.contains { $0.lowercased() == name }
|
||||
}
|
||||
|
||||
/// The primary key for table named `tableName`.
|
||||
@@ -271,23 +262,10 @@ extension Database {
|
||||
|
||||
/// Returns the actual name of the database table
|
||||
func canonicalTableName(_ tableName: String) throws -> String {
|
||||
if let canonicalTableName = schemaCache.canonicalTableName(tableName) {
|
||||
return canonicalTableName
|
||||
}
|
||||
|
||||
guard let canonicalTableName = try String.fetchOne(self, """
|
||||
SELECT name FROM (
|
||||
SELECT name, type FROM sqlite_master
|
||||
UNION
|
||||
SELECT name, type FROM sqlite_temp_master)
|
||||
WHERE type = 'table' AND LOWER(name) = ?
|
||||
""", arguments: [tableName.lowercased()])
|
||||
else {
|
||||
guard let name = try schema().canonicalName(tableName, ofType: .table) else {
|
||||
throw DatabaseError(message: "no such table: \(tableName)")
|
||||
}
|
||||
|
||||
schemaCache.set(canonicalTableName: canonicalTableName, forTable: tableName)
|
||||
return canonicalTableName
|
||||
return name
|
||||
}
|
||||
|
||||
func schema() throws -> SchemaInfo {
|
||||
@@ -591,13 +569,14 @@ public struct ForeignKeyInfo {
|
||||
}
|
||||
|
||||
enum SchemaObjectType: String {
|
||||
case view
|
||||
case index
|
||||
case table
|
||||
case trigger
|
||||
case view
|
||||
}
|
||||
|
||||
struct SchemaInfo: Equatable {
|
||||
var objects: [SchemaKey: String?]
|
||||
private var objects: [SchemaKey: String?]
|
||||
|
||||
init(_ db: Database) throws {
|
||||
objects = try Dictionary(uniqueKeysWithValues: Row
|
||||
@@ -609,6 +588,7 @@ struct SchemaInfo: Equatable {
|
||||
.map { row in (SchemaKey(row: row), row["sql"]) })
|
||||
}
|
||||
|
||||
/// All names for a given type
|
||||
func names(ofType type: SchemaObjectType) -> Set<String> {
|
||||
return objects.keys.reduce(into: []) { (set, key) in
|
||||
if key.type == type.rawValue {
|
||||
@@ -617,6 +597,15 @@ struct SchemaInfo: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the canonical name of the object:
|
||||
///
|
||||
/// try db.execute("CREATE TABLE FooBar (...)")
|
||||
/// try db.schema().canonicalName("foobar", ofType: .table) // "FooBar"
|
||||
func canonicalName(_ name: String, ofType type: SchemaObjectType) -> String? {
|
||||
let name = name.lowercased()
|
||||
return objects.keys.first { $0.name.lowercased() == name }?.name
|
||||
}
|
||||
|
||||
struct SchemaKey: Codable, Hashable, FetchableRecord {
|
||||
var type: String
|
||||
var name: String
|
||||
|
||||
@@ -4,9 +4,6 @@ protocol DatabaseSchemaCache {
|
||||
|
||||
var schemaInfo: SchemaInfo? { get set }
|
||||
|
||||
func canonicalTableName(_ table: String) -> String?
|
||||
mutating func set(canonicalTableName: String, forTable table: String)
|
||||
|
||||
func primaryKey(_ table: String) -> PrimaryKeyInfo?
|
||||
mutating func set(primaryKey: PrimaryKeyInfo, forTable table: String)
|
||||
|
||||
@@ -23,14 +20,12 @@ protocol DatabaseSchemaCache {
|
||||
/// A thread-unsafe database schema cache
|
||||
struct SimpleDatabaseSchemaCache: DatabaseSchemaCache {
|
||||
var schemaInfo: SchemaInfo?
|
||||
private var canonicalTableNames: [String: String] = [:]
|
||||
private var primaryKeys: [String: PrimaryKeyInfo] = [:]
|
||||
private var columns: [String: [ColumnInfo]] = [:]
|
||||
private var indexes: [String: [IndexInfo]] = [:]
|
||||
private var foreignKeys: [String: [ForeignKeyInfo]] = [:]
|
||||
|
||||
mutating func clear() {
|
||||
canonicalTableNames = [:]
|
||||
primaryKeys = [:]
|
||||
columns = [:]
|
||||
indexes = [:]
|
||||
@@ -38,14 +33,6 @@ struct SimpleDatabaseSchemaCache: DatabaseSchemaCache {
|
||||
schemaInfo = nil
|
||||
}
|
||||
|
||||
func canonicalTableName(_ table: String) -> String? {
|
||||
return canonicalTableNames[table]
|
||||
}
|
||||
|
||||
mutating func set(canonicalTableName: String, forTable table: String) {
|
||||
canonicalTableNames[table] = table
|
||||
}
|
||||
|
||||
func primaryKey(_ table: String) -> PrimaryKeyInfo? {
|
||||
return primaryKeys[table]
|
||||
}
|
||||
@@ -88,9 +75,6 @@ struct EmptyDatabaseSchemaCache: DatabaseSchemaCache {
|
||||
set { }
|
||||
}
|
||||
|
||||
func canonicalTableName(_ table: String) -> String? { return nil }
|
||||
func set(canonicalTableName: String, forTable table: String) { }
|
||||
|
||||
func primaryKey(_ table: String) -> PrimaryKeyInfo? { return nil }
|
||||
func set(primaryKey: PrimaryKeyInfo, forTable table: String) { }
|
||||
|
||||
|
||||
@@ -49,17 +49,14 @@ final class StatementCompilationAuthorizer : StatementAuthorizer {
|
||||
|
||||
switch actionCode {
|
||||
case SQLITE_DROP_TABLE, SQLITE_DROP_VTABLE, SQLITE_DROP_TEMP_TABLE,
|
||||
SQLITE_DROP_INDEX, SQLITE_DROP_TEMP_INDEX:
|
||||
SQLITE_DROP_INDEX, SQLITE_DROP_TEMP_INDEX,
|
||||
SQLITE_DROP_VIEW, SQLITE_DROP_TEMP_VIEW,
|
||||
SQLITE_DROP_TRIGGER, SQLITE_DROP_TEMP_TRIGGER:
|
||||
isDropStatement = true
|
||||
invalidatesDatabaseSchemaCache = true
|
||||
return SQLITE_OK
|
||||
|
||||
case SQLITE_DROP_VIEW, SQLITE_DROP_TEMP_VIEW,
|
||||
SQLITE_DROP_TRIGGER, SQLITE_DROP_TEMP_TRIGGER:
|
||||
isDropStatement = true
|
||||
return SQLITE_OK
|
||||
|
||||
case SQLITE_DETACH, SQLITE_ALTER_TABLE,
|
||||
case SQLITE_ALTER_TABLE, SQLITE_DETACH,
|
||||
SQLITE_CREATE_INDEX, SQLITE_CREATE_TABLE,
|
||||
SQLITE_CREATE_TEMP_INDEX, SQLITE_CREATE_TEMP_TABLE,
|
||||
SQLITE_CREATE_TEMP_TRIGGER, SQLITE_CREATE_TEMP_VIEW,
|
||||
|
||||
Reference in new issue
Block a user