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
use crate::schema::dialect::SQLDialect;
use crate::schema::value::encode::ToSQLString;

pub struct SQLDeleteFromStatement<'a> {
    pub(crate) from: &'a str,
    pub(crate) r#where: Option<String>,
}

impl<'a> SQLDeleteFromStatement<'a> {

    pub fn r#where(&mut self, r#where: String) -> &mut Self {
        self.r#where = Some(r#where);
        self
    }
}

impl<'a> ToSQLString for SQLDeleteFromStatement<'a> {
    fn to_string(&self, dialect: SQLDialect) -> String {
        let r#where = if let Some(r#where) = &self.r#where {
            if !r#where.is_empty() {
                " WHERE ".to_owned() + r#where
            } else {
                "".to_owned()
            }
        } else {
            "".to_owned()
        };
        let escape = dialect.escape();
        format!("DELETE FROM {}{}{}{}", escape, self.from, escape, r#where)
    }
}