rorm_sql/
drop_table.rs

1/**
2Trait representing a drop table builder.
3*/
4pub trait DropTable {
5    /**
6    Drops the table only, if it exists.
7     */
8    fn if_exists(self) -> Self;
9
10    /**
11    This method is used to build the drop table statement.
12     */
13    fn build(self) -> String;
14}
15
16/**
17The representation of data of the drop table statement.
18*/
19#[derive(Debug, Copy, Clone)]
20pub struct DropTableData<'until_build> {
21    pub(crate) name: &'until_build str,
22    pub(crate) if_exists: bool,
23}
24
25/**
26Implementation of the [DropTable] trait for the different implementations.
27
28Should only be constructed via [crate::DBImpl::drop_table].
29*/
30#[derive(Debug)]
31pub enum DropTableImpl<'until_build> {
32    /**
33    SQLite representation of the DROP TABLE operation.
34     */
35    #[cfg(feature = "sqlite")]
36    SQLite(DropTableData<'until_build>),
37    /**
38    MySQL representation of the DROP TABLE operation.
39     */
40    #[cfg(feature = "mysql")]
41    MySQL(DropTableData<'until_build>),
42    /**
43    Postgres representation of the DROP TABLE operation.
44     */
45    #[cfg(feature = "postgres")]
46    Postgres(DropTableData<'until_build>),
47}
48
49impl DropTable for DropTableImpl<'_> {
50    fn if_exists(mut self) -> Self {
51        match self {
52            #[cfg(feature = "sqlite")]
53            DropTableImpl::SQLite(ref mut d) => d.if_exists = true,
54            #[cfg(feature = "mysql")]
55            DropTableImpl::MySQL(ref mut d) => d.if_exists = true,
56            #[cfg(feature = "postgres")]
57            DropTableImpl::Postgres(ref mut d) => d.if_exists = true,
58        };
59        self
60    }
61
62    fn build(self) -> String {
63        match self {
64            #[cfg(feature = "sqlite")]
65            DropTableImpl::SQLite(d) => format!(
66                "DROP TABLE {}{};",
67                d.name,
68                if d.if_exists { " IF EXISTS" } else { "" }
69            ),
70
71            #[cfg(feature = "mysql")]
72            DropTableImpl::MySQL(d) => format!(
73                "DROP TABLE {}{};",
74                d.name,
75                if d.if_exists { " IF EXISTS" } else { "" }
76            ),
77
78            #[cfg(feature = "postgres")]
79            DropTableImpl::Postgres(d) => format!(
80                "DROP TABLE \"{}\"{};",
81                d.name,
82                if d.if_exists { " IF EXISTS" } else { "" }
83            ),
84        }
85    }
86}