use crate::{
behavior::TransactionQuery,
concat::Concat,
fmt,
structure::{Delete, DeleteClause, LogicalOperator},
utils::push_unique,
};
impl TransactionQuery for Delete {}
impl Delete {
pub fn as_string(&self) -> String {
let fmts = fmt::one_line();
self.concat(&fmts)
}
pub fn debug(self) -> Self {
let fmts = fmt::multiline();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}
pub fn delete_from(mut self, table: &str) -> Self {
self._delete_from = table.trim().to_string();
self
}
pub fn new() -> Self {
Self::default()
}
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, clause: DeleteClause, raw_sql: &str) -> Self {
self._raw_after.push((clause, raw_sql.trim().to_string()));
self
}
pub fn raw_before(mut self, clause: DeleteClause, raw_sql: &str) -> Self {
self._raw_before.push((clause, raw_sql.trim().to_string()));
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(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
use crate::behavior::WithQuery;
#[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
impl WithQuery for Delete {}
#[cfg(any(doc, feature = "postgresql", feature = "sqlite", feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl Delete {
#[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
pub fn with(mut self, name: &str, query: impl WithQuery + 'static + Send + Sync) -> Self {
self._with.push((name.trim().to_string(), std::sync::Arc::new(query)));
self
}
}
#[cfg(any(doc, feature = "postgresql", feature = "sqlite"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
impl Delete {
pub fn returning(mut self, output_name: &str) -> Self {
push_unique(&mut self._returning, output_name.trim().to_string());
self
}
}
#[cfg(any(doc, feature = "sqlite", feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl Delete {
pub fn order_by(mut self, column: &str) -> Self {
push_unique(&mut self._order_by, column.trim().to_string());
self
}
}
#[cfg(any(doc, feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl Delete {
pub fn delete(mut self, table: &str) -> Self {
push_unique(&mut self._delete, table.trim().to_string());
self
}
pub fn from(mut self, table: &str) -> Self {
push_unique(&mut self._from, table.trim().to_string());
self
}
pub fn cross_join(mut self, table: &str) -> Self {
let table = table.trim();
if table.is_empty() == false {
let join = format!("CROSS JOIN {table}");
push_unique(&mut self._join, join);
}
self
}
pub fn inner_join(mut self, table: &str) -> Self {
let table = table.trim();
if table.is_empty() == false {
let join = format!("INNER JOIN {table}");
push_unique(&mut self._join, join);
}
self
}
pub fn left_join(mut self, table: &str) -> Self {
let table = table.trim();
if table.is_empty() == false {
let join = format!("LEFT JOIN {table}");
push_unique(&mut self._join, join);
}
self
}
pub fn right_join(mut self, table: &str) -> Self {
let table = table.trim();
if table.is_empty() == false {
let join = format!("RIGHT JOIN {table}");
push_unique(&mut self._join, join);
}
self
}
pub fn limit(mut self, num: &str) -> Self {
self._limit = num.trim().to_string();
self
}
pub fn partition(mut self, name: &str) -> Self {
push_unique(&mut self._partition, name.trim().to_string());
self
}
}
impl std::fmt::Display for Delete {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}
impl std::fmt::Debug for Delete {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let fmts = fmt::multiline();
write!(f, "{}", fmt::format(self.concat(&fmts), &fmts))
}
}