Skip to content

Commit 06ea416

Browse files
authored
Add some conditional conformances to TableAlias (#54)
* Add conditional codable conformance to TableAlias. * wip
1 parent 1c1e6af commit 06ea416

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Sources/StructuredQueriesCore/TableAlias.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,31 @@ extension TableAlias: QueryRepresentable where Base: QueryRepresentable {
193193

194194
extension TableAlias: Sendable where Base: Sendable {}
195195

196+
extension TableAlias: Equatable where Base: Equatable {}
197+
198+
extension TableAlias: Hashable where Base: Hashable {}
199+
200+
extension TableAlias: Decodable where Base: Decodable {
201+
public init(from decoder: Decoder) throws {
202+
do {
203+
self.init(base: try decoder.singleValueContainer().decode(Base.self))
204+
} catch {
205+
self.init(base: try Base(from: decoder))
206+
}
207+
}
208+
}
209+
210+
extension TableAlias: Encodable where Base: Encodable {
211+
public func encode(to encoder: Encoder) throws {
212+
do {
213+
var container = encoder.singleValueContainer()
214+
try container.encode(self.base)
215+
} catch {
216+
try self.base.encode(to: encoder)
217+
}
218+
}
219+
}
220+
196221
extension QueryFragment {
197222
fileprivate func replacingOccurrences<T: Table, A: AliasName>(
198223
of _: T.Type, with _: A.Type

Tests/StructuredQueriesTests/SelectTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,42 @@ extension SnapshotTests {
984984
└───┴───┘
985985
"""
986986
}
987+
988+
assertQuery(
989+
Reminder.as(R1.self)
990+
.group(by: \.id)
991+
.leftJoin(Reminder.as(R2.self).all) { $0.id.eq($1.id) }
992+
.limit(1)
993+
.select { ($0, $1.jsonGroupArray()) }
994+
) {
995+
"""
996+
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)
997+
FROM "reminders" AS "r1s"
998+
LEFT JOIN "reminders" AS "r2s" ON ("r1s"."id" = "r2s"."id")
999+
GROUP BY "r1s"."id"
1000+
LIMIT 1
1001+
"""
1002+
}results: {
1003+
"""
1004+
┌────────────────────────────────────────────┬────────────────────────────────────────────────┐
1005+
│ Reminder( │ [ │
1006+
│ id: 1, │ [0]: TableAlias( │
1007+
│ assignedUserID: 1, │ base: Reminder( │
1008+
│ dueDate: Date(2001-01-01T00:00:00.000Z), │ id: 1, │
1009+
│ isCompleted: false, │ assignedUserID: 1, │
1010+
│ isFlagged: false, │ dueDate: Date(2001-01-01T00:00:00.000Z), │
1011+
│ notes: "Milk, Eggs, Apples", │ isCompleted: false, │
1012+
│ priority: nil, │ isFlagged: false, │
1013+
│ remindersListID: 1, │ notes: "Milk, Eggs, Apples", │
1014+
│ title: "Groceries" │ priority: nil, │
1015+
│ ) │ remindersListID: 1, │
1016+
│ │ title: "Groceries"
1017+
│ │ ) │
1018+
│ │ ) │
1019+
│ │ ] │
1020+
└────────────────────────────────────────────┴────────────────────────────────────────────────┘
1021+
"""
1022+
}
9871023
}
9881024

9891025
@Test func `case`() {

0 commit comments

Comments
 (0)