rbatis_codegen/codegen/syntax_tree_pysql/
mod.rs

1/// this the py_sql syntax tree
2pub mod bind_node;
3pub mod break_node;
4pub mod choose_node;
5pub mod continue_node;
6pub mod error;
7pub mod foreach_node;
8pub mod if_node;
9pub mod otherwise_node;
10pub mod set_node;
11pub mod sql_node;
12pub mod string_node;
13pub mod trim_node;
14pub mod when_node;
15pub mod where_node;
16
17use crate::codegen::syntax_tree_pysql::bind_node::BindNode;
18use crate::codegen::syntax_tree_pysql::break_node::BreakNode;
19use crate::codegen::syntax_tree_pysql::choose_node::ChooseNode;
20use crate::codegen::syntax_tree_pysql::continue_node::ContinueNode;
21use crate::codegen::syntax_tree_pysql::foreach_node::ForEachNode;
22use crate::codegen::syntax_tree_pysql::if_node::IfNode;
23use crate::codegen::syntax_tree_pysql::otherwise_node::OtherwiseNode;
24use crate::codegen::syntax_tree_pysql::set_node::SetNode;
25use crate::codegen::syntax_tree_pysql::sql_node::SqlNode;
26use crate::codegen::syntax_tree_pysql::string_node::StringNode;
27use crate::codegen::syntax_tree_pysql::trim_node::TrimNode;
28use crate::codegen::syntax_tree_pysql::when_node::WhenNode;
29use crate::codegen::syntax_tree_pysql::where_node::WhereNode;
30
31/// the pysql syntax tree
32#[derive(Clone, Debug, Eq, PartialEq)]
33pub enum NodeType {
34    NString(StringNode),
35    NIf(IfNode),
36    NTrim(TrimNode),
37    NForEach(ForEachNode),
38    NChoose(ChooseNode),
39    NOtherwise(OtherwiseNode),
40    NWhen(WhenNode),
41    NBind(BindNode),
42    NSet(SetNode),
43    NWhere(WhereNode),
44    NContinue(ContinueNode),
45    NBreak(BreakNode),
46    NSql(SqlNode),
47}
48
49/// the node name
50pub trait Name {
51    fn name() -> &'static str;
52}
53
54/// node default name
55pub trait DefaultName {
56    fn default_name() -> &'static str;
57}
58
59/// Convert syntax tree to HTML deconstruction
60pub trait AsHtml {
61    fn as_html(&self) -> String;
62}
63
64impl AsHtml for StringNode {
65    fn as_html(&self) -> String {
66        if self.value.starts_with("`") && self.value.ends_with("`") {
67            self.value.to_string()
68        } else {
69            let mut v = self.value.clone();
70            v.insert(0, '`');
71            v.push('`');
72            v
73        }
74    }
75}
76
77impl AsHtml for IfNode {
78    fn as_html(&self) -> String {
79        let mut childs = String::new();
80        for x in &self.childs {
81            childs.push_str(&x.as_html());
82        }
83        format!("<if test=\"{}\">{}</if>", self.test, childs)
84    }
85}
86
87impl AsHtml for TrimNode {
88    fn as_html(&self) -> String {
89        let mut childs = String::new();
90        for x in &self.childs {
91            childs.push_str(&x.as_html());
92        }
93        format!(
94            "<trim prefixOverrides=\"{}\" suffixOverrides=\"{}\">{}</trim>",
95            self.start, self.end, childs
96        )
97    }
98}
99
100impl AsHtml for ForEachNode {
101    fn as_html(&self) -> String {
102        let mut childs = String::new();
103        for x in &self.childs {
104            childs.push_str(&x.as_html());
105        }
106        format!(
107            "<foreach collection=\"{}\" index=\"{}\" item=\"{}\" >{}</foreach>",
108            self.collection, self.index, self.item, childs
109        )
110    }
111}
112
113impl AsHtml for ChooseNode {
114    fn as_html(&self) -> String {
115        let mut childs = String::new();
116        for x in &self.when_nodes {
117            childs.push_str(&x.as_html());
118        }
119        let mut other_html = String::new();
120        match &self.otherwise_node {
121            None => {}
122            Some(v) => {
123                other_html = v.as_html();
124            }
125        }
126        format!("<choose>{}{}</choose>", childs, other_html)
127    }
128}
129
130impl AsHtml for OtherwiseNode {
131    fn as_html(&self) -> String {
132        let mut childs = String::new();
133        for x in &self.childs {
134            childs.push_str(&x.as_html());
135        }
136        format!("<otherwise>{}</otherwise>", childs)
137    }
138}
139
140impl AsHtml for WhenNode {
141    fn as_html(&self) -> String {
142        let mut childs = String::new();
143        for x in &self.childs {
144            childs.push_str(&x.as_html());
145        }
146        format!("<when test=\"{}\">{}</when>", self.test, childs)
147    }
148}
149
150impl AsHtml for BindNode {
151    fn as_html(&self) -> String {
152        format!("<bind name=\"{}\" value=\"{}\"/>", self.name, self.value)
153    }
154}
155
156impl AsHtml for SetNode {
157    fn as_html(&self) -> String {
158        let mut childs = String::new();
159        for x in &self.childs {
160            childs.push_str(&x.as_html());
161        }
162        format!("<set>{}</set>", childs)
163    }
164}
165
166impl AsHtml for WhereNode {
167    fn as_html(&self) -> String {
168        let mut childs = String::new();
169        for x in &self.childs {
170            childs.push_str(&x.as_html());
171        }
172        format!("<where>{}</where>", childs)
173    }
174}
175
176impl AsHtml for NodeType {
177    fn as_html(&self) -> String {
178        match self {
179            NodeType::NString(n) => n.as_html(),
180            NodeType::NIf(n) => n.as_html(),
181            NodeType::NTrim(n) => n.as_html(),
182            NodeType::NForEach(n) => n.as_html(),
183            NodeType::NChoose(n) => n.as_html(),
184            NodeType::NOtherwise(n) => n.as_html(),
185            NodeType::NWhen(n) => n.as_html(),
186            NodeType::NBind(n) => n.as_html(),
187            NodeType::NSet(n) => n.as_html(),
188            NodeType::NWhere(n) => n.as_html(),
189            NodeType::NContinue(n) => n.as_html(),
190            NodeType::NBreak(n) => n.as_html(),
191            NodeType::NSql(n) => n.as_html(),
192        }
193    }
194}
195
196impl AsHtml for Vec<NodeType> {
197    fn as_html(&self) -> String {
198        let mut htmls = String::new();
199        for x in self {
200            htmls.push_str(&x.as_html());
201        }
202        htmls
203    }
204}
205
206pub fn to_html(args: &Vec<NodeType>, is_select: bool, fn_name: &str) -> String {
207    let htmls = args.as_html();
208    if is_select {
209        format!(
210            "<mapper><select id=\"{}\">{}</select></mapper>",
211            fn_name, htmls
212        )
213    } else {
214        format!(
215            "<mapper><update id=\"{}\">{}</update></mapper>",
216            fn_name, htmls
217        )
218    }
219}