Skip to content

Commit 7b81eb4

Browse files
committed
wip
1 parent 6ade611 commit 7b81eb4

File tree

9 files changed

+259
-248
lines changed

9 files changed

+259
-248
lines changed

Sources/StructuredQueriesCore/Documentation.docc/Articles/Triggers.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ triggers in a type-safe and schema-safe fashion.
1111
### Trigger basics
1212

1313
One of the most common use cases for a trigger is refreshing an "updatedAt" timestamp on a row when
14-
it is updated in the database. One can create such a trigger SQL statement using the
14+
it is updated in the database. One can create such a trigger SQL statement using the
1515
``Table/createTemporaryTrigger(_:ifNotExists:after:fileID:line:column:)`` static method:
1616

1717
@Row {
@@ -20,8 +20,8 @@ it is updated in the database. One can create such a trigger SQL statement using
2020
Reminder.createTemporaryTrigger(
2121
"reminders_updatedAt",
2222
after: .update { _, _ in
23-
Reminder.update {
24-
$0.updatedAt = #sql("datetime('subsec')")
23+
Reminder.update {
24+
$0.updatedAt = #sql("datetime('subsec')")
2525
}
2626
}
2727
)
@@ -40,7 +40,7 @@ it is updated in the database. One can create such a trigger SQL statement using
4040
}
4141
}
4242

43-
This will make it so that anytime a reminder is updated in the database its `updatedAt` will be
43+
This will make it so that anytime a reminder is updated in the database its `updatedAt` will be
4444
refreshed with the current time immediately.
4545

4646
This pattern of updating a timestamp when a row changes is so common that the library comes with
@@ -52,7 +52,7 @@ a specialized tool just for that kind of trigger,
5252
```swift
5353
Reminder
5454
.createTemporaryTrigger(
55-
afterUpdateTouch: {
55+
afterUpdateTouch: {
5656
$0.updatedAt = datetime('subsec')
5757
}
5858
)
@@ -72,7 +72,7 @@ a specialized tool just for that kind of trigger,
7272
}
7373

7474
And further, the pattern of specifically updating a _timestamp_ column is so common that the library
75-
comes with another specialized too just for that kind of trigger,
75+
comes with another specialized too just for that kind of trigger,
7676
``Table/createTemporaryTrigger(_:ifNotExists:afterUpdateTouch:fileID:line:column:)``:
7777

7878

@@ -127,8 +127,8 @@ last list was deleted:
127127
"nonEmptyRemindersLists",
128128
after: .delete { _ in
129129
RemindersList
130-
.insert {
131-
RemindersList.Draft(title: "Personal")
130+
.insert {
131+
RemindersList.Draft(title: "Personal")
132132
}
133133
} when: { _ in
134134
!RemindersList.all.exists()

Sources/StructuredQueriesCore/Triggers.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ extension Table {
7878
)
7979
}
8080

81-
8281
// TODO: createTemporaryTrigger(afterUpdateTouch: \.updatedAt)
8382
// TODO: createTemporaryTrigger(afterUpdate: { $0... }, touch: { $0... = })
8483
// TODO: createTemporaryTrigger(afterUpdate: \.self, touch: \.updatedAt)
@@ -97,7 +96,7 @@ public struct TemporaryTrigger<On: Table>: Statement {
9796

9897
public typealias Old = TableAlias<On, _Old>.TableColumns
9998
public typealias New = TableAlias<On, _New>.TableColumns
100-
99+
101100
/// An `AFTER INSERT` trigger operation.
102101
///
103102
/// - Parameters:

Tests/StructuredQueriesTests/DeleteTests.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,18 @@ extension SnapshotTests {
135135
"""
136136
DELETE FROM "remindersLists" AS "rs"
137137
WHERE ("rs"."id" = 1)
138-
RETURNING "id", "color", "title"
138+
RETURNING "id", "color", "title", "position"
139139
"""
140140
} results: {
141141
"""
142-
┌─────────────────────┐
143-
│ RemindersList( │
144-
│ id: 1, │
145-
│ color: 4889071, │
146-
│ title: "Personal"
147-
│ ) │
148-
└─────────────────────┘
142+
┌──────────────────────┐
143+
│ RemindersList( │
144+
│ id: 1, │
145+
│ color: 4889071, │
146+
│ title: "Personal", │
147+
│ position: 0 │
148+
│ ) │
149+
└──────────────────────┘
149150
"""
150151
}
151152
}

Tests/StructuredQueriesTests/InsertTests.swift

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,12 @@ extension SnapshotTests {
411411
) {
412412
"""
413413
INSERT INTO "remindersLists"
414-
("id", "color", "title")
414+
("id", "color", "title", "position")
415415
VALUES
416-
(NULL, 4889071, 'Personal')
416+
(NULL, 4889071, 'Personal', 0)
417417
ON CONFLICT ("id")
418-
DO UPDATE SET "color" = "excluded"."color", "title" = "excluded"."title"
419-
RETURNING "id", "color", "title"
418+
DO UPDATE SET "color" = "excluded"."color", "title" = "excluded"."title", "position" = "excluded"."position"
419+
RETURNING "id", "color", "title", "position"
420420
"""
421421
} results: {
422422
"""
@@ -437,22 +437,23 @@ extension SnapshotTests {
437437
) {
438438
"""
439439
INSERT INTO "remindersLists"
440-
("id", "color", "title")
440+
("id", "color", "title", "position")
441441
VALUES
442-
(NULL, 4889071, 'Personal')
442+
(NULL, 4889071, 'Personal', 0)
443443
ON CONFLICT ("title")
444444
DO UPDATE SET "color" = 65280
445-
RETURNING "id", "color", "title"
445+
RETURNING "id", "color", "title", "position"
446446
"""
447447
} results: {
448448
"""
449-
┌─────────────────────┐
450-
│ RemindersList( │
451-
│ id: 1, │
452-
│ color: 65280, │
453-
│ title: "Personal"
454-
│ ) │
455-
└─────────────────────┘
449+
┌──────────────────────┐
450+
│ RemindersList( │
451+
│ id: 1, │
452+
│ color: 65280, │
453+
│ title: "Personal", │
454+
│ position: 0 │
455+
│ ) │
456+
└──────────────────────┘
456457
"""
457458
}
458459
}
@@ -525,17 +526,18 @@ extension SnapshotTests {
525526
("title")
526527
VALUES
527528
('cruise')
528-
RETURNING "id", "color", "title"
529+
RETURNING "id", "color", "title", "position"
529530
"""
530531
} results: {
531532
"""
532-
┌───────────────────┐
533-
│ RemindersList( │
534-
│ id: 4, │
535-
│ color: 4889071, │
536-
│ title: "cruise"
537-
│ ) │
538-
└───────────────────┘
533+
┌────────────────────┐
534+
│ RemindersList( │
535+
│ id: 4, │
536+
│ color: 4889071, │
537+
│ title: "cruise", │
538+
│ position: 0 │
539+
│ ) │
540+
└────────────────────┘
539541
"""
540542
}
541543
}

Tests/StructuredQueriesTests/JSONFunctionsTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ extension SnapshotTests {
224224
.limit(1)
225225
) {
226226
"""
227-
SELECT "remindersLists"."id", "remindersLists"."color", "remindersLists"."title" AS "remindersList", json_group_array(CASE WHEN ("reminders"."id" IS NOT NULL) THEN json_object('id', json_quote("reminders"."id"), 'assignedUserID', json_quote("reminders"."assignedUserID"), 'dueDate', json_quote("reminders"."dueDate"), 'isCompleted', json(CASE "reminders"."isCompleted" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'isFlagged', json(CASE "reminders"."isFlagged" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'notes', json_quote("reminders"."notes"), 'priority', json_quote("reminders"."priority"), 'remindersListID', json_quote("reminders"."remindersListID"), 'title', json_quote("reminders"."title")) END) AS "reminders"
227+
SELECT "remindersLists"."id", "remindersLists"."color", "remindersLists"."title", "remindersLists"."position" AS "remindersList", json_group_array(CASE WHEN ("reminders"."id" IS NOT NULL) THEN json_object('id', json_quote("reminders"."id"), 'assignedUserID', json_quote("reminders"."assignedUserID"), 'dueDate', json_quote("reminders"."dueDate"), 'isCompleted', json(CASE "reminders"."isCompleted" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'isFlagged', json(CASE "reminders"."isFlagged" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'notes', json_quote("reminders"."notes"), 'priority', json_quote("reminders"."priority"), 'remindersListID', json_quote("reminders"."remindersListID"), 'title', json_quote("reminders"."title")) END) AS "reminders"
228228
FROM "remindersLists"
229229
LEFT JOIN "reminders" ON ("remindersLists"."id" = "reminders"."remindersListID")
230230
WHERE NOT ("reminders"."isCompleted")
@@ -238,7 +238,8 @@ extension SnapshotTests {
238238
│ remindersList: RemindersList( │
239239
│ id: 1, │
240240
│ color: 4889071, │
241-
│ title: "Personal"
241+
│ title: "Personal", │
242+
│ position: 0 │
242243
│ ), │
243244
│ reminders: [ │
244245
│ [0]: Reminder( │

Tests/StructuredQueriesTests/LiveTests.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,32 +184,35 @@ extension SnapshotTests {
184184
.select { ($0, $1.id.count()) }
185185
) {
186186
"""
187-
SELECT "remindersLists"."id", "remindersLists"."color", "remindersLists"."title", count("reminders"."id")
187+
SELECT "remindersLists"."id", "remindersLists"."color", "remindersLists"."title", "remindersLists"."position", count("reminders"."id")
188188
FROM "remindersLists"
189189
JOIN "reminders" ON ("remindersLists"."id" = "reminders"."remindersListID")
190190
GROUP BY "remindersLists"."id"
191191
"""
192192
} results: {
193193
"""
194-
┌─────────────────────┬───┐
195-
│ RemindersList( │ 5 │
196-
│ id: 1, │ │
197-
│ color: 4889071, │ │
198-
│ title: "Personal" │ │
199-
│ ) │ │
200-
├─────────────────────┼───┤
201-
│ RemindersList( │ 3 │
202-
│ id: 2, │ │
203-
│ color: 15567157, │ │
204-
│ title: "Family" │ │
205-
│ ) │ │
206-
├─────────────────────┼───┤
207-
│ RemindersList( │ 2 │
208-
│ id: 3, │ │
209-
│ color: 11689427, │ │
210-
│ title: "Business" │ │
211-
│ ) │ │
212-
└─────────────────────┴───┘
194+
┌──────────────────────┬───┐
195+
│ RemindersList( │ 5 │
196+
│ id: 1, │ │
197+
│ color: 4889071, │ │
198+
│ title: "Personal", │ │
199+
│ position: 0 │ │
200+
│ ) │ │
201+
├──────────────────────┼───┤
202+
│ RemindersList( │ 3 │
203+
│ id: 2, │ │
204+
│ color: 15567157, │ │
205+
│ title: "Family", │ │
206+
│ position: 0 │ │
207+
│ ) │ │
208+
├──────────────────────┼───┤
209+
│ RemindersList( │ 2 │
210+
│ id: 3, │ │
211+
│ color: 11689427, │ │
212+
│ title: "Business", │ │
213+
│ position: 0 │ │
214+
│ ) │ │
215+
└──────────────────────┴───┘
213216
"""
214217
}
215218
}

Tests/StructuredQueriesTests/SQLMacroTests.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,27 @@ extension SnapshotTests {
6060
"""
6161
SELECT
6262
"reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title",
63-
"remindersLists"."id", "remindersLists"."color", "remindersLists"."title"
63+
"remindersLists"."id", "remindersLists"."color", "remindersLists"."title", "remindersLists"."position"
6464
FROM "reminders"
6565
JOIN "remindersLists"
6666
ON "reminders"."remindersListID" = "remindersLists"."id"
6767
LIMIT 1
6868
"""
6969
} results: {
7070
"""
71-
┌────────────────────────────────────────────┬─────────────────────┐
72-
│ Reminder( │ RemindersList( │
73-
│ id: 1, │ id: 1, │
74-
│ assignedUserID: 1, │ color: 4889071, │
75-
│ dueDate: Date(2001-01-01T00:00:00.000Z), │ title: "Personal"
76-
│ isCompleted: false, │ )
77-
│ isFlagged: false, │ │
78-
│ notes: "Milk, Eggs, Apples", │ │
79-
│ priority: nil, │ │
80-
│ remindersListID: 1, │ │
81-
│ title: "Groceries" │ │
82-
│ ) │ │
83-
└────────────────────────────────────────────┴─────────────────────┘
71+
┌────────────────────────────────────────────┬─────────────────────
72+
│ Reminder( │ RemindersList(
73+
│ id: 1, │ id: 1,
74+
│ assignedUserID: 1, │ color: 4889071,
75+
│ dueDate: Date(2001-01-01T00:00:00.000Z), │ title: "Personal",
76+
│ isCompleted: false, │ position: 0
77+
│ isFlagged: false, │ )
78+
│ notes: "Milk, Eggs, Apples", │
79+
│ priority: nil, │
80+
│ remindersListID: 1, │
81+
│ title: "Groceries"
82+
│ ) │
83+
└────────────────────────────────────────────┴─────────────────────
8484
"""
8585
}
8686
}
@@ -99,7 +99,7 @@ extension SnapshotTests {
9999
)
100100
) {
101101
"""
102-
SELECT "reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title", "remindersLists"."id", "remindersLists"."color", "remindersLists"."title"
102+
SELECT "reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title", "remindersLists"."id", "remindersLists"."color", "remindersLists"."title", "remindersLists"."position"
103103
FROM "reminders" JOIN "remindersLists" ON "reminders"."remindersListID" = "remindersLists"."id" LIMIT 1
104104
"""
105105
} results: {
@@ -120,7 +120,8 @@ extension SnapshotTests {
120120
│ list: RemindersList( │
121121
│ id: 1, │
122122
│ color: 4889071, │
123-
│ title: "Personal"
123+
│ title: "Personal", │
124+
│ position: 0 │
124125
│ ) │
125126
│ ) │
126127
└──────────────────────────────────────────────┘

0 commit comments

Comments
 (0)