1pub trait DropIndex {
5 fn if_exists(self) -> Self;
9
10 fn build(self) -> String;
14}
15
16#[derive(Debug, Copy, Clone)]
20pub struct DropIndexData<'until_build> {
21 pub(crate) name: &'until_build str,
22 pub(crate) if_exists: bool,
23}
24
25#[derive(Debug)]
31pub enum DropIndexImpl<'until_build> {
32 #[cfg(feature = "sqlite")]
36 SQLite(DropIndexData<'until_build>),
37 #[cfg(feature = "postgres")]
41 Postgres(DropIndexData<'until_build>),
42}
43
44impl DropIndex for DropIndexImpl<'_> {
45 fn if_exists(mut self) -> Self {
46 match self {
47 #[cfg(feature = "sqlite")]
48 DropIndexImpl::SQLite(ref mut d) => d.if_exists = true,
49 #[cfg(feature = "postgres")]
50 DropIndexImpl::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 DropIndexImpl::SQLite(d) => format!(
59 "DROP INDEX{} \"{}\";",
60 if d.if_exists { " IF EXISTS" } else { "" },
61 d.name
62 ),
63
64 #[cfg(feature = "postgres")]
65 DropIndexImpl::Postgres(d) => format!(
66 "DROP INDEX{} \"{}\";",
67 if d.if_exists { " IF EXISTS" } else { "" },
68 d.name
69 ),
70 }
71 }
72}
73
74#[cfg(test)]
75mod test {
76 use crate::drop_index::DropIndex;
77 use crate::DBImpl;
78
79 #[cfg(feature = "sqlite")]
80 #[test]
81 fn drop_index_sqlite() {
82 assert_eq!(
83 DBImpl::SQLite.drop_index("user_login_idx").build(),
84 r#"DROP INDEX "user_login_idx";"#
85 );
86 }
87
88 #[cfg(feature = "postgres")]
89 #[test]
90 fn drop_index_postgres() {
91 assert_eq!(
92 DBImpl::Postgres.drop_index("user_login_idx").build(),
93 r#"DROP INDEX "user_login_idx";"#
94 );
95 }
96
97 #[cfg(feature = "sqlite")]
98 #[test]
99 fn drop_index_if_exists() {
100 assert_eq!(
101 DBImpl::SQLite
102 .drop_index("user_login_idx")
103 .if_exists()
104 .build(),
105 r#"DROP INDEX IF EXISTS "user_login_idx";"#
106 );
107 }
108}