use crate::ast::Ident;
#[cfg(feature = "bigdecimal")]
use bigdecimal::BigDecimal;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Value {
#[cfg(not(feature = "bigdecimal"))]
Number(String),
#[cfg(feature = "bigdecimal")]
Number(BigDecimal),
SingleQuotedString(String),
NationalStringLiteral(String),
HexStringLiteral(String),
Boolean(bool),
Identifier(Ident),
Null,
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Value::Number(v) => write!(f, "{}", v),
Value::SingleQuotedString(v) => write!(f, "'{}'", escape_single_quote_string(v)),
Value::NationalStringLiteral(v) => write!(f, "N'{}'", v),
Value::HexStringLiteral(v) => write!(f, "X'{}'", v),
Value::Boolean(v) => write!(f, "{}", v),
Value::Identifier(i) => write!(f, "{}", i),
Value::Null => write!(f, "NULL"),
}
}
}
pub struct EscapeSingleQuoteString<'a>(&'a str);
impl<'a> fmt::Display for EscapeSingleQuoteString<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for c in self.0.chars() {
if c == '\'' {
write!(f, "\'\'")?;
} else {
write!(f, "{}", c)?;
}
}
Ok(())
}
}
pub fn escape_single_quote_string(s: &str) -> EscapeSingleQuoteString<'_> {
EscapeSingleQuoteString(s)
}