From 0ea9a1ce0d206e735bccf668a3be42f1bfcaab05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 17 Nov 2018 15:59:09 +0100 Subject: [PATCH] Use SchemaInfo more --- GRDB/Core/Database+Schema.swift | 47 +++++++++++------------------ GRDB/Core/DatabaseSchemaCache.swift | 16 ---------- GRDB/Core/StatementAuthorizer.swift | 11 +++---- 3 files changed, 22 insertions(+), 52 deletions(-) diff --git a/GRDB/Core/Database+Schema.swift b/GRDB/Core/Database+Schema.swift index 3b7597546..651682b81 100644 --- a/GRDB/Core/Database+Schema.swift +++ b/GRDB/Core/Database+Schema.swift @@ -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 { 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 diff --git a/GRDB/Core/DatabaseSchemaCache.swift b/GRDB/Core/DatabaseSchemaCache.swift index 6ef445ebc..4f29cef90 100644 --- a/GRDB/Core/DatabaseSchemaCache.swift +++ b/GRDB/Core/DatabaseSchemaCache.swift @@ -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) { } diff --git a/GRDB/Core/StatementAuthorizer.swift b/GRDB/Core/StatementAuthorizer.swift index ef6064578..0c9957878 100644 --- a/GRDB/Core/StatementAuthorizer.swift +++ b/GRDB/Core/StatementAuthorizer.swift @@ -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,