Skip to content

Commit 6f8bd35

Browse files
connor-ricksmbrandonwstephencelis
authored
Add datetime customization to createTemporaryTrigger (#143)
* Add datetime customization to createTemporaryTrigger Adds support for providing a customized expression that will be used when creating a temporary trigger. * wip * Use query expression instead * wip --------- Co-authored-by: Brandon Williams <[email protected]> Co-authored-by: Stephen Celis <[email protected]>
1 parent 9f35bc0 commit 6f8bd35

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

Sources/StructuredQueriesCore/Triggers.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,19 @@ extension Table {
127127
/// - name: The trigger's name. By default a unique name is generated depending using the table,
128128
/// operation, and source location.
129129
/// - ifNotExists: Adds an `IF NOT EXISTS` clause to the `CREATE TRIGGER` statement.
130-
/// - date: A key path to a datetime column.
130+
/// - dateColumn: A key path to a datetime column.
131+
/// - dateFunction: A database function that returns the current datetime, _e.g._,
132+
/// `#sql("datetime('subsec'))"`.
133+
/// - expression: The expression used to generate the datetime.
131134
/// - fileID: The source `#fileID` associated with the trigger.
132135
/// - line: The source `#line` associated with the trigger.
133136
/// - column: The source `#column` associated with the trigger.
134137
/// - Returns: A temporary trigger.
135138
public static func createTemporaryTrigger<D: _OptionalPromotable<Date?>>(
136139
_ name: String? = nil,
137140
ifNotExists: Bool = false,
138-
afterUpdateTouch date: KeyPath<TableColumns, TableColumn<Self, D>>,
141+
afterUpdateTouch dateColumn: KeyPath<TableColumns, TableColumn<Self, D>>,
142+
date dateFunction: any QueryExpression<D> = SQLQueryExpression<D>("datetime('subsec')"),
139143
fileID: StaticString = #fileID,
140144
line: UInt = #line,
141145
column: UInt = #column
@@ -144,7 +148,7 @@ extension Table {
144148
name,
145149
ifNotExists: ifNotExists,
146150
afterUpdateTouch: {
147-
$0[dynamicMember: date] = SQLQueryExpression("datetime('subsec')")
151+
$0[dynamicMember: dateColumn] = dateFunction
148152
},
149153
fileID: fileID,
150154
line: line,
@@ -205,15 +209,18 @@ extension Table {
205209
/// - name: The trigger's name. By default a unique name is generated depending using the table,
206210
/// operation, and source location.
207211
/// - ifNotExists: Adds an `IF NOT EXISTS` clause to the `CREATE TRIGGER` statement.
208-
/// - date: A key path to a datetime column.
212+
/// - dateColumn: A key path to a datetime column.
213+
/// - dateFunction: A database function that returns the current datetime, _e.g._,
214+
/// `#sql("datetime('subsec'))"`.
209215
/// - fileID: The source `#fileID` associated with the trigger.
210216
/// - line: The source `#line` associated with the trigger.
211217
/// - column: The source `#column` associated with the trigger.
212218
/// - Returns: A temporary trigger.
213219
public static func createTemporaryTrigger<D: _OptionalPromotable<Date?>>(
214220
_ name: String? = nil,
215221
ifNotExists: Bool = false,
216-
afterInsertTouch date: KeyPath<TableColumns, TableColumn<Self, D>>,
222+
afterInsertTouch dateColumn: KeyPath<TableColumns, TableColumn<Self, D>>,
223+
date dateFunction: any QueryExpression<D> = SQLQueryExpression<D>("datetime('subsec')"),
217224
fileID: StaticString = #fileID,
218225
line: UInt = #line,
219226
column: UInt = #column
@@ -222,7 +229,7 @@ extension Table {
222229
name,
223230
ifNotExists: ifNotExists,
224231
afterInsertTouch: {
225-
$0[dynamicMember: date] = SQLQueryExpression("datetime('subsec')")
232+
$0[dynamicMember: dateColumn] = dateFunction
226233
},
227234
fileID: fileID,
228235
line: line,

Tests/StructuredQueriesTests/TriggersTests.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ extension SnapshotTests {
115115
}
116116
}
117117

118+
@Test func afterUpdateTouchCustomDate() {
119+
assertQuery(
120+
Reminder.createTemporaryTrigger(afterUpdateTouch: \.updatedAt, date: #sql("customDate()"))
121+
) {
122+
"""
123+
CREATE TEMPORARY TRIGGER
124+
"after_update_on_reminders@StructuredQueriesTests/TriggersTests.swift:120:40"
125+
AFTER UPDATE ON "reminders"
126+
FOR EACH ROW BEGIN
127+
UPDATE "reminders"
128+
SET "updatedAt" = customDate()
129+
WHERE ("reminders"."rowid" = "new"."rowid");
130+
END
131+
"""
132+
}
133+
}
134+
118135
@Test func multiStatement() {
119136
let trigger = RemindersList.createTemporaryTrigger(
120137
after: .insert { new in
@@ -133,7 +150,7 @@ extension SnapshotTests {
133150
assertQuery(trigger) {
134151
"""
135152
CREATE TEMPORARY TRIGGER
136-
"after_insert_on_remindersLists@StructuredQueriesTests/TriggersTests.swift:119:57"
153+
"after_insert_on_remindersLists@StructuredQueriesTests/TriggersTests.swift:136:57"
137154
AFTER INSERT ON "remindersLists"
138155
FOR EACH ROW BEGIN
139156
UPDATE "remindersLists"

0 commit comments

Comments
 (0)