databend_common_ast/ast/statements/
view.rs1use 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::Query;
24use crate::ast::ShowLimit;
25use crate::ast::write_comma_separated_list;
26use crate::ast::write_dot_separated_list;
27
28#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
29pub struct CreateViewStmt {
30 pub create_option: CreateOption,
31 pub catalog: Option<Identifier>,
32 pub database: Option<Identifier>,
33 pub view: Identifier,
34 pub columns: Vec<Identifier>,
35 pub query: Box<Query>,
36}
37
38impl Display for CreateViewStmt {
39 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
40 write!(f, "CREATE ")?;
41 if let CreateOption::CreateOrReplace = self.create_option {
42 write!(f, "OR REPLACE ")?;
43 }
44 write!(f, "VIEW ")?;
45 if let CreateOption::CreateIfNotExists = self.create_option {
46 write!(f, "IF NOT EXISTS ")?;
47 }
48 write_dot_separated_list(
49 f,
50 self.catalog
51 .iter()
52 .chain(&self.database)
53 .chain(Some(&self.view)),
54 )?;
55 if !self.columns.is_empty() {
56 write!(f, " (")?;
57 write_comma_separated_list(f, &self.columns)?;
58 write!(f, ")")?;
59 }
60 write!(f, " AS {}", self.query)
61 }
62}
63
64#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
65pub struct AlterViewStmt {
66 pub catalog: Option<Identifier>,
67 pub database: Option<Identifier>,
68 pub view: Identifier,
69 pub columns: Vec<Identifier>,
70 pub query: Box<Query>,
71}
72
73impl Display for AlterViewStmt {
74 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
75 write!(f, "ALTER VIEW ")?;
76 write_dot_separated_list(
77 f,
78 self.catalog
79 .iter()
80 .chain(&self.database)
81 .chain(Some(&self.view)),
82 )?;
83 if !self.columns.is_empty() {
84 write!(f, " (")?;
85 write_comma_separated_list(f, &self.columns)?;
86 write!(f, ")")?;
87 }
88 write!(f, " AS {}", self.query)
89 }
90}
91
92#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
93pub struct DropViewStmt {
94 pub if_exists: bool,
95 pub catalog: Option<Identifier>,
96 pub database: Option<Identifier>,
97 pub view: Identifier,
98}
99
100impl Display for DropViewStmt {
101 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
102 write!(f, "DROP VIEW ")?;
103 if self.if_exists {
104 write!(f, "IF EXISTS ")?;
105 }
106 write_dot_separated_list(
107 f,
108 self.catalog
109 .iter()
110 .chain(&self.database)
111 .chain(Some(&self.view)),
112 )
113 }
114}
115
116#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
117pub struct ShowViewsStmt {
118 pub catalog: Option<Identifier>,
119 pub database: Option<Identifier>,
120 pub full: bool,
121 pub limit: Option<ShowLimit>,
122 pub with_history: bool,
123}
124
125impl Display for ShowViewsStmt {
126 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
127 write!(f, "SHOW")?;
128 if self.full {
129 write!(f, " FULL")?;
130 }
131 write!(f, " VIEWS")?;
132 if self.with_history {
133 write!(f, " HISTORY")?;
134 }
135 if let Some(database) = &self.database {
136 write!(f, " FROM ")?;
137 if let Some(catalog) = &self.catalog {
138 write!(f, "{catalog}.",)?;
139 }
140 write!(f, "{database}")?;
141 }
142 if let Some(limit) = &self.limit {
143 write!(f, " {limit}")?;
144 }
145
146 Ok(())
147 }
148}
149
150#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
151pub struct DescribeViewStmt {
152 pub catalog: Option<Identifier>,
153 pub database: Option<Identifier>,
154 pub view: Identifier,
155}
156
157impl Display for DescribeViewStmt {
158 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
159 write!(f, "DESCRIBE VIEW ")?;
160 write_dot_separated_list(
161 f,
162 self.catalog
163 .iter()
164 .chain(self.database.iter().chain(Some(&self.view))),
165 )
166 }
167}