rbatis_codegen/codegen/syntax_tree_pysql/
set_node.rs

1use crate::codegen::syntax_tree_pysql::{Name, NodeType, ToHtml};
2
3/// Represents a `set` node in py_sql.
4/// It's typically used in `UPDATE` statements to dynamically include `SET` clauses.
5/// It will automatically remove trailing commas if present.
6///
7/// # Example
8///
9/// PySQL syntax:
10/// ```py
11/// UPDATE table
12/// set:
13///   if name != null:
14///     name = #{name},
15///   if age != null:
16///     age = #{age},
17/// WHERE id = #{id}
18/// ```
19#[derive(Clone, Debug, Eq, PartialEq)]
20pub struct SetNode {
21    pub childs: Vec<NodeType>,
22    pub collection: String,
23    pub skips: String,
24    pub skip_null: bool,
25}
26
27impl Name for SetNode {
28    fn name() -> &'static str {
29        "set"
30    }
31}
32
33
34impl ToHtml for SetNode {
35    fn as_html(&self) -> String {
36        let mut childs_html = String::new();
37        for x in &self.childs {
38            childs_html.push_str(&x.as_html());
39        }
40
41        let mut attrs_string = String::new();
42        if !self.collection.is_empty() {
43            attrs_string.push_str(&format!(" collection=\"{}\"", self.collection));
44        }
45        if !self.skips.is_empty() {
46            attrs_string.push_str(&format!(" skips=\"{}\"", self.skips));
47        }
48        if self.skip_null {
49            attrs_string.push_str(" skip_null=\"true\"");
50        }
51
52        format!("<set{}>{}</set>", attrs_string, childs_html)
53    }
54}