diff --git a/Sources/StructuredQueriesCore/Updates.swift b/Sources/StructuredQueriesCore/Updates.swift index 44c3602c..4bcf062d 100644 --- a/Sources/StructuredQueriesCore/Updates.swift +++ b/Sources/StructuredQueriesCore/Updates.swift @@ -56,8 +56,8 @@ public struct Updates { public subscript( dynamicMember keyPath: KeyPath> - ) -> Updates { - get { Updates { _ in } } + ) -> Updates>> { + get { Updates>> { _ in } } set { updates.append(contentsOf: newValue.updates) } } @@ -89,3 +89,7 @@ extension Updates: QueryExpression { "SET \(updates.map { "\(quote: $0) = \($1)" }.joined(separator: ", "))" } } + +public struct _TableAliasName: AliasName { + public static var aliasName: String { Base.tableName } +} diff --git a/Tests/StructuredQueriesTests/NestedTests.swift b/Tests/StructuredQueriesTests/NestedTests.swift index 2c3720e6..d141a7a9 100644 --- a/Tests/StructuredQueriesTests/NestedTests.swift +++ b/Tests/StructuredQueriesTests/NestedTests.swift @@ -202,6 +202,22 @@ extension SnapshotTests { └──────────────────────────┘ """ } + assertQuery( + Item.select { dump($0.status).isOutOfStock } + ) { + """ + SELECT "items"."isOutOfStock" + FROM "items" + """ + } results: { + """ + ┌──────┐ + │ true │ + │ true │ + │ true │ + └──────┘ + """ + } } @Test func optionalDoubleNested() async throws { diff --git a/Tests/StructuredQueriesTests/UpdateTests.swift b/Tests/StructuredQueriesTests/UpdateTests.swift index 6608ce62..dbf9f0bb 100644 --- a/Tests/StructuredQueriesTests/UpdateTests.swift +++ b/Tests/StructuredQueriesTests/UpdateTests.swift @@ -357,9 +357,68 @@ extension SnapshotTests { } } } + + @Suite struct SelectionUpdateTests { + @Dependency(\.defaultDatabase) var db + + @Test func setSelectionFields() { + let honestValue: Int = 1 + let optionalValue: Int? = 1 + assertInlineSnapshot( + of: Root.update { + $0.fields.honestCount = honestValue + $0.fields.optionalCount = #bind(honestValue) + }, + as: .sql + ) { + """ + UPDATE "roots" + SET "honestCount" = 1, "optionalCount" = 1 + """ + } + } + + @Test func setSelectionValue() { + assertInlineSnapshot( + of: Root.update { + $0.fields = NestedFields(honestCount: 1, optionalCount: 1) + }, + as: .sql + ) { + """ + UPDATE "roots" + SET "honestCount" = 1, "optionalCount" = 1 + """ + } + } + + @Test func readSelectionFields() { + assertInlineSnapshot( + of: Root.update { + $0.fields.optionalCount = $0.fields.optionalCount + $0.fields.honestCount = $0.fields.honestCount + }, + as: .sql + ) { + """ + UPDATE "roots" + SET "optionalCount" = "roots"."optionalCount", "honestCount" = "roots"."honestCount" + """ + } + } + } } @Table private struct Item { var title = "" var quantity = 0 } + +@Table private struct Root { + @Columns var fields: NestedFields +} + +@Selection struct NestedFields { + var honestCount: Int = 0 + var optionalCount: Int? +}