Skip to content

Commit 71de4f0

Browse files
committed
Relax INSERT-SELECT to work with partial select statements
This allows you to insert from compound selects, values, and more.
1 parent b90c144 commit 71de4f0

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

Sources/StructuredQueriesCore/Statements/Insert.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,11 @@ extension Table {
303303
V1,
304304
each V2,
305305
C1: QueryExpression,
306-
each C2: QueryExpression,
307-
From,
308-
Joins
306+
each C2: QueryExpression
309307
>(
310308
or conflictResolution: ConflictResolution? = nil,
311309
_ columns: (TableColumns) -> (TableColumn<Self, V1>, repeat TableColumn<Self, each V2>),
312-
select selection: () -> Select<(C1, repeat each C2), From, Joins>,
310+
select selection: () -> some PartialSelectStatement<(C1, repeat each C2)>,
313311
onConflictDoUpdate updates: ((inout Updates<Self>) -> Void)? = nil,
314312
@QueryFragmentBuilder<Bool>
315313
where updateFilter: (TableColumns) -> [QueryFragment] = { _ in [] }
@@ -346,14 +344,12 @@ extension Table {
346344
each V2,
347345
C1: QueryExpression,
348346
each C2: QueryExpression,
349-
From,
350-
Joins,
351347
T1,
352348
each T2
353349
>(
354350
or conflictResolution: ConflictResolution? = nil,
355351
_ columns: (TableColumns) -> (TableColumn<Self, V1>, repeat TableColumn<Self, each V2>),
356-
select selection: () -> Select<(C1, repeat each C2), From, Joins>,
352+
select selection: () -> some PartialSelectStatement<(C1, repeat each C2)>,
357353
onConflict conflictTargets: (TableColumns) -> (
358354
TableColumn<Self, T1>, repeat TableColumn<Self, each T2>
359355
),
@@ -380,13 +376,11 @@ extension Table {
380376
private static func _insert<
381377
each Value,
382378
each ResultColumn: QueryExpression,
383-
From,
384-
Joins,
385379
each ConflictTarget
386380
>(
387381
or conflictResolution: ConflictResolution? = nil,
388382
_ columns: (TableColumns) -> (repeat TableColumn<Self, each Value>),
389-
select selection: () -> Select<(repeat each ResultColumn), From, Joins>,
383+
select selection: () -> some PartialSelectStatement<(repeat each ResultColumn)>,
390384
onConflict conflictTargets: (TableColumns) -> (repeat TableColumn<Self, each ConflictTarget>)?,
391385
@QueryFragmentBuilder<Bool>
392386
where targetFilter: (TableColumns) -> [QueryFragment] = { _ in [] },

Tests/StructuredQueriesTests/InsertTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,31 @@ extension SnapshotTests {
227227
└─────────────────────┘
228228
"""
229229
}
230+
231+
assertQuery(
232+
Tag.insert {
233+
$0.title
234+
} select: {
235+
Values("vacation")
236+
}
237+
.returning(\.self)
238+
) {
239+
"""
240+
INSERT INTO "tags"
241+
("title")
242+
SELECT 'vacation'
243+
RETURNING "id", "title"
244+
"""
245+
} results: {
246+
"""
247+
┌─────────────────────┐
248+
│ Tag( │
249+
│ id: 8, │
250+
│ title: "vacation"
251+
│ ) │
252+
└─────────────────────┘
253+
"""
254+
}
230255
}
231256

232257
@Test func draft() {

0 commit comments

Comments
 (0)