fix(mysql): tweak JSON type so it accepts BINARY in addition to CHAR (and make tests pass in MySQL 5.5)

This commit is contained in:
Ryan Leckey 2020-06-10 00:40:30 -07:00
parent 6fcb520ac9
commit a2c55b9f31
3 changed files with 7 additions and 6 deletions

View File

@ -34,7 +34,9 @@ where
T: 'r + DeserializeOwned,
{
fn accepts(ty: &MySqlTypeInfo) -> bool {
ty.r#type == ColumnType::Json || <&str as Decode<MySql>>::accepts(ty)
ty.r#type == ColumnType::Json
|| <&str as Decode<MySql>>::accepts(ty)
|| <&[u8] as Decode<MySql>>::accepts(ty)
}
fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> {

View File

@ -2,7 +2,6 @@ use futures::TryStreamExt;
use sqlx::mysql::{MySql, MySqlPool, MySqlRow};
use sqlx::{Connection, Executor, Row};
use sqlx_test::new;
use std::env;
#[sqlx_macros::test]
async fn it_connects() -> anyhow::Result<()> {

View File

@ -123,11 +123,11 @@ mod json_tests {
test_type!(json<JsonValue>(
MySql,
"SELECT CAST({0} AS JSON) <=> CAST(? AS JSON), CAST({0} AS JSON) as _2, ? as _3",
"SELECT CAST({0} AS BINARY) <=> CAST(? AS BINARY), CAST({0} AS BINARY) as _2, ? as _3",
"'\"Hello, World\"'" == json!("Hello, World"),
"'\"😎\"'" == json!("😎"),
"'\"🙋‍♀️\"'" == json!("🙋‍♀️"),
"'[\"Hello\", \"World!\"]'" == json!(["Hello", "World!"])
"'[\"Hello\",\"World!\"]'" == json!(["Hello", "World!"])
));
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq)]
@ -138,7 +138,7 @@ mod json_tests {
test_type!(json_struct<Json<Friend>>(
MySql,
"SELECT CAST({0} AS JSON) <=> CAST(? AS JSON), CAST({0} AS JSON) as _2, ? as _3",
"\'{\"name\": \"Joe\", \"age\":33}\'" == Json(Friend { name: "Joe".to_string(), age: 33 })
"SELECT CAST({0} AS BINARY) <=> CAST(? AS BINARY), CAST({0} AS BINARY) as _2, ? as _3",
"\'{\"name\":\"Joe\",\"age\":33}\'" == Json(Friend { name: "Joe".to_string(), age: 33 })
));
}