rorm_sql/
delete.rs

1use std::fmt::Write;
2
3use crate::conditional::{BuildCondition, Condition};
4use crate::{DBImpl, Value};
5
6/**
7Trait representing a delete builder.
8*/
9pub trait Delete<'until_build, 'post_query> {
10    /**
11    Adds the a [Condition] to the delete query.
12
13    **Parameter**:
14    - `condition`: Condition to apply to the delete operation
15     */
16    fn where_clause(self, condition: &'until_build Condition<'post_query>) -> Self;
17
18    /**
19    Build the delete operation.
20
21    **Returns**:
22    - SQL query string
23    - List of [Value] parameters to bind to the query.
24    */
25    fn build(self) -> (String, Vec<Value<'post_query>>);
26}
27
28/**
29Representation of the data of a DELETE operation.
30*/
31#[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/**
39Implementation of the [Delete] trait for the different implementations
40
41Should only be constructed via [DBImpl::delete].
42*/
43#[derive(Debug)]
44pub enum DeleteImpl<'until_build, 'post_query> {
45    /**
46    SQLite representation of the DELETE operation.
47     */
48    #[cfg(feature = "sqlite")]
49    SQLite(DeleteData<'until_build, 'post_query>),
50    /**
51    MySQL representation of the DELETE operation.
52     */
53    #[cfg(feature = "mysql")]
54    MySQL(DeleteData<'until_build, 'post_query>),
55    /**
56    Postgres representation of the DELETE operation.
57     */
58    #[cfg(feature = "postgres")]
59    Postgres(DeleteData<'until_build, 'post_query>),
60}
61
62impl<'until_build, 'post_query> Delete<'until_build, 'post_query>
63    for DeleteImpl<'until_build, 'post_query>
64{
65    fn where_clause(mut self, condition: &'until_build Condition<'post_query>) -> Self {
66        match self {
67            #[cfg(feature = "sqlite")]
68            DeleteImpl::SQLite(ref mut data) => data.where_clause = Some(condition),
69            #[cfg(feature = "mysql")]
70            DeleteImpl::MySQL(ref mut data) => data.where_clause = Some(condition),
71            #[cfg(feature = "postgres")]
72            DeleteImpl::Postgres(ref mut data) => data.where_clause = Some(condition),
73        };
74        self
75    }
76
77    fn build(self) -> (String, Vec<Value<'post_query>>) {
78        match self {
79            #[cfg(feature = "sqlite")]
80            DeleteImpl::SQLite(mut d) => {
81                let mut s = format!("DELETE FROM \"{}\" ", d.model);
82
83                if d.where_clause.is_some() {
84                    write!(
85                        s,
86                        "WHERE {} ",
87                        d.where_clause.unwrap().build(DBImpl::SQLite, &mut d.lookup)
88                    )
89                    .unwrap();
90                }
91
92                write!(s, ";").unwrap();
93                (s, d.lookup)
94            }
95            #[cfg(feature = "mysql")]
96            DeleteImpl::MySQL(mut d) => {
97                let mut s = format!("DELETE FROM {} ", d.model);
98
99                if d.where_clause.is_some() {
100                    write!(
101                        s,
102                        "WHERE {} ",
103                        d.where_clause.unwrap().build(DBImpl::MySQL, &mut d.lookup)
104                    )
105                    .unwrap();
106                }
107
108                write!(s, ";").unwrap();
109                (s, d.lookup)
110            }
111            #[cfg(feature = "postgres")]
112            DeleteImpl::Postgres(mut d) => {
113                let mut s = format!("DELETE FROM \"{}\" ", d.model);
114
115                if d.where_clause.is_some() {
116                    write!(
117                        s,
118                        "WHERE {} ",
119                        d.where_clause
120                            .unwrap()
121                            .build(DBImpl::Postgres, &mut d.lookup)
122                    )
123                    .unwrap();
124                }
125
126                write!(s, ";").unwrap();
127                (s, d.lookup)
128            }
129        }
130    }
131}