Skip to content

Add some conditional conformances to TableAlias #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2025
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
25 changes: 25 additions & 0 deletions Sources/StructuredQueriesCore/TableAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,31 @@ extension TableAlias: QueryRepresentable where Base: QueryRepresentable {

extension TableAlias: Sendable where Base: Sendable {}

extension TableAlias: Equatable where Base: Equatable {}

extension TableAlias: Hashable where Base: Hashable {}

extension TableAlias: Decodable where Base: Decodable {
public init(from decoder: Decoder) throws {
do {
self.init(base: try decoder.singleValueContainer().decode(Base.self))
} catch {
self.init(base: try Base(from: decoder))
}
}
}

extension TableAlias: Encodable where Base: Encodable {
public func encode(to encoder: Encoder) throws {
do {
var container = encoder.singleValueContainer()
try container.encode(self.base)
} catch {
try self.base.encode(to: encoder)
}
}
}

extension QueryFragment {
fileprivate func replacingOccurrences<T: Table, A: AliasName>(
of _: T.Type, with _: A.Type
Expand Down
36 changes: 36 additions & 0 deletions Tests/StructuredQueriesTests/SelectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,42 @@ extension SnapshotTests {
└───┴───┘
"""
}

assertQuery(
Reminder.as(R1.self)
.group(by: \.id)
.leftJoin(Reminder.as(R2.self).all) { $0.id.eq($1.id) }
.limit(1)
.select { ($0, $1.jsonGroupArray()) }
) {
"""
SELECT "r1s"."id", "r1s"."assignedUserID", "r1s"."dueDate", "r1s"."isCompleted", "r1s"."isFlagged", "r1s"."notes", "r1s"."priority", "r1s"."remindersListID", "r1s"."title", json_group_array(CASE WHEN ("r2s"."id" IS NOT NULL) THEN json_object('id', json_quote("r2s"."id"), 'assignedUserID', json_quote("r2s"."assignedUserID"), 'dueDate', json_quote("r2s"."dueDate"), 'isCompleted', json(CASE "r2s"."isCompleted" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'isFlagged', json(CASE "r2s"."isFlagged" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'notes', json_quote("r2s"."notes"), 'priority', json_quote("r2s"."priority"), 'remindersListID', json_quote("r2s"."remindersListID"), 'title', json_quote("r2s"."title")) END)
FROM "reminders" AS "r1s"
LEFT JOIN "reminders" AS "r2s" ON ("r1s"."id" = "r2s"."id")
GROUP BY "r1s"."id"
LIMIT 1
"""
}results: {
"""
┌────────────────────────────────────────────┬────────────────────────────────────────────────┐
│ Reminder( │ [ │
│ id: 1, │ [0]: TableAlias( │
│ assignedUserID: 1, │ base: Reminder( │
│ dueDate: Date(2001-01-01T00:00:00.000Z), │ id: 1, │
│ isCompleted: false, │ assignedUserID: 1, │
│ isFlagged: false, │ dueDate: Date(2001-01-01T00:00:00.000Z), │
│ notes: "Milk, Eggs, Apples", │ isCompleted: false, │
│ priority: nil, │ isFlagged: false, │
│ remindersListID: 1, │ notes: "Milk, Eggs, Apples", │
│ title: "Groceries" │ priority: nil, │
│ ) │ remindersListID: 1, │
│ │ title: "Groceries" │
│ │ ) │
│ │ ) │
│ │ ] │
└────────────────────────────────────────────┴────────────────────────────────────────────────┘
"""
}
}

@Test func `case`() {
Expand Down