Skip to main content

databend_common_ast/ast/statements/
database.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::DatabaseRef;
23use crate::ast::Identifier;
24use crate::ast::statements::show::ShowLimit;
25use crate::ast::write_dot_separated_list;
26
27#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
28pub struct ShowDatabasesStmt {
29    pub catalog: Option<Identifier>,
30    pub full: bool,
31    pub limit: Option<ShowLimit>,
32}
33
34impl Display for ShowDatabasesStmt {
35    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
36        write!(f, "SHOW ")?;
37        if self.full {
38            write!(f, "FULL ")?;
39        }
40        write!(f, "DATABASES")?;
41        if let Some(catalog) = &self.catalog {
42            write!(f, " FROM {catalog}")?;
43        }
44        if let Some(limit) = &self.limit {
45            write!(f, " {limit}")?;
46        }
47
48        Ok(())
49    }
50}
51
52#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
53pub struct ShowDropDatabasesStmt {
54    pub catalog: Option<Identifier>,
55    pub limit: Option<ShowLimit>,
56}
57
58impl Display for ShowDropDatabasesStmt {
59    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
60        write!(f, "SHOW DROP DATABASES")?;
61        if let Some(catalog) = &self.catalog {
62            write!(f, " FROM {catalog}")?;
63        }
64        if let Some(limit) = &self.limit {
65            write!(f, " {limit}")?;
66        }
67
68        Ok(())
69    }
70}
71
72#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
73pub struct ShowCreateDatabaseStmt {
74    pub catalog: Option<Identifier>,
75    pub database: Identifier,
76}
77
78impl Display for ShowCreateDatabaseStmt {
79    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
80        write!(f, "SHOW CREATE DATABASE ")?;
81        write_dot_separated_list(f, self.catalog.iter().chain(Some(&self.database)))?;
82
83        Ok(())
84    }
85}
86
87#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
88pub struct CreateDatabaseStmt {
89    pub create_option: CreateOption,
90    pub database: DatabaseRef,
91    pub engine: Option<DatabaseEngine>,
92    pub options: Vec<SQLProperty>,
93}
94
95impl Display for CreateDatabaseStmt {
96    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
97        write!(f, "CREATE ")?;
98        if let CreateOption::CreateOrReplace = self.create_option {
99            write!(f, "OR REPLACE ")?;
100        }
101        write!(f, "DATABASE ")?;
102        if let CreateOption::CreateIfNotExists = self.create_option {
103            write!(f, "IF NOT EXISTS ")?;
104        }
105
106        write!(f, "{}", self.database)?;
107
108        if let Some(engine) = &self.engine {
109            write!(f, " ENGINE = {engine}")?;
110        }
111
112        if !self.options.is_empty() {
113            write!(f, " OPTIONS (")?;
114            for (i, option) in self.options.iter().enumerate() {
115                if i > 0 {
116                    write!(f, ", ")?;
117                }
118                write!(f, "{} = '{}'", option.name, option.value)?;
119            }
120            write!(f, ")")?;
121        }
122
123        Ok(())
124    }
125}
126
127#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
128pub struct DropDatabaseStmt {
129    pub if_exists: bool,
130    pub catalog: Option<Identifier>,
131    pub database: Identifier,
132}
133
134impl Display for DropDatabaseStmt {
135    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
136        write!(f, "DROP DATABASE ")?;
137        if self.if_exists {
138            write!(f, "IF EXISTS ")?;
139        }
140        write_dot_separated_list(f, self.catalog.iter().chain(Some(&self.database)))?;
141
142        Ok(())
143    }
144}
145
146#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
147pub struct UndropDatabaseStmt {
148    pub catalog: Option<Identifier>,
149    pub database: Identifier,
150}
151
152impl Display for UndropDatabaseStmt {
153    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
154        write!(f, "UNDROP DATABASE ")?;
155        write_dot_separated_list(f, self.catalog.iter().chain(Some(&self.database)))?;
156        Ok(())
157    }
158}
159
160#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
161pub struct AlterDatabaseStmt {
162    pub if_exists: bool,
163    pub catalog: Option<Identifier>,
164    pub database: Identifier,
165    pub action: AlterDatabaseAction,
166}
167
168impl Display for AlterDatabaseStmt {
169    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
170        write!(f, "ALTER DATABASE ")?;
171        if self.if_exists {
172            write!(f, "IF EXISTS ")?;
173        }
174        write_dot_separated_list(f, self.catalog.iter().chain(Some(&self.database)))?;
175        match &self.action {
176            AlterDatabaseAction::RenameDatabase { new_db } => {
177                write!(f, " RENAME TO {new_db}")?;
178            }
179            AlterDatabaseAction::RefreshDatabaseCache => {
180                write!(f, " REFRESH CACHE")?;
181            }
182            AlterDatabaseAction::SetOptions { options } => {
183                write!(f, " SET OPTIONS (")?;
184                for (i, option) in options.iter().enumerate() {
185                    if i > 0 {
186                        write!(f, ", ")?;
187                    }
188                    write!(f, "{} = '{}'", option.name, option.value)?;
189                }
190                write!(f, ")")?;
191            }
192        }
193
194        Ok(())
195    }
196}
197
198#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
199pub enum AlterDatabaseAction {
200    RenameDatabase { new_db: Identifier },
201    RefreshDatabaseCache,
202    SetOptions { options: Vec<SQLProperty> },
203}
204
205#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
206pub enum DatabaseEngine {
207    Default,
208    Share,
209}
210
211impl Display for DatabaseEngine {
212    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
213        match self {
214            DatabaseEngine::Default => write!(f, "DEFAULT"),
215            DatabaseEngine::Share => write!(f, "SHARE"),
216        }
217    }
218}
219
220#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
221pub struct SQLProperty {
222    pub name: String,
223    pub value: String,
224}