feat(macros): turn on support for JSON in mysql and arrays of JSON in postgres

This commit is contained in:
Ryan Leckey 2020-07-15 02:31:44 -07:00
parent 9b956c0d2c
commit b2ba00f4be
3 changed files with 39 additions and 2 deletions

View file

@ -14,7 +14,7 @@ use crate::types::Type;
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct Json<T>(pub T);
pub struct Json<T: ?Sized>(pub T);
impl<T> Deref for Json<T> {
type Target = T;
@ -44,6 +44,34 @@ where
}
}
impl<DB> Type<DB> for Vec<JsonValue>
where
Vec<Json<JsonValue>>: Type<DB>,
DB: Database,
{
fn type_info() -> DB::TypeInfo {
<Vec<Json<JsonValue>> as Type<DB>>::type_info()
}
fn compatible(ty: &DB::TypeInfo) -> bool {
<Vec<Json<JsonValue>> as Type<DB>>::compatible(ty)
}
}
impl<DB> Type<DB> for [JsonValue]
where
[Json<JsonValue>]: Type<DB>,
DB: Database,
{
fn type_info() -> DB::TypeInfo {
<[Json<JsonValue>] as Type<DB>>::type_info()
}
fn compatible(ty: &DB::TypeInfo) -> bool {
<[Json<JsonValue>] as Type<DB>>::compatible(ty)
}
}
impl<'q, DB> Encode<'q, DB> for JsonValue
where
Self: Type<DB>,

View file

@ -49,6 +49,12 @@ impl_database_ext! {
#[cfg(feature = "decimal")]
sqlx::types::Decimal,
#[cfg(feature = "json")]
serde_json::Value,
#[cfg(feature = "json")]
sqlx::types::Json<_>,
},
ParamChecking::Weak,
feature-types: info => info.__type_feature_gate(),

View file

@ -56,6 +56,7 @@ impl_database_ext! {
serde_json::Value,
// Arrays
Vec<bool> | &[bool],
Vec<String> | &[String],
Vec<i8> | &[i8],
@ -66,7 +67,6 @@ impl_database_ext! {
Vec<f32> | &[f32],
Vec<f64> | &[f64],
#[cfg(feature = "uuid")]
Vec<sqlx::types::Uuid> | &[sqlx::types::Uuid],
@ -101,6 +101,9 @@ impl_database_ext! {
#[cfg(feature = "ipnetwork")]
Vec<sqlx::types::ipnetwork::IpNetwork> | &[sqlx::types::ipnetwork::IpNetwork],
#[cfg(feature = "json")]
Vec<serde_json::Value> | &[serde_json::Value],
},
ParamChecking::Strong,
feature-types: info => info.__type_feature_gate(),