@@ -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+ }
0 commit comments