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    After,
114    Before,
115    InsteadOf,
116}
117
118impl fmt::Display for TriggerPeriod {
119    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120        match self {
121            TriggerPeriod::After => write!(f, "AFTER"),
122            TriggerPeriod::Before => write!(f, "BEFORE"),
123            TriggerPeriod::InsteadOf => write!(f, "INSTEAD OF"),
124        }
125    }
126}
127
128/// Types of trigger body execution body.
129#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
130#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
131#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
132pub enum TriggerExecBodyType {
133    Function,
134    Procedure,
135}
136
137impl fmt::Display for TriggerExecBodyType {
138    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139        match self {
140            TriggerExecBodyType::Function => write!(f, "FUNCTION"),
141            TriggerExecBodyType::Procedure => write!(f, "PROCEDURE"),
142        }
143    }
144}
145/// This keyword immediately precedes the declaration of one or two relation names that provide access to the transition relations of the triggering statement
146#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
147#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
148#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
149pub struct TriggerExecBody {
150    pub exec_type: TriggerExecBodyType,
151    pub func_desc: FunctionDesc,
152}
153
154impl fmt::Display for TriggerExecBody {
155    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156        write!(
157            f,
158            "{exec_type} {func_desc}",
159            exec_type = self.exec_type,
160            func_desc = self.func_desc
161        )
162    }
163}