Skip to content

Commit da05323

Browse files
authored
Find records through query output (pointfreeco#35)
Improves representable values like UUID: ```diff -Reminder.find(UUID.LowercasedRepresentation(queryOutput: reminderID)) +Reminder.find(reminderID) ```
1 parent 6a13b80 commit da05323

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

Sources/StructuredQueriesCore/PrimaryKeyed.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ extension PrimaryKeyedTable {
4949
///
5050
/// - Parameter primaryKey: A primary key identifying a table row.
5151
/// - Returns: A `WHERE` clause.
52-
public static func find(_ primaryKey: TableColumns.PrimaryKey) -> Where<Self> {
53-
Self.where { $0.primaryKey.eq(primaryKey) }
52+
public static func find(_ primaryKey: TableColumns.PrimaryKey.QueryOutput) -> Where<Self> {
53+
Self.where { $0.primaryKey.eq(TableColumns.PrimaryKey(queryOutput: primaryKey)) }
5454
}
5555
}
5656

@@ -59,8 +59,8 @@ extension Where where From: PrimaryKeyedTable {
5959
///
6060
/// - Parameter primaryKey: A primary key.
6161
/// - Returns: A where clause with the added primary key.
62-
public func find(_ primaryKey: From.TableColumns.PrimaryKey) -> Self {
63-
self.where { $0.primaryKey.eq(primaryKey) }
62+
public func find(_ primaryKey: From.TableColumns.PrimaryKey.QueryOutput) -> Self {
63+
self.where { $0.primaryKey.eq(From.TableColumns.PrimaryKey(queryOutput: primaryKey)) }
6464
}
6565
}
6666

@@ -69,8 +69,8 @@ extension Update where From: PrimaryKeyedTable {
6969
///
7070
/// - Parameter primaryKey: A primary key identifying a table row.
7171
/// - Returns: An update statement filtered by the given key.
72-
public func find(_ primaryKey: From.TableColumns.PrimaryKey) -> Self {
73-
self.where { $0.primaryKey.eq(primaryKey) }
72+
public func find(_ primaryKey: From.TableColumns.PrimaryKey.QueryOutput) -> Self {
73+
self.where { $0.primaryKey.eq(From.TableColumns.PrimaryKey(queryOutput: primaryKey)) }
7474
}
7575
}
7676

@@ -79,8 +79,8 @@ extension Delete where From: PrimaryKeyedTable {
7979
///
8080
/// - Parameter primaryKey: A primary key identifying a table row.
8181
/// - Returns: A delete statement filtered by the given key.
82-
public func find(_ primaryKey: From.TableColumns.PrimaryKey) -> Self {
83-
self.where { $0.primaryKey.eq(primaryKey) }
82+
public func find(_ primaryKey: From.TableColumns.PrimaryKey.QueryOutput) -> Self {
83+
self.where { $0.primaryKey.eq(From.TableColumns.PrimaryKey(queryOutput: primaryKey)) }
8484
}
8585
}
8686

@@ -89,7 +89,7 @@ extension Select where From: PrimaryKeyedTable {
8989
///
9090
/// - Parameter primaryKey: A primary key identifying a table row.
9191
/// - Returns: A select statement filtered by the given key.
92-
public func find(_ primaryKey: From.TableColumns.PrimaryKey) -> Self {
92+
public func find(_ primaryKey: From.TableColumns.PrimaryKey.QueryOutput) -> Self {
9393
self.and(From.find(primaryKey))
9494
}
9595
}

Tests/StructuredQueriesTests/PrimaryKeyedTableTests.swift

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import Dependencies
12
import Foundation
23
import InlineSnapshotTesting
34
import StructuredQueries
5+
import StructuredQueriesSQLite
46
import Testing
57

68
extension SnapshotTests {
79
struct PrimaryKeyedTableTests {
10+
@Dependency(\.defaultDatabase) var database
11+
812
@Test func count() {
913
assertQuery(Reminder.select { $0.count() }) {
1014
"""
@@ -150,9 +154,41 @@ extension SnapshotTests {
150154
}
151155
}
152156

157+
@Test func uuid() throws {
158+
try database.execute(
159+
#sql(
160+
"""
161+
CREATE TABLE "rows" (id TEXT PRIMARY KEY NOT NULL)
162+
"""
163+
)
164+
)
165+
try database.execute(Row.insert(Row(id: UUID(1))))
166+
assertQuery(
167+
Row.find(UUID(1))
168+
) {
169+
"""
170+
SELECT "rows"."id"
171+
FROM "rows"
172+
WHERE ("rows"."id" = '00000000-0000-0000-0000-000000000001')
173+
"""
174+
} results: {
175+
"""
176+
┌─────────────────────────────────────────────────────┐
177+
│ Row(id: UUID(00000000-0000-0000-0000-000000000001)) │
178+
└─────────────────────────────────────────────────────┘
179+
"""
180+
}
181+
}
182+
153183
@Test func joinWith() {
154-
//RemindersList.join(Reminder.all, with: \.remindersListID)
155-
//Reminder.join(RemindersList.all, with: \.remindersListID)
184+
// RemindersList.join(Reminder.all, with: \.remindersListID)
185+
// Reminder.join(RemindersList.all, with: \.remindersListID)
156186
}
157187
}
158188
}
189+
190+
@Table
191+
private struct Row {
192+
@Column(as: UUID.LowercasedRepresentation.self)
193+
let id: UUID
194+
}

0 commit comments

Comments
 (0)