mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
1 parent
3049b88fea
commit
effc991de1
6 files changed
+29
-22
No files matched your search
@@ -55,18 +55,25 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable {
|
||||
/// from all tables.
|
||||
public static let fullDatabase = DatabaseRegion(tableRegions: nil)
|
||||
|
||||
// TODO: rename to init(table:) when this initializer is no longer public
|
||||
/// The region that covers a full database table: all columns and all rows
|
||||
/// from this table.
|
||||
static func fullTable(_ table: String) -> DatabaseRegion {
|
||||
let table = CaseInsensitiveIdentifier(rawValue: table)
|
||||
return DatabaseRegion(tableRegions: [table: TableRegion(columns: nil, rowIds: nil)])
|
||||
}
|
||||
|
||||
/// Creates an empty database region.
|
||||
public init() {
|
||||
self.init(tableRegions: [:])
|
||||
}
|
||||
|
||||
// TODO: @available(*, deprecated, message: "In order to specify a table region, prefer `Table(tableName)`")
|
||||
/// Creates a region that spans all rows and columns of a database table.
|
||||
///
|
||||
/// - parameter table: A table name.
|
||||
@available(*, deprecated, message: "In order to specify a table region, prefer `Table(tableName)`")
|
||||
public init(table: String) {
|
||||
let table = CaseInsensitiveIdentifier(rawValue: table)
|
||||
self.init(tableRegions: [table: TableRegion(columns: nil, rowIds: nil)])
|
||||
self = .fullTable(table)
|
||||
}
|
||||
|
||||
/// Full columns in a table: (some columns in a table) × (all rows)
|
||||
|
||||
@@ -70,7 +70,7 @@ final class StatementCompilationAuthorizer: StatementAuthorizer {
|
||||
guard let columnName = cString2.map(String.init) else { return SQLITE_OK }
|
||||
if columnName.isEmpty {
|
||||
// SELECT COUNT(*) FROM table
|
||||
selectedRegion.formUnion(DatabaseRegion(table: tableName))
|
||||
selectedRegion.formUnion(DatabaseRegion.fullTable(tableName))
|
||||
} else {
|
||||
// SELECT column FROM table
|
||||
selectedRegion.formUnion(DatabaseRegion(table: tableName, columns: [columnName]))
|
||||
|
||||
@@ -895,9 +895,9 @@ public enum DatabaseEventKind {
|
||||
var modifiedRegion: DatabaseRegion {
|
||||
switch self {
|
||||
case let .delete(tableName):
|
||||
return DatabaseRegion(table: tableName)
|
||||
return DatabaseRegion.fullTable(tableName)
|
||||
case let .insert(tableName):
|
||||
return DatabaseRegion(table: tableName)
|
||||
return DatabaseRegion.fullTable(tableName)
|
||||
case let .update(tableName, updatedColumnNames):
|
||||
return DatabaseRegion(table: tableName, columns: updatedColumnNames)
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ extension Table where RowDecoder == Row {
|
||||
|
||||
extension Table: DatabaseRegionConvertible {
|
||||
public func databaseRegion(_ db: Database) throws -> DatabaseRegion {
|
||||
DatabaseRegion(table: tableName)
|
||||
DatabaseRegion.fullTable(tableName)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,12 @@ class DatabaseRegionTests : GRDBTestCase {
|
||||
let regions = [
|
||||
DatabaseRegion.fullDatabase,
|
||||
DatabaseRegion(),
|
||||
DatabaseRegion(table: "foo"),
|
||||
DatabaseRegion.fullTable("foo"),
|
||||
DatabaseRegion(table: "foo", columns: ["a", "b"]),
|
||||
DatabaseRegion(table: "foo", columns: ["b", "c"]),
|
||||
DatabaseRegion(table: "foo", rowIds: [1, 2]),
|
||||
DatabaseRegion(table: "foo", rowIds: [2, 3]),
|
||||
DatabaseRegion(table: "bar")]
|
||||
DatabaseRegion.fullTable("bar")]
|
||||
|
||||
for (i1, s1) in regions.enumerated() {
|
||||
for (i2, s2) in regions.enumerated() {
|
||||
@@ -27,8 +27,8 @@ class DatabaseRegionTests : GRDBTestCase {
|
||||
|
||||
// Case insensitivity
|
||||
XCTAssertEqual(
|
||||
DatabaseRegion(table: "foo"),
|
||||
DatabaseRegion(table: "FOO"))
|
||||
DatabaseRegion.fullTable("foo"),
|
||||
DatabaseRegion.fullTable("FOO"))
|
||||
XCTAssertEqual(
|
||||
DatabaseRegion(table: "foo", columns: ["a", "b"]),
|
||||
DatabaseRegion(table: "FOO", columns: ["A", "B"]))
|
||||
@@ -38,12 +38,12 @@ class DatabaseRegionTests : GRDBTestCase {
|
||||
let regions = [
|
||||
DatabaseRegion.fullDatabase,
|
||||
DatabaseRegion(),
|
||||
DatabaseRegion(table: "foo"),
|
||||
DatabaseRegion.fullTable("foo"),
|
||||
DatabaseRegion(table: "foo", columns: ["a", "b"]),
|
||||
DatabaseRegion(table: "foo", columns: ["b", "c"]),
|
||||
DatabaseRegion(table: "foo", rowIds: [1, 2]),
|
||||
DatabaseRegion(table: "foo", rowIds: [2, 3]),
|
||||
DatabaseRegion(table: "bar")]
|
||||
DatabaseRegion.fullTable("bar")]
|
||||
|
||||
var unions: [DatabaseRegion] = []
|
||||
for s1 in regions {
|
||||
@@ -146,12 +146,12 @@ class DatabaseRegionTests : GRDBTestCase {
|
||||
let regions = [
|
||||
DatabaseRegion.fullDatabase,
|
||||
DatabaseRegion(),
|
||||
DatabaseRegion(table: "foo"),
|
||||
DatabaseRegion.fullTable("foo"),
|
||||
DatabaseRegion(table: "foo", columns: ["a", "b"]),
|
||||
DatabaseRegion(table: "foo", columns: ["b", "c"]),
|
||||
DatabaseRegion(table: "foo", rowIds: [1, 2]),
|
||||
DatabaseRegion(table: "foo", rowIds: [2, 3]),
|
||||
DatabaseRegion(table: "bar")]
|
||||
DatabaseRegion.fullTable("bar")]
|
||||
|
||||
var intersection: [DatabaseRegion] = []
|
||||
for s1 in regions {
|
||||
@@ -266,13 +266,13 @@ class DatabaseRegionTests : GRDBTestCase {
|
||||
do {
|
||||
// Select the rowid
|
||||
let statement = try db.makeStatement(sql: "SELECT id FROM foo")
|
||||
let expectedRegion = DatabaseRegion(table: "foo")
|
||||
let expectedRegion = DatabaseRegion.fullTable("foo")
|
||||
XCTAssertEqual(statement.databaseRegion, expectedRegion)
|
||||
XCTAssertEqual(statement.databaseRegion.description, "foo(*)")
|
||||
}
|
||||
do {
|
||||
let statement = try db.makeStatement(sql: "SELECT ID FROM FOO")
|
||||
let expectedRegion = DatabaseRegion(table: "foo")
|
||||
let expectedRegion = DatabaseRegion.fullTable("foo")
|
||||
XCTAssertEqual(statement.databaseRegion, expectedRegion)
|
||||
XCTAssertEqual(statement.databaseRegion.description, "foo(*)")
|
||||
}
|
||||
@@ -318,7 +318,7 @@ class DatabaseRegionTests : GRDBTestCase {
|
||||
XCTAssertEqual(statement.databaseRegion, expectedRegion)
|
||||
XCTAssertEqual(statement.databaseRegion.description, "full database")
|
||||
} else {
|
||||
let expectedRegion = DatabaseRegion(table: "foo")
|
||||
let expectedRegion = DatabaseRegion.fullTable("foo")
|
||||
XCTAssertEqual(statement.databaseRegion, expectedRegion)
|
||||
XCTAssertEqual(statement.databaseRegion.description, "foo(*)")
|
||||
}
|
||||
@@ -330,7 +330,7 @@ class DatabaseRegionTests : GRDBTestCase {
|
||||
XCTAssertEqual(statement.databaseRegion, expectedRegion)
|
||||
XCTAssertEqual(statement.databaseRegion.description, "full database")
|
||||
} else {
|
||||
let expectedRegion = DatabaseRegion(table: "foo")
|
||||
let expectedRegion = DatabaseRegion.fullTable("foo")
|
||||
XCTAssertEqual(statement.databaseRegion, expectedRegion)
|
||||
XCTAssertEqual(statement.databaseRegion.description, "FOO(*)")
|
||||
}
|
||||
@@ -755,7 +755,7 @@ class DatabaseRegionTests : GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Complex selection
|
||||
let region = DatabaseRegion(table: "foo")
|
||||
let region = DatabaseRegion.fullTable("foo")
|
||||
.union(DatabaseRegion(table: "bar", columns: ["a"])
|
||||
.intersection(DatabaseRegion(table: "bar", rowIds: [1])))
|
||||
XCTAssertEqual(region.description, "bar(a)[1],foo(*)")
|
||||
|
||||
@@ -23,7 +23,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
var region = DatabaseRegion(table: "player")
|
||||
var region = DatabaseRegion.fullTable("player")
|
||||
db.recordingSelection(®ion) { }
|
||||
XCTAssertEqual(region.description, "player(*)")
|
||||
}
|
||||
@@ -45,7 +45,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
var region = DatabaseRegion(table: "player")
|
||||
var region = DatabaseRegion.fullTable("player")
|
||||
_ = try db.recordingSelection(®ion) {
|
||||
_ = try Row.fetchAll(db, sql: "SELECT * FROM team")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user