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
use super::{JoinBuilder, JoinStatement};
use crate::QueryBuilder;

pub trait JoinMethods {
    fn join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
    fn left_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
}

impl JoinMethods for QueryBuilder {
    fn join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
        let mut join = JoinBuilder {
            table: table.to_string(),
            statement: vec![],
            join_type: "JOIN".into(),
        };
        on(&mut join);
        self.join.push(join);
    }

    fn left_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
        let mut join = JoinBuilder {
            table: table.to_string(),
            statement: vec![],
            join_type: "LEFT JOIN".into(),
        };
        on(&mut join);
        self.join.push(join);
    }
}

impl JoinBuilder {
    pub fn on(&mut self, column: &str, operator: &str, column2: &str) -> &mut Self {
        self.statement.push(JoinStatement::On(
            column.to_string(),
            operator.to_string(),
            column2.to_string(),
        ));
        self
    }

    pub fn or(&mut self) -> &mut JoinBuilder {
        let mut chain = self.clone();
        chain.statement = vec![];
        self.statement.push(JoinStatement::OrChain(Box::new(chain)));
        // SAFETY: unwrap() is safe because we just pushed an OrChain
        self.statement.last_mut().unwrap().as_mut_join_builder()
    }

    pub fn on_val(&mut self, column: &str, operator: &str, value: serde_json::Value) -> &mut Self {
        self.statement.push(JoinStatement::OnVal(
            column.to_string(),
            operator.to_string(),
            value,
        ));
        self
    }
}