sqlparser/ast/
trigger.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! SQL Abstract Syntax Tree (AST) for triggers.
19use super::*;
20
21/// This specifies whether the trigger function should be fired once for every row affected by the trigger event, or just once per SQL statement.
22#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
23#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
24#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
25pub enum TriggerObject {
26    Row,
27    Statement,
28}
29
30impl fmt::Display for TriggerObject {
31    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32        match self {
33            TriggerObject::Row => write!(f, "ROW"),
34            TriggerObject::Statement => write!(f, "STATEMENT"),
35        }
36    }
37}
38
39/// This clause indicates whether the following relation name is for the before-image transition relation or the after-image transition relation
40#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
41#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
42#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
43pub enum TriggerReferencingType {
44    OldTable,
45    NewTable,
46}
47
48impl fmt::Display for TriggerReferencingType {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        match self {
51            TriggerReferencingType::OldTable => write!(f, "OLD TABLE"),
52            TriggerReferencingType::NewTable => write!(f, "NEW TABLE"),
53        }
54    }
55}
56
57/// This keyword immediately precedes the declaration of one or two relation names that provide access to the transition relations of the triggering statement
58#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
59#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
60#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
61pub struct TriggerReferencing {
62    pub refer_type: TriggerReferencingType,
63    pub is_as: bool,
64    pub transition_relation_name: ObjectName,
65}
66
67impl fmt::Display for TriggerReferencing {
68    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69        write!(
70            f,
71            "{refer_type}{is_as} {relation_name}",
72            refer_type = self.refer_type,
73            is_as = if self.is_as { " AS" } else { "" },
74            relation_name = self.transition_relation_name
75        )
76    }
77}
78
79/// Used to describe trigger events
80#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
81#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
82#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
83pub enum TriggerEvent {
84    Insert,
85    Update(Vec<Ident>),
86    Delete,
87    Truncate,
88}
89
90impl fmt::Display for TriggerEvent {
91    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92        match self {
93            TriggerEvent::Insert => write!(f, "INSERT"),
94            TriggerEvent::Update(columns) => {
95                write!(f, "UPDATE")?;
96                if !columns.is_empty() {
97                    write!(f, " OF")?;
98                    write!(f, " {}", display_comma_separated(columns))?;
99                }
100                Ok(())
101            }
102            TriggerEvent::Delete => write!(f, "DELETE"),
103            TriggerEvent::Truncate => write!(f, "TRUNCATE"),
104        }
105    }
106}
107
108/// Trigger period
109#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
110#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
111#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
112pub enum TriggerPeriod {
113    For,
114    After,
115    Before,
116    InsteadOf,
117}
118
119impl fmt::Display for TriggerPeriod {
120    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121        match self {
122            TriggerPeriod::For => write!(f, "FOR"),
123            TriggerPeriod::After => write!(f, "AFTER"),
124            TriggerPeriod::Before => write!(f, "BEFORE"),
125            TriggerPeriod::InsteadOf => write!(f, "INSTEAD OF"),
126        }
127    }
128}
129
130/// Types of trigger body execution body.
131#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
132#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
133#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
134pub enum TriggerExecBodyType {
135    Function,
136    Procedure,
137}
138
139impl fmt::Display for TriggerExecBodyType {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        match self {
142            TriggerExecBodyType::Function => write!(f, "FUNCTION"),
143            TriggerExecBodyType::Procedure => write!(f, "PROCEDURE"),
144        }
145    }
146}
147/// This keyword immediately precedes the declaration of one or two relation names that provide access to the transition relations of the triggering statement
148#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
149#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
150#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
151pub struct TriggerExecBody {
152    pub exec_type: TriggerExecBodyType,
153    pub func_desc: FunctionDesc,
154}
155
156impl fmt::Display for TriggerExecBody {
157    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158        write!(
159            f,
160            "{exec_type} {func_desc}",
161            exec_type = self.exec_type,
162            func_desc = self.func_desc
163        )
164    }
165}