rbatis_codegen/codegen/syntax_tree_pysql/where_node.rs
1use crate::codegen::syntax_tree_pysql::{Name, NodeType, ToHtml};
2
3/// Represents a `where` node in py_sql.
4/// It's used to dynamically build the `WHERE` clause of a SQL query.
5/// It will automatically prepend `WHERE` if needed and remove leading `AND` or `OR` keywords from its content.
6///
7/// # Example
8///
9/// PySQL syntax:
10/// ```py
11/// SELECT * FROM table
12/// where:
13/// if id != null:
14/// AND id = #{id}
15/// if name != null:
16/// AND name = #{name}
17/// ```
18/// This would result in `SELECT * FROM table WHERE id = #{id} AND name = #{name}` (if both conditions are true),
19/// or `SELECT * FROM table WHERE id = #{id}` (if only id is not null),
20/// or `SELECT * FROM table WHERE name = #{name}` (if only name is not null).
21/// If no conditions are met, the `WHERE` clause is omitted entirely.
22#[derive(Clone, Debug, Eq, PartialEq)]
23pub struct WhereNode {
24 pub childs: Vec<NodeType>,
25}
26
27impl Name for WhereNode {
28 fn name() -> &'static str {
29 "where"
30 }
31}
32
33
34impl ToHtml for WhereNode {
35 fn as_html(&self) -> String {
36 let mut childs = String::new();
37 for x in &self.childs {
38 childs.push_str(&x.as_html());
39 }
40 format!("<where>{}</where>", childs)
41 }
42}