rbatis_codegen/codegen/syntax_tree_pysql/string_node.rs
1use crate::codegen::syntax_tree_pysql::{Name, ToHtml};
2
3/// Represents a plain string, a SQL text segment, or a string with preserved whitespace in py_sql.
4/// This node holds parts of the SQL query that are not dynamic tags or raw text.
5///
6/// # Examples
7///
8/// PySQL syntax for simple text segments:
9/// ```py
10/// SELECT * FROM users WHERE
11/// if name != null:
12/// name = #{name}
13/// // In the above, "SELECT * FROM users WHERE " is a StringNode.
14/// ```
15///
16/// PySQL syntax for strings with preserved whitespace (using backticks - single line only):
17/// ```py
18/// ` SELECT column1, column2 FROM my_table `
19/// ```
20///
21/// It also handles simple quoted strings if they are part of the py_sql structure:
22/// ```py
23/// // Example within a more complex structure (e.g., an expression):
24/// // if status == 'active':
25/// ```
26#[derive(Clone, Debug, Eq, PartialEq)]
27pub struct StringNode {
28 pub value: String,
29}
30
31impl Name for String {
32 fn name() -> &'static str {
33 "string"
34 }
35}
36
37
38impl ToHtml for StringNode {
39 fn as_html(&self) -> String {
40 if self.value.starts_with("`") && self.value.ends_with("`") {
41 self.value.to_string()
42 } else {
43 let mut v = self.value.clone();
44 v.insert(0, '`');
45 v.push('`');
46 v
47 }
48 }
49}