pub trait DropIndex {
fn if_exists(self) -> Self;
fn build(self) -> String;
}
#[derive(Debug, Copy, Clone)]
pub struct DropIndexData<'until_build> {
pub(crate) name: &'until_build str,
pub(crate) if_exists: bool,
}
#[derive(Debug)]
pub enum DropIndexImpl<'until_build> {
#[cfg(feature = "sqlite")]
SQLite(DropIndexData<'until_build>),
#[cfg(feature = "postgres")]
Postgres(DropIndexData<'until_build>),
}
impl DropIndex for DropIndexImpl<'_> {
fn if_exists(mut self) -> Self {
match self {
#[cfg(feature = "sqlite")]
DropIndexImpl::SQLite(ref mut d) => d.if_exists = true,
#[cfg(feature = "postgres")]
DropIndexImpl::Postgres(ref mut d) => d.if_exists = true,
};
self
}
fn build(self) -> String {
match self {
#[cfg(feature = "sqlite")]
DropIndexImpl::SQLite(d) => format!(
"DROP INDEX{} \"{}\";",
if d.if_exists { " IF EXISTS" } else { "" },
d.name
),
#[cfg(feature = "postgres")]
DropIndexImpl::Postgres(d) => format!(
"DROP INDEX{} \"{}\";",
if d.if_exists { " IF EXISTS" } else { "" },
d.name
),
}
}
}
#[cfg(test)]
mod test {
use crate::drop_index::DropIndex;
use crate::DBImpl;
#[cfg(feature = "sqlite")]
#[test]
fn drop_index_sqlite() {
assert_eq!(
DBImpl::SQLite.drop_index("user_login_idx").build(),
r#"DROP INDEX "user_login_idx";"#
);
}
#[cfg(feature = "postgres")]
#[test]
fn drop_index_postgres() {
assert_eq!(
DBImpl::Postgres.drop_index("user_login_idx").build(),
r#"DROP INDEX "user_login_idx";"#
);
}
#[cfg(feature = "sqlite")]
#[test]
fn drop_index_if_exists() {
assert_eq!(
DBImpl::SQLite
.drop_index("user_login_idx")
.if_exists()
.build(),
r#"DROP INDEX IF EXISTS "user_login_idx";"#
);
}
}