use crate::{
behavior::{push_unique, Concat, WithQuery},
fmt,
structure::{Delete, DeleteClause},
};
impl<'a> Delete<'a> {
pub fn and(mut self, condition: &str) -> Self {
self = self.where_clause(condition);
self
}
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_name: &'a str) -> Self {
self._delete_from = table_name.trim();
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_owned());
self
}
pub fn raw_after(mut self, clause: DeleteClause, raw_sql: &str) -> Self {
self._raw_after.push((clause, raw_sql.trim().to_owned()));
self
}
pub fn raw_before(mut self, clause: DeleteClause, raw_sql: &str) -> Self {
self._raw_before.push((clause, raw_sql.trim().to_owned()));
self
}
pub fn where_clause(mut self, condition: &str) -> Self {
push_unique(&mut self._where, condition.trim().to_owned());
self
}
}
#[cfg(any(doc, feature = "postgresql"))]
impl<'a> Delete<'a> {
pub fn returning(mut self, output_name: &str) -> Self {
push_unique(&mut self._returning, output_name.trim().to_owned());
self
}
pub fn with(mut self, name: &'a str, query: impl WithQuery + 'static) -> Self {
self._with.push((name.trim(), std::sync::Arc::new(query)));
self
}
}
impl WithQuery for Delete<'_> {}
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))
}
}