Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Sources/StructuredQueriesCore/Triggers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,19 @@ extension Table {
/// - name: The trigger's name. By default a unique name is generated depending using the table,
/// operation, and source location.
/// - ifNotExists: Adds an `IF NOT EXISTS` clause to the `CREATE TRIGGER` statement.
/// - date: A key path to a datetime column.
/// - dateColumn: A key path to a datetime column.
/// - dateFunction: A database function that returns the current datetime, _e.g._,
/// `#sql("datetime('subsec'))"`.
/// - expression: The expression used to generate the datetime.
/// - fileID: The source `#fileID` associated with the trigger.
/// - line: The source `#line` associated with the trigger.
/// - column: The source `#column` associated with the trigger.
/// - Returns: A temporary trigger.
public static func createTemporaryTrigger<D: _OptionalPromotable<Date?>>(
_ name: String? = nil,
ifNotExists: Bool = false,
afterUpdateTouch date: KeyPath<TableColumns, TableColumn<Self, D>>,
afterUpdateTouch dateColumn: KeyPath<TableColumns, TableColumn<Self, D>>,
date dateFunction: any QueryExpression<D> = SQLQueryExpression<D>("datetime('subsec')"),
fileID: StaticString = #fileID,
line: UInt = #line,
column: UInt = #column
Expand All @@ -144,7 +148,7 @@ extension Table {
name,
ifNotExists: ifNotExists,
afterUpdateTouch: {
$0[dynamicMember: date] = SQLQueryExpression("datetime('subsec')")
$0[dynamicMember: dateColumn] = dateFunction
},
fileID: fileID,
line: line,
Expand Down Expand Up @@ -205,15 +209,18 @@ extension Table {
/// - name: The trigger's name. By default a unique name is generated depending using the table,
/// operation, and source location.
/// - ifNotExists: Adds an `IF NOT EXISTS` clause to the `CREATE TRIGGER` statement.
/// - date: A key path to a datetime column.
/// - dateColumn: A key path to a datetime column.
/// - dateFunction: A database function that returns the current datetime, _e.g._,
/// `#sql("datetime('subsec'))"`.
/// - fileID: The source `#fileID` associated with the trigger.
/// - line: The source `#line` associated with the trigger.
/// - column: The source `#column` associated with the trigger.
/// - Returns: A temporary trigger.
public static func createTemporaryTrigger<D: _OptionalPromotable<Date?>>(
_ name: String? = nil,
ifNotExists: Bool = false,
afterInsertTouch date: KeyPath<TableColumns, TableColumn<Self, D>>,
afterInsertTouch dateColumn: KeyPath<TableColumns, TableColumn<Self, D>>,
date dateFunction: any QueryExpression<D> = SQLQueryExpression<D>("datetime('subsec')"),
fileID: StaticString = #fileID,
line: UInt = #line,
column: UInt = #column
Expand All @@ -222,7 +229,7 @@ extension Table {
name,
ifNotExists: ifNotExists,
afterInsertTouch: {
$0[dynamicMember: date] = SQLQueryExpression("datetime('subsec')")
$0[dynamicMember: dateColumn] = dateFunction
},
fileID: fileID,
line: line,
Expand Down
19 changes: 18 additions & 1 deletion Tests/StructuredQueriesTests/TriggersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ extension SnapshotTests {
}
}

@Test func afterUpdateTouchCustomDate() {
assertQuery(
Reminder.createTemporaryTrigger(afterUpdateTouch: \.updatedAt, date: #sql("customDate()"))
) {
"""
CREATE TEMPORARY TRIGGER
"after_update_on_reminders@StructuredQueriesTests/TriggersTests.swift:120:40"
AFTER UPDATE ON "reminders"
FOR EACH ROW BEGIN
UPDATE "reminders"
SET "updatedAt" = customDate()
WHERE ("reminders"."rowid" = "new"."rowid");
END
"""
}
}

@Test func multiStatement() {
let trigger = RemindersList.createTemporaryTrigger(
after: .insert { new in
Expand All @@ -133,7 +150,7 @@ extension SnapshotTests {
assertQuery(trigger) {
"""
CREATE TEMPORARY TRIGGER
"after_insert_on_remindersLists@StructuredQueriesTests/TriggersTests.swift:119:57"
"after_insert_on_remindersLists@StructuredQueriesTests/TriggersTests.swift:136:57"
AFTER INSERT ON "remindersLists"
FOR EACH ROW BEGIN
UPDATE "remindersLists"
Expand Down