1use std::fmt::Write;
2
3use crate::conditional::{BuildCondition, Condition};
4use crate::{DBImpl, Value};
5
6pub trait Delete<'until_build, 'post_query> {
10 fn where_clause(self, condition: &'until_build Condition<'post_query>) -> Self;
17
18 fn build(self) -> (String, Vec<Value<'post_query>>);
26}
27
28#[derive(Debug)]
32pub struct DeleteData<'until_build, 'post_query> {
33 pub(crate) model: &'until_build str,
34 pub(crate) lookup: Vec<Value<'post_query>>,
35 pub(crate) where_clause: Option<&'until_build Condition<'post_query>>,
36}
37
38#[derive(Debug)]
44pub enum DeleteImpl<'until_build, 'post_query> {
45 #[cfg(feature = "sqlite")]
49 SQLite(DeleteData<'until_build, 'post_query>),
50 #[cfg(feature = "postgres")]
54 Postgres(DeleteData<'until_build, 'post_query>),
55}
56
57impl<'until_build, 'post_query> Delete<'until_build, 'post_query>
58 for DeleteImpl<'until_build, 'post_query>
59{
60 fn where_clause(mut self, condition: &'until_build Condition<'post_query>) -> Self {
61 match self {
62 #[cfg(feature = "sqlite")]
63 DeleteImpl::SQLite(ref mut data) => data.where_clause = Some(condition),
64 #[cfg(feature = "postgres")]
65 DeleteImpl::Postgres(ref mut data) => data.where_clause = Some(condition),
66 };
67 self
68 }
69
70 fn build(self) -> (String, Vec<Value<'post_query>>) {
71 match self {
72 #[cfg(feature = "sqlite")]
73 DeleteImpl::SQLite(mut d) => {
74 let mut s = format!("DELETE FROM \"{}\" ", d.model);
75
76 if d.where_clause.is_some() {
77 write!(
78 s,
79 "WHERE {} ",
80 d.where_clause.unwrap().build(DBImpl::SQLite, &mut d.lookup)
81 )
82 .unwrap();
83 }
84
85 write!(s, ";").unwrap();
86 (s, d.lookup)
87 }
88 #[cfg(feature = "postgres")]
89 DeleteImpl::Postgres(mut d) => {
90 let mut s = format!("DELETE FROM \"{}\" ", d.model);
91
92 if d.where_clause.is_some() {
93 write!(
94 s,
95 "WHERE {} ",
96 d.where_clause
97 .unwrap()
98 .build(DBImpl::Postgres, &mut d.lookup)
99 )
100 .unwrap();
101 }
102
103 write!(s, ";").unwrap();
104 (s, d.lookup)
105 }
106 }
107 }
108}