Skip to content

Commit 3905f40

Browse files
committed
wip
1 parent 1974bb0 commit 3905f40

File tree

11 files changed

+68
-49
lines changed

11 files changed

+68
-49
lines changed

Sources/StructuredQueriesCore/Operators.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ extension SQLQueryExpression<String> {
816816
}
817817
}
818818

819-
extension QueryExpression where QueryValue: QueryBindable {
819+
extension QueryExpression where QueryValue: QueryExpression {
820820
/// Returns a predicate expression indicating whether the expression is in a sequence.
821821
///
822822
/// - Parameter expression: A sequence of expressions.
@@ -972,15 +972,15 @@ private struct LikeOperator<
972972
}
973973
}
974974

975-
extension Sequence where Element: QueryExpression, Element.QueryValue: QueryBindable {
975+
extension Sequence where Element: QueryExpression, Element.QueryValue: QueryExpression {
976976
fileprivate typealias Expression = _SequenceExpression<Self>
977977
}
978978

979979
private struct _SequenceExpression<S: Sequence>: QueryExpression
980-
where S.Element: QueryExpression, S.Element.QueryValue: QueryBindable {
980+
where S.Element: QueryExpression, S.Element.QueryValue: QueryExpression {
981981
typealias QueryValue = S
982982
let queryFragment: QueryFragment
983983
init(elements: S) {
984-
queryFragment = "\(elements.map(\.queryFragment).joined(separator: ", "))"
984+
queryFragment = elements.map { "(\($0.queryFragment))" }.joined(separator: ", ")
985985
}
986986
}

Sources/StructuredQueriesCore/PrimaryKeyed.swift

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ extension PrimaryKeyedTableDefinition where PrimaryKeyColumn: TableColumnExpress
8282
}
8383
}
8484

85-
// TODO: Support composite keys.
86-
extension PrimaryKeyedTable where PrimaryKey: QueryBindable {
85+
extension PrimaryKeyedTable {
8786
/// A where clause filtered by a primary key.
8887
///
8988
/// - Parameter primaryKey: A primary key identifying a table row.
@@ -109,8 +108,7 @@ extension PrimaryKeyedTable where PrimaryKey: QueryBindable {
109108
}
110109
}
111110

112-
// TODO: Support composite keys.
113-
extension TableDraft where PrimaryTable.PrimaryKey: QueryBindable {
111+
extension TableDraft {
114112
/// A where clause filtered by a primary key.
115113
///
116114
/// - Parameter primaryKey: A primary key identifying a table row.
@@ -132,8 +130,7 @@ extension TableDraft where PrimaryTable.PrimaryKey: QueryBindable {
132130
}
133131
}
134132

135-
// TODO: Support composite keys.
136-
extension Where where From: PrimaryKeyedTable, From.PrimaryKey: QueryBindable {
133+
extension Where where From: PrimaryKeyedTable {
137134
/// Adds a primary key condition to a where clause.
138135
///
139136
/// - Parameter primaryKey: A primary key.
@@ -153,8 +150,7 @@ extension Where where From: PrimaryKeyedTable, From.PrimaryKey: QueryBindable {
153150
}
154151
}
155152

156-
// TODO: Support composite keys.
157-
extension Where where From: TableDraft, From.PrimaryTable.PrimaryKey: QueryBindable {
153+
extension Where where From: TableDraft {
158154
/// Adds a primary key condition to a where clause.
159155
///
160156
/// - Parameter primaryKey: A primary key.
@@ -176,8 +172,7 @@ extension Where where From: TableDraft, From.PrimaryTable.PrimaryKey: QueryBinda
176172
}
177173
}
178174

179-
// TODO: Support composite keys.
180-
extension Select where From: PrimaryKeyedTable, From.PrimaryKey: QueryBindable {
175+
extension Select where From: PrimaryKeyedTable {
181176
/// A select statement filtered by a primary key.
182177
///
183178
/// - Parameter primaryKey: A primary key identifying a table row.
@@ -197,8 +192,7 @@ extension Select where From: PrimaryKeyedTable, From.PrimaryKey: QueryBindable {
197192
}
198193
}
199194

200-
// TODO: Support composite keys.
201-
extension Select where From: TableDraft, From.PrimaryTable.PrimaryKey: QueryBindable {
195+
extension Select where From: TableDraft {
202196
/// A select statement filtered by a primary key.
203197
///
204198
/// - Parameter primaryKey: A primary key identifying a table row.
@@ -220,8 +214,7 @@ extension Select where From: TableDraft, From.PrimaryTable.PrimaryKey: QueryBind
220214
}
221215
}
222216

223-
// TODO: Support composite keys.
224-
extension Update where From: PrimaryKeyedTable, From.PrimaryKey: QueryBindable {
217+
extension Update where From: PrimaryKeyedTable {
225218
/// An update statement filtered by a primary key.
226219
///
227220
/// - Parameter primaryKey: A primary key identifying a table row.
@@ -241,8 +234,7 @@ extension Update where From: PrimaryKeyedTable, From.PrimaryKey: QueryBindable {
241234
}
242235
}
243236

244-
// TODO: Support composite keys.
245-
extension Update where From: TableDraft, From.PrimaryTable.PrimaryKey: QueryBindable {
237+
extension Update where From: TableDraft {
246238
/// An update statement filtered by a primary key.
247239
///
248240
/// - Parameter primaryKey: A primary key identifying a table row.
@@ -264,8 +256,7 @@ extension Update where From: TableDraft, From.PrimaryTable.PrimaryKey: QueryBind
264256
}
265257
}
266258

267-
// TODO: Support composite keys.
268-
extension Delete where From: PrimaryKeyedTable, From.PrimaryKey: QueryBindable {
259+
extension Delete where From: PrimaryKeyedTable {
269260
/// A delete statement filtered by a primary key.
270261
///
271262
/// - Parameter primaryKey: A primary key identifying a table row.
@@ -285,8 +276,7 @@ extension Delete where From: PrimaryKeyedTable, From.PrimaryKey: QueryBindable {
285276
}
286277
}
287278

288-
// TODO: Support composite keys.
289-
extension Delete where From: TableDraft, From.PrimaryTable.PrimaryKey: QueryBindable {
279+
extension Delete where From: TableDraft {
290280
/// A delete statement filtered by a primary key.
291281
///
292282
/// - Parameter primaryKey: A primary key identifying a table row.

Tests/StructuredQueriesMacrosTests/SelectionMacroTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ extension SnapshotTests {
270270
271271
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
272272
public typealias QueryValue = Row
273+
public typealias PrimaryKey = Int
273274
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int>("id", keyPath: \QueryValue.id)
274275
public let title = StructuredQueriesCore.TableColumn<QueryValue, Swift.String>("title", keyPath: \QueryValue.title, default: "")
275276
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, Int> {

Tests/StructuredQueriesMacrosTests/TableMacroTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ extension SnapshotTests {
8989
9090
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
9191
public typealias QueryValue = User
92+
public typealias PrimaryKey = Int
9293
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int>("id", keyPath: \QueryValue.id)
9394
public let email = StructuredQueriesCore.TableColumn<QueryValue, String?>("email", keyPath: \QueryValue.email)
9495
public let age = StructuredQueriesCore.TableColumn<QueryValue, Int>("age", keyPath: \QueryValue.age)
@@ -826,6 +827,7 @@ extension SnapshotTests {
826827
827828
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
828829
public typealias QueryValue = User
830+
public typealias PrimaryKey = Int
829831
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int>("id", keyPath: \QueryValue.id)
830832
public let name = StructuredQueriesCore.TableColumn<QueryValue, String>("name", keyPath: \QueryValue.name)
831833
public var generated: StructuredQueriesCore.GeneratedColumn<QueryValue, Int> {
@@ -1265,6 +1267,7 @@ extension SnapshotTests {
12651267
12661268
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
12671269
public typealias QueryValue = User
1270+
public typealias PrimaryKey = ID<User, UUID.BytesRepresentation>
12681271
public let id = StructuredQueriesCore.TableColumn<QueryValue, ID<User, UUID.BytesRepresentation>>("id", keyPath: \QueryValue.id)
12691272
public let referrerID = StructuredQueriesCore.TableColumn<QueryValue, ID<User, UUID.BytesRepresentation>?>("referrerID", keyPath: \QueryValue.referrerID)
12701273
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, ID<User, UUID.BytesRepresentation>> {
@@ -1453,6 +1456,7 @@ extension SnapshotTests {
14531456
14541457
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
14551458
public typealias QueryValue = SyncUp
1459+
public typealias PrimaryKey = Int
14561460
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int>("id", keyPath: \QueryValue.id)
14571461
public let name = StructuredQueriesCore.TableColumn<QueryValue, String>("name", keyPath: \QueryValue.name)
14581462
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, Int> {
@@ -1602,6 +1606,7 @@ extension SnapshotTests {
16021606
16031607
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
16041608
public typealias QueryValue = SyncUp
1609+
public typealias PrimaryKey = Int
16051610
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int>("id", keyPath: \QueryValue.id)
16061611
public let seconds = StructuredQueriesCore.TableColumn<QueryValue, <#Type#>>("seconds", keyPath: \QueryValue.seconds, default: 60 * 5)
16071612
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, Int> {
@@ -1730,6 +1735,7 @@ extension SnapshotTests {
17301735
17311736
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
17321737
public typealias QueryValue = RemindersList
1738+
public typealias PrimaryKey = Int
17331739
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int>("id", keyPath: \QueryValue.id)
17341740
public let color = StructuredQueriesCore.TableColumn<QueryValue, Color.HexRepresentation>("color", keyPath: \QueryValue.color, default: Color(red: 0x4a / 255, green: 0x99 / 255, blue: 0xef / 255))
17351741
public let name = StructuredQueriesCore.TableColumn<QueryValue, Swift.String>("name", keyPath: \QueryValue.name, default: "")
@@ -1947,6 +1953,7 @@ extension SnapshotTests {
19471953
19481954
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
19491955
public typealias QueryValue = Foo
1956+
public typealias PrimaryKey = Int
19501957
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int>("id", keyPath: \QueryValue.id)
19511958
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, Int> {
19521959
self.id
@@ -2168,6 +2175,7 @@ extension SnapshotTests {
21682175
21692176
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
21702177
public typealias QueryValue = Foo
2178+
public typealias PrimaryKey = Int
21712179
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int>("id", keyPath: \QueryValue.id)
21722180
public let name = StructuredQueriesCore.TableColumn<QueryValue, String>("name", keyPath: \QueryValue.name)
21732181
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, Int> {
@@ -2306,6 +2314,7 @@ extension SnapshotTests {
23062314
23072315
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
23082316
public typealias QueryValue = Reminder
2317+
public typealias PrimaryKey = Int
23092318
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int>("id", keyPath: \QueryValue.id)
23102319
public let title = StructuredQueriesCore.TableColumn<QueryValue, Swift.String>("title", keyPath: \QueryValue.title, default: "")
23112320
public let date = StructuredQueriesCore.TableColumn<QueryValue, Date.UnixTimeRepresentation?>("date", keyPath: \QueryValue.date)
@@ -2450,6 +2459,7 @@ extension SnapshotTests {
24502459
24512460
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
24522461
public typealias QueryValue = Reminder
2462+
public typealias PrimaryKey = UUID.BytesRepresentation
24532463
public let id = StructuredQueriesCore.TableColumn<QueryValue, UUID.BytesRepresentation>("id", keyPath: \QueryValue.id)
24542464
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, UUID.BytesRepresentation> {
24552465
self.id
@@ -2625,6 +2635,7 @@ extension SnapshotTests {
26252635
26262636
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
26272637
public typealias QueryValue = Reminder
2638+
public typealias PrimaryKey = Int?
26282639
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int?>("id", keyPath: \QueryValue.id)
26292640
public let title = StructuredQueriesCore.TableColumn<QueryValue, Swift.String>("title", keyPath: \QueryValue.title, default: "")
26302641
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, Int?> {
@@ -2748,6 +2759,7 @@ extension SnapshotTests {
27482759
27492760
public nonisolated struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
27502761
public typealias QueryValue = Row
2762+
public typealias PrimaryKey = UUID
27512763
public let id = StructuredQueriesCore.TableColumn<QueryValue, UUID>("id", keyPath: \QueryValue.id)
27522764
public let timestamps = StructuredQueriesCore.ColumnGroup<QueryValue, Timestamps>(keyPath: \QueryValue.timestamps)
27532765
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, UUID> {

Tests/StructuredQueriesTests/BindingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extension SnapshotTests {
7878
}
7979
) {
8080
"""
81-
SELECT ('00000000-0000-0000-0000-000000000000') IN ('00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000002')
81+
SELECT ('00000000-0000-0000-0000-000000000000') IN (('00000000-0000-0000-0000-000000000001'), ('00000000-0000-0000-0000-000000000002'))
8282
"""
8383
} results: {
8484
"""

Tests/StructuredQueriesTests/NestedTests.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,26 @@ extension SnapshotTests {
302302
└───────────────────────────────────────────────────────────┘
303303
"""
304304
}
305-
// TODO: Make work.
306-
// assertQuery(
307-
// Metadata.find(MetadataID(recordID: UUID(0), recordType: "reminders"))
308-
// )
305+
assertQuery(
306+
Metadata.find(MetadataID(recordID: UUID(0), recordType: "reminders"))
307+
) {
308+
"""
309+
SELECT "metadatas"."recordID", "metadatas"."recordType"
310+
FROM "metadatas"
311+
WHERE ("metadatas"."recordID", "metadatas"."recordType") IN (('00000000-0000-0000-0000-000000000000', 'reminders'))
312+
"""
313+
} results: {
314+
"""
315+
┌───────────────────────────────────────────────────────────┐
316+
│ Metadata( │
317+
│ id: MetadataID( │
318+
│ recordID: UUID(00000000-0000-0000-0000-000000000000), │
319+
│ recordType: "reminders"
320+
│ ) │
321+
│ ) │
322+
└───────────────────────────────────────────────────────────┘
323+
"""
324+
}
309325
}
310326
}
311327
}

Tests/StructuredQueriesTests/OperatorsTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ extension SnapshotTests {
374374
as: .sql
375375
) {
376376
"""
377-
("rows"."c") IN (1, 2, 3)
377+
("rows"."c") IN ((1), (2), (3))
378378
"""
379379
}
380380
assertInlineSnapshot(
@@ -391,7 +391,7 @@ extension SnapshotTests {
391391
as: .sql
392392
) {
393393
"""
394-
("rows"."c") IN (1, 2, 3)
394+
("rows"."c") IN ((1), (2), (3))
395395
"""
396396
}
397397
assertInlineSnapshot(
@@ -540,7 +540,7 @@ extension SnapshotTests {
540540
"""
541541
SELECT "reminders"."id"
542542
FROM "reminders"
543-
WHERE ("reminders"."id") IN (1, 2)
543+
WHERE ("reminders"."id") IN ((1), (2))
544544
"""
545545
} results: {
546546
"""

0 commit comments

Comments
 (0)