use keelson_core::clause::{HasReturning, HasWhere, HasWith, Returning, TableRef, Where, With};
use keelson_core::expr::{Expr, IntoExpr, IntoExprList};
use keelson_core::{Dialect, Error, Expression, Mod, Query, QueryExtensions, QueryType, SqlWriter};
use super::HasTargetTable;
use crate::Sqlite;
#[derive(Debug, Clone, Default)]
pub struct DeleteQuery {
pub with: With,
pub table: TableRef,
pub where_: Where,
pub returning: Returning,
}
impl DeleteQuery {
pub fn new() -> DeleteQuery {
DeleteQuery::default()
}
pub fn apply(&mut self, mods: impl Mod<DeleteQuery>) {
mods.apply(self);
}
}
impl Expression for DeleteQuery {
fn write_sql(&self, w: &mut SqlWriter<'_>) {
w.write_if(!self.with.is_empty(), "", &self.with, " ");
if self.table.is_empty() {
w.record_error(Error::Incomplete("the table of a DELETE"));
return;
}
w.push_str("DELETE FROM ");
w.write_expr(&self.table);
w.write_if(!self.where_.is_empty(), " ", &self.where_, "");
w.write_if(!self.returning.is_empty(), " ", &self.returning, "");
}
}
impl Query for DeleteQuery {
fn query_type(&self) -> QueryType {
QueryType::Delete
}
fn dialect(&self) -> &dyn Dialect {
&Sqlite
}
}
impl<H, L, M> QueryExtensions<H, L, M> for DeleteQuery {}
impl IntoExpr for DeleteQuery {
fn into_expr(self) -> Expr {
crate::query(self)
}
}
impl IntoExprList for DeleteQuery {
fn into_expr_list(self) -> Vec<Expr> {
vec![self.into_expr()]
}
}
impl HasWith for DeleteQuery {
fn with_mut(&mut self) -> &mut With {
&mut self.with
}
}
impl HasTargetTable for DeleteQuery {
fn target_table_mut(&mut self) -> &mut TableRef {
&mut self.table
}
}
impl HasWhere for DeleteQuery {
fn where_mut(&mut self) -> &mut Where {
&mut self.where_
}
}
impl HasReturning for DeleteQuery {
fn returning_mut(&mut self) -> &mut Returning {
&mut self.returning
}
}