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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/// this the py_sql syntax tree
pub mod bind_node;
pub mod choose_node;
pub mod continue_node;
pub mod error;
pub mod foreach_node;
pub mod if_node;
pub mod otherwise_node;
pub mod set_node;
pub mod sql_node;
pub mod string_node;
pub mod trim_node;
pub mod when_node;
pub mod where_node;

use crate::codegen::loader_html::Element;
use crate::codegen::syntax_tree_pysql::bind_node::BindNode;
use crate::codegen::syntax_tree_pysql::choose_node::ChooseNode;
use crate::codegen::syntax_tree_pysql::continue_node::ContinueNode;
use crate::codegen::syntax_tree_pysql::foreach_node::ForEachNode;
use crate::codegen::syntax_tree_pysql::if_node::IfNode;
use crate::codegen::syntax_tree_pysql::otherwise_node::OtherwiseNode;
use crate::codegen::syntax_tree_pysql::set_node::SetNode;
use crate::codegen::syntax_tree_pysql::sql_node::SqlNode;
use crate::codegen::syntax_tree_pysql::string_node::StringNode;
use crate::codegen::syntax_tree_pysql::trim_node::TrimNode;
use crate::codegen::syntax_tree_pysql::when_node::WhenNode;
use crate::codegen::syntax_tree_pysql::where_node::WhereNode;
use std::collections::HashMap;

/// the pysql syntax tree
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum NodeType {
    NString(StringNode),
    NIf(IfNode),
    NTrim(TrimNode),
    NForEach(ForEachNode),
    NChoose(ChooseNode),
    NOtherwise(OtherwiseNode),
    NWhen(WhenNode),
    NBind(BindNode),
    NSet(SetNode),
    NWhere(WhereNode),
    NContinue(ContinueNode),
    NSql(SqlNode),
}

/// the node name
pub trait Name {
    fn name() -> &'static str;
}

/// node default name
pub trait DefaultName {
    fn default_name() -> &'static str;
}

/// Convert syntax tree to HTML deconstruction
pub trait AsHtml {
    fn as_html(&self) -> String;
}

impl AsHtml for StringNode {
    fn as_html(&self) -> String {
        if self.value.starts_with("`") && self.value.ends_with("`") {
            self.value.to_string()
        } else {
            let mut v = self.value.clone();
            v.insert(0, '`');
            v.push('`');
            v
        }
    }
}

impl AsHtml for IfNode {
    fn as_html(&self) -> String {
        let mut childs = String::new();
        for x in &self.childs {
            childs.push_str(&x.as_html());
        }
        format!("<if test=\"{}\">{}</if>", self.test, childs)
    }
}

impl AsHtml for TrimNode {
    fn as_html(&self) -> String {
        let mut childs = String::new();
        for x in &self.childs {
            childs.push_str(&x.as_html());
        }
        format!(
            "<trim prefixOverrides=\"{}\" suffixOverrides=\"{}\">{}</trim>",
            self.trim, self.trim, childs
        )
    }
}

impl AsHtml for ForEachNode {
    fn as_html(&self) -> String {
        let mut childs = String::new();
        for x in &self.childs {
            childs.push_str(&x.as_html());
        }
        format!(
            "<foreach collection=\"{}\" index=\"{}\" item=\"{}\" >{}</foreach>",
            self.collection, self.index, self.item, childs
        )
    }
}

impl AsHtml for ChooseNode {
    fn as_html(&self) -> String {
        let mut childs = String::new();
        for x in &self.when_nodes {
            childs.push_str(&x.as_html());
        }
        let mut other_html = String::new();
        match &self.otherwise_node {
            None => {}
            Some(v) => {
                other_html = v.as_html();
            }
        }
        format!("<choose>{}{}</choose>", childs, other_html)
    }
}

impl AsHtml for OtherwiseNode {
    fn as_html(&self) -> String {
        let mut childs = String::new();
        for x in &self.childs {
            childs.push_str(&x.as_html());
        }
        format!("<otherwise>{}</otherwise>", childs)
    }
}

impl AsHtml for WhenNode {
    fn as_html(&self) -> String {
        let mut childs = String::new();
        for x in &self.childs {
            childs.push_str(&x.as_html());
        }
        format!("<when test=\"{}\">{}</when>", self.test, childs)
    }
}

impl AsHtml for BindNode {
    fn as_html(&self) -> String {
        format!("<bind name=\"{}\" value=\"{}\"/>", self.name, self.value)
    }
}

impl AsHtml for SetNode {
    fn as_html(&self) -> String {
        let mut childs = String::new();
        for x in &self.childs {
            childs.push_str(&x.as_html());
        }
        format!("<set>{}</set>", childs)
    }
}

impl AsHtml for WhereNode {
    fn as_html(&self) -> String {
        let mut childs = String::new();
        for x in &self.childs {
            childs.push_str(&x.as_html());
        }
        format!("<where>{}</where>", childs)
    }
}

impl AsHtml for NodeType {
    fn as_html(&self) -> String {
        match self {
            NodeType::NString(n) => n.as_html(),
            NodeType::NIf(n) => n.as_html(),
            NodeType::NTrim(n) => n.as_html(),
            NodeType::NForEach(n) => n.as_html(),
            NodeType::NChoose(n) => n.as_html(),
            NodeType::NOtherwise(n) => n.as_html(),
            NodeType::NWhen(n) => n.as_html(),
            NodeType::NBind(n) => n.as_html(),
            NodeType::NSet(n) => n.as_html(),
            NodeType::NWhere(n) => n.as_html(),
            NodeType::NContinue(n) => n.as_html(),
            NodeType::NSql(n) => n.as_html(),
        }
    }
}

impl AsHtml for Vec<NodeType> {
    fn as_html(&self) -> String {
        let mut htmls = String::new();
        for x in self {
            htmls.push_str(&x.as_html());
        }
        htmls
    }
}

pub fn to_html(args: &Vec<NodeType>, is_select: bool, fn_name: &str) -> String {
    let htmls = args.as_html();
    if is_select {
        format!(
            "<mapper><select id=\"{}\">{}</select></mapper>",
            fn_name, htmls
        )
    } else {
        format!(
            "<mapper><update id=\"{}\">{}</update></mapper>",
            fn_name, htmls
        )
    }
}

impl From<NodeType> for Element {
    fn from(arg: NodeType) -> Self {
        match arg {
            NodeType::NString(n) => {
                return Element {
                    tag: "".to_string(),
                    data: n.value,
                    attrs: Default::default(),
                    childs: vec![],
                };
            }
            NodeType::NSql(n) => {
                return Element {
                    tag: "sql".to_string(),
                    data: String::new(),
                    attrs: Default::default(),
                    childs: as_elements(n.childs),
                };
            }
            NodeType::NIf(n) => {
                let mut m = HashMap::new();
                m.insert("test".to_string(), n.test);
                return Element {
                    tag: "if".to_string(),
                    data: "".to_string(),
                    attrs: m,
                    childs: as_elements(n.childs),
                };
            }
            NodeType::NTrim(n) => {
                let mut m = HashMap::new();
                m.insert("trim".to_string(), n.trim);
                return Element {
                    tag: "trim".to_string(),
                    data: "".to_string(),
                    attrs: m,
                    childs: as_elements(n.childs),
                };
            }
            NodeType::NForEach(n) => {
                let mut m = HashMap::new();
                m.insert("collection".to_string(), n.collection);
                m.insert("index".to_string(), n.index);
                m.insert("item".to_string(), n.item);
                return Element {
                    tag: "foreach".to_string(),
                    data: "".to_string(),
                    attrs: m,
                    childs: as_elements(n.childs),
                };
            }
            NodeType::NChoose(n) => {
                let mut whens = as_elements(n.when_nodes);
                if let Some(v) = n.otherwise_node {
                    whens.push(Element::from(*v));
                }
                return Element {
                    tag: "choose".to_string(),
                    data: "".to_string(),
                    attrs: Default::default(),
                    childs: whens,
                };
            }
            NodeType::NOtherwise(n) => {
                return Element {
                    tag: "otherwise".to_string(),
                    data: "".to_string(),
                    attrs: Default::default(),
                    childs: as_elements(n.childs),
                };
            }
            NodeType::NWhen(n) => {
                let mut m = HashMap::new();
                m.insert("test".to_string(), n.test);
                return Element {
                    tag: "when".to_string(),
                    data: "".to_string(),
                    attrs: m,
                    childs: as_elements(n.childs),
                };
            }
            NodeType::NBind(n) => {
                let mut m = HashMap::new();
                m.insert("name".to_string(), n.name);
                m.insert("value".to_string(), n.value);
                return Element {
                    tag: "bind".to_string(),
                    data: "".to_string(),
                    attrs: m,
                    childs: vec![],
                };
            }
            NodeType::NSet(n) => {
                return Element {
                    tag: "set".to_string(),
                    data: "".to_string(),
                    attrs: Default::default(),
                    childs: as_elements(n.childs),
                };
            }
            NodeType::NWhere(n) => {
                return Element {
                    tag: "where".to_string(),
                    data: "".to_string(),
                    attrs: Default::default(),
                    childs: as_elements(n.childs),
                };
            }
            NodeType::NContinue(_n) => {
                return Element {
                    tag: "continue".to_string(),
                    data: "".to_string(),
                    attrs: Default::default(),
                    childs: vec![],
                };
            }
        }
    }
}

fn as_elements(arg: Vec<NodeType>) -> Vec<Element> {
    let mut res = vec![];
    for x in arg {
        res.push(Element::from(x));
    }
    res
}