Skip to content

Commit 6f9cddd

Browse files
authored
Fix INSERT-SELECT type-checking for #sql selections (pointfreeco#91)
1 parent a083372 commit 6f9cddd

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

Sources/StructuredQueriesCore/Internal/Deprecations.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ extension Table {
8484

8585
@available(*, deprecated, renamed: "insert(or:_:select:onConflictDoUpdate:)")
8686
public static func insert<
87-
V1, each V2, C1: QueryExpression, each C2: QueryExpression, From, Joins
87+
V1, each V2, From, Joins
8888
>(
8989
or conflictResolution: ConflictResolution? = nil,
9090
_ columns: (TableColumns) -> (TableColumn<Self, V1>, repeat TableColumn<Self, each V2>),
91-
select selection: () -> Select<(C1, repeat each C2), From, Joins>,
91+
select selection: () -> Select<(V1, repeat each V2), From, Joins>,
9292
onConflict updates: ((inout Updates<Self>) -> Void)?
93-
) -> InsertOf<Self>
94-
where C1.QueryValue == V1, (repeat (each C2).QueryValue) == (repeat each V2) {
93+
) -> InsertOf<Self> {
9594
insert(or: conflictResolution, columns, select: selection, onConflictDoUpdate: updates)
9695
}
9796
}

Sources/StructuredQueriesCore/Statements/Insert.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,15 @@ extension Table {
301301
/// - Returns: An insert statement.
302302
public static func insert<
303303
V1,
304-
each V2,
305-
C1: QueryExpression,
306-
each C2: QueryExpression
304+
each V2
307305
>(
308306
or conflictResolution: ConflictResolution? = nil,
309307
_ columns: (TableColumns) -> (TableColumn<Self, V1>, repeat TableColumn<Self, each V2>),
310-
select selection: () -> some PartialSelectStatement<(C1, repeat each C2)>,
308+
select selection: () -> some PartialSelectStatement<(V1, repeat each V2)>,
311309
onConflictDoUpdate updates: ((inout Updates<Self>) -> Void)? = nil,
312310
@QueryFragmentBuilder<Bool>
313311
where updateFilter: (TableColumns) -> [QueryFragment] = { _ in [] }
314-
) -> InsertOf<Self>
315-
where C1.QueryValue == V1, (repeat (each C2).QueryValue) == (repeat each V2) {
312+
) -> InsertOf<Self> {
316313
_insert(
317314
or: conflictResolution,
318315
columns,
@@ -342,14 +339,12 @@ extension Table {
342339
public static func insert<
343340
V1,
344341
each V2,
345-
C1: QueryExpression,
346-
each C2: QueryExpression,
347342
T1,
348343
each T2
349344
>(
350345
or conflictResolution: ConflictResolution? = nil,
351346
_ columns: (TableColumns) -> (TableColumn<Self, V1>, repeat TableColumn<Self, each V2>),
352-
select selection: () -> some PartialSelectStatement<(C1, repeat each C2)>,
347+
select selection: () -> some PartialSelectStatement<(V1, repeat each V2)>,
353348
onConflict conflictTargets: (TableColumns) -> (
354349
TableColumn<Self, T1>, repeat TableColumn<Self, each T2>
355350
),
@@ -358,8 +353,7 @@ extension Table {
358353
doUpdate updates: (inout Updates<Self>) -> Void = { _ in },
359354
@QueryFragmentBuilder<Bool>
360355
where updateFilter: (TableColumns) -> [QueryFragment] = { _ in [] }
361-
) -> InsertOf<Self>
362-
where C1.QueryValue == V1, (repeat (each C2).QueryValue) == (repeat each V2) {
356+
) -> InsertOf<Self> {
363357
withoutActuallyEscaping(updates) { updates in
364358
_insert(
365359
or: conflictResolution,
@@ -375,20 +369,18 @@ extension Table {
375369

376370
private static func _insert<
377371
each Value,
378-
each ResultColumn: QueryExpression,
379372
each ConflictTarget
380373
>(
381374
or conflictResolution: ConflictResolution? = nil,
382375
_ columns: (TableColumns) -> (repeat TableColumn<Self, each Value>),
383-
select selection: () -> some PartialSelectStatement<(repeat each ResultColumn)>,
376+
select selection: () -> some PartialSelectStatement<(repeat each Value)>,
384377
onConflict conflictTargets: (TableColumns) -> (repeat TableColumn<Self, each ConflictTarget>)?,
385378
@QueryFragmentBuilder<Bool>
386379
where targetFilter: (TableColumns) -> [QueryFragment] = { _ in [] },
387380
doUpdate updates: ((inout Updates<Self>) -> Void)?,
388381
@QueryFragmentBuilder<Bool>
389382
where updateFilter: (TableColumns) -> [QueryFragment] = { _ in [] }
390-
) -> InsertOf<Self>
391-
where (repeat (each ResultColumn).QueryValue) == (repeat each Value) {
383+
) -> InsertOf<Self> {
392384
var columnNames: [String] = []
393385
for column in repeat each columns(Self.columns) {
394386
columnNames.append(column.name)

Tests/StructuredQueriesTests/InsertTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,30 @@ extension SnapshotTests {
647647
"""
648648
}
649649
}
650+
651+
@Test func insertSelectSQL() {
652+
assertQuery(
653+
RemindersList.insert {
654+
$0.title
655+
} select: {
656+
Values(#sql("'Groceries'"))
657+
}
658+
.returning(\.id)
659+
) {
660+
"""
661+
INSERT INTO "remindersLists"
662+
("title")
663+
SELECT 'Groceries'
664+
RETURNING "id"
665+
"""
666+
} results: {
667+
"""
668+
┌───┐
669+
│ 4 │
670+
└───┘
671+
"""
672+
}
673+
}
650674
}
651675
}
652676

0 commit comments

Comments
 (0)