Skip to content

Commit 21e0c9a

Browse files
committed
wip
1 parent 7800ea8 commit 21e0c9a

File tree

8 files changed

+37
-20
lines changed

8 files changed

+37
-20
lines changed

Sources/StructuredQueriesCore/ColumnGroup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public struct ColumnGroup<Root: Table, Values: Table>: _TableColumnExpression
33
where Values.QueryOutput == Values {
44
public typealias Value = Values
55

6-
public var name: String { Values.tableName }
6+
public var _names: [String] { Values.TableColumns.allColumns.map(\.name) }
77

88
public typealias QueryValue = Values
99

Sources/StructuredQueriesCore/Internal/Deprecations.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ extension PrimaryKeyedTable {
110110
) -> InsertOf<Self> {
111111
insert(values: { rows }, onConflictDoUpdate: updates)
112112
}
113-
}
114113

115-
// TODO: Support composite keys.
116-
extension PrimaryKeyedTable where TableColumns.PrimaryColumn == TableColumn<Self, PrimaryKey> {
117114
@available(
118115
*, deprecated, message: "Use a trailing closure, instead: 'Table.upsert { draft }'"
119116
)

Sources/StructuredQueriesCore/Statements/Insert.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,7 @@ extension Table {
616616
}
617617
}
618618

619-
// TODO: Support composite keys.
620-
extension PrimaryKeyedTable where TableColumns.PrimaryColumn == TableColumn<Self, PrimaryKey> {
619+
extension PrimaryKeyedTable {
621620
/// An upsert statement for given drafts.
622621
///
623622
/// Generates an insert statement with an upsert clause. Useful for building forms that can both
@@ -636,15 +635,20 @@ extension PrimaryKeyedTable where TableColumns.PrimaryColumn == TableColumn<Self
636635
public static func upsert(
637636
@InsertValuesBuilder<Self> values: () -> [[QueryFragment]]
638637
) -> InsertOf<Self> {
639-
insert(
640-
values: values,
641-
onConflict: { $0.primaryKey },
642-
doUpdate: { updates, _ in
638+
Insert(
639+
conflictResolution: nil,
640+
columnNames: TableColumns.writableColumns.map(\.name),
641+
conflictTargetColumnNames: columns.primaryKey._names,
642+
conflictTargetFilter: [],
643+
values: .values(values()),
644+
updates: Updates { updates in
643645
for (column, excluded) in zip(Draft.TableColumns.writableColumns, Excluded.writableColumns)
644-
where column.name != columns.primaryKey.name {
646+
where !columns.primaryKey._names.contains(column.name) {
645647
updates.set(column, excluded.queryFragment)
646648
}
647-
}
649+
},
650+
updateFilter: [],
651+
returning: []
648652
)
649653
}
650654
}

Sources/StructuredQueriesCore/Statements/Update.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ extension PrimaryKeyedTable {
6262
_ row: Self
6363
) -> UpdateOf<Self> {
6464
update { updates in
65-
for column in TableColumns.writableColumns where column.name != columns.primaryKey.name {
65+
for column in TableColumns.writableColumns
66+
where !columns.primaryKey._names.contains(column.name) {
6667
func open<Root, Value>(_ column: some WritableTableColumnExpression<Root, Value>) {
6768
updates.set(
6869
column,

Sources/StructuredQueriesCore/TableColumn.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ public protocol _TableColumnExpression<Root, Value>: QueryExpression where Value
22
associatedtype Root: Table
33
associatedtype Value: QueryRepresentable
44

5-
/// The name of the table column.
6-
var name: String { get }
5+
var _names: [String] { get }
76

87
/// The table model key path associated with this table column.
98
var keyPath: KeyPath<Root, Value.QueryOutput> { get }
@@ -26,6 +25,10 @@ where Value: QueryBindable {
2625
) -> any TableColumnExpression<TableAlias<Root, Name>, Value>
2726
}
2827

28+
extension TableColumnExpression {
29+
public var _names: [String] { [name] }
30+
}
31+
2932
/// A type representing a _writable_ table column, _i.e._ not a generated column.
3033
public protocol WritableTableColumnExpression<Root, Value>: TableColumnExpression {
3134
func _aliased<Name: AliasName>(

Sources/StructuredQueriesSQLiteCore/Insert.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,7 @@ extension Table {
450450
}
451451
}
452452

453-
// TODO: Support composite keys.
454-
extension PrimaryKeyedTable where TableColumns.PrimaryColumn == TableColumn<Self, PrimaryKey> {
453+
extension PrimaryKeyedTable {
455454
/// An upsert statement for given drafts.
456455
///
457456
/// - Parameters:

Sources/StructuredQueriesSQLiteCore/Internal/Deprecations.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ extension PrimaryKeyedTable {
8282
) -> InsertOf<Self> {
8383
insert(or: conflictResolution, values: { rows }, onConflictDoUpdate: updates)
8484
}
85-
}
8685

87-
// TODO: Support composite keys.
88-
extension PrimaryKeyedTable where TableColumns.PrimaryColumn == TableColumn<Self, PrimaryKey> {
8986
@available(
9087
*, deprecated, message: "Use a trailing closure, instead: 'Table.upsert { draft }'"
9188
)

Tests/StructuredQueriesTests/NestedTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,22 @@ extension SnapshotTests {
353353
└───────────────────────────────────────────────────────────┘
354354
"""
355355
}
356+
assertQuery(
357+
Metadata.upsert {
358+
Metadata(
359+
id: MetadataID(recordID: UUID(0), recordType: "reminders")
360+
)
361+
}
362+
) {
363+
"""
364+
INSERT INTO "metadatas"
365+
("recordID", "recordType")
366+
VALUES
367+
('00000000-0000-0000-0000-000000000000', 'reminders')
368+
ON CONFLICT ("recordID", "recordType")
369+
DO NOTHING
370+
"""
371+
}
356372
}
357373
}
358374
}

0 commit comments

Comments
 (0)