Skip to main content

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    Postgres representation of the DROP TABLE operation.
39     */
40    #[cfg(feature = "postgres")]
41    Postgres(DropTableData<'until_build>),
42}
43
44impl DropTable for DropTableImpl<'_> {
45    fn if_exists(mut self) -> Self {
46        match self {
47            #[cfg(feature = "sqlite")]
48            DropTableImpl::SQLite(ref mut d) => d.if_exists = true,
49            #[cfg(feature = "postgres")]
50            DropTableImpl::Postgres(ref mut d) => d.if_exists = true,
51        };
52        self
53    }
54
55    fn build(self) -> String {
56        match self {
57            #[cfg(feature = "sqlite")]
58            DropTableImpl::SQLite(d) => format!(
59                "DROP TABLE \"{}\"{};",
60                d.name,
61                if d.if_exists { " IF EXISTS" } else { "" }
62            ),
63            #[cfg(feature = "postgres")]
64            DropTableImpl::Postgres(d) => format!(
65                "DROP TABLE \"{}\"{};",
66                d.name,
67                if d.if_exists { " IF EXISTS" } else { "" }
68            ),
69        }
70    }
71}