Skip to content

Commit 1be6e0b

Browse files
committed
Support tuple operations with Table records
1 parent 4b95802 commit 1be6e0b

24 files changed

+252
-203
lines changed

Sources/StructuredQueriesCore/Operators.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extension QueryExpression where QueryValue: QueryBindable {
1+
extension QueryExpression where QueryValue: _OptionalPromotable {
22
/// A predicate expression indicating whether two query expressions are equal.
33
///
44
/// ```swift
@@ -241,7 +241,7 @@ public func != <QueryValue: QueryBindable>(
241241
SQLQueryExpression(lhs).isNot(rhs)
242242
}
243243

244-
extension QueryExpression where QueryValue: QueryBindable {
244+
extension QueryExpression where QueryValue: _OptionalPromotable {
245245
/// Returns a predicate expression indicating whether the value of the first expression is less
246246
/// than that of the second expression.
247247
///
@@ -697,7 +697,7 @@ extension QueryExpression where QueryValue == String {
697697
/// - Parameter collation: A collating sequence name.
698698
/// - Returns: An expression that is compared using the given collating sequence.
699699
public func collate(_ collation: Collation) -> some QueryExpression<QueryValue> {
700-
BinaryOperator(lhs: self, operator: "COLLATE", rhs: collation)
700+
SQLQueryExpression("\(self) COLLATE \(collation)")
701701
}
702702

703703
/// A predicate expression from this string expression matched against another _via_ the `GLOB`
@@ -850,11 +850,7 @@ extension QueryExpression where QueryValue: QueryBindable {
850850
_ lowerBound: some QueryExpression<QueryValue>,
851851
and upperBound: some QueryExpression<QueryValue>
852852
) -> some QueryExpression<Bool> {
853-
BinaryOperator(
854-
lhs: self,
855-
operator: "BETWEEN",
856-
rhs: SQLQueryExpression("\(lowerBound) AND \(upperBound)")
857-
)
853+
SQLQueryExpression("\(self) BETWEEN \(lowerBound) AND \(upperBound)")
858854
}
859855
}
860856

@@ -952,7 +948,7 @@ struct BinaryOperator<QueryValue>: QueryExpression {
952948
}
953949

954950
var queryFragment: QueryFragment {
955-
"(\(lhs) \(`operator`) \(rhs))"
951+
"(\(lhs)) \(`operator`) (\(rhs))"
956952
}
957953
}
958954

@@ -985,6 +981,6 @@ where S.Element: QueryExpression, S.Element.QueryValue: QueryBindable {
985981
typealias QueryValue = S
986982
let queryFragment: QueryFragment
987983
init(elements: S) {
988-
queryFragment = "(\(elements.map(\.queryFragment).joined(separator: ", ")))"
984+
queryFragment = "\(elements.map(\.queryFragment).joined(separator: ", "))"
989985
}
990986
}

Sources/StructuredQueriesCore/Table.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ extension Table {
136136
func open<Root, Value>(_ column: some TableColumnExpression<Root, Value>) -> QueryFragment {
137137
Value(queryOutput: (self as! Root)[keyPath: column.keyPath]).queryFragment
138138
}
139-
return "(\(TableColumns.allColumns.map { open($0) }.joined(separator: ", ")))"
139+
return TableColumns.allColumns.map { open($0) }.joined(separator: ", ")
140140
}
141141
}
142142

Tests/StructuredQueriesTests/AggregateFunctionsTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ extension SnapshotTests {
269269
.order(by: \.title)
270270
) {
271271
"""
272-
SELECT group_concat(iif((length("tags"."title") > 5), "tags"."title", NULL))
272+
SELECT group_concat(iif((length("tags"."title")) > (5), "tags"."title", NULL))
273273
FROM "tags"
274274
ORDER BY "tags"."title"
275275
"""
@@ -290,7 +290,7 @@ extension SnapshotTests {
290290
.order(by: \.title)
291291
) {
292292
"""
293-
SELECT group_concat(CASE WHEN (length("tags"."title") > 5) THEN "tags"."title" END)
293+
SELECT group_concat(CASE WHEN (length("tags"."title")) > (5) THEN "tags"."title" END)
294294
FROM "tags"
295295
ORDER BY "tags"."title"
296296
"""
@@ -334,7 +334,7 @@ extension SnapshotTests {
334334

335335
assertInlineSnapshot(of: (User.columns.name + "!").groupConcat(", "), as: .sql) {
336336
"""
337-
group_concat(("users"."name" || '!'), ', ')
337+
group_concat(("users"."name") || ('!'), ', ')
338338
"""
339339
}
340340

@@ -352,7 +352,7 @@ extension SnapshotTests {
352352
}
353353
assertQuery(Tag.select { ($0.title + "!").groupConcat(", ") }) {
354354
"""
355-
SELECT group_concat(("tags"."title" || '!'), ', ')
355+
SELECT group_concat(("tags"."title") || ('!'), ', ')
356356
FROM "tags"
357357
"""
358358
} results: {

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/CommonTableExpressionTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ extension SnapshotTests {
7979
)
8080
SELECT "incompleteReminders"."isFlagged", "incompleteReminders"."title"
8181
FROM "incompleteReminders"
82-
WHERE (("incompleteReminders"."title" COLLATE "NOCASE") LIKE '%groceries%')
82+
WHERE ("incompleteReminders"."title" COLLATE "NOCASE" LIKE '%groceries%')
8383
"""
8484
} results: {
8585
"""
@@ -121,7 +121,7 @@ extension SnapshotTests {
121121
("remindersListID", "title", "isFlagged", "isCompleted")
122122
SELECT "reminders"."remindersListID", "incompleteReminders"."title", NOT ("incompleteReminders"."isFlagged"), 1
123123
FROM "incompleteReminders"
124-
JOIN "reminders" ON ("incompleteReminders"."title" = "reminders"."title")
124+
JOIN "reminders" ON ("incompleteReminders"."title") = ("reminders"."title")
125125
LIMIT 1
126126
RETURNING "id", "title"
127127
"""
@@ -155,7 +155,7 @@ extension SnapshotTests {
155155
)
156156
UPDATE "reminders"
157157
SET "title" = upper("reminders"."title")
158-
WHERE ("reminders"."title" IN (SELECT "incompleteReminders"."title"
158+
WHERE ("reminders"."title") IN ((SELECT "incompleteReminders"."title"
159159
FROM "incompleteReminders"))
160160
RETURNING "title"
161161
"""
@@ -194,7 +194,7 @@ extension SnapshotTests {
194194
WHERE NOT ("reminders"."isCompleted")
195195
)
196196
DELETE FROM "reminders"
197-
WHERE ("reminders"."title" IN (SELECT "incompleteReminders"."title"
197+
WHERE ("reminders"."title") IN ((SELECT "incompleteReminders"."title"
198198
FROM "incompleteReminders"))
199199
RETURNING "reminders"."title"
200200
"""
@@ -309,7 +309,7 @@ extension SnapshotTests {
309309
WITH "counts" AS (
310310
SELECT 1 AS "value"
311311
UNION
312-
SELECT ("counts"."value" + 1) AS "value"
312+
SELECT ("counts"."value") + (1) AS "value"
313313
FROM "counts"
314314
)
315315
SELECT "counts"."value"
@@ -348,7 +348,7 @@ extension SnapshotTests {
348348
)
349349
SELECT "incompleteReminders"."isFlagged"
350350
FROM "incompleteReminders"
351-
WHERE (("incompleteReminders"."title" COLLATE "NOCASE") LIKE '%groceries%')
351+
WHERE ("incompleteReminders"."title" COLLATE "NOCASE" LIKE '%groceries%')
352352
"""
353353
} results: {
354354
"""
@@ -397,7 +397,7 @@ extension SnapshotTests {
397397
UNION ALL
398398
SELECT "employees"."id" AS "id", "employees"."height" AS "height", "employees"."name" AS "name"
399399
FROM "employees"
400-
JOIN "employeeReports" ON ("employees"."bossID" = "employeeReports"."id")
400+
JOIN "employeeReports" ON ("employees"."bossID") = ("employeeReports"."id")
401401
)
402402
SELECT avg("employeeReports"."height")
403403
FROM "employeeReports"
@@ -466,7 +466,7 @@ extension SnapshotTests {
466466
WITH "fibonaccis" AS (
467467
SELECT 1 AS "n", 0 AS "prevFib", 1 AS "fib"
468468
UNION
469-
SELECT ("fibonaccis"."n" + 1) AS "n", "fibonaccis"."fib" AS "prevFib", ("fibonaccis"."prevFib" + "fibonaccis"."fib") AS "fib"
469+
SELECT ("fibonaccis"."n") + (1) AS "n", "fibonaccis"."fib" AS "prevFib", ("fibonaccis"."prevFib") + ("fibonaccis"."fib") AS "fib"
470470
FROM "fibonaccis"
471471
)
472472
SELECT "fibonaccis"."fib"
@@ -511,10 +511,10 @@ extension SnapshotTests {
511511
WITH "fibonaccis" AS (
512512
SELECT 1 AS "n", 0 AS "prevFib", 1 AS "fib"
513513
UNION
514-
SELECT ("fibonaccis"."n" + 1) AS "n", "fibonaccis"."fib" AS "prevFib", ("fibonaccis"."prevFib" + "fibonaccis"."fib") AS "fib"
514+
SELECT ("fibonaccis"."n") + (1) AS "n", "fibonaccis"."fib" AS "prevFib", ("fibonaccis"."prevFib") + ("fibonaccis"."fib") AS "fib"
515515
FROM "fibonaccis"
516516
)
517-
SELECT (CAST("fibonaccis"."fib" AS REAL) / CAST("fibonaccis"."prevFib" AS REAL))
517+
SELECT (CAST("fibonaccis"."fib" AS REAL)) / (CAST("fibonaccis"."prevFib" AS REAL))
518518
FROM "fibonaccis"
519519
LIMIT 1 OFFSET 30
520520
"""

Tests/StructuredQueriesTests/DatabaseFunctionTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ extension SnapshotTests {
288288
.select { $joinTags($2.jsonGroupArray()) }
289289
) {
290290
"""
291-
SELECT "joinTags"(json_group_array(CASE WHEN ("tags"."rowid" IS NOT NULL) THEN json_object('id', json_quote("tags"."id"), 'title', json_quote("tags"."title")) END) FILTER (WHERE ("tags"."id" IS NOT NULL)))
291+
SELECT "joinTags"(json_group_array(CASE WHEN ("tags"."rowid") IS NOT (NULL) THEN json_object('id', json_quote("tags"."id"), 'title', json_quote("tags"."title")) END) FILTER (WHERE ("tags"."id") IS NOT (NULL)))
292292
FROM "reminders"
293-
LEFT JOIN "remindersTags" ON ("reminders"."id" = "remindersTags"."reminderID")
294-
LEFT JOIN "tags" ON ("remindersTags"."tagID" = "tags"."id")
293+
LEFT JOIN "remindersTags" ON ("reminders"."id") = ("remindersTags"."reminderID")
294+
LEFT JOIN "tags" ON ("remindersTags"."tagID") = ("tags"."id")
295295
GROUP BY "reminders"."id"
296296
"""
297297
} results: {

Tests/StructuredQueriesTests/DeleteTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension SnapshotTests {
4646
assertQuery(Reminder.delete().where { $0.id == 1 }.returning(\.self)) {
4747
"""
4848
DELETE FROM "reminders"
49-
WHERE ("reminders"."id" = 1)
49+
WHERE ("reminders"."id") = (1)
5050
RETURNING "id", "assignedUserID", "dueDate", "isCompleted", "isFlagged", "notes", "priority", "remindersListID", "title", "updatedAt"
5151
"""
5252
} results: {
@@ -85,7 +85,7 @@ extension SnapshotTests {
8585
assertQuery(Reminder.delete(Reminder(id: 1, remindersListID: 1))) {
8686
"""
8787
DELETE FROM "reminders"
88-
WHERE ("reminders"."id" = 1)
88+
WHERE ("reminders"."id") = (1)
8989
"""
9090
}
9191
assertQuery(Reminder.count()) {
@@ -135,7 +135,7 @@ extension SnapshotTests {
135135
) {
136136
"""
137137
DELETE FROM "remindersLists" AS "rs"
138-
WHERE ("rs"."id" = 1)
138+
WHERE ("rs"."id") = (1)
139139
RETURNING "id", "color", "title", "position"
140140
"""
141141
} results: {

Tests/StructuredQueriesTests/EphemeralTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension SnapshotTests {
1212
as: .sql
1313
) {
1414
"""
15-
SELECT (("testTables"."firstName" || ', ') || "testTables"."lastName")
15+
SELECT (("testTables"."firstName") || (', ')) || ("testTables"."lastName")
1616
FROM "testTables"
1717
"""
1818
}

Tests/StructuredQueriesTests/FTSTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ extension SnapshotTests {
152152
SELECT highlight("reminderTexts", (SELECT "cid" FROM pragma_table_info('reminderTexts') WHERE "name" = 'tags'),
153153
'**', '**')
154154
FROM "reminders"
155-
LEFT JOIN "reminderTexts" ON ("reminders"."rowid" = "reminderTexts"."rowid")
155+
LEFT JOIN "reminderTexts" ON ("reminders"."rowid") = ("reminderTexts"."rowid")
156156
ORDER BY bm25("reminderTexts")
157157
"""
158158
} results: {
@@ -185,7 +185,7 @@ extension SnapshotTests {
185185
SELECT highlight("reminderTexts", (SELECT "cid" FROM pragma_table_info('reminderTexts') WHERE "name" = 'tags'),
186186
'**', '**')
187187
FROM "reminders"
188-
LEFT JOIN "reminderTexts" AS "rTs" ON ("reminders"."rowid" = "rTs"."rowid")
188+
LEFT JOIN "reminderTexts" AS "rTs" ON ("reminders"."rowid") = ("rTs"."rowid")
189189
ORDER BY bm25("reminderTexts")
190190
"""
191191
} results: {

Tests/StructuredQueriesTests/InsertTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension SnapshotTests {
2424
("remindersListID", "title", "isCompleted", "dueDate", "priority")
2525
VALUES
2626
(1, 'Groceries', 1, '2001-01-01 00:00:00.000', 3), (2, 'Haircut', 0, '1970-01-01 00:00:00.000', 1), (3, 'Schedule doctor appointment', 0, NULL, 2)
27-
ON CONFLICT DO UPDATE SET "title" = ("reminders"."title" || ' Copy')
27+
ON CONFLICT DO UPDATE SET "title" = ("reminders"."title") || (' Copy')
2828
RETURNING "id"
2929
"""
3030
} results: {
@@ -301,7 +301,7 @@ extension SnapshotTests {
301301
"""
302302
SELECT "reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title", "reminders"."updatedAt"
303303
FROM "reminders"
304-
WHERE ("reminders"."id" = 1)
304+
WHERE ("reminders"."id") = (1)
305305
"""
306306
} results: {
307307
"""

0 commit comments

Comments
 (0)