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