From 2157712d3ef091106b1b1b5bfb55d001fb477656 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 17 Feb 2022 15:45:51 +0800 Subject: [PATCH] Fixing and testing `into_json` of various types --- src/query/json.rs | 60 +++++++++++++++++++++++++++++++++++++- tests/timestamp_tests.rs | 63 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/src/query/json.rs b/src/query/json.rs index 589f0fada3..1ba72c2b5d 100644 --- a/src/query/json.rs +++ b/src/query/json.rs @@ -28,6 +28,17 @@ impl FromQueryResult for JsonValue { } }; } + macro_rules! compatible_mysql_type { + ( $type: ty ) => { + if <$type as Type>::compatible(col_type) { + map.insert( + col.to_owned(), + json!(res.try_get::>(pre, &col)?), + ); + continue; + } + }; + } match_mysql_type!(bool); match_mysql_type!(i8); match_mysql_type!(i16); @@ -40,6 +51,20 @@ impl FromQueryResult for JsonValue { match_mysql_type!(f32); match_mysql_type!(f64); match_mysql_type!(String); + #[cfg(feature = "with-chrono")] + match_mysql_type!(chrono::NaiveDate); + #[cfg(feature = "with-chrono")] + match_mysql_type!(chrono::NaiveTime); + #[cfg(feature = "with-chrono")] + match_mysql_type!(chrono::NaiveDateTime); + #[cfg(feature = "with-chrono")] + match_mysql_type!(chrono::DateTime); + #[cfg(feature = "with-uuid")] + match_mysql_type!(uuid::Uuid); + #[cfg(feature = "with-rust_decimal")] + match_mysql_type!(rust_decimal::Decimal); + #[cfg(feature = "with-json")] + compatible_mysql_type!(serde_json::Value); } Ok(JsonValue::Object(map)) } @@ -66,6 +91,17 @@ impl FromQueryResult for JsonValue { } }; } + macro_rules! compatible_postgres_type { + ( $type: ty ) => { + if <$type as Type>::compatible(col_type) { + map.insert( + col.to_owned(), + json!(res.try_get::>(pre, &col)?), + ); + continue; + } + }; + } match_postgres_type!(bool); match_postgres_type!(i8); match_postgres_type!(i16); @@ -77,7 +113,21 @@ impl FromQueryResult for JsonValue { // match_postgres_type!(u64); // unsupported by SQLx Postgres match_postgres_type!(f32); match_postgres_type!(f64); - match_postgres_type!(String); + #[cfg(feature = "with-chrono")] + match_postgres_type!(chrono::NaiveDate); + #[cfg(feature = "with-chrono")] + match_postgres_type!(chrono::NaiveTime); + #[cfg(feature = "with-chrono")] + match_postgres_type!(chrono::NaiveDateTime); + #[cfg(feature = "with-chrono")] + match_postgres_type!(chrono::DateTime); + #[cfg(feature = "with-uuid")] + match_postgres_type!(uuid::Uuid); + #[cfg(feature = "with-rust_decimal")] + match_postgres_type!(rust_decimal::Decimal); + #[cfg(feature = "with-json")] + compatible_postgres_type!(serde_json::Value); + compatible_postgres_type!(String); } Ok(JsonValue::Object(map)) } @@ -116,6 +166,14 @@ impl FromQueryResult for JsonValue { match_sqlite_type!(f32); match_sqlite_type!(f64); match_sqlite_type!(String); + #[cfg(feature = "with-chrono")] + match_sqlite_type!(chrono::NaiveDate); + #[cfg(feature = "with-chrono")] + match_sqlite_type!(chrono::NaiveTime); + #[cfg(feature = "with-chrono")] + match_sqlite_type!(chrono::NaiveDateTime); + #[cfg(feature = "with-uuid")] + match_sqlite_type!(uuid::Uuid); } Ok(JsonValue::Object(map)) } diff --git a/tests/timestamp_tests.rs b/tests/timestamp_tests.rs index d0334a5cbc..b7718ef32c 100644 --- a/tests/timestamp_tests.rs +++ b/tests/timestamp_tests.rs @@ -1,6 +1,7 @@ pub mod common; pub use common::{features::*, setup::*, TestContext}; use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel}; +use serde_json::json; #[sea_orm_macros::test] #[cfg(any( @@ -34,6 +35,37 @@ pub async fn create_applog(db: &DatabaseConnection) -> Result<(), DbErr> { assert_eq!(log.id, res.last_insert_id); assert_eq!(Applog::find().one(db).await?, Some(log.clone())); + #[cfg(feature = "sqlx-sqlite")] + assert_eq!( + Applog::find().into_json().one(db).await?, + Some(json!({ + "id": 1, + "action": "Testing", + "json": r#""HI""#, + "created_at": "2021-09-17 09:50:20", + })) + ); + #[cfg(feature = "sqlx-mysql")] + assert_eq!( + Applog::find().into_json().one(db).await?, + Some(json!({ + "id": 1, + "action": "Testing", + "json": "HI", + "created_at": "2021-09-17T09:50:20Z", + })) + ); + #[cfg(feature = "sqlx-postgres")] + assert_eq!( + Applog::find().into_json().one(db).await?, + Some(json!({ + "id": 1, + "action": "Testing", + "json": "HI", + "created_at": "2021-09-17T09:50:20+00:00", + })) + ); + Ok(()) } @@ -52,5 +84,36 @@ pub async fn create_satellites_log(db: &DatabaseConnection) -> Result<(), DbErr> assert_eq!(archive.id, res.last_insert_id); assert_eq!(Satellite::find().one(db).await?, Some(archive.clone())); + #[cfg(feature = "sqlx-sqlite")] + assert_eq!( + Satellite::find().into_json().one(db).await?, + Some(json!({ + "id": 1, + "satellite_name": "Sea-00001-2022", + "launch_date": "2022-01-07 12:11:23", + "deployment_date": "2022-01-07 12:11:23", + })) + ); + #[cfg(feature = "sqlx-mysql")] + assert_eq!( + Satellite::find().into_json().one(db).await?, + Some(json!({ + "id": 1, + "satellite_name": "Sea-00001-2022", + "launch_date": "2022-01-07T12:11:23Z", + "deployment_date": "2022-01-07T12:11:23Z", + })) + ); + #[cfg(feature = "sqlx-postgres")] + assert_eq!( + Satellite::find().into_json().one(db).await?, + Some(json!({ + "id": 1, + "satellite_name": "Sea-00001-2022", + "launch_date": "2022-01-07T12:11:23+00:00", + "deployment_date": "2022-01-07T12:11:23+00:00", + })) + ); + Ok(()) }