rbatis_codegen/codegen/syntax_tree_pysql/
if_node.rs

1use crate::codegen::syntax_tree_pysql::{Name, NodeType, ToHtml};
2
3/// Represents an `if` conditional node in py_sql.
4/// It executes the nested SQL block if the `test` condition evaluates to true.
5///
6/// # Attributes
7///
8/// - `test`: The boolean expression to evaluate.
9///
10/// # Example
11///
12/// PySQL syntax:
13/// ```py
14/// if name != null:
15///   AND name = #{name}
16/// ```
17#[derive(Clone, Debug, Eq, PartialEq)]
18pub struct IfNode {
19    pub childs: Vec<NodeType>,
20    pub test: String,
21}
22
23impl Name for IfNode {
24    fn name() -> &'static str {
25        "if"
26    }
27}
28
29
30impl ToHtml for IfNode {
31    fn as_html(&self) -> String {
32        let mut childs = String::new();
33        for x in &self.childs {
34            childs.push_str(&x.as_html());
35        }
36        format!("<if test=\"{}\">{}</if>", self.test, childs)
37    }
38}