use ipnetwork::IpNetwork;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::fmt;
use std::net::IpAddr;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SqlValue {
Null,
Default,
Bool(bool),
TinyInt(i8), SmallInt(i16), Int(i32), BigInt(i64),
UnsignedTinyInt(u8), UnsignedSmallInt(u16), UnsignedInt(u32), UnsignedBigInt(u64),
Float(f32), Double(f64), #[cfg(feature = "decimal")]
Decimal(rust_decimal::Decimal), #[cfg(not(feature = "decimal"))]
Decimal(String),
String(String), Text(String),
Bytes(Vec<u8>),
Array(Vec<SqlValue>),
Enum(String), Uuid(String), Json(JsonValue), Date(String), Time(String), DateTime(String), Timestamp(i64),
Inet(IpAddr), Cidr(IpAddr, u8), }
impl SqlValue {
pub fn is_null(&self) -> bool {
matches!(self, SqlValue::Null)
}
pub fn is_default(&self) -> bool {
matches!(self, SqlValue::Default)
}
pub fn as_bool(&self) -> Option<bool> {
match self {
SqlValue::Bool(b) => Some(*b),
SqlValue::TinyInt(i) => Some(*i != 0),
SqlValue::SmallInt(i) => Some(*i != 0),
SqlValue::Int(i) => Some(*i != 0),
SqlValue::BigInt(i) => Some(*i != 0),
SqlValue::UnsignedTinyInt(i) => Some(*i != 0),
SqlValue::UnsignedSmallInt(i) => Some(*i != 0),
SqlValue::UnsignedInt(i) => Some(*i != 0),
SqlValue::UnsignedBigInt(i) => Some(*i != 0),
SqlValue::String(s) | SqlValue::Text(s) => match s.to_lowercase().as_str() {
"true" | "t" | "yes" | "y" | "1" => Some(true),
"false" | "f" | "no" | "n" | "0" => Some(false),
_ => None,
},
_ => None,
}
}
pub fn as_i32(&self) -> Option<i32> {
match self {
SqlValue::TinyInt(i) => Some(*i as i32),
SqlValue::SmallInt(i) => Some(*i as i32),
SqlValue::Int(i) => Some(*i),
SqlValue::UnsignedTinyInt(i) => Some(*i as i32),
SqlValue::UnsignedSmallInt(i) => Some(*i as i32),
SqlValue::UnsignedInt(i) if *i <= i32::MAX as u32 => Some(*i as i32),
SqlValue::String(s) | SqlValue::Text(s) => s.parse().ok(),
_ => None,
}
}
pub fn as_i64(&self) -> Option<i64> {
match self {
SqlValue::TinyInt(i) => Some(*i as i64),
SqlValue::SmallInt(i) => Some(*i as i64),
SqlValue::Int(i) => Some(*i as i64),
SqlValue::BigInt(i) => Some(*i),
SqlValue::UnsignedTinyInt(i) => Some(*i as i64),
SqlValue::UnsignedSmallInt(i) => Some(*i as i64),
SqlValue::UnsignedInt(i) => Some(*i as i64),
SqlValue::UnsignedBigInt(i) if *i <= i64::MAX as u64 => Some(*i as i64),
SqlValue::String(s) | SqlValue::Text(s) => s.parse().ok(),
SqlValue::Timestamp(ts) => Some(*ts),
_ => None,
}
}
pub fn as_string(&self) -> Option<String> {
match self {
SqlValue::String(s) | SqlValue::Text(s) => Some(s.clone()),
SqlValue::Enum(s) | SqlValue::Uuid(s) => Some(s.clone()),
SqlValue::Date(s) | SqlValue::Time(s) | SqlValue::DateTime(s) => Some(s.clone()),
SqlValue::Json(j) => Some(j.to_string()),
SqlValue::Bool(b) => Some(b.to_string()),
SqlValue::TinyInt(i) => Some(i.to_string()),
SqlValue::SmallInt(i) => Some(i.to_string()),
SqlValue::Int(i) => Some(i.to_string()),
SqlValue::BigInt(i) => Some(i.to_string()),
SqlValue::UnsignedTinyInt(i) => Some(i.to_string()),
SqlValue::UnsignedSmallInt(i) => Some(i.to_string()),
SqlValue::UnsignedInt(i) => Some(i.to_string()),
SqlValue::UnsignedBigInt(i) => Some(i.to_string()),
SqlValue::Float(f) => Some(f.to_string()),
SqlValue::Double(f) => Some(f.to_string()),
#[cfg(feature = "decimal")]
SqlValue::Decimal(d) => Some(d.to_string()),
#[cfg(not(feature = "decimal"))]
SqlValue::Decimal(s) => Some(s.clone()),
SqlValue::Timestamp(ts) => Some(ts.to_string()),
SqlValue::Inet(ip) => Some(ip.to_string()),
SqlValue::Cidr(ip, prefix) => Some(format!("{}/{}", ip, prefix)),
SqlValue::Null => None,
SqlValue::Default => None,
SqlValue::Bytes(_) => None,
SqlValue::Array(_) => None,
}
}
pub fn to_json(&self) -> JsonValue {
match self {
SqlValue::Null => JsonValue::Null,
SqlValue::Default => JsonValue::String("DEFAULT".to_string()),
SqlValue::Bool(b) => JsonValue::Bool(*b),
SqlValue::TinyInt(i) => JsonValue::Number((*i).into()),
SqlValue::SmallInt(i) => JsonValue::Number((*i).into()),
SqlValue::Int(i) => JsonValue::Number((*i).into()),
SqlValue::BigInt(i) => JsonValue::Number((*i).into()),
SqlValue::UnsignedTinyInt(i) => JsonValue::Number((*i).into()),
SqlValue::UnsignedSmallInt(i) => JsonValue::Number((*i).into()),
SqlValue::UnsignedInt(i) => JsonValue::Number((*i).into()),
SqlValue::UnsignedBigInt(i) => JsonValue::Number((*i).into()),
SqlValue::Float(f) => serde_json::Number::from_f64(*f as f64)
.map(JsonValue::Number)
.unwrap_or(JsonValue::Null),
SqlValue::Double(f) => serde_json::Number::from_f64(*f)
.map(JsonValue::Number)
.unwrap_or(JsonValue::Null),
#[cfg(feature = "decimal")]
SqlValue::Decimal(d) => JsonValue::String(d.to_string()),
#[cfg(not(feature = "decimal"))]
SqlValue::Decimal(s) => JsonValue::String(s.clone()),
SqlValue::String(s) | SqlValue::Text(s) => JsonValue::String(s.clone()),
SqlValue::Enum(s) | SqlValue::Uuid(s) => JsonValue::String(s.clone()),
SqlValue::Date(s) | SqlValue::Time(s) | SqlValue::DateTime(s) => {
JsonValue::String(s.clone())
}
SqlValue::Json(j) => j.clone(),
SqlValue::Timestamp(ts) => JsonValue::Number((*ts).into()),
SqlValue::Inet(ip) => JsonValue::String(ip.to_string()),
SqlValue::Cidr(ip, prefix) => JsonValue::String(format!("{}/{}", ip, prefix)),
SqlValue::Bytes(bytes) => JsonValue::String(base64_encode(bytes)),
SqlValue::Array(values) => {
JsonValue::Array(values.iter().map(|v| v.to_json()).collect())
}
}
}
pub fn to_sql_string(&self) -> String {
match self {
SqlValue::Null => "NULL".to_string(),
SqlValue::Default => "DEFAULT".to_string(),
SqlValue::Bool(b) => b.to_string(),
SqlValue::TinyInt(i) => i.to_string(),
SqlValue::SmallInt(i) => i.to_string(),
SqlValue::Int(i) => i.to_string(),
SqlValue::BigInt(i) => i.to_string(),
SqlValue::UnsignedTinyInt(i) => i.to_string(),
SqlValue::UnsignedSmallInt(i) => i.to_string(),
SqlValue::UnsignedInt(i) => i.to_string(),
SqlValue::UnsignedBigInt(i) => i.to_string(),
SqlValue::Float(f) => f.to_string(),
SqlValue::Double(f) => f.to_string(),
#[cfg(feature = "decimal")]
SqlValue::Decimal(d) => d.to_string(),
#[cfg(not(feature = "decimal"))]
SqlValue::Decimal(s) => s.clone(),
SqlValue::String(s) | SqlValue::Text(s) => format!("'{}'", s.replace('\'', "''")),
SqlValue::Bytes(bytes) => format!("X'{}'", hex_encode(bytes)),
SqlValue::Enum(val) => format!("'{}'", val.replace('\'', "''")),
SqlValue::Uuid(uuid) => format!("'{}'", uuid),
SqlValue::Json(json) => format!("'{}'", json.to_string().replace('\'', "''")),
SqlValue::Date(date) => format!("'{}'", date),
SqlValue::Time(time) => format!("'{}'", time),
SqlValue::DateTime(dt) => format!("'{}'", dt),
SqlValue::Timestamp(ts) => ts.to_string(),
SqlValue::Inet(ip) => format!("'{}'::inet", ip),
SqlValue::Cidr(ip, prefix) => format!("'{}/{}'::cidr", ip, prefix),
SqlValue::Array(values) => {
let elements: Vec<String> = values.iter().map(|v| v.to_sql_string()).collect();
format!("ARRAY[{}]", elements.join(", "))
}
}
}
}
impl fmt::Display for SqlValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SqlValue::Null => write!(f, "NULL"),
SqlValue::Default => write!(f, "DEFAULT"),
SqlValue::Bool(b) => write!(f, "{}", b),
SqlValue::TinyInt(i) => write!(f, "{}", i),
SqlValue::SmallInt(i) => write!(f, "{}", i),
SqlValue::Int(i) => write!(f, "{}", i),
SqlValue::BigInt(i) => write!(f, "{}", i),
SqlValue::UnsignedTinyInt(i) => write!(f, "{}", i),
SqlValue::UnsignedSmallInt(i) => write!(f, "{}", i),
SqlValue::UnsignedInt(i) => write!(f, "{}", i),
SqlValue::UnsignedBigInt(i) => write!(f, "{}", i),
SqlValue::Float(fl) => write!(f, "{}", fl),
SqlValue::Double(d) => write!(f, "{}", d),
#[cfg(feature = "decimal")]
SqlValue::Decimal(d) => write!(f, "{}", d),
#[cfg(not(feature = "decimal"))]
SqlValue::Decimal(s) => write!(f, "{}", s),
SqlValue::String(s) | SqlValue::Text(s) => write!(f, "{}", s),
SqlValue::Enum(s) | SqlValue::Uuid(s) => write!(f, "{}", s),
SqlValue::Date(s) | SqlValue::Time(s) | SqlValue::DateTime(s) => write!(f, "{}", s),
SqlValue::Json(j) => write!(f, "{}", j),
SqlValue::Timestamp(ts) => write!(f, "{}", ts),
SqlValue::Inet(ip) => write!(f, "{}", ip),
SqlValue::Cidr(ip, prefix) => write!(f, "{}/{}", ip, prefix),
SqlValue::Bytes(b) => write!(f, "<binary:{} bytes>", b.len()),
SqlValue::Array(values) => {
write!(f, "[")?;
for (i, val) in values.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", val)?;
}
write!(f, "]")
}
}
}
}
fn base64_encode(data: &[u8]) -> String {
use base64::Engine;
base64::engine::general_purpose::STANDARD.encode(data)
}
fn hex_encode(data: &[u8]) -> String {
data.iter().map(|b| format!("{:02X}", b)).collect()
}
impl From<bool> for SqlValue {
fn from(v: bool) -> Self {
SqlValue::Bool(v)
}
}
impl From<i8> for SqlValue {
fn from(v: i8) -> Self {
SqlValue::TinyInt(v)
}
}
impl From<i16> for SqlValue {
fn from(v: i16) -> Self {
SqlValue::SmallInt(v)
}
}
impl From<i32> for SqlValue {
fn from(v: i32) -> Self {
SqlValue::Int(v)
}
}
impl From<i64> for SqlValue {
fn from(v: i64) -> Self {
SqlValue::BigInt(v)
}
}
impl From<u8> for SqlValue {
fn from(v: u8) -> Self {
SqlValue::UnsignedTinyInt(v)
}
}
impl From<u16> for SqlValue {
fn from(v: u16) -> Self {
SqlValue::UnsignedSmallInt(v)
}
}
impl From<u32> for SqlValue {
fn from(v: u32) -> Self {
SqlValue::UnsignedInt(v)
}
}
impl From<u64> for SqlValue {
fn from(v: u64) -> Self {
SqlValue::UnsignedBigInt(v)
}
}
impl From<f32> for SqlValue {
fn from(v: f32) -> Self {
SqlValue::Float(v)
}
}
impl From<f64> for SqlValue {
fn from(v: f64) -> Self {
SqlValue::Double(v)
}
}
impl From<String> for SqlValue {
fn from(s: String) -> Self {
if s.contains("::") {
SqlValue::Enum(s)
} else {
SqlValue::String(s)
}
}
}
impl From<&str> for SqlValue {
fn from(s: &str) -> Self {
if s.contains("::") {
SqlValue::Enum(s.to_string())
} else {
SqlValue::String(s.to_string())
}
}
}
impl From<Vec<u8>> for SqlValue {
fn from(v: Vec<u8>) -> Self {
SqlValue::Bytes(v)
}
}
impl From<JsonValue> for SqlValue {
fn from(v: JsonValue) -> Self {
SqlValue::Json(v)
}
}
impl<T> From<Option<T>> for SqlValue
where
T: Into<SqlValue>,
{
fn from(v: Option<T>) -> Self {
match v {
Some(val) => val.into(),
None => SqlValue::Null,
}
}
}
impl From<&String> for SqlValue {
fn from(s: &String) -> Self {
if s.contains("::") {
SqlValue::Enum(s.clone())
} else {
SqlValue::String(s.clone())
}
}
}
impl From<&i32> for SqlValue {
fn from(i: &i32) -> Self {
SqlValue::Int(*i)
}
}
impl From<&i64> for SqlValue {
fn from(i: &i64) -> Self {
SqlValue::BigInt(*i)
}
}
impl From<&bool> for SqlValue {
fn from(b: &bool) -> Self {
SqlValue::Bool(*b)
}
}
impl From<&f32> for SqlValue {
fn from(f: &f32) -> Self {
SqlValue::Float(*f)
}
}
impl From<&f64> for SqlValue {
fn from(f: &f64) -> Self {
SqlValue::Double(*f)
}
}
impl From<chrono::DateTime<chrono::Utc>> for SqlValue {
fn from(dt: chrono::DateTime<chrono::Utc>) -> Self {
SqlValue::DateTime(dt.to_rfc3339())
}
}
impl From<chrono::NaiveDate> for SqlValue {
fn from(date: chrono::NaiveDate) -> Self {
SqlValue::Date(date.to_string())
}
}
impl From<chrono::NaiveTime> for SqlValue {
fn from(time: chrono::NaiveTime) -> Self {
SqlValue::Time(time.to_string())
}
}
impl From<chrono::NaiveDateTime> for SqlValue {
fn from(dt: chrono::NaiveDateTime) -> Self {
let utc_dt = chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(dt, chrono::Utc);
SqlValue::DateTime(utc_dt.to_rfc3339())
}
}
impl From<uuid::Uuid> for SqlValue {
fn from(uuid: uuid::Uuid) -> Self {
SqlValue::Uuid(uuid.to_string())
}
}
#[cfg(feature = "decimal")]
impl From<rust_decimal::Decimal> for SqlValue {
fn from(d: rust_decimal::Decimal) -> Self {
SqlValue::Decimal(d)
}
}
impl From<IpAddr> for SqlValue {
fn from(ip: IpAddr) -> Self {
SqlValue::Inet(ip)
}
}
impl From<std::net::Ipv4Addr> for SqlValue {
fn from(ip: std::net::Ipv4Addr) -> Self {
SqlValue::Inet(IpAddr::V4(ip))
}
}
impl From<std::net::Ipv6Addr> for SqlValue {
fn from(ip: std::net::Ipv6Addr) -> Self {
SqlValue::Inet(IpAddr::V6(ip))
}
}
impl From<(IpAddr, u8)> for SqlValue {
fn from((ip, prefix): (IpAddr, u8)) -> Self {
SqlValue::Cidr(ip, prefix)
}
}
impl From<IpNetwork> for SqlValue {
fn from(network: IpNetwork) -> Self {
match network {
IpNetwork::V4(v4) => {
if v4.prefix() == 32 {
SqlValue::Inet(IpAddr::V4(v4.ip()))
} else {
SqlValue::Cidr(IpAddr::V4(v4.ip()), v4.prefix())
}
}
IpNetwork::V6(v6) => {
if v6.prefix() == 128 {
SqlValue::Inet(IpAddr::V6(v6.ip()))
} else {
SqlValue::Cidr(IpAddr::V6(v6.ip()), v6.prefix())
}
}
}
}
}