chain_builder/mysql/
mod.rs

1use serde_json::Value;
2
3// inner
4use crate::{
5    builder::ChainBuilder,
6    common::{
7        join_compiler::join_compiler,
8        method_compiler::{method_compiler_with_provider, ToSqlProvider},
9        statement_compiler::statement_compiler,
10    },
11};
12
13struct MySqlToSqlProvider;
14
15impl ToSqlProvider for MySqlToSqlProvider {
16    fn to_sql(&self, chain_builder: &ChainBuilder) -> (String, Vec<Value>) {
17        merge_to_sql(to_sql(chain_builder))
18    }
19}
20
21#[derive(Debug, Clone, Default)]
22pub struct ToSql {
23    pub statement: (String, Vec<Value>),
24    pub method: (String, Vec<Value>),
25    pub join: (String, Vec<Value>),
26    pub raw: (String, Vec<Value>),
27    pub sql_with: (String, Vec<Value>),
28    pub sql_union: (String, Vec<Value>),
29    pub limit: Option<usize>,
30    pub offset: Option<usize>,
31    pub group_by: Vec<String>,
32    pub group_by_raw: (String, Vec<Value>),
33    pub having: (String, Vec<Value>),
34    pub order_by: Vec<String>,
35    pub order_by_raw: (String, Vec<Value>),
36}
37
38pub fn to_sql(chain_builder: &ChainBuilder) -> ToSql {
39    // statement compiler
40    let mut statement = statement_compiler(chain_builder);
41    if !statement.0.is_empty() {
42        statement.0 = format!("WHERE {}", statement.0);
43    }
44    // compiler method
45    let method = method_compiler_with_provider(chain_builder, &MySqlToSqlProvider);
46    // join compiler
47    let join = join_compiler(chain_builder, true);
48
49    // QueryCommon
50    // - with
51    let mut with = String::new();
52    let mut with_binds: Vec<serde_json::Value> = vec![];
53    let mut is_first_with = true;
54    //  - union
55    let mut sql_union = String::new();
56    let mut sql_union_binds: Vec<serde_json::Value> = vec![];
57    let mut is_first_union = true;
58    // - limit
59    let mut limit = None;
60    // - offset
61    let mut offset = None;
62    // - group by
63    let mut group_by: Vec<String> = vec![];
64    // - group by raw
65    let mut group_by_raw = String::new();
66    let mut group_by_raw_binds: Vec<serde_json::Value> = vec![];
67    // - having
68    let mut having = String::new();
69    let mut having_binds: Vec<serde_json::Value> = vec![];
70    // - order by
71    let mut order_by: Vec<String> = vec![];
72    // - order by raw
73    let mut order_by_raw = String::new();
74    let mut order_by_raw_binds: Vec<serde_json::Value> = vec![];
75
76    for common in chain_builder.query.query_common.iter() {
77        match common {
78            crate::types::Common::With(alias, recursive, chain_builder) => {
79                with.push_str("WITH");
80                with.push(' ');
81                if !is_first_with {
82                    with.push_str(", ");
83                }
84                is_first_with = false;
85                if *recursive {
86                    with.push_str("RECURSIVE");
87                    with.push(' ');
88                }
89                with.push_str(alias.as_str());
90                with.push_str(" AS (");
91                let sql = merge_to_sql(to_sql(chain_builder));
92                with.push_str(sql.0.as_str());
93                with.push(')');
94                with_binds.extend(sql.1);
95                with.push(' ');
96            }
97            crate::types::Common::Union(is_all, chain_builder) => {
98                if !is_first_union {
99                    sql_union.push(' ');
100                }
101                is_first_union = false;
102                if *is_all {
103                    sql_union.push_str("UNION ALL");
104                } else {
105                    sql_union.push_str("UNION");
106                }
107                sql_union.push(' ');
108                let sql = merge_to_sql(to_sql(chain_builder));
109                sql_union.push_str(sql.0.as_str());
110                sql_union_binds.extend(sql.1);
111            }
112            crate::types::Common::Limit(l) => {
113                limit = Some(*l);
114            }
115            crate::types::Common::Offset(o) => {
116                offset = Some(*o);
117            }
118            crate::types::Common::GroupBy(g) => {
119                group_by.extend(g.clone());
120            }
121            crate::types::Common::GroupByRaw(g, b) => {
122                group_by_raw.push_str(g.as_str());
123                if let Some(b) = b {
124                    group_by_raw_binds.extend(b.clone());
125                }
126            }
127            crate::types::Common::OrderBy(column, order) => {
128                order_by.push(format!("{} {}", column, order));
129            }
130            crate::types::Common::OrderByRaw(sql, val) => {
131                order_by_raw.push_str(sql.as_str());
132                if let Some(val) = val {
133                    order_by_raw_binds.extend(val.clone());
134                }
135            }
136            crate::types::Common::Having(sql, val) => {
137                if !having.is_empty() {
138                    having.push_str(" AND ");
139                }
140                having.push_str(sql);
141                if let Some(val) = val {
142                    having_binds.extend(val.clone());
143                }
144            }
145        }
146    }
147
148    // raw compiler
149    let mut raw_sql = String::new();
150    let mut raw_binds: Vec<serde_json::Value> = vec![];
151    if !chain_builder.query.raw.is_empty() {
152        for (i, raw) in chain_builder.query.raw.iter().enumerate() {
153            if i > 0 {
154                raw_sql.push(' ');
155            }
156            raw_sql.push_str(&raw.0);
157            if let Some(binds) = &raw.1 {
158                raw_binds.extend(binds.clone());
159            }
160        }
161    }
162
163    ToSql {
164        statement,
165        method,
166        join,
167        raw: (raw_sql, raw_binds),
168        sql_with: (with, with_binds),
169        sql_union: (sql_union, sql_union_binds),
170        limit,
171        offset,
172        group_by,
173        group_by_raw: (group_by_raw, group_by_raw_binds),
174        having: (having, having_binds),
175        order_by,
176        order_by_raw: (order_by_raw, order_by_raw_binds),
177    }
178}
179
180pub fn merge_to_sql(to_sql: ToSql) -> (String, Vec<Value>) {
181    let mut select_sql = String::new();
182    let mut select_binds: Vec<serde_json::Value> = vec![];
183
184    // Add all order by
185    // - with,
186    // - method
187    // - join
188    // - statement
189    // - limit
190    // - group by
191    // - group by raw
192    // - order by
193    // - order by raw
194    // - offset
195    // - union
196    // - raw
197
198    if !to_sql.sql_with.0.is_empty() {
199        select_sql.push_str(to_sql.sql_with.0.as_str());
200        select_binds.extend(to_sql.sql_with.1);
201    }
202    if !to_sql.method.0.is_empty() {
203        select_sql.push_str(to_sql.method.0.as_str());
204        select_binds.extend(to_sql.method.1);
205    }
206    if !to_sql.join.0.is_empty() {
207        select_sql.push(' ');
208        select_sql.push_str(to_sql.join.0.as_str());
209        select_binds.extend(to_sql.join.1);
210    }
211    if !to_sql.statement.0.is_empty() {
212        select_sql.push(' ');
213        select_sql.push_str(to_sql.statement.0.as_str());
214        select_binds.extend(to_sql.statement.1);
215    }
216    if !to_sql.group_by.is_empty() {
217        select_sql.push_str(" GROUP BY ");
218        select_sql.push_str(to_sql.group_by.join(", ").as_str());
219    }
220    if !to_sql.group_by_raw.0.is_empty() {
221        select_sql.push_str(" GROUP BY ");
222        select_sql.push_str(to_sql.group_by_raw.0.as_str());
223        select_binds.extend(to_sql.group_by_raw.1);
224    }
225    if !to_sql.having.0.is_empty() {
226        select_sql.push_str(" HAVING ");
227        select_sql.push_str(to_sql.having.0.as_str());
228        select_binds.extend(to_sql.having.1);
229    }
230    if !to_sql.order_by.is_empty() {
231        select_sql.push_str(" ORDER BY ");
232        select_sql.push_str(to_sql.order_by.join(", ").as_str());
233    }
234    if !to_sql.order_by_raw.0.is_empty() {
235        select_sql.push_str(" ORDER BY ");
236        select_sql.push_str(to_sql.order_by_raw.0.as_str());
237        select_binds.extend(to_sql.order_by_raw.1);
238    }
239    if let Some(limit) = to_sql.limit {
240        select_sql.push(' ');
241        select_sql.push_str("LIMIT ?");
242        select_binds.push(serde_json::Value::Number(serde_json::Number::from(limit)));
243    }
244    if let Some(offset) = to_sql.offset {
245        select_sql.push(' ');
246        select_sql.push_str("OFFSET ?");
247        select_binds.push(serde_json::Value::Number(serde_json::Number::from(offset)));
248    }
249    if !to_sql.sql_union.0.is_empty() {
250        select_sql.push(' ');
251        select_sql.push_str(to_sql.sql_union.0.as_str());
252        select_binds.extend(to_sql.sql_union.1);
253    }
254    if !to_sql.raw.0.is_empty() {
255        select_sql.push(' ');
256        select_sql.push_str(to_sql.raw.0.as_str());
257        select_binds.extend(to_sql.raw.1);
258    }
259
260    (select_sql, select_binds)
261}