use crate::{
behavior::TransactionQuery,
concat::Concat,
fmt,
structure::{CreateIndex, CreateIndexParams},
utils::push_unique,
};
impl TransactionQuery for CreateIndex {}
impl CreateIndex {
pub fn as_string(&self) -> String {
let fmts = fmt::one_line();
self.concat(&fmts)
}
pub fn column(mut self, column_name: &str) -> Self {
push_unique(&mut self._column, column_name.trim().to_string());
self
}
pub fn create_index(mut self, index_name: &str) -> Self {
self._index_name = index_name.trim().to_string();
self._create_index = true;
self
}
pub fn debug(self) -> Self {
let fmts = fmt::multiline();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}
pub fn new() -> Self {
Self::default()
}
pub fn on(mut self, table_name: &str) -> Self {
self._on = table_name.trim().to_string();
self
}
pub fn print(self) -> Self {
let fmts = fmt::one_line();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}
pub fn raw(mut self, raw_sql: &str) -> Self {
push_unique(&mut self._raw, raw_sql.trim().to_string());
self
}
pub fn raw_after(mut self, param: CreateIndexParams, raw_sql: &str) -> Self {
self._raw_after.push((param, raw_sql.trim().to_string()));
self
}
pub fn raw_before(mut self, param: CreateIndexParams, raw_sql: &str) -> Self {
self._raw_before.push((param, raw_sql.trim().to_string()));
self
}
pub fn unique(mut self) -> Self {
self._unique = true;
#[cfg(feature = "mysql")]
{
self._fulltext = false;
self._spatial = false;
}
self
}
}
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
use crate::structure::LogicalOperator;
#[cfg(any(doc, feature = "postgresql", feature = "sqlite"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
impl CreateIndex {
pub fn create_index_if_not_exists(mut self, index_name: &str) -> Self {
self._index_name = index_name.trim().to_string();
self._create_index = true;
self._if_not_exists = true;
self
}
pub fn where_and(self, condition: &str) -> Self {
self.where_clause(condition)
}
pub fn where_clause(mut self, condition: &str) -> Self {
push_unique(&mut self._where, (LogicalOperator::And, condition.trim().to_string()));
self
}
pub fn where_or(mut self, condition: &str) -> Self {
push_unique(&mut self._where, (LogicalOperator::Or, condition.trim().to_string()));
self
}
}
#[cfg(any(doc, feature = "postgresql", feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl CreateIndex {
pub fn using(mut self, index_method: &str) -> Self {
self._using = index_method.trim().to_string();
self
}
}
#[cfg(any(doc, feature = "postgresql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
impl CreateIndex {
pub fn concurrently(mut self) -> Self {
self._concurrently = true;
self
}
pub fn include(mut self, column_name: &str) -> Self {
push_unique(&mut self._include, column_name.trim().to_string());
self
}
pub fn only(mut self) -> Self {
self._only = true;
self
}
}
#[cfg(any(doc, feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl CreateIndex {
pub fn fulltext(mut self) -> Self {
self._fulltext = true;
self._unique = false;
self._spatial = false;
self
}
pub fn lock(mut self, lock_option: &str) -> Self {
self._lock = lock_option.trim().to_string();
self
}
pub fn spatial(mut self) -> Self {
self._spatial = true;
self._unique = false;
self._fulltext = false;
self
}
}
impl std::fmt::Display for CreateIndex {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}
impl std::fmt::Debug for CreateIndex {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let fmts = fmt::multiline();
write!(f, "{}", fmt::format(self.concat(&fmts), &fmts))
}
}