Skip to main content

uqa_sql/ast/
events.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Row-trigger and rewrite-rule catalog statements.
8
9use serde::{Deserialize, Serialize};
10
11use super::{Expr, Statement};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
14pub enum TriggerTiming {
15    Before,
16    After,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
20pub enum TriggerEvent {
21    Insert,
22    Update,
23    Delete,
24    Truncate,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct CreateTrigger {
29    pub name: String,
30    pub table: String,
31    pub function: String,
32    pub arguments: Vec<String>,
33    pub row: bool,
34    pub timing: TriggerTiming,
35    pub events: Vec<TriggerEvent>,
36    pub update_columns: Vec<String>,
37    pub when: Option<Expr>,
38    pub or_replace: bool,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct DropTrigger {
43    pub name: String,
44    pub table: String,
45    pub if_exists: bool,
46    pub cascade: bool,
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
50pub enum RuleEvent {
51    Select,
52    Insert,
53    Update,
54    Delete,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct CreateRule {
59    pub name: String,
60    pub table: String,
61    pub event: RuleEvent,
62    pub instead: bool,
63    pub condition: Option<Expr>,
64    pub actions: Vec<Statement>,
65    #[serde(default)]
66    pub action_sql: Vec<String>,
67    pub or_replace: bool,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct DropRule {
72    pub name: String,
73    pub table: String,
74    pub if_exists: bool,
75    pub cascade: bool,
76}
77
78#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
79pub enum EventEnableMode {
80    #[default]
81    Origin,
82    Disabled,
83    Replica,
84    Always,
85}
86
87impl EventEnableMode {
88    #[must_use]
89    pub const fn catalog_code(self) -> &'static str {
90        match self {
91            Self::Origin => "O",
92            Self::Disabled => "D",
93            Self::Replica => "R",
94            Self::Always => "A",
95        }
96    }
97
98    #[must_use]
99    pub const fn fires_in_origin(self) -> bool {
100        matches!(self, Self::Origin | Self::Always)
101    }
102}