Skip to content

Commit

Permalink
Merge pull request prisma#116 from prisma/mysql/support-newdecimal
Browse files Browse the repository at this point in the history
Convert MySQL NEWDECIMAL to a numeric value
  • Loading branch information
tomhoule authored Apr 9, 2020
2 parents 693906b + ac34a83 commit f85183c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/connector/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,15 @@ VALUES (1, 'Joe', 27, 20000.00 );

assert_eq!(result.into_single().unwrap().at(0).unwrap().as_f64().unwrap(), 6.412345);
}

#[tokio::test]
async fn newdecimal_conversion_is_handled_correctly() {
let conn = Quaint::new(&CONN_STR).await.unwrap();
let result = conn.query_raw("SELECT SUM(1) AS THEONE", &[]).await.unwrap();

assert_eq!(
result.into_single().unwrap()[0],
ParameterizedValue::Real("1.0".parse().unwrap())
);
}
}
14 changes: 14 additions & 0 deletions src/connector/mysql/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ impl TakeRow for my::Row {
};
let res = match value {
my::Value::NULL => ParameterizedValue::Null,
// NEWDECIMAL returned as bytes. See https://mariadb.com/kb/en/resultset-row/#decimal-binary-encoding
my::Value::Bytes(b)
if column.column_type() == mysql_async::consts::ColumnType::MYSQL_TYPE_NEWDECIMAL =>
{
ParameterizedValue::Real(
String::from_utf8(b)
.expect("MySQL NEWDECIMAL as string")
.parse()
.map_err(|_err| {
crate::error::Error::builder(ErrorKind::ConversionError("mysql NEWDECIMAL conversion"))
.build()
})?,
)
}
// https://dev.mysql.com/doc/internals/en/character-set.html
my::Value::Bytes(b) if column.character_set() == 63 => ParameterizedValue::Bytes(b.into()),
my::Value::Bytes(s) => ParameterizedValue::Text(String::from_utf8(s)?.into()),
Expand Down

0 comments on commit f85183c

Please sign in to comment.