databend_common_ast/ast/statements/
notification.rs

1// Copyright 2021 Datafuse Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::Display;
16use std::fmt::Formatter;
17
18use derive_visitor::Drive;
19use derive_visitor::DriveMut;
20
21#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
22pub struct CreateNotificationStmt {
23    pub if_not_exists: bool,
24    pub name: String,
25    pub notification_type: String,
26    pub enabled: bool,
27    pub webhook_opts: Option<NotificationWebhookOptions>,
28    pub comments: Option<String>,
29}
30
31impl Display for CreateNotificationStmt {
32    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
33        write!(f, "CREATE NOTIFICATION INTEGRATION")?;
34        if self.if_not_exists {
35            write!(f, " IF NOT EXISTS")?;
36        }
37        write!(f, " {}", self.name)?;
38        write!(f, " TYPE = {}", self.notification_type)?;
39        write!(f, " ENABLED = {}", self.enabled)?;
40        if let Some(webhook_opts) = &self.webhook_opts {
41            write!(f, " {}", webhook_opts)?;
42        }
43        if let Some(comments) = &self.comments {
44            write!(f, " COMMENTS = '{comments}'")?;
45        }
46        Ok(())
47    }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
51pub struct NotificationWebhookOptions {
52    pub url: Option<String>,
53    pub method: Option<String>,
54    pub authorization_header: Option<String>,
55}
56
57impl Display for NotificationWebhookOptions {
58    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
59        let NotificationWebhookOptions {
60            url,
61            method,
62            authorization_header,
63        } = self;
64        {
65            write!(f, "WEBHOOK = (")?;
66            if let Some(url) = url {
67                write!(f, " URL = '{}'", url)?;
68            }
69            if let Some(method) = method {
70                write!(f, " METHOD = '{}'", method)?;
71            }
72            if let Some(authorization_header) = authorization_header {
73                write!(f, " AUTHORIZATION_HEADER = '{}'", authorization_header)?;
74            }
75            write!(f, " )")?;
76            Ok(())
77        }
78    }
79}
80
81impl FromIterator<(String, String)> for NotificationWebhookOptions {
82    fn from_iter<T: IntoIterator<Item = (String, String)>>(iter: T) -> Self {
83        let mut url = None;
84        let mut method = None;
85        let mut authorization_header = None;
86        for (k, v) in iter {
87            match k.to_uppercase().as_str() {
88                "URL" => url = Some(v),
89                "METHOD" => method = Some(v),
90                "AUTHORIZATION_HEADER" => authorization_header = Some(v),
91                _ => {}
92            }
93        }
94        NotificationWebhookOptions {
95            url,
96            method,
97            authorization_header,
98        }
99    }
100}
101
102// drop notification
103#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
104pub struct DropNotificationStmt {
105    pub if_exists: bool,
106    pub name: String,
107}
108
109impl Display for DropNotificationStmt {
110    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
111        write!(f, "DROP NOTIFICATION INTEGRATION")?;
112        if self.if_exists {
113            write!(f, " IF EXISTS")?;
114        }
115        write!(f, " {}", self.name)
116    }
117}
118
119// alter notification
120#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
121pub struct AlterNotificationStmt {
122    pub if_exists: bool,
123    pub name: String,
124    pub options: AlterNotificationOptions,
125}
126#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
127pub enum AlterNotificationOptions {
128    Set(AlterNotificationSetOptions),
129}
130#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
131pub struct AlterNotificationSetOptions {
132    pub enabled: Option<bool>,
133    pub webhook_opts: Option<NotificationWebhookOptions>,
134    pub comments: Option<String>,
135}
136
137impl AlterNotificationSetOptions {
138    pub fn enabled(enabled: bool) -> Self {
139        AlterNotificationSetOptions {
140            enabled: Some(enabled),
141            webhook_opts: None,
142            comments: None,
143        }
144    }
145
146    pub fn webhook_opts(webhook_opts: NotificationWebhookOptions) -> Self {
147        AlterNotificationSetOptions {
148            enabled: None,
149            webhook_opts: Some(webhook_opts),
150            comments: None,
151        }
152    }
153
154    pub fn comments(comments: String) -> Self {
155        AlterNotificationSetOptions {
156            enabled: None,
157            webhook_opts: None,
158            comments: Some(comments),
159        }
160    }
161}
162
163impl Display for AlterNotificationStmt {
164    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
165        write!(f, "ALTER NOTIFICATION INTEGRATION {}", self.name)?;
166        match &self.options {
167            AlterNotificationOptions::Set(set_opts) => {
168                write!(f, " SET ")?;
169                if let Some(enabled) = set_opts.enabled {
170                    write!(f, "ENABLED = {}", enabled)?;
171                }
172                if let Some(webhook_opts) = &set_opts.webhook_opts {
173                    write!(f, " {}", webhook_opts)?;
174                }
175                if let Some(comments) = &set_opts.comments {
176                    write!(f, " COMMENTS = '{}'", comments)?;
177                }
178            }
179        }
180        Ok(())
181    }
182}
183
184// describe notification
185#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
186pub struct DescribeNotificationStmt {
187    pub name: String,
188}
189
190impl Display for DescribeNotificationStmt {
191    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
192        write!(f, "DESCRIBE NOTIFICATION INTEGRATION {}", self.name)
193    }
194}