nodedb_sql/parser/preprocess/
literal.rs1pub fn value_to_sql_literal(value: &nodedb_types::Value) -> String {
10 match value {
11 nodedb_types::Value::String(s) => format!("'{}'", s.replace('\'', "''")),
12 nodedb_types::Value::Integer(n) => n.to_string(),
13 nodedb_types::Value::Float(f) => {
14 if f.is_finite() {
15 format!("{f}")
16 } else {
17 "NULL".to_string()
24 }
25 }
26 nodedb_types::Value::Bool(b) => if *b { "TRUE" } else { "FALSE" }.to_string(),
27 nodedb_types::Value::Null => "NULL".to_string(),
28 nodedb_types::Value::Array(items) => {
29 let inner: Vec<String> = items.iter().map(value_to_sql_literal).collect();
30 format!("ARRAY[{}]", inner.join(", "))
31 }
32 nodedb_types::Value::Bytes(b) => {
33 let hex: String = b.iter().map(|byte| format!("{byte:02x}")).collect();
34 format!("'\\x{hex}'")
35 }
36 nodedb_types::Value::Object(map) => {
37 let json = super::function_args::value_map_to_json(map);
38 format!("'{}'", json.replace('\'', "''"))
39 }
40 nodedb_types::Value::Uuid(u) => format!("'{u}'"),
41 nodedb_types::Value::Ulid(u) => format!("'{u}'"),
42 nodedb_types::Value::DateTime(dt) | nodedb_types::Value::NaiveDateTime(dt) => {
43 format!("'{dt}'")
44 }
45 nodedb_types::Value::Duration(d) => format!("'{d}'"),
46 nodedb_types::Value::Decimal(d) => d.to_string(),
47 other => format!("'{}'", format!("{other:?}").replace('\'', "''")),
48 }
49}