rbatis_codegen/codegen/syntax_tree_pysql/
mod.rs1pub 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#[derive(Clone, Debug, Eq, PartialEq)]
128pub enum NodeType {
129 NString(StringNode),
130 NIf(IfNode),
131 NTrim(TrimNode),
132 NForEach(ForEachNode),
133 NChoose(ChooseNode),
134 NOtherwise(OtherwiseNode),
135 NWhen(WhenNode),
136 NBind(BindNode),
137 NSet(SetNode),
138 NWhere(WhereNode),
139 NContinue(ContinueNode),
140 NBreak(BreakNode),
141 NSql(SqlNode),
142}
143
144pub trait Name {
146 fn name() -> &'static str;
147}
148
149pub trait DefaultName {
151 fn default_name() -> &'static str;
152}
153
154pub trait AsHtml {
156 fn as_html(&self) -> String;
157}
158
159impl AsHtml for StringNode {
160 fn as_html(&self) -> String {
161 if self.value.starts_with("`") && self.value.ends_with("`") {
162 self.value.to_string()
163 } else {
164 let mut v = self.value.clone();
165 v.insert(0, '`');
166 v.push('`');
167 v
168 }
169 }
170}
171
172impl AsHtml for IfNode {
173 fn as_html(&self) -> String {
174 let mut childs = String::new();
175 for x in &self.childs {
176 childs.push_str(&x.as_html());
177 }
178 format!("<if test=\"{}\">{}</if>", self.test, childs)
179 }
180}
181
182impl AsHtml for TrimNode {
183 fn as_html(&self) -> String {
184 let mut childs = String::new();
185 for x in &self.childs {
186 childs.push_str(&x.as_html());
187 }
188 format!(
189 "<trim prefixOverrides=\"{}\" suffixOverrides=\"{}\">{}</trim>",
190 self.start, self.end, childs
191 )
192 }
193}
194
195impl AsHtml for ForEachNode {
196 fn as_html(&self) -> String {
197 let mut childs = String::new();
198 for x in &self.childs {
199 childs.push_str(&x.as_html());
200 }
201 format!(
202 "<foreach collection=\"{}\" index=\"{}\" item=\"{}\" >{}</foreach>",
203 self.collection, self.index, self.item, childs
204 )
205 }
206}
207
208impl AsHtml for ChooseNode {
209 fn as_html(&self) -> String {
210 let mut childs = String::new();
211 for x in &self.when_nodes {
212 childs.push_str(&x.as_html());
213 }
214 let mut other_html = String::new();
215 match &self.otherwise_node {
216 None => {}
217 Some(v) => {
218 other_html = v.as_html();
219 }
220 }
221 format!("<choose>{}{}</choose>", childs, other_html)
222 }
223}
224
225impl AsHtml for OtherwiseNode {
226 fn as_html(&self) -> String {
227 let mut childs = String::new();
228 for x in &self.childs {
229 childs.push_str(&x.as_html());
230 }
231 format!("<otherwise>{}</otherwise>", childs)
232 }
233}
234
235impl AsHtml for WhenNode {
236 fn as_html(&self) -> String {
237 let mut childs = String::new();
238 for x in &self.childs {
239 childs.push_str(&x.as_html());
240 }
241 format!("<when test=\"{}\">{}</when>", self.test, childs)
242 }
243}
244
245impl AsHtml for BindNode {
246 fn as_html(&self) -> String {
247 format!("<bind name=\"{}\" value=\"{}\"/>", self.name, self.value)
248 }
249}
250
251impl AsHtml for SetNode {
252 fn as_html(&self) -> String {
253 let mut childs_html = String::new();
254 for x in &self.childs {
255 childs_html.push_str(&x.as_html());
256 }
257
258 let mut attrs_string = String::new();
259 if !self.collection.is_empty() {
260 attrs_string.push_str(&format!(" collection=\"{}\"", self.collection));
261 }
262 if !self.skips.is_empty() {
263 attrs_string.push_str(&format!(" skips=\"{}\"", self.skips));
264 }
265 if self.skip_null {
266 attrs_string.push_str(" skip_null=\"true\"");
267 }
268
269 format!("<set{}>{}</set>", attrs_string, childs_html)
270 }
271}
272
273impl AsHtml for WhereNode {
274 fn as_html(&self) -> String {
275 let mut childs = String::new();
276 for x in &self.childs {
277 childs.push_str(&x.as_html());
278 }
279 format!("<where>{}</where>", childs)
280 }
281}
282
283impl AsHtml for NodeType {
284 fn as_html(&self) -> String {
285 match self {
286 NodeType::NString(n) => n.as_html(),
287 NodeType::NIf(n) => n.as_html(),
288 NodeType::NTrim(n) => n.as_html(),
289 NodeType::NForEach(n) => n.as_html(),
290 NodeType::NChoose(n) => n.as_html(),
291 NodeType::NOtherwise(n) => n.as_html(),
292 NodeType::NWhen(n) => n.as_html(),
293 NodeType::NBind(n) => n.as_html(),
294 NodeType::NSet(n) => n.as_html(),
295 NodeType::NWhere(n) => n.as_html(),
296 NodeType::NContinue(n) => n.as_html(),
297 NodeType::NBreak(n) => n.as_html(),
298 NodeType::NSql(n) => n.as_html(),
299 }
300 }
301}
302
303impl AsHtml for Vec<NodeType> {
304 fn as_html(&self) -> String {
305 let mut htmls = String::new();
306 for x in self {
307 htmls.push_str(&x.as_html());
308 }
309 htmls
310 }
311}
312
313pub fn to_html(args: &Vec<NodeType>, is_select: bool, fn_name: &str) -> String {
314 let htmls = args.as_html();
315 if is_select {
316 format!(
317 "<mapper><select id=\"{}\">{}</select></mapper>",
318 fn_name, htmls
319 )
320 } else {
321 format!(
322 "<mapper><update id=\"{}\">{}</update></mapper>",
323 fn_name, htmls
324 )
325 }
326}