mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Add schemaName parameter to columns(in:)
This commit is contained in:
1 parent
ac475cc158
commit
f00a974211
3 files changed
+103
-6
No files matched your search
@@ -720,10 +720,23 @@ extension Database {
|
||||
|
||||
/// Returns the columns in a table or a view.
|
||||
///
|
||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or if no
|
||||
/// such table or view with this name exists in the main or temp schema, or
|
||||
/// in an attached database.
|
||||
public func columns(in tableName: String) throws -> [ColumnInfo] {
|
||||
/// 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, if
|
||||
/// the specified schema does not exist,or if no such table or view
|
||||
/// with this name exists in the main or temp schema, or in an attached
|
||||
/// database.
|
||||
public func columns(in tableName: String, in schemaName: String? = nil) throws -> [ColumnInfo] {
|
||||
if let schemaName {
|
||||
let schemaIdentifier = try schemaIdentifier(named: schemaName)
|
||||
if let result = try columns(in: TableIdentifier(schemaID: schemaIdentifier, name: tableName)) {
|
||||
return result
|
||||
} else {
|
||||
throw DatabaseError.noSuchTable(tableName)
|
||||
}
|
||||
}
|
||||
|
||||
for schemaIdentifier in try schemaIdentifiers() {
|
||||
if let result = try columns(in: TableIdentifier(schemaID: schemaIdentifier, name: tableName)) {
|
||||
return result
|
||||
@@ -881,7 +894,7 @@ extension Database {
|
||||
|
||||
/// Information about a column of a database table.
|
||||
///
|
||||
/// You get `ColumnInfo` instances with the ``Database/columns(in:)``
|
||||
/// You get `ColumnInfo` instances with the ``Database/columns(in:in:)``
|
||||
/// `Database` method.
|
||||
///
|
||||
/// Related SQLite documentation:
|
||||
|
||||
@@ -391,7 +391,7 @@ extension Team: TableRecord {
|
||||
|
||||
### Querying the Database Schema
|
||||
|
||||
- ``Database/columns(in:)``
|
||||
- ``Database/columns(in:in:)``
|
||||
- ``Database/foreignKeys(on:)``
|
||||
- ``Database/indexes(on:)``
|
||||
- ``Database/isGRDBInternalTable(_:)``
|
||||
|
||||
@@ -289,4 +289,88 @@ class ColumnInfoTests: GRDBTestCase {
|
||||
_ = try db.columns(in: "t1")
|
||||
}
|
||||
}
|
||||
|
||||
func testUnknownSchema() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id INTEGER)")
|
||||
do {
|
||||
_ = try db.columns(in: "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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testSpecifiedMainSchema() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id INTEGER)")
|
||||
let columns = try db.columns(in: "t")
|
||||
XCTAssertEqual(columns.count, 1)
|
||||
XCTAssertEqual(columns[0].name, "id")
|
||||
XCTAssertEqual(columns[0].type, "INTEGER")
|
||||
}
|
||||
}
|
||||
|
||||
func testSpecifiedSchemaWithTableNameCollisions() throws {
|
||||
let attached = try makeDatabaseQueue(filename: "attached1")
|
||||
try attached.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id2 TEXT)")
|
||||
}
|
||||
let main = try makeDatabaseQueue(filename: "main")
|
||||
try main.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id1 INTEGER)")
|
||||
try db.execute(literal: "ATTACH DATABASE \(attached.path) AS attached")
|
||||
|
||||
let columnsMain = try db.columns(in: "t", in: "main")
|
||||
XCTAssertEqual(columnsMain.count, 1)
|
||||
XCTAssertEqual(columnsMain[0].name, "id1")
|
||||
XCTAssertEqual(columnsMain[0].type, "INTEGER")
|
||||
|
||||
let columnsAttached = try db.columns(in: "t", in: "attached")
|
||||
XCTAssertEqual(columnsAttached.count, 1)
|
||||
XCTAssertEqual(columnsAttached[0].name, "id2")
|
||||
XCTAssertEqual(columnsAttached[0].type, "TEXT")
|
||||
}
|
||||
}
|
||||
|
||||
// The `t` table in the attached database should never
|
||||
// be found unless explicitly specified as it is after
|
||||
// `main.t` in resolution order.
|
||||
func testUnspecifiedSchemaWithTableNameCollisions() throws {
|
||||
let attached = try makeDatabaseQueue(filename: "attached1")
|
||||
try attached.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id2 TEXT)")
|
||||
}
|
||||
let main = try makeDatabaseQueue(filename: "main")
|
||||
try main.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id1 INTEGER)")
|
||||
try db.execute(literal: "ATTACH DATABASE \(attached.path) AS attached")
|
||||
|
||||
let columnsMain = try db.columns(in: "t")
|
||||
XCTAssertEqual(columnsMain.count, 1)
|
||||
XCTAssertEqual(columnsMain[0].name, "id1")
|
||||
XCTAssertEqual(columnsMain[0].type, "INTEGER")
|
||||
}
|
||||
}
|
||||
|
||||
func testUnspecifiedSchemaFindsAttachedDatabase() throws {
|
||||
let attached = try makeDatabaseQueue(filename: "attached1")
|
||||
try attached.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id2 TEXT)")
|
||||
}
|
||||
let main = try makeDatabaseQueue(filename: "main")
|
||||
try main.inDatabase { db in
|
||||
try db.execute(literal: "ATTACH DATABASE \(attached.path) AS attached")
|
||||
|
||||
let columnsMain = try db.columns(in: "t")
|
||||
XCTAssertEqual(columnsMain.count, 1)
|
||||
XCTAssertEqual(columnsMain[0].name, "id2")
|
||||
XCTAssertEqual(columnsMain[0].type, "TEXT")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user