1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::fmt::Write;

use crate::conditional::{BuildCondition, Condition};
use crate::{DBImpl, Value};

/**
Trait representing a delete builder.
*/
pub trait Delete<'until_build, 'post_query> {
    /**
    Adds the a [Condition] to the delete query.

    **Parameter**:
    - `condition`: Condition to apply to the delete operation
     */
    fn where_clause(self, condition: &'until_build Condition<'post_query>) -> Self;

    /**
    Build the delete operation.

    **Returns**:
    - SQL query string
    - List of [Value] parameters to bind to the query.
    */
    fn build(self) -> (String, Vec<Value<'post_query>>);
}

/**
Representation of the data of a DELETE operation.
*/
#[derive(Debug)]
pub struct DeleteData<'until_build, 'post_query> {
    pub(crate) model: &'until_build str,
    pub(crate) lookup: Vec<Value<'post_query>>,
    pub(crate) where_clause: Option<&'until_build Condition<'post_query>>,
}

/**
Implementation of the [Delete] trait for the different implementations

Should only be constructed via [DBImpl::delete].
*/
#[derive(Debug)]
pub enum DeleteImpl<'until_build, 'post_query> {
    /**
    SQLite representation of the DELETE operation.
     */
    #[cfg(feature = "sqlite")]
    SQLite(DeleteData<'until_build, 'post_query>),
    /**
    MySQL representation of the DELETE operation.
     */
    #[cfg(feature = "mysql")]
    MySQL(DeleteData<'until_build, 'post_query>),
    /**
    Postgres representation of the DELETE operation.
     */
    #[cfg(feature = "postgres")]
    Postgres(DeleteData<'until_build, 'post_query>),
}

impl<'until_build, 'post_query> Delete<'until_build, 'post_query>
    for DeleteImpl<'until_build, 'post_query>
{
    fn where_clause(mut self, condition: &'until_build Condition<'post_query>) -> Self {
        match self {
            #[cfg(feature = "sqlite")]
            DeleteImpl::SQLite(ref mut data) => data.where_clause = Some(condition),
            #[cfg(feature = "mysql")]
            DeleteImpl::MySQL(ref mut data) => data.where_clause = Some(condition),
            #[cfg(feature = "postgres")]
            DeleteImpl::Postgres(ref mut data) => data.where_clause = Some(condition),
        };
        self
    }

    fn build(self) -> (String, Vec<Value<'post_query>>) {
        match self {
            #[cfg(feature = "sqlite")]
            DeleteImpl::SQLite(mut d) => {
                let mut s = format!("DELETE FROM {} ", d.model);

                if d.where_clause.is_some() {
                    write!(
                        s,
                        "WHERE {} ",
                        d.where_clause.unwrap().build(DBImpl::SQLite, &mut d.lookup)
                    )
                    .unwrap();
                }

                write!(s, ";").unwrap();
                (s, d.lookup)
            }
            #[cfg(feature = "mysql")]
            DeleteImpl::MySQL(mut d) => {
                let mut s = format!("DELETE FROM {} ", d.model);

                if d.where_clause.is_some() {
                    write!(
                        s,
                        "WHERE {} ",
                        d.where_clause.unwrap().build(DBImpl::MySQL, &mut d.lookup)
                    )
                    .unwrap();
                }

                write!(s, ";").unwrap();
                (s, d.lookup)
            }
            #[cfg(feature = "postgres")]
            DeleteImpl::Postgres(mut d) => {
                let mut s = format!("DELETE FROM \"{}\" ", d.model);

                if d.where_clause.is_some() {
                    write!(
                        s,
                        "WHERE {} ",
                        d.where_clause
                            .unwrap()
                            .build(DBImpl::Postgres, &mut d.lookup)
                    )
                    .unwrap();
                }

                write!(s, ";").unwrap();
                (s, d.lookup)
            }
        }
    }
}