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

pub enum WhereClause {
    And(Vec<String>),
    Or(Vec<String>),
    Not(String),
}

pub trait ToWrappedSQLString {
    fn to_wrapped_string(&self, dialect: SQLDialect) -> String;
}

impl ToWrappedSQLString for WhereClause {
    fn to_wrapped_string(&self, dialect: SQLDialect) -> String {
        "(".to_owned() + &self.to_string(dialect) + ")"
    }
}

impl ToSQLString for WhereClause {
    fn to_string(&self, _dialect: SQLDialect) -> String {
        match self {
            WhereClause::And(items) => items.join(" AND "),
            WhereClause::Or(items) => items.join(" OR "),
            WhereClause::Not(item) => format!("NOT {item}"),
        }
    }
}

pub struct WhereItem<'a>(&'a str, &'a str, &'a str);

impl<'a> ToSQLString for WhereItem<'a> {
    fn to_string(&self, _dialect: SQLDialect) -> String {
        format!("{} {} {}", self.0, self.1, self.2)
    }
}