@@ -20,12 +20,27 @@ public protocol TableColumnExpression<Root, Value>: QueryExpression where Value
2020 ) -> any TableColumnExpression < TableAlias < Root , Name > , Value >
2121}
2222
23+ /// A type representing a _writable_ table column, _i.e._ not a generated column.
24+ public protocol WritableTableColumnExpression < Root, Value> : TableColumnExpression {
25+ func _aliased< Name: AliasName > (
26+ _ alias: Name . Type
27+ ) -> any WritableTableColumnExpression < TableAlias < Root , Name > , Value >
28+ }
29+
30+ extension WritableTableColumnExpression {
31+ public func _aliased< Name: AliasName > (
32+ _ alias: Name . Type
33+ ) -> any TableColumnExpression < TableAlias < Root , Name > , Value > {
34+ _aliased ( alias)
35+ }
36+ }
37+
2338/// A type representing a table column.
2439///
2540/// Don't create instances of this value directly. Instead, use the `@Table` and `@Column` macros to
2641/// generate values of this type.
2742public struct TableColumn < Root: Table , Value: QueryRepresentable & QueryBindable > :
28- TableColumnExpression ,
43+ WritableTableColumnExpression ,
2944 Sendable
3045where Value. QueryOutput: Sendable {
3146 public typealias QueryValue = Value
@@ -70,22 +85,80 @@ where Value.QueryOutput: Sendable {
7085
7186 public func _aliased< Name> (
7287 _ alias: Name . Type
73- ) -> any TableColumnExpression < TableAlias < Root , Name > , Value > {
88+ ) -> any WritableTableColumnExpression < TableAlias < Root , Name > , Value > {
7489 TableColumn < TableAlias < Root , Name > , Value > (
7590 name,
7691 keyPath: \. [ member: \Value . self, column: _keyPath]
7792 )
7893 }
7994}
8095
81- /// A type that describes how a table column is generated (e .g. SQLite generated columns).
96+ /// A type that describes how a table column is generated (_e .g._, SQLite generated columns).
8297///
8398/// You provide a value of this type to a `@Column` macro to differentiate between generated columns
8499/// that are physically stored in the database table and those that are "virtual".
85100///
86101/// ```swift
87102/// @Column(generated: .stored)
88103/// ```
89- public enum GeneratedColumn {
104+ public enum GeneratedColumnStorage {
90105 case virtual, stored
91106}
107+
108+ /// A type representing a generated column.
109+ ///
110+ /// Don't create instances of this value directly. Instead, use the `@Table` and `@Column` macros to
111+ /// generate values of this type.
112+ public struct GeneratedColumn < Root: Table , Value: QueryRepresentable & QueryBindable > :
113+ TableColumnExpression ,
114+ Sendable
115+ where Value. QueryOutput: Sendable {
116+ public typealias QueryValue = Value
117+
118+ public let name : String
119+
120+ public let defaultValue : Value . QueryOutput ?
121+
122+ let _keyPath : KeyPath < Root , Value . QueryOutput > & Sendable
123+
124+ public var keyPath : KeyPath < Root , Value . QueryOutput > {
125+ _keyPath
126+ }
127+
128+ public init (
129+ _ name: String ,
130+ keyPath: KeyPath < Root , Value . QueryOutput > & Sendable ,
131+ default defaultValue: Value . QueryOutput ? = nil
132+ ) {
133+ self . name = name
134+ self . defaultValue = defaultValue
135+ self . _keyPath = keyPath
136+ }
137+
138+ public init (
139+ _ name: String ,
140+ keyPath: KeyPath < Root , Value . QueryOutput > & Sendable ,
141+ default defaultValue: Value ? = nil
142+ ) where Value == Value . QueryOutput {
143+ self . name = name
144+ self . defaultValue = defaultValue
145+ self . _keyPath = keyPath
146+ }
147+
148+ public func decode( _ decoder: inout some QueryDecoder ) throws -> Value . QueryOutput {
149+ try Value ( decoder: & decoder) . queryOutput
150+ }
151+
152+ public var queryFragment : QueryFragment {
153+ " \( Root . self) . \( quote: name) "
154+ }
155+
156+ public func _aliased< Name> (
157+ _ alias: Name . Type
158+ ) -> any TableColumnExpression < TableAlias < Root , Name > , Value > {
159+ TableColumn < TableAlias < Root , Name > , Value > (
160+ name,
161+ keyPath: \. [ member: \Value . self, column: _keyPath]
162+ )
163+ }
164+ }
0 commit comments