Skip to content

Commit c975361

Browse files
committed
wip
1 parent 8b0e6fd commit c975361

File tree

1 file changed

+23
-30
lines changed
  • Sources/StructuredQueriesCore/Documentation.docc/Articles

1 file changed

+23
-30
lines changed

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

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ it is updated in the database. One can create such a trigger SQL statement using
1818
@Column {
1919
```swift
2020
Reminder.createTemporaryTrigger(
21-
"reminders_updatedAt",
2221
after: .update { _, _ in
2322
Reminder.update {
2423
$0.updatedAt = #sql("datetime('subsec')")
@@ -29,7 +28,7 @@ it is updated in the database. One can create such a trigger SQL statement using
2928
}
3029
@Column {
3130
```sql
32-
CREATE TEMPORARY TRIGGER "reminders_updatedAt"
31+
CREATE TEMPORARY TRIGGER "after_update_on_reminders@…"
3332
AFTER UPDATE ON "reminders"
3433
FOR EACH ROW
3534
BEGIN
@@ -50,17 +49,16 @@ a specialized tool just for that kind of trigger,
5049
@Row {
5150
@Column {
5251
```swift
53-
Reminder
54-
.createTemporaryTrigger(
55-
afterUpdateTouch: {
56-
$0.updatedAt = datetime('subsec')
57-
}
58-
)
52+
Reminder.createTemporaryTrigger(
53+
afterUpdateTouch: {
54+
$0.updatedAt = datetime('subsec')
55+
}
56+
)
5957
```
6058
}
6159
@Column {
6260
```sql
63-
CREATE TEMPORARY TRIGGER "reminders_updatedAt"
61+
CREATE TEMPORARY TRIGGER "after_update_on_reminders@…"
6462
AFTER UPDATE ON "reminders"
6563
FOR EACH ROW
6664
BEGIN
@@ -79,15 +77,14 @@ comes with another specialized too just for that kind of trigger,
7977
@Row {
8078
@Column {
8179
```swift
82-
Reminder
83-
.createTemporaryTrigger(
84-
afterUpdateTouch: \.updatedAt
85-
)
80+
Reminder.createTemporaryTrigger(
81+
afterUpdateTouch: \.updatedAt
82+
)
8683
```
8784
}
8885
@Column {
8986
```sql
90-
CREATE TEMPORARY TRIGGER "reminders_updatedAt"
87+
CREATE TEMPORARY TRIGGER "after_update_on_reminders@…"
9188
AFTER UPDATE ON "reminders"
9289
FOR EACH ROW
9390
BEGIN
@@ -102,14 +99,13 @@ comes with another specialized too just for that kind of trigger,
10299

103100
There are 3 kinds of triggers depending on the event being listened for in the database: inserts,
104101
updates, and deletes. For each of these kinds of triggers one can perform 4 kinds of actions: a
105-
select, insert, update, or delete. All 12 combinations of these kinds of triggers are supported by
106-
the library.
102+
select, insert, update, or delete. Each action can be performed either before or after the event
103+
being listened for executes. All 24 combinations of these kinds of triggers are supported by the
104+
library.
107105

108-
> Note: Technically SQLite supports `BEFORE` triggers and `AFTER` triggers, but the documentation
109-
> recommends against using `BEFORE` triggers as it can lead to undefined behavior. Therefore
110-
> StructuredQueries does not expose `BEFORE` triggers in its API. If you want to go against the
111-
> recommendations of SQLite and create a `BEFORE` trigger, you can always write a trigger as a SQL
112-
> string (see <docs:SafeSQLStrings> for more info).
106+
> Tip: SQLite generally
107+
> [recommends against](https://sqlite.org/lang_createtrigger.html#cautions_on_the_use_of_before_triggers)
108+
> using `BEFORE` triggers, as it can lead to undefined behavior.
113109
114110
Here are a few examples to show you the possibilities with triggers:
115111

@@ -124,21 +120,19 @@ last list was deleted:
124120
@Column {
125121
```swift
126122
RemindersList.createTemporaryTrigger(
127-
"nonEmptyRemindersLists",
128123
after: .delete { _ in
129-
RemindersList
130-
.insert {
131-
RemindersList.Draft(title: "Personal")
132-
}
124+
RemindersList.insert {
125+
RemindersList.Draft(title: "Personal")
126+
}
133127
} when: { _ in
134-
!RemindersList.all.exists()
128+
!RemindersList.exists()
135129
}
136130
)
137131
```
138132
}
139133
@Column {
140134
```sql
141-
CREATE TEMPORARY TRIGGER "nonEmptyRemindersLists"
135+
CREATE TEMPORARY TRIGGER "after_delete_on_remindersLists@…"
142136
AFTER DELETE ON "remindersLists"
143137
FOR EACH ROW WHEN NOT (EXISTS (SELECT * FROM "remindersLists"))
144138
BEGIN
@@ -168,7 +162,6 @@ reminder is inserted into the database with the following trigger:
168162
@Column {
169163
```swift
170164
RemindersList.createTemporaryTrigger(
171-
"insertReminderCallback",
172165
after: .insert { new in
173166
#sql("SELECT didInsertReminder(\(new.id))")
174167
}
@@ -177,7 +170,7 @@ reminder is inserted into the database with the following trigger:
177170
}
178171
@Column {
179172
```sql
180-
CREATE TEMPORARY TRIGGER "insertReminderCallback"
173+
CREATE TEMPORARY TRIGGER "after_insert_on_remindersLists@"
181174
AFTER DELETE ON "reminders"
182175
FOR EACH ROW
183176
BEGIN

0 commit comments

Comments
 (0)