Skip to main content

databend_common_ast/ast/statements/
sequence.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
21use crate::ast::CreateOption;
22use crate::ast::Identifier;
23use crate::ast::quote::QuotedString;
24
25#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
26pub struct CreateSequenceStmt {
27    pub create_option: CreateOption,
28    pub sequence: Identifier,
29    pub start: Option<u64>,
30    pub increment: Option<u64>,
31    pub comment: Option<String>,
32}
33
34impl Display for CreateSequenceStmt {
35    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
36        write!(f, "CREATE ")?;
37        if let CreateOption::CreateOrReplace = self.create_option {
38            write!(f, "OR REPLACE ")?;
39        }
40        write!(f, "SEQUENCE ")?;
41        if let CreateOption::CreateIfNotExists = self.create_option {
42            write!(f, "IF NOT EXISTS ")?;
43        }
44        write!(f, "{}", self.sequence)?;
45
46        if let Some(s) = &self.start {
47            write!(f, " START = {}", s)?;
48        }
49
50        if let Some(i) = &self.increment {
51            write!(f, " INCREMENT = {}", i)?;
52        }
53        if let Some(comment) = &self.comment {
54            write!(f, " COMMENT = {}", QuotedString(comment, '\''))?;
55        }
56        Ok(())
57    }
58}
59
60#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
61pub struct DropSequenceStmt {
62    pub if_exists: bool,
63    pub sequence: Identifier,
64}
65
66impl Display for DropSequenceStmt {
67    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
68        write!(f, "DROP SEQUENCE ")?;
69        if self.if_exists {
70            write!(f, "IF EXISTS ")?;
71        }
72        write!(f, "{}", self.sequence)?;
73        Ok(())
74    }
75}