Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/StructuredQueriesCore/Updates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public struct Updates<Base: Table> {

public subscript<Value: QueryExpression>(
dynamicMember keyPath: KeyPath<Base.TableColumns, ColumnGroup<Base, Value>>
) -> Updates<Value> {
get { Updates<Value> { _ in } }
) -> Updates<TableAlias<Value, _TableAliasName<Base>>> {
get { Updates<TableAlias<Value, _TableAliasName<Base>>> { _ in } }
set { updates.append(contentsOf: newValue.updates) }
}

Expand Down Expand Up @@ -89,3 +89,7 @@ extension Updates: QueryExpression {
"SET \(updates.map { "\(quote: $0) = \($1)" }.joined(separator: ", "))"
}
}

public struct _TableAliasName<Base: Table>: AliasName {
public static var aliasName: String { Base.tableName }
}
Comment on lines +93 to +95
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the simplest way I could come up with to fix the nested update issue. Underscored type for now since I'm not sure folks should be using it, especially if we come up with a better way to handle things in the future. But if we come up with more use cases for this type, we could consider making a proper public API.

16 changes: 16 additions & 0 deletions Tests/StructuredQueriesTests/NestedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
59 changes: 59 additions & 0 deletions Tests/StructuredQueriesTests/UpdateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rcarver I'm not sure if I mentioned before, but #bind is the best way to coerce a query expression's underlying value. You could also have done Optional(honestValue) here.

While I think it's possible to add overloads to fix this. It's a pervasive problem and I'm not sure overloads are the solution...open to discuss, though!

},
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should now be fixed! Thanks for the test case!

"""
}
}
}
}

@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?
}