1use super::{JoinBuilder, JoinStatement};
2use crate::QueryBuilder;
3
4pub trait JoinMethods {
5 fn join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
6 fn inner_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
7 fn left_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
8 fn right_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
9 fn left_outer_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
10 fn right_outer_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
11 fn full_outer_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
12 fn cross_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder));
13 fn raw_join(&mut self, raw: &str, val: Option<Vec<serde_json::Value>>);
14}
15
16impl JoinMethods for QueryBuilder {
17 fn join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
18 let mut join = JoinBuilder {
19 table: table.to_string(),
20 statement: vec![],
21 join_type: "JOIN".into(),
22 raw: None,
23 as_name: None,
24 };
25 on(&mut join);
26 self.join.push(join);
27 }
28
29 fn inner_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
30 let mut join = JoinBuilder {
31 table: table.to_string(),
32 statement: vec![],
33 join_type: "INNER JOIN".into(),
34 raw: None,
35 as_name: None,
36 };
37 on(&mut join);
38 self.join.push(join);
39 }
40
41 fn left_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
42 let mut join = JoinBuilder {
43 table: table.to_string(),
44 statement: vec![],
45 join_type: "LEFT JOIN".into(),
46 raw: None,
47 as_name: None,
48 };
49 on(&mut join);
50 self.join.push(join);
51 }
52
53 fn right_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
54 let mut join = JoinBuilder {
55 table: table.to_string(),
56 statement: vec![],
57 join_type: "RIGHT JOIN".into(),
58 raw: None,
59 as_name: None,
60 };
61 on(&mut join);
62 self.join.push(join);
63 }
64
65 fn left_outer_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
66 let mut join = JoinBuilder {
67 table: table.to_string(),
68 statement: vec![],
69 join_type: "LEFT OUTER JOIN".into(),
70 raw: None,
71 as_name: None,
72 };
73 on(&mut join);
74 self.join.push(join);
75 }
76
77 fn right_outer_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
78 let mut join = JoinBuilder {
79 table: table.to_string(),
80 statement: vec![],
81 join_type: "RIGHT OUTER JOIN".into(),
82 raw: None,
83 as_name: None,
84 };
85 on(&mut join);
86 self.join.push(join);
87 }
88
89 fn full_outer_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
90 let mut join = JoinBuilder {
91 table: table.to_string(),
92 statement: vec![],
93 join_type: "FULL OUTER JOIN".into(),
94 raw: None,
95 as_name: None,
96 };
97 on(&mut join);
98 self.join.push(join);
99 }
100
101 fn cross_join(&mut self, table: &str, on: impl FnOnce(&mut JoinBuilder)) {
102 let mut join = JoinBuilder {
103 table: table.to_string(),
104 statement: vec![],
105 join_type: "CROSS JOIN".into(),
106 raw: None,
107 as_name: None,
108 };
109 on(&mut join);
110 self.join.push(join);
111 }
112
113 fn raw_join(&mut self, raw: &str, val: Option<Vec<serde_json::Value>>) {
114 self.join.push(JoinBuilder {
115 table: raw.to_string(),
116 statement: vec![],
117 join_type: "".into(),
118 raw: Some((raw.to_string(), val)),
119 as_name: None,
120 });
121 }
122}
123
124impl JoinBuilder {
125 pub fn as_name(&mut self, name: &str) -> &mut Self {
126 self.as_name = Some(name.to_string());
127 self
128 }
129
130 pub fn on(&mut self, column: &str, operator: &str, column2: &str) -> &mut Self {
131 self.statement.push(JoinStatement::On(
132 column.to_string(),
133 operator.to_string(),
134 column2.to_string(),
135 ));
136 self
137 }
138
139 pub fn or(&mut self) -> &mut JoinBuilder {
140 let mut chain = self.clone();
141 chain.statement = vec![];
142 self.statement.push(JoinStatement::OrChain(Box::new(chain)));
143 self.statement.last_mut().unwrap().as_mut_join_builder()
145 }
146
147 pub fn on_val(&mut self, column: &str, operator: &str, value: serde_json::Value) -> &mut Self {
148 self.statement.push(JoinStatement::OnVal(
149 column.to_string(),
150 operator.to_string(),
151 value,
152 ));
153 self
154 }
155
156 pub fn on_raw(&mut self, raw: &str, val: Option<Vec<serde_json::Value>>) -> &mut Self {
157 self.statement
158 .push(JoinStatement::OnRaw(raw.to_string(), val));
159 self
160 }
161}