use sea_query::{OptionEnum, Value, Values};
use super::time_conv as tc;
use crate::error::SqlError;
use crate::value::SqlValue;
enum Unbridged {
OutOfRange(String),
Unsupported(String),
}
impl Unbridged {
fn into_error(self, column: String) -> SqlError {
match self {
Unbridged::OutOfRange(message) => SqlError::ValueOutOfRange { column, message },
Unbridged::Unsupported(what) => {
SqlError::Unsupported(format!("{column}: sea-query value {what} has no SqlValue"))
}
}
}
}
fn some<T>(v: Option<T>, f: impl FnOnce(T) -> SqlValue) -> SqlValue {
v.map_or(SqlValue::Null, f)
}
fn some_try<T>(
v: Option<T>,
f: impl FnOnce(T) -> Result<SqlValue, String>,
) -> Result<SqlValue, Unbridged> {
v.map_or(Ok(SqlValue::Null), |x| f(x).map_err(Unbridged::OutOfRange))
}
fn bridge(v: Value) -> Result<SqlValue, Unbridged> {
Ok(match v {
Value::Bool(b) => some(b, SqlValue::Bool),
Value::TinyInt(n) => some(n, |n| SqlValue::I16(n.into())),
Value::SmallInt(n) => some(n, SqlValue::I16),
Value::Int(n) => some(n, SqlValue::I32),
Value::BigInt(n) => some(n, SqlValue::I64),
Value::TinyUnsigned(n) => some(n, |n| SqlValue::I16(n.into())),
Value::SmallUnsigned(n) => some(n, |n| SqlValue::I32(n.into())),
Value::Unsigned(n) => some(n, |n| SqlValue::I64(n.into())),
Value::BigUnsigned(n) => some_try(n, |n| {
i64::try_from(n)
.map(SqlValue::I64)
.map_err(|_| format!("{n} does not fit an I64"))
})?,
Value::Float(x) => some(x, SqlValue::F32),
Value::Double(x) => some(x, SqlValue::F64),
Value::String(s) => some(s, SqlValue::Text),
Value::Char(c) => some(c, |c| SqlValue::Text(c.to_string())),
Value::Enum(OptionEnum::Some(e)) => SqlValue::Text(e.value.into_owned()),
Value::Enum(OptionEnum::None(_)) => SqlValue::Null,
Value::Bytes(b) => some(b, SqlValue::Bytes),
Value::Json(j) => some(j, |j| SqlValue::Json(*j)),
Value::Uuid(u) => some(u, SqlValue::Uuid),
Value::Decimal(d) => some(d, SqlValue::Decimal),
Value::JiffTimestamp(t) => some(t, |t| SqlValue::Timestamp(*t)),
Value::JiffDateTime(dt) => some(dt, |dt| SqlValue::DateTime(*dt)),
Value::JiffDate(d) => some(d, SqlValue::Date),
Value::JiffTime(t) => some(t, SqlValue::Time),
Value::TimeDateTimeWithTimeZone(t) => {
some_try(t, |t| tc::timestamp(t).map(SqlValue::Timestamp))?
}
Value::TimeDateTime(dt) => some_try(dt, |dt| tc::datetime(dt).map(SqlValue::DateTime))?,
Value::TimeDate(d) => some_try(d, |d| tc::date(d).map(SqlValue::Date))?,
Value::TimeTime(t) => some(t, |t| SqlValue::Time(tc::time_of_day(t))),
#[allow(unreachable_patterns)]
other => return Err(Unbridged::Unsupported(format!("{other:?}"))),
})
}
impl TryFrom<Value> for SqlValue {
type Error = SqlError;
fn try_from(v: Value) -> Result<Self, SqlError> {
bridge(v).map_err(|e| e.into_error("value".into()))
}
}
pub fn sql_values(values: Values) -> Result<Vec<SqlValue>, SqlError> {
values
.0
.into_iter()
.enumerate()
.map(|(i, v)| bridge(v).map_err(|e| e.into_error(format!("${}", i + 1))))
.collect()
}
impl From<SqlValue> for Value {
fn from(v: SqlValue) -> Self {
match v {
SqlValue::Null => Value::String(None),
SqlValue::Bool(b) => b.into(),
SqlValue::I16(n) => n.into(),
SqlValue::I32(n) => n.into(),
SqlValue::I64(n) => n.into(),
SqlValue::F32(x) => x.into(),
SqlValue::F64(x) => x.into(),
SqlValue::Text(s) => s.into(),
SqlValue::Bytes(b) => b.into(),
SqlValue::Uuid(u) => u.into(),
SqlValue::Timestamp(t) => t.into(),
SqlValue::DateTime(dt) => dt.into(),
SqlValue::Date(d) => d.into(),
SqlValue::Time(t) => t.into(),
SqlValue::Decimal(d) => d.into(),
SqlValue::Json(j) => j.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_unsupported_variant_names_the_parameter() {
assert_eq!(
Unbridged::Unsupported("Array(..)".into()).into_error("$3".into()),
SqlError::Unsupported("$3: sea-query value Array(..) has no SqlValue".into())
);
}
}