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::statements::show::ShowLimit;
22use crate::ast::write_dot_separated_list;
23use crate::ast::CreateOption;
24use crate::ast::DatabaseRef;
25use crate::ast::Identifier;
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        // TODO(leiysky): display rest information
113        Ok(())
114    }
115}
116
117#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
118pub struct DropDatabaseStmt {
119    pub if_exists: bool,
120    pub catalog: Option<Identifier>,
121    pub database: Identifier,
122}
123
124impl Display for DropDatabaseStmt {
125    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
126        write!(f, "DROP DATABASE ")?;
127        if self.if_exists {
128            write!(f, "IF EXISTS ")?;
129        }
130        write_dot_separated_list(f, self.catalog.iter().chain(Some(&self.database)))?;
131
132        Ok(())
133    }
134}
135
136#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
137pub struct UndropDatabaseStmt {
138    pub catalog: Option<Identifier>,
139    pub database: Identifier,
140}
141
142impl Display for UndropDatabaseStmt {
143    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
144        write!(f, "UNDROP DATABASE ")?;
145        write_dot_separated_list(f, self.catalog.iter().chain(Some(&self.database)))?;
146        Ok(())
147    }
148}
149
150#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
151pub struct AlterDatabaseStmt {
152    pub if_exists: bool,
153    pub catalog: Option<Identifier>,
154    pub database: Identifier,
155    pub action: AlterDatabaseAction,
156}
157
158impl Display for AlterDatabaseStmt {
159    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
160        write!(f, "ALTER DATABASE ")?;
161        if self.if_exists {
162            write!(f, "IF EXISTS ")?;
163        }
164        write_dot_separated_list(f, self.catalog.iter().chain(Some(&self.database)))?;
165        match &self.action {
166            AlterDatabaseAction::RenameDatabase { new_db } => {
167                write!(f, " RENAME TO {new_db}")?;
168            }
169            AlterDatabaseAction::RefreshDatabaseCache => {
170                write!(f, " REFRESH CACHE")?;
171            }
172        }
173
174        Ok(())
175    }
176}
177
178#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
179pub enum AlterDatabaseAction {
180    RenameDatabase { new_db: Identifier },
181    RefreshDatabaseCache,
182}
183
184#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
185pub enum DatabaseEngine {
186    Default,
187    Share,
188}
189
190impl Display for DatabaseEngine {
191    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
192        match self {
193            DatabaseEngine::Default => write!(f, "DEFAULT"),
194            DatabaseEngine::Share => write!(f, "SHARE"),
195        }
196    }
197}
198
199#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
200pub struct SQLProperty {
201    pub name: String,
202    pub value: String,
203}