Skip to content

Commit 6cda01e

Browse files
committed
Handle MySQL into_json
1 parent 1228845 commit 6cda01e

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

src/query/json.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ impl FromQueryResult for JsonValue {
3434
}
3535
};
3636
}
37+
macro_rules! match_mysql_compatible_type {
38+
( $type: ty ) => {
39+
if <$type as Type<MySql>>::compatible(col_type) {
40+
try_get_type!($type, col)
41+
}
42+
};
43+
}
3744
match_mysql_type!(bool);
3845
match_mysql_type!(i8);
3946
match_mysql_type!(i16);
@@ -64,9 +71,9 @@ impl FromQueryResult for JsonValue {
6471
match_mysql_type!(time::OffsetDateTime);
6572
#[cfg(feature = "with-rust_decimal")]
6673
match_mysql_type!(rust_decimal::Decimal);
74+
match_mysql_compatible_type!(String);
6775
#[cfg(feature = "with-json")]
6876
try_get_type!(serde_json::Value, col);
69-
try_get_type!(String, col);
7077
#[cfg(feature = "with-uuid")]
7178
try_get_type!(uuid::Uuid, col);
7279
try_get_type!(Vec<u8>, col);

tests/uuid_tests.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,18 @@ pub async fn insert_metadata(db: &DatabaseConnection) -> Result<(), DbErr> {
3939
.one(db)
4040
.await?;
4141

42-
if cfg!(feature = "sqlx-mysql") {
43-
assert_eq!(
44-
json,
45-
Some(json!({
46-
"uuid": metadata.uuid,
47-
"type": metadata.ty,
48-
"key": metadata.key,
49-
"value": 1.18,
50-
"bytes": "\u{1}\u{2}\u{3}",
51-
"date": metadata.date,
52-
"time": metadata.time,
53-
}))
54-
);
55-
} else {
56-
assert_eq!(
57-
json,
58-
Some(json!({
59-
"uuid": metadata.uuid,
60-
"type": metadata.ty,
61-
"key": metadata.key,
62-
"value": metadata.value,
63-
"bytes": metadata.bytes,
64-
"date": metadata.date,
65-
"time": metadata.time,
66-
}))
67-
);
68-
}
42+
assert_eq!(
43+
json,
44+
Some(json!({
45+
"uuid": metadata.uuid,
46+
"type": metadata.ty,
47+
"key": metadata.key,
48+
"value": metadata.value,
49+
"bytes": metadata.bytes,
50+
"date": metadata.date,
51+
"time": metadata.time,
52+
}))
53+
);
6954

7055
Ok(())
7156
}

0 commit comments

Comments
 (0)