Skip to content

Commit

Permalink
Handle MySQL into_json
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Aug 9, 2024
1 parent 1228845 commit 6cda01e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
9 changes: 8 additions & 1 deletion src/query/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ impl FromQueryResult for JsonValue {
}
};
}
macro_rules! match_mysql_compatible_type {
( $type: ty ) => {
if <$type as Type<MySql>>::compatible(col_type) {
try_get_type!($type, col)
}
};
}
match_mysql_type!(bool);
match_mysql_type!(i8);
match_mysql_type!(i16);
Expand Down Expand Up @@ -64,9 +71,9 @@ impl FromQueryResult for JsonValue {
match_mysql_type!(time::OffsetDateTime);
#[cfg(feature = "with-rust_decimal")]
match_mysql_type!(rust_decimal::Decimal);
match_mysql_compatible_type!(String);
#[cfg(feature = "with-json")]
try_get_type!(serde_json::Value, col);
try_get_type!(String, col);
#[cfg(feature = "with-uuid")]
try_get_type!(uuid::Uuid, col);
try_get_type!(Vec<u8>, col);
Expand Down
39 changes: 12 additions & 27 deletions tests/uuid_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,18 @@ pub async fn insert_metadata(db: &DatabaseConnection) -> Result<(), DbErr> {
.one(db)
.await?;

if cfg!(feature = "sqlx-mysql") {
assert_eq!(
json,
Some(json!({
"uuid": metadata.uuid,
"type": metadata.ty,
"key": metadata.key,
"value": 1.18,
"bytes": "\u{1}\u{2}\u{3}",
"date": metadata.date,
"time": metadata.time,
}))
);
} else {
assert_eq!(
json,
Some(json!({
"uuid": metadata.uuid,
"type": metadata.ty,
"key": metadata.key,
"value": metadata.value,
"bytes": metadata.bytes,
"date": metadata.date,
"time": metadata.time,
}))
);
}
assert_eq!(
json,
Some(json!({
"uuid": metadata.uuid,
"type": metadata.ty,
"key": metadata.key,
"value": metadata.value,
"bytes": metadata.bytes,
"date": metadata.date,
"time": metadata.time,
}))
);

Ok(())
}
Expand Down

0 comments on commit 6cda01e

Please sign in to comment.