rbatis_codegen/codegen/syntax_tree_pysql/
choose_node.rs

1use crate::codegen::syntax_tree_pysql::{Name, NodeType, ToHtml};
2
3/// Represents a `choose` node in py_sql.
4/// It provides a way to conditionally execute different blocks of SQL, similar to a switch statement.
5/// It must contain one or more `when` child nodes and can optionally have an `otherwise` child node.
6///
7/// # Example
8///
9/// PySQL syntax:
10/// ```py
11/// choose:
12///   when test="type == 'A'":
13///     sql_block_A
14///   when test="type == 'B'":
15///     sql_block_B
16///   otherwise:
17///     sql_block_default
18/// ```
19#[derive(Clone, Debug, Eq, PartialEq)]
20pub struct ChooseNode {
21    pub when_nodes: Vec<NodeType>,
22    pub otherwise_node: Option<Box<NodeType>>,
23}
24
25impl Name for ChooseNode {
26    fn name() -> &'static str {
27        "choose"
28    }
29}
30
31
32impl ToHtml for ChooseNode {
33    fn as_html(&self) -> String {
34        let mut childs = String::new();
35        for x in &self.when_nodes {
36            childs.push_str(&x.as_html());
37        }
38        let mut other_html = String::new();
39        match &self.otherwise_node {
40            None => {}
41            Some(v) => {
42                other_html = v.as_html();
43            }
44        }
45        format!("<choose>{}{}</choose>", childs, other_html)
46    }
47}