rbatis_codegen/codegen/syntax_tree_pysql/
when_node.rs

1use crate::codegen::syntax_tree_pysql::{Name, NodeType, ToHtml};
2
3/// Represents a `when` node in py_sql.
4/// It's used as a child of a `choose` node to define a conditional block of SQL.
5/// The SQL block within `when` is executed if its `test` condition evaluates to true and no preceding `when` in the same `choose` was true.
6///
7/// # Attributes
8///
9/// - `test`: The boolean expression to evaluate.
10///
11/// # Example
12///
13/// PySQL syntax (inside a `choose` block):
14/// ```py
15/// choose:
16///   when test="type == 'A'":
17///     sql_for_type_A
18///   when test="type == 'B'":
19///     sql_for_type_B
20/// ```
21#[derive(Clone, Debug, Eq, PartialEq)]
22pub struct WhenNode {
23    pub childs: Vec<NodeType>,
24    pub test: String,
25}
26
27impl Name for WhenNode {
28    fn name() -> &'static str {
29        "when"
30    }
31}
32
33impl ToHtml for WhenNode {
34    fn as_html(&self) -> String {
35        let mut childs = String::new();
36        for x in &self.childs {
37            childs.push_str(&x.as_html());
38        }
39        format!("<when test=\"{}\">{}</when>", self.test, childs)
40    }
41}