Skip to content

Commit 11c348e

Browse files
committed
fix mysql TinyInt serialization
fixes #91
1 parent 1f68c04 commit 11c348e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/mysql/serialize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(super) struct ToSqlHelper {
1212
fn to_value((metadata, bind): (MysqlType, Option<Vec<u8>>)) -> Value {
1313
match bind {
1414
Some(bind) => match metadata {
15-
MysqlType::Tiny => Value::Int(bind[0] as _),
15+
MysqlType::Tiny => Value::Int((bind[0] as i8) as i64),
1616
MysqlType::Short => Value::Int(i16::from_ne_bytes(bind.try_into().unwrap()) as _),
1717
MysqlType::Long => Value::Int(i32::from_ne_bytes(bind.try_into().unwrap()) as _),
1818
MysqlType::LongLong => Value::Int(i64::from_ne_bytes(bind.try_into().unwrap())),

tests/type_check.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@ async fn check_tiny_int() {
6767
type_check::<_, sql_types::TinyInt>(conn, -1_i8).await;
6868
type_check::<_, sql_types::TinyInt>(conn, i8::MIN).await;
6969
type_check::<_, sql_types::TinyInt>(conn, i8::MAX).await;
70+
71+
#[derive(QueryableByName, Debug)]
72+
#[diesel(table_name = test_small)]
73+
struct Test {
74+
id: i8,
75+
}
76+
77+
table!(test_small(id){
78+
id -> TinyInt,
79+
});
80+
81+
// test case for https://github.com/weiznich/diesel_async/issues/91
82+
diesel::sql_query("drop table if exists test_small")
83+
.execute(conn)
84+
.await
85+
.unwrap();
86+
diesel::sql_query("create table test_small(id smallint primary key)")
87+
.execute(conn)
88+
.await
89+
.unwrap();
90+
diesel::sql_query("insert into test_small(id) values(-1)")
91+
.execute(conn)
92+
.await
93+
.unwrap();
94+
let got = diesel::sql_query("select id from test_small where id = ?")
95+
.bind::<sql_types::TinyInt, _>(-1)
96+
.load::<Test>(conn)
97+
.await
98+
.unwrap();
99+
assert_eq!(got[0].id, -1);
70100
}
71101

72102
#[cfg(feature = "mysql")]

0 commit comments

Comments
 (0)