keelson_sqlite/statement/
delete.rs1use keelson_core::clause::{HasReturning, HasWhere, HasWith, Returning, TableRef, Where, With};
2use keelson_core::expr::{Expr, IntoExpr, IntoExprList};
3use keelson_core::{Dialect, Error, Expression, Mod, Query, QueryExtensions, QueryType, SqlWriter};
4
5use super::HasTargetTable;
6use crate::Sqlite;
7
8#[derive(Debug, Clone, Default)]
26pub struct DeleteQuery {
27 pub with: With,
29 pub table: TableRef,
31 pub where_: Where,
33 pub returning: Returning,
35}
36
37impl DeleteQuery {
38 pub fn new() -> DeleteQuery {
40 DeleteQuery::default()
41 }
42
43 pub fn apply(&mut self, mods: impl Mod<DeleteQuery>) {
45 mods.apply(self);
46 }
47}
48
49impl Expression for DeleteQuery {
50 fn write_sql(&self, w: &mut SqlWriter<'_>) {
51 w.write_if(!self.with.is_empty(), "", &self.with, " ");
52
53 if self.table.is_empty() {
54 w.record_error(Error::Incomplete("the table of a DELETE"));
55 return;
56 }
57
58 w.push_str("DELETE FROM ");
59 w.write_expr(&self.table);
60
61 w.write_if(!self.where_.is_empty(), " ", &self.where_, "");
62 w.write_if(!self.returning.is_empty(), " ", &self.returning, "");
63 }
64}
65
66impl Query for DeleteQuery {
67 fn query_type(&self) -> QueryType {
68 QueryType::Delete
69 }
70
71 fn dialect(&self) -> &dyn Dialect {
72 &Sqlite
73 }
74}
75
76impl<H, L, M> QueryExtensions<H, L, M> for DeleteQuery {}
77
78impl IntoExpr for DeleteQuery {
79 fn into_expr(self) -> Expr {
80 crate::query(self)
81 }
82}
83
84impl IntoExprList for DeleteQuery {
85 fn into_expr_list(self) -> Vec<Expr> {
86 vec![self.into_expr()]
87 }
88}
89
90impl HasWith for DeleteQuery {
91 fn with_mut(&mut self) -> &mut With {
92 &mut self.with
93 }
94}
95
96impl HasTargetTable for DeleteQuery {
97 fn target_table_mut(&mut self) -> &mut TableRef {
98 &mut self.table
99 }
100}
101
102impl HasWhere for DeleteQuery {
103 fn where_mut(&mut self) -> &mut Where {
104 &mut self.where_
105 }
106}
107
108impl HasReturning for DeleteQuery {
109 fn returning_mut(&mut self) -> &mut Returning {
110 &mut self.returning
111 }
112}