use crate::{
ConditionHolder, IntoColumnRef, IntoTableRef, OrderExpr, QueryStatement, ReturningClause,
SubQueryStatement, TableRef, Value, WithClause, WithQuery, conditional_statement,
ordered_statement, query_statement_builder, query_statement_writer, statements,
};
#[derive(Default, Debug, Clone, PartialEq)]
pub struct DeleteStatement {
pub(crate) table: Option<Box<TableRef>>,
pub(crate) r#where: ConditionHolder,
pub(crate) orders: Vec<OrderExpr>,
pub(crate) limit: Option<Value>,
pub(crate) returning: Option<ReturningClause>,
pub(crate) with: Option<WithClause>,
}
impl DeleteStatement {
pub fn new() -> Self {
Self::default()
}
pub fn take(&mut self) -> Self {
Self {
table: self.table.take(),
r#where: std::mem::take(&mut self.r#where),
orders: std::mem::take(&mut self.orders),
limit: self.limit.take(),
returning: self.returning.take(),
with: self.with.take(),
}
}
pub fn from_table<T>(&mut self, tbl_ref: T) -> &mut Self
where
T: IntoTableRef,
{
self.table = Some(Box::new(tbl_ref.into_table_ref()));
self
}
pub fn limit(&mut self, limit: u64) -> &mut Self {
self.limit = Some(limit.into());
self
}
pub fn returning(&mut self, returning_cols: ReturningClause) -> &mut Self {
self.returning = Some(returning_cols);
self
}
pub fn returning_col<C>(&mut self, col: C) -> &mut Self
where
C: IntoColumnRef,
{
self.returning(ReturningClause::Columns(vec![col.into_column_ref()]))
}
pub fn returning_all(&mut self) -> &mut Self {
self.returning(ReturningClause::All)
}
pub fn with(self, clause: WithClause) -> WithQuery {
clause.query(self)
}
pub fn with_cte<C: Into<WithClause>>(&mut self, clause: C) -> &mut Self {
self.with = Some(clause.into());
self
}
}
query_statement_builder!(DeleteStatement, statements::prepare_delete_statement);
impl From<DeleteStatement> for QueryStatement {
fn from(s: DeleteStatement) -> Self {
Self::Delete(s)
}
}
impl From<DeleteStatement> for SubQueryStatement {
fn from(s: DeleteStatement) -> Self {
Self::DeleteStatement(s)
}
}
query_statement_writer!(DeleteStatement);
ordered_statement!(DeleteStatement, orders);
conditional_statement!(DeleteStatement);