Skip to content

Commit f2a8684

Browse files
committed
Add columns via Expressions
1 parent 5de1420 commit f2a8684

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Sources/SQLite/Schema/SchemaChanger.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ public class SchemaChanger: CustomStringConvertible {
136136
columnDefinitions.append(column)
137137
}
138138

139+
public func add<T>(expression: Expression<T>) where T: Value {
140+
add(column: .init(name: columnName(for: expression), type: .init(expression: expression), nullable: false))
141+
}
142+
143+
public func add<T>(expression: Expression<T?>) where T: Value {
144+
add(column: .init(name: columnName(for: expression), type: .init(expression: expression), nullable: true))
145+
}
146+
139147
public func add(index: IndexDefinition) {
140148
indexDefinitions.append(index)
141149
}
@@ -146,6 +154,13 @@ public class SchemaChanger: CustomStringConvertible {
146154
.createTable(columns: columnDefinitions, ifNotExists: ifNotExists)
147155
] + indexDefinitions.map { .addIndex($0) }
148156
}
157+
158+
private func columnName<T>(for expression: Expression<T>) -> String {
159+
switch LiteralValue(expression.template) {
160+
case .stringLiteral(let string): return string
161+
default: fatalError("expression is not a literal string value")
162+
}
163+
}
149164
}
150165

151166
private let connection: Connection
@@ -331,3 +346,13 @@ extension TableDefinition {
331346
}
332347
}
333348
}
349+
350+
extension ColumnDefinition.Affinity {
351+
init<T>(expression: Expression<T>) where T: Value {
352+
self.init(T.declaredDatatype)
353+
}
354+
355+
init<T>(expression: Expression<T?>) where T: Value {
356+
self.init(T.declaredDatatype)
357+
}
358+
}

Tests/SQLiteTests/Schema/SchemaChangerTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,36 @@ class SchemaChangerTests: SQLiteTestCase {
201201
])
202202
}
203203

204+
func test_create_table_add_column_expression() throws {
205+
try schemaChanger.create(table: "foo") { table in
206+
table.add(expression: Expression<String>("name"))
207+
table.add(expression: Expression<Int>("age"))
208+
table.add(expression: Expression<Double?>("salary"))
209+
}
210+
211+
let columns = try schema.columnDefinitions(table: "foo")
212+
XCTAssertEqual(columns, [
213+
ColumnDefinition(name: "name",
214+
primaryKey: nil,
215+
type: .TEXT,
216+
nullable: false,
217+
defaultValue: .NULL,
218+
references: nil),
219+
ColumnDefinition(name: "age",
220+
primaryKey: nil,
221+
type: .INTEGER,
222+
nullable: false,
223+
defaultValue: .NULL,
224+
references: nil),
225+
ColumnDefinition(name: "salary",
226+
primaryKey: nil,
227+
type: .REAL,
228+
nullable: true,
229+
defaultValue: .NULL,
230+
references: nil)
231+
])
232+
}
233+
204234
func test_create_table_if_not_exists() throws {
205235
try schemaChanger.create(table: "foo") { table in
206236
table.add(column: .init(name: "id", primaryKey: .init(autoIncrement: true), type: .INTEGER))

0 commit comments

Comments
 (0)