Skip to main content

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    Postgres representation of the DELETE operation.
52     */
53    #[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}