databend_common_ast/ast/statements/
dictionary.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::collections::BTreeMap;
16use std::fmt::Display;
17use std::fmt::Formatter;
18
19use derive_visitor::Drive;
20use derive_visitor::DriveMut;
21
22use super::ShowLimit;
23use crate::ast::quote::QuotedString;
24use crate::ast::write_comma_separated_list;
25use crate::ast::write_dot_separated_list;
26use crate::ast::write_space_separated_string_map;
27use crate::ast::ColumnDefinition;
28use crate::ast::CreateOption;
29use crate::ast::Identifier;
30
31#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
32pub struct CreateDictionaryStmt {
33    pub create_option: CreateOption,
34    pub catalog: Option<Identifier>,
35    pub database: Option<Identifier>,
36    pub dictionary_name: Identifier,
37    pub columns: Vec<ColumnDefinition>,
38    pub primary_keys: Vec<Identifier>,
39    pub source_name: Identifier,
40    pub source_options: BTreeMap<String, String>,
41    pub comment: Option<String>,
42}
43
44impl Display for CreateDictionaryStmt {
45    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
46        write!(f, "CREATE ")?;
47        if let CreateOption::CreateOrReplace = self.create_option {
48            write!(f, "OR REPLACE ")?;
49        }
50        write!(f, "DICTIONARY ")?;
51        if let CreateOption::CreateIfNotExists = self.create_option {
52            write!(f, "IF NOT EXISTS ")?;
53        }
54        write_dot_separated_list(
55            f,
56            self.catalog
57                .iter()
58                .chain(&self.database)
59                .chain(Some(&self.dictionary_name)),
60        )?;
61        if !self.columns.is_empty() {
62            write!(f, "(")?;
63            write_comma_separated_list(f, &self.columns)?;
64            write!(f, ")")?;
65        }
66        if !self.primary_keys.is_empty() {
67            write!(f, "PRIMARY KEY ")?;
68            write_comma_separated_list(f, &self.primary_keys)?;
69        }
70        write!(f, " SOURCE(")?;
71        write!(f, "{}( ", &self.source_name)?;
72        if !self.source_options.is_empty() {
73            write_space_separated_string_map(f, &self.source_options)?;
74        }
75        write!(f, ")")?;
76        write!(f, ")")?;
77        if let Some(comment) = &self.comment {
78            write!(f, "COMMENT {} ", QuotedString(comment.clone(), '\''))?;
79        }
80        Ok(())
81    }
82}
83
84#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
85pub struct DropDictionaryStmt {
86    pub if_exists: bool,
87    pub catalog: Option<Identifier>,
88    pub database: Option<Identifier>,
89    pub dictionary_name: Identifier,
90}
91
92impl Display for DropDictionaryStmt {
93    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
94        write!(f, "DROP DICTIONARY ")?;
95        if self.if_exists {
96            write!(f, "IF EXISTS ")?;
97        }
98        write_dot_separated_list(
99            f,
100            self.catalog
101                .iter()
102                .chain(&self.database)
103                .chain(Some(&self.dictionary_name)),
104        )?;
105        Ok(())
106    }
107}
108
109#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
110pub struct ShowCreateDictionaryStmt {
111    pub catalog: Option<Identifier>,
112    pub database: Option<Identifier>,
113    pub dictionary_name: Identifier,
114}
115
116impl Display for ShowCreateDictionaryStmt {
117    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
118        write!(f, "SHOW CREATE DICTIONARY ")?;
119        write_dot_separated_list(
120            f,
121            self.catalog
122                .iter()
123                .chain(&self.database)
124                .chain(Some(&self.dictionary_name)),
125        )
126    }
127}
128
129#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
130pub struct ShowDictionariesStmt {
131    pub database: Option<Identifier>,
132    pub limit: Option<ShowLimit>,
133}
134
135impl Display for ShowDictionariesStmt {
136    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
137        write!(f, "SHOW DICTIONARIES")?;
138        if let Some(database) = &self.database {
139            write!(f, " FROM {database}")?;
140        }
141        if let Some(limit) = &self.limit {
142            write!(f, " {limit}")?;
143        }
144        Ok(())
145    }
146}
147
148#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
149pub struct RenameDictionaryStmt {
150    pub if_exists: bool,
151    pub catalog: Option<Identifier>,
152    pub database: Option<Identifier>,
153    pub dictionary: Identifier,
154    pub new_catalog: Option<Identifier>,
155    pub new_database: Option<Identifier>,
156    pub new_dictionary: Identifier,
157}
158
159impl Display for RenameDictionaryStmt {
160    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
161        write!(f, "RENAME DICTIONARY ")?;
162        if self.if_exists {
163            write!(f, "IF EXISTS ")?;
164        }
165        write_dot_separated_list(
166            f,
167            self.catalog
168                .iter()
169                .chain(&self.database)
170                .chain(Some(&self.dictionary)),
171        )?;
172        write!(f, " TO ")?;
173        write_dot_separated_list(
174            f,
175            self.new_catalog
176                .iter()
177                .chain(&self.new_database)
178                .chain(Some(&self.new_dictionary)),
179        )
180    }
181}