reinhardt_query/query/
drop_table.rs1use crate::{
6 backend::QueryBuilder,
7 types::{IntoTableRef, TableRef},
8};
9
10use super::traits::{QueryBuilderTrait, QueryStatementBuilder, QueryStatementWriter};
11
12#[derive(Debug, Clone)]
26pub struct DropTableStatement {
27 pub(crate) tables: Vec<TableRef>,
28 pub(crate) if_exists: bool,
29 pub(crate) cascade: bool,
30 pub(crate) restrict: bool,
31}
32
33impl DropTableStatement {
34 pub fn new() -> Self {
36 Self {
37 tables: Vec::new(),
38 if_exists: false,
39 cascade: false,
40 restrict: false,
41 }
42 }
43
44 pub fn take(&mut self) -> Self {
46 Self {
47 tables: std::mem::take(&mut self.tables),
48 if_exists: self.if_exists,
49 cascade: self.cascade,
50 restrict: self.restrict,
51 }
52 }
53
54 pub fn table<T>(&mut self, tbl: T) -> &mut Self
65 where
66 T: IntoTableRef,
67 {
68 self.tables.push(tbl.into_table_ref());
69 self
70 }
71
72 pub fn tables<I, T>(&mut self, tbls: I) -> &mut Self
83 where
84 I: IntoIterator<Item = T>,
85 T: IntoTableRef,
86 {
87 for tbl in tbls {
88 self.tables.push(tbl.into_table_ref());
89 }
90 self
91 }
92
93 pub fn if_exists(&mut self) -> &mut Self {
105 self.if_exists = true;
106 self
107 }
108
109 pub fn cascade(&mut self) -> &mut Self {
124 self.cascade = true;
125 self.restrict = false;
126 self
127 }
128
129 pub fn restrict(&mut self) -> &mut Self {
144 self.restrict = true;
145 self.cascade = false;
146 self
147 }
148}
149
150impl Default for DropTableStatement {
151 fn default() -> Self {
152 Self::new()
153 }
154}
155
156impl QueryStatementBuilder for DropTableStatement {
157 fn build_any(&self, query_builder: &dyn QueryBuilderTrait) -> (String, crate::value::Values) {
158 use std::any::Any;
160 if let Some(builder) =
161 (query_builder as &dyn Any).downcast_ref::<crate::backend::PostgresQueryBuilder>()
162 {
163 return builder.build_drop_table(self);
164 }
165 if let Some(builder) =
166 (query_builder as &dyn Any).downcast_ref::<crate::backend::MySqlQueryBuilder>()
167 {
168 return builder.build_drop_table(self);
169 }
170 if let Some(builder) =
171 (query_builder as &dyn Any).downcast_ref::<crate::backend::SqliteQueryBuilder>()
172 {
173 return builder.build_drop_table(self);
174 }
175 panic!("Unsupported query builder type");
176 }
177}
178
179impl QueryStatementWriter for DropTableStatement {}