Add schemaName parameter to table/view/triggerExists()

This commit is contained in:
Ben Barnett committed 2023-12-08 18:22:49 +00:00
1 parent 73489fc0e7
commit 65167ec7da
3 files changed
+206 -11

No files matched your search

+54 -8
View File
@@ -223,10 +223,19 @@ extension Database {
return tableInfo
}
/// Returns whether a table exists, in the main or temp schema, or in an
/// attached database.
public func tableExists(_ name: String) throws -> Bool {
try schemaIdentifiers().contains {
/// Returns whether a table exists
///
/// When `schemaName` is not specified, known schemas are iterated in
/// SQLite resolution order and the first matching result is returned.
///
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or
/// if the specified schema does not exist
public func tableExists(_ name: String, in schemaName: String? = nil) throws -> Bool {
if let schemaName {
return try exists(type: .table, name: name, in: schemaName)
}
return try schemaIdentifiers().contains {
try exists(type: .table, name: name, in: $0)
}
}
@@ -259,20 +268,57 @@ extension Database {
/// Returns whether a view exists, in the main or temp schema, or in an
/// attached database.
public func viewExists(_ name: String) throws -> Bool {
try schemaIdentifiers().contains {
///
/// When `schemaName` is not specified, known schemas are iterated in
/// SQLite resolution order and the first matching result is returned.
///
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or
/// if the specified schema does not exist
public func viewExists(_ name: String, in schemaName: String? = nil) throws -> Bool {
if let schemaName {
return try exists(type: .view, name: name, in: schemaName)
}
return try schemaIdentifiers().contains {
try exists(type: .view, name: name, in: $0)
}
}
/// Returns whether a trigger exists, in the main or temp schema, or in an
/// attached database.
public func triggerExists(_ name: String) throws -> Bool {
try schemaIdentifiers().contains {
///
/// When `schemaName` is not specified, known schemas are iterated in
/// SQLite resolution order and the first matching result is returned.
///
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or
/// if the specified schema does not exist
public func triggerExists(_ name: String, in schemaName: String? = nil) throws -> Bool {
if let schemaName {
return try exists(type: .trigger, name: name, in: schemaName)
}
return try schemaIdentifiers().contains {
try exists(type: .trigger, name: name, in: $0)
}
}
/// Checks if an entity exists in a given schema
///
/// This is checking for the existence of the entity specified by
/// `type` and `name`. It is assumed that the existence of a schema
/// named `schemaName` is already known and will throw an error if it
/// cannot be found.
///
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or
/// if the specified schema does not exist
private func exists(type: SchemaObjectType, name: String, in schemaName: String) throws -> Bool {
if let schemaID = try schemaIdentifiers().first(where: { $0.sql.lowercased() == schemaName.lowercased() }) {
return try exists(type: type, name: name, in: schemaID)
} else {
throw DatabaseError.noSuchSchema(schemaName)
}
}
private func exists(type: SchemaObjectType, name: String, in schemaID: SchemaIdentifier) throws -> Bool {
// SQLite identifiers are case-insensitive, case-preserving:
// http://www.alberton.info/dbms_identifiers_and_case_sensitivity.html
+3 -3
View File
@@ -399,9 +399,9 @@ extension Team: TableRecord {
- ``Database/primaryKey(_:in:)``
- ``Database/schemaVersion()``
- ``Database/table(_:hasUniqueKey:)``
- ``Database/tableExists(_:)``
- ``Database/triggerExists(_:)``
- ``Database/viewExists(_:)``
- ``Database/tableExists(_:in:)``
- ``Database/triggerExists(_:in:)``
- ``Database/viewExists(_:in:)``
- ``ColumnInfo``
- ``ForeignKeyInfo``
- ``IndexInfo``
@@ -229,4 +229,153 @@ class DatabaseQueueSchemaCacheTests : GRDBTestCase {
try XCTAssertFalse(db.tableExists("item"))
}
}
func testTableExistsThrowsWhenUnknownSchema() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.execute(sql: """
CREATE TABLE t (id INTEGER);
CREATE VIEW v AS SELECT * FROM t;
CREATE TRIGGER tr AFTER INSERT ON t BEGIN SELECT 1; END;
""")
do {
_ = try db.tableExists("t", in: "invalid")
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
XCTAssertEqual(error.message, "no such schema: invalid")
XCTAssertEqual(error.description, "SQLite error 1: no such schema: invalid")
}
do {
_ = try db.viewExists("v", in: "invalid")
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
XCTAssertEqual(error.message, "no such schema: invalid")
XCTAssertEqual(error.description, "SQLite error 1: no such schema: invalid")
}
do {
_ = try db.triggerExists("tr", in: "invalid")
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
XCTAssertEqual(error.message, "no such schema: invalid")
XCTAssertEqual(error.description, "SQLite error 1: no such schema: invalid")
}
}
}
func testExistsWithSpecifiedMainSchema() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.execute(sql: """
CREATE TABLE t (id INTEGER);
CREATE VIEW v AS SELECT * FROM t;
CREATE TRIGGER tr AFTER INSERT ON t BEGIN SELECT 1; END;
""")
let tableExists = try db.tableExists("t", in: "main")
let viewExists = try db.viewExists("v", in: "main")
let triggerExists = try db.triggerExists("tr", in: "main")
XCTAssertTrue(tableExists)
XCTAssertTrue(viewExists)
XCTAssertTrue(triggerExists)
}
}
func testNotExistsWithSpecifiedMainSchema() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let tableExists = try db.tableExists("t", in: "main")
let viewExists = try db.viewExists("v", in: "main")
let triggerExists = try db.triggerExists("tr", in: "main")
XCTAssertFalse(tableExists)
XCTAssertFalse(viewExists)
XCTAssertFalse(triggerExists)
}
}
func testExistsWithSpecifiedSchemaWithEntityNameCollisions() throws {
let attached = try makeDatabaseQueue(filename: "attached")
try attached.inDatabase { db in
try db.execute(sql: """
CREATE TABLE t (id INTEGER);
CREATE VIEW v AS SELECT * FROM t;
CREATE TRIGGER tr AFTER INSERT ON t BEGIN SELECT 1; END;
""")
}
let main = try makeDatabaseQueue(filename: "main")
try main.inDatabase { db in
try db.execute(sql: """
CREATE TABLE t (id INTEGER);
CREATE VIEW v AS SELECT * FROM t;
CREATE TRIGGER tr AFTER INSERT ON t BEGIN SELECT 1; END;
""")
try db.execute(literal: "ATTACH DATABASE \(attached.path) AS attached")
let tableExistsInMain = try db.tableExists("t", in: "main")
let viewExistsInMain = try db.viewExists("v", in: "main")
let triggerExistsInMain = try db.triggerExists("tr", in: "main")
XCTAssertTrue(tableExistsInMain)
XCTAssertTrue(viewExistsInMain)
XCTAssertTrue(triggerExistsInMain)
let tableExistsInAttached = try db.tableExists("t", in: "attached")
let viewExistsInAttached = try db.viewExists("v", in: "attached")
let triggerExistsInAttached = try db.triggerExists("tr", in: "attached")
XCTAssertTrue(tableExistsInAttached)
XCTAssertTrue(viewExistsInAttached)
XCTAssertTrue(triggerExistsInAttached)
}
}
func testExistsWithUnspecifiedSchemaWithEntityNameCollisions() throws {
let attached = try makeDatabaseQueue(filename: "attached")
try attached.inDatabase { db in
try db.execute(sql: """
CREATE TABLE t (id INTEGER);
CREATE VIEW v AS SELECT * FROM t;
CREATE TRIGGER tr AFTER INSERT ON t BEGIN SELECT 1; END;
""")
}
let main = try makeDatabaseQueue(filename: "main")
try main.inDatabase { db in
try db.execute(sql: """
CREATE TABLE t (id INTEGER);
CREATE VIEW v AS SELECT * FROM t;
CREATE TRIGGER tr AFTER INSERT ON t BEGIN SELECT 1; END;
""")
try db.execute(literal: "ATTACH DATABASE \(attached.path) AS attached")
// Some entity with the name exists, but we can't prove from this information which one
// it's getting. `true` is still the correct result.
let tableExists = try db.tableExists("t")
let viewExists = try db.viewExists("v")
let triggerExists = try db.triggerExists("tr")
XCTAssertTrue(tableExists)
XCTAssertTrue(viewExists)
XCTAssertTrue(triggerExists)
}
}
func testExistsWithUnspecifiedSchemaFindsAttachedDatabase() throws {
let attached = try makeDatabaseQueue(filename: "attached")
try attached.inDatabase { db in
try db.execute(sql: """
CREATE TABLE t (id INTEGER);
CREATE VIEW v AS SELECT * FROM t;
CREATE TRIGGER tr AFTER INSERT ON t BEGIN SELECT 1; END;
""")
}
let main = try makeDatabaseQueue(filename: "main")
try main.inDatabase { db in
try db.execute(literal: "ATTACH DATABASE \(attached.path) AS attached")
let tableExists = try db.tableExists("t")
let viewExists = try db.viewExists("v")
let triggerExists = try db.triggerExists("tr")
XCTAssertTrue(tableExists)
XCTAssertTrue(viewExists)
XCTAssertTrue(triggerExists)
}
}
}