py_sql/node/
string_node.rs1use crate::ast::RbatisAST;
2
3use crate::string_util;
4use rexpr;
5use rexpr::ast::Node;
6use rexpr::runtime::RExprRuntime;
7use serde_json::Value;
8use std::collections::LinkedList;
9
10pub trait StringConvert {
12 fn convert(&self, index: usize) -> String;
13}
14
15#[derive(Clone, Debug)]
17pub struct StringNode {
18 pub value: String,
19 pub express_map: LinkedList<(String, String, Node)>,
21}
22
23impl StringNode {
24 pub fn new(runtime: &RExprRuntime, v: &str) -> Result<Self, crate::error::Error> {
25 let mut express_map = LinkedList::new();
26 let list = string_util::find_convert_string(v);
27 for (k, v) in list {
28 let node = runtime.parse(&k)?;
29 express_map.push_back((k, v, node));
30 }
31 Ok(Self {
32 value: v.to_string(),
33 express_map: express_map,
34 })
35 }
36}
37
38impl RbatisAST for StringNode {
39 fn name() -> &'static str {
40 "string"
41 }
42 fn eval(
43 &self,
44 convert: &dyn StringConvert,
45 env: &mut Value,
46 engine: &RExprRuntime,
47 arg_array: &mut Vec<Value>,
48 arg_sql: &mut String,
49 ) -> Result<serde_json::Value, crate::error::Error> {
50 let mut result = self.value.clone();
51 for (item, value, node) in &self.express_map {
52 if item.is_empty() {
53 result = result.replace(value, "");
54 continue;
55 }
56 if value.starts_with("#") {
57 result = result.replace(value, convert.convert(arg_array.len()).as_str());
58 let v = node.eval(env)?;
59 arg_array.push(v);
60 } else {
61 let v = node.eval(env)?;
62 if v.is_string() {
63 result = result.replace(value, &v.as_str().unwrap());
64 } else {
65 result = result.replace(value, &v.to_string());
66 }
67 }
68 }
69 arg_sql.push_str(result.as_str());
70 return Result::Ok(serde_json::Value::Null);
71 }
72}