|
8 | 8 | import OrderedCollections |
9 | 9 |
|
10 | 10 | public struct Schema { |
11 | | - public var tables: OrderedDictionary<QualifiedTableName, Table> = [:] |
12 | | - public var triggers: OrderedDictionary<QualifiedTableName, Trigger> = [:] |
13 | | - public var indices: OrderedDictionary<QualifiedTableName, Index> = [:] |
| 11 | + public var tables: OrderedDictionary<QualifiedName, Table> = [:] |
| 12 | + public var triggers: OrderedDictionary<QualifiedName, Trigger> = [:] |
| 13 | + public var indices: OrderedDictionary<QualifiedName, Index> = [:] |
14 | 14 |
|
15 | | - public subscript(tableName: QualifiedTableName) -> Table? { |
| 15 | + public subscript(tableName: QualifiedName) -> Table? { |
16 | 16 | _read { yield tables[tableName] } |
17 | 17 | _modify { yield &tables[tableName] } |
18 | 18 | } |
19 | 19 |
|
20 | | - public subscript(trigger triggerName: QualifiedTableName) -> Trigger? { |
| 20 | + public subscript(trigger triggerName: QualifiedName) -> Trigger? { |
21 | 21 | _read { yield triggers[triggerName] } |
22 | 22 | _modify { yield &triggers[triggerName] } |
23 | 23 | } |
24 | 24 |
|
25 | | - public subscript(index indexName: QualifiedTableName) -> Index? { |
| 25 | + public subscript(index indexName: QualifiedName) -> Index? { |
26 | 26 | _read { yield indices[indexName] } |
27 | 27 | _modify { yield &indices[indexName] } |
28 | 28 | } |
29 | 29 | } |
30 | | - |
31 | | -// TODO: An ordered dictionary may not be the best representation of the |
32 | | -// TODO: columns. Since this is used even in selects, the user could |
33 | | -// TODO: technically do `SELECT foo, foo FROM bar;` which have the same |
34 | | -// TODO: name which the ordered dictionary wouldnt catch. Or just error? |
35 | | -public typealias Columns = OrderedDictionary<Substring, Type> |
36 | | - |
37 | | -extension Columns { |
38 | | - /// Initializes the columns with their default names that SQLite gives to them. |
39 | | - init(withDefaultNames types: [Type]) { |
40 | | - self = types.enumerated() |
41 | | - .reduce(into: [:]) { c, v in c["column\(v.offset + 1)"] = v.element } |
42 | | - } |
43 | | -} |
44 | | - |
45 | | -/// A table within the database schema |
46 | | -public struct Table: Sendable, Equatable { |
47 | | - /// The name of the table |
48 | | - public var name: QualifiedTableName |
49 | | - /// The columns of the table |
50 | | - public var columns: Columns |
51 | | - /// The columns that make up the primary key |
52 | | - public let primaryKey: [Substring] |
53 | | - /// What kind of table it is (FTS/CTE...) |
54 | | - public let kind: Kind |
55 | | - |
56 | | - public enum Kind: Sendable { |
57 | | - case normal |
58 | | - case view |
59 | | - case fts5 |
60 | | - case cte |
61 | | - case subquery |
62 | | - } |
63 | | - |
64 | | - var type: Type { |
65 | | - return .row(.fixed(columns.map(\.value))) |
66 | | - } |
67 | | - |
68 | | - /// A table to be returned incase of an error in type checking |
69 | | - static let error = Table( |
70 | | - name: QualifiedTableName(name: "<<error>>", schema: nil), |
71 | | - columns: [:], |
72 | | - primaryKey: [], |
73 | | - kind: .normal |
74 | | - ) |
75 | | - |
76 | | - /// The table but with the name of the alias. |
77 | | - /// Used in `FROM foo AS bar` |
78 | | - func aliased(to alias: Substring) -> Table { |
79 | | - var copy = self |
80 | | - // Alias erases schema on purpose. |
81 | | - // main.foo AS bar does not equal main.bar |
82 | | - copy.name = QualifiedTableName(name: alias, schema: nil) |
83 | | - return copy |
84 | | - } |
85 | | - |
86 | | - /// Function to map over the column types and perform any |
87 | | - /// transformations needed |
88 | | - func mapTypes(_ transform: (Type) -> Type) -> Table { |
89 | | - var copy = self |
90 | | - copy.columns = columns.mapValues(transform) |
91 | | - return copy |
92 | | - } |
93 | | -} |
94 | | - |
95 | | -/// A trigger to be run on certain SQL operations |
96 | | -public struct Trigger { |
97 | | - /// The name of the trigger |
98 | | - public let name: QualifiedTableName |
99 | | - /// The table the trigger is watching |
100 | | - public let targetTable: QualifiedTableName |
101 | | - /// Any table accessed in the `BEGIN/END` |
102 | | - public let usedTables: Set<Substring> |
103 | | -} |
104 | | - |
105 | | -/// An index created within the schema |
106 | | -public struct Index { |
107 | | - /// The name given too the index |
108 | | - public let name: QualifiedTableName |
109 | | - /// The name of the table the index was created for. |
110 | | - public let table: QualifiedTableName |
111 | | -} |
0 commit comments