feat(sql): Improve type decoding. Adds support for time and dates (#266)
* feat(sql): Improve type decoding. Adds support for time and dates * remove default feat * typopull/269/head
parent
265acf7454
commit
1d52416f70
@ -0,0 +1,15 @@
|
||||
#[cfg(feature = "mysql")]
|
||||
mod mysql;
|
||||
#[cfg(feature = "postgres")]
|
||||
mod postgres;
|
||||
#[cfg(feature = "sqlite")]
|
||||
mod sqlite;
|
||||
|
||||
#[cfg(feature = "mysql")]
|
||||
pub(crate) use mysql::to_json;
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
pub(crate) use postgres::to_json;
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
pub(crate) use sqlite::to_json;
|
@ -0,0 +1,90 @@
|
||||
use serde_json::Value as JsonValue;
|
||||
use sqlx::{mysql::MySqlValueRef, TypeInfo, Value, ValueRef};
|
||||
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
|
||||
|
||||
use crate::Error;
|
||||
|
||||
pub(crate) fn to_json(v: MySqlValueRef) -> Result<JsonValue, Error> {
|
||||
if v.is_null() {
|
||||
return Ok(JsonValue::Null);
|
||||
}
|
||||
|
||||
let res = match v.type_info().name() {
|
||||
"CHAR" | "VARCHAR" | "TINYTEXT" | "TEXT" | "MEDIUMTEXT" | "LONGTEXT" | "ENUM" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode() {
|
||||
JsonValue::String(v)
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"FLOAT" | "DOUBLE" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<f64>() {
|
||||
JsonValue::from(v)
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"TINYINT" | "SMALLINT" | "INT" | "MEDIUMINT" | "BIGINT" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<i64>() {
|
||||
JsonValue::Number(v.into())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"TINYINT UNSIGNED" | "SMALLINT UNSIGNED" | "INT UNSIGNED" | "MEDIUMINT UNSIGNED"
|
||||
| "BIGINT UNSIGNED" | "YEAR" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<u64>() {
|
||||
JsonValue::Number(v.into())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"BOOLEAN" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode() {
|
||||
JsonValue::Bool(v)
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"DATE" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<Date>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"TIME" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<Time>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"DATETIME" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<PrimitiveDateTime>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"TIMESTAMP" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<OffsetDateTime>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"JSON" => ValueRef::to_owned(&v).try_decode().unwrap_or_default(),
|
||||
"TINIYBLOB" | "MEDIUMBLOB" | "BLOB" | "LONGBLOB" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<Vec<u8>>() {
|
||||
JsonValue::Array(v.into_iter().map(|n| JsonValue::Number(n.into())).collect())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"NULL" => JsonValue::Null,
|
||||
_ => return Err(Error::UnsupportedDatatype(v.type_info().name().to_string())),
|
||||
};
|
||||
|
||||
Ok(res)
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
use serde_json::Value as JsonValue;
|
||||
use sqlx::{postgres::PgValueRef, TypeInfo, Value, ValueRef};
|
||||
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
|
||||
|
||||
use crate::Error;
|
||||
|
||||
pub(crate) fn to_json(v: PgValueRef) -> Result<JsonValue, Error> {
|
||||
if v.is_null() {
|
||||
return Ok(JsonValue::Null);
|
||||
}
|
||||
|
||||
let res = match v.type_info().name() {
|
||||
"CHAR" | "VARCHAR" | "TEXT" | "NAME" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode() {
|
||||
JsonValue::String(v)
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"FLOAT4" | "FLOAT8" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<f64>() {
|
||||
JsonValue::from(v)
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"INT2" | "INT4" | "INT8" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<i64>() {
|
||||
JsonValue::Number(v.into())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"BOOL" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode() {
|
||||
JsonValue::Bool(v)
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"DATE" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<Date>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"TIME" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<Time>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"TIMESTAMP" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<PrimitiveDateTime>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"TIMESTAMPTZ" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<OffsetDateTime>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"JSON" | "JSONB" => ValueRef::to_owned(&v).try_decode().unwrap_or_default(),
|
||||
"BYTEA" => {
|
||||
if let Ok(v) = ValueRef::to_owned(&v).try_decode::<Vec<u8>>() {
|
||||
JsonValue::Array(v.into_iter().map(|n| JsonValue::Number(n.into())).collect())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"VOID" => JsonValue::Null,
|
||||
_ => return Err(Error::UnsupportedDatatype(v.type_info().name().to_string())),
|
||||
};
|
||||
|
||||
Ok(res)
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
use serde_json::Value as JsonValue;
|
||||
use sqlx::{sqlite::SqliteValueRef, TypeInfo, Value, ValueRef};
|
||||
use time::{Date, PrimitiveDateTime, Time};
|
||||
|
||||
use crate::Error;
|
||||
|
||||
pub(crate) fn to_json(v: SqliteValueRef) -> Result<JsonValue, Error> {
|
||||
if v.is_null() {
|
||||
return Ok(JsonValue::Null);
|
||||
}
|
||||
|
||||
let res = match v.type_info().name() {
|
||||
"TEXT" => {
|
||||
if let Ok(v) = v.to_owned().try_decode() {
|
||||
JsonValue::String(v)
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"REAL" => {
|
||||
if let Ok(v) = v.to_owned().try_decode::<f64>() {
|
||||
JsonValue::from(v)
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"INTEGER" | "NUMERIC" => {
|
||||
if let Ok(v) = v.to_owned().try_decode::<i64>() {
|
||||
JsonValue::Number(v.into())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"BOOLEAN" => {
|
||||
if let Ok(v) = v.to_owned().try_decode() {
|
||||
JsonValue::Bool(v)
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"DATE" => {
|
||||
if let Ok(v) = v.to_owned().try_decode::<Date>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"TIME" => {
|
||||
if let Ok(v) = v.to_owned().try_decode::<Time>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"DATETIME" => {
|
||||
if let Ok(v) = v.to_owned().try_decode::<PrimitiveDateTime>() {
|
||||
JsonValue::String(v.to_string())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"BLOB" => {
|
||||
if let Ok(v) = v.to_owned().try_decode::<Vec<u8>>() {
|
||||
JsonValue::Array(v.into_iter().map(|n| JsonValue::Number(n.into())).collect())
|
||||
} else {
|
||||
JsonValue::Null
|
||||
}
|
||||
}
|
||||
"NULL" => JsonValue::Null,
|
||||
_ => return Err(Error::UnsupportedDatatype(v.type_info().name().to_string())),
|
||||
};
|
||||
|
||||
Ok(res)
|
||||
}
|
Loading…
Reference in new issue