sea_query/table/
drop.rs

1use inherent::inherent;
2
3use crate::{SchemaStatementBuilder, backend::SchemaBuilder, types::*};
4
5/// Drop a table
6///
7/// # Examples
8///
9/// ```
10/// use sea_query::{tests_cfg::*, *};
11///
12/// let table = Table::drop()
13///     .table(Glyph::Table)
14///     .table(Char::Table)
15///     .to_owned();
16///
17/// assert_eq!(
18///     table.to_string(MysqlQueryBuilder),
19///     r#"DROP TABLE `glyph`, `character`"#
20/// );
21/// assert_eq!(
22///     table.to_string(PostgresQueryBuilder),
23///     r#"DROP TABLE "glyph", "character""#
24/// );
25/// assert_eq!(
26///     table.to_string(SqliteQueryBuilder),
27///     r#"DROP TABLE "glyph", "character""#
28/// );
29/// ```
30#[derive(Default, Debug, Clone)]
31pub struct TableDropStatement {
32    pub(crate) tables: Vec<TableRef>,
33    pub(crate) options: Vec<TableDropOpt>,
34    pub(crate) if_exists: bool,
35}
36
37/// All available table drop options
38#[derive(Debug, Clone)]
39#[non_exhaustive]
40pub enum TableDropOpt {
41    Restrict,
42    Cascade,
43}
44
45impl TableDropStatement {
46    /// Construct drop table statement
47    pub fn new() -> Self {
48        Self::default()
49    }
50
51    /// Set table name
52    pub fn table<T>(&mut self, table: T) -> &mut Self
53    where
54        T: IntoTableRef,
55    {
56        self.tables.push(table.into_table_ref());
57        self
58    }
59
60    /// Drop table if exists
61    pub fn if_exists(&mut self) -> &mut Self {
62        self.if_exists = true;
63        self
64    }
65
66    /// Drop option restrict
67    pub fn restrict(&mut self) -> &mut Self {
68        self.options.push(TableDropOpt::Restrict);
69        self
70    }
71
72    /// Drop option cacade
73    pub fn cascade(&mut self) -> &mut Self {
74        self.options.push(TableDropOpt::Cascade);
75        self
76    }
77
78    pub fn take(&mut self) -> Self {
79        Self {
80            tables: std::mem::take(&mut self.tables),
81            options: std::mem::take(&mut self.options),
82            if_exists: self.if_exists,
83        }
84    }
85}
86
87#[inherent]
88impl SchemaStatementBuilder for TableDropStatement {
89    pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String {
90        let mut sql = String::with_capacity(256);
91        schema_builder.prepare_table_drop_statement(self, &mut sql);
92        sql
93    }
94
95    pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String {
96        let mut sql = String::with_capacity(256);
97        schema_builder.prepare_table_drop_statement(self, &mut sql);
98        sql
99    }
100
101    pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String;
102}