Skip to content

Commit 460b941

Browse files
committed
wip
1 parent b36ad58 commit 460b941

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

Sources/StructuredQueriesCore/TableColumn.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
///
33
/// This protocol has a single conformance, ``TableColumn``, and simply provides type erasure over
44
/// a table's columns. You should not conform to this protocol directly.
5-
public protocol TableColumnExpression<Root, Value>: QueryExpression where Value == QueryValue {
5+
public protocol TableColumnExpression<Root, Value>: QueryExpression
6+
where Value == QueryValue, Value.QueryOutput: Sendable {
67
associatedtype Root: Table
78
associatedtype Value: QueryRepresentable & QueryBindable
89

Sources/StructuredQueriesCore/Updates.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ extension Updates {
7373
public struct SubtableColumns<Root: Table, Value: Table>: QueryExpression {
7474
public typealias QueryValue = Value
7575

76+
public static func allColumns(keyPath: KeyPath<Root, Value> & Sendable) -> [any TableColumnExpression] {
77+
return Value.TableColumns.allColumns.map { column in
78+
func open<R, V>(
79+
_ column: some TableColumnExpression<R, V>
80+
) -> any TableColumnExpression {
81+
let keyPath = keyPath.appending(
82+
path: unsafeDowncast(column.keyPath, to: KeyPath<Value, V.QueryOutput>.self)
83+
)
84+
return TableColumn<Root, V>(
85+
column.name,
86+
keyPath: keyPath.unsafeSendable(),
87+
default: column.defaultValue
88+
)
89+
}
90+
return open(column)
91+
}
92+
}
93+
7694
let keyPath: KeyPath<Root, Value> & Sendable
7795

7896
public init(keyPath: KeyPath<Root, Value> & Sendable) {

Tests/StructuredQueriesTests/NestedTableTests.swift

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ extension SnapshotTests {
4141
someColumns: SomeColumns(isCompleted: true, isPastDue: false)
4242
)
4343
}
44-
)
44+
) {
45+
"""
46+
INSERT INTO "items"
47+
("title", "quantity", "isCompleted", "isPastDue")
48+
VALUES
49+
('Hello', 24, 1, 0)
50+
"""
51+
}
4552
assertQuery(
4653
Item.insert {
4754
($0.title, $0.quantity, $0.someColumns.isCompleted, $0.someColumns.isPastDue)
@@ -104,6 +111,11 @@ extension SnapshotTests {
104111
│ ) │
105112
├───────────────────────┤
106113
│ SomeColumns( │
114+
│ isCompleted: true, │
115+
│ isPastDue: false │
116+
│ ) │
117+
├───────────────────────┤
118+
│ SomeColumns( │
107119
│ isCompleted: false, │
108120
│ isPastDue: false │
109121
│ ) │
@@ -136,6 +148,15 @@ extension SnapshotTests {
136148
│ ) │
137149
├─────────────────────────────┤
138150
│ Item( │
151+
│ title: "Hello", │
152+
│ quantity: 24, │
153+
│ someColumns: SomeColumns( │
154+
│ isCompleted: false, │
155+
│ isPastDue: true │
156+
│ ) │
157+
│ ) │
158+
├─────────────────────────────┤
159+
│ Item( │
139160
│ title: "Blob", │
140161
│ quantity: 42, │
141162
│ someColumns: SomeColumns( │
@@ -168,6 +189,11 @@ extension SnapshotTests {
168189
│ ) │
169190
├───────────────────────┤
170191
│ SomeColumns( │
192+
│ isCompleted: true, │
193+
│ isPastDue: false │
194+
│ ) │
195+
├───────────────────────┤
196+
│ SomeColumns( │
171197
│ isCompleted: false, │
172198
│ isPastDue: false │
173199
│ ) │
@@ -196,6 +222,15 @@ extension SnapshotTests {
196222
│ isPastDue: false │
197223
│ ) │
198224
│ ) │
225+
├─────────────────────────────┤
226+
│ Item( │
227+
│ title: "Hello", │
228+
│ quantity: 24, │
229+
│ someColumns: SomeColumns( │
230+
│ isCompleted: true, │
231+
│ isPastDue: false │
232+
│ ) │
233+
│ ) │
199234
└─────────────────────────────┘
200235
"""
201236
}
@@ -255,7 +290,7 @@ private struct Item {
255290
public static var allColumns: [any StructuredQueriesCore.TableColumnExpression] {
256291
[QueryValue.columns.title]
257292
+ [QueryValue.columns.quantity]
258-
+ SomeColumns.TableColumns.allColumns
293+
+ SubtableColumns<QueryValue, SomeColumns>.allColumns(keyPath: \.someColumns)
259294
}
260295
public var queryFragment: QueryFragment {
261296
"\(self.title), \(self.quantity), \(self.someColumns)"

0 commit comments

Comments
 (0)