use crate::backend::Backend;
use crate::value::Value;
pub fn render_value(value: &Value, backend: Backend) -> String {
match value {
Value::Null => "NULL".to_string(),
Value::Bool(b) => match backend {
#[cfg(feature = "oracle")]
Backend::Oracle => {
if *b {
"1".to_string()
} else {
"0".to_string()
}
}
#[cfg(feature = "postgres")]
Backend::Postgres => bool_literal(*b),
#[cfg(feature = "mysql")]
Backend::MySql => bool_literal(*b),
#[cfg(feature = "mssql")]
Backend::MsSql => bool_literal(*b),
#[cfg(feature = "sqlite")]
Backend::Sqlite => bool_literal(*b),
},
Value::Int64(i) => i.to_string(),
Value::Float64(f) => f.to_string(),
Value::Decimal(d) => d.clone(),
Value::String(s) => quote_string(s),
Value::Bytes(_b) => {
let hex: String = _b.iter().map(|b| format!("{:02x}", b)).collect();
format!("X'{}'", hex)
}
other => quote_string(&other.to_string()),
}
}
fn bool_literal(b: bool) -> String {
if b {
"TRUE".to_string()
} else {
"FALSE".to_string()
}
}
pub fn quote_string(v: &str) -> String {
let mut out = String::with_capacity(v.len() + 2);
out.push('\'');
for ch in v.chars() {
if ch == '\'' {
out.push('\'');
out.push('\'');
} else {
out.push(ch);
}
}
out.push('\'');
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_quote_escape() {
assert_eq!(quote_string("O'Brien"), "'O''Brien'");
assert_eq!(quote_string("hello"), "'hello'");
assert_eq!(quote_string("it's a test"), "'it''s a test'");
}
}