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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use super::{JoinBuilder, JoinStatement};
use crate::QueryBuilder;

pub trait JoinMethods {
    fn join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
    fn inner_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
    fn left_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
    fn right_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
    fn left_outer_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
    fn right_outer_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
    fn full_outer_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
    fn cross_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
    fn raw_join(&mut self, raw: &str, val: Option<Vec<serde_json::Value>>);
}

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(),
            raw: None,
            as_name: None,
        };
        on(&mut join);
        self.join.push(join);
    }

    fn inner_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
        let mut join = JoinBuilder {
            table: table.to_string(),
            statement: vec![],
            join_type: "INNER JOIN".into(),
            raw: None,
            as_name: None,
        };
        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(),
            raw: None,
            as_name: None,
        };
        on(&mut join);
        self.join.push(join);
    }

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

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

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

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

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

    fn raw_join(&mut self, raw: &str, val: Option<Vec<serde_json::Value>>) {
        self.join.push(JoinBuilder {
            table: raw.to_string(),
            statement: vec![],
            join_type: "".into(),
            raw: Some((raw.to_string(), val)),
            as_name: None,
        });
    }
}

impl JoinBuilder {
    pub fn as_name(&mut self, name: &str) -> &mut Self {
        self.as_name = Some(name.to_string());
        self
    }

    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
    }

    pub fn on_raw(&mut self, raw: &str, val: Option<Vec<serde_json::Value>>) -> &mut Self {
        self.statement
            .push(JoinStatement::OnRaw(raw.to_string(), val));
        self
    }
}