reinhardt_query/types/trigger.rs
1//! Trigger-related types for DDL operations
2
3/// Trigger event type
4///
5/// Specifies which DML operation triggers the trigger execution.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum TriggerEvent {
8 /// INSERT operation
9 Insert,
10 /// UPDATE operation (optionally on specific columns)
11 Update {
12 /// Optional list of column names that restrict trigger activation.
13 /// When `None`, the trigger fires on any column update.
14 columns: Option<Vec<String>>,
15 },
16 /// DELETE operation
17 Delete,
18}
19
20/// Trigger timing
21///
22/// Specifies when the trigger executes relative to the triggering event.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum TriggerTiming {
25 /// Execute before the triggering event
26 Before,
27 /// Execute after the triggering event
28 After,
29 /// Execute instead of the triggering event (SQLite views only)
30 InsteadOf,
31}
32
33/// Trigger scope
34///
35/// Specifies whether the trigger executes for each row or once per statement.
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum TriggerScope {
38 /// Execute for each affected row (FOR EACH ROW)
39 Row,
40 /// Execute once per statement (FOR EACH STATEMENT) - PostgreSQL only
41 Statement,
42}
43
44/// Trigger action timing for MySQL
45///
46/// MySQL-specific feature to control trigger execution order.
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub enum TriggerOrder {
49 /// Execute before another trigger
50 Precedes(String),
51 /// Execute after another trigger
52 Follows(String),
53}
54
55/// Trigger body representation
56///
57/// Contains the SQL statements to execute when the trigger fires.
58#[derive(Debug, Clone, PartialEq)]
59pub enum TriggerBody {
60 /// Single SQL statement
61 Single(String),
62 /// Multiple SQL statements (MySQL/SQLite BEGIN...END block)
63 Multiple(Vec<String>),
64 /// PostgreSQL function call (EXECUTE FUNCTION function_name())
65 PostgresFunction(String),
66}
67
68impl TriggerBody {
69 /// Create a single-statement trigger body
70 pub fn single<S: Into<String>>(statement: S) -> Self {
71 Self::Single(statement.into())
72 }
73
74 /// Create a multi-statement trigger body
75 pub fn multiple<I, S>(statements: I) -> Self
76 where
77 I: IntoIterator<Item = S>,
78 S: Into<String>,
79 {
80 Self::Multiple(statements.into_iter().map(|s| s.into()).collect())
81 }
82
83 /// Create a PostgreSQL function call trigger body
84 pub fn postgres_function<S: Into<String>>(function_name: S) -> Self {
85 Self::PostgresFunction(function_name.into())
86 }
87}