11use diesel:: mysql:: data_types:: MysqlTime ;
22use diesel:: mysql:: MysqlType ;
33use diesel:: mysql:: MysqlValue ;
4+ use diesel:: QueryResult ;
45use mysql_async:: { Params , Value } ;
56use std:: convert:: TryInto ;
67
@@ -9,10 +10,11 @@ pub(super) struct ToSqlHelper {
910 pub ( super ) binds : Vec < Option < Vec < u8 > > > ,
1011}
1112
12- fn to_value ( ( metadata, bind) : ( MysqlType , Option < Vec < u8 > > ) ) -> Value {
13- match bind {
13+ fn to_value ( ( metadata, bind) : ( MysqlType , Option < Vec < u8 > > ) ) -> QueryResult < Value > {
14+ let cast_helper = |e| diesel:: result:: Error :: SerializationError ( Box :: new ( e) ) ;
15+ let v = match bind {
1416 Some ( bind) => match metadata {
15- MysqlType :: Tiny => Value :: Int ( ( bind[ 0 ] as i8 ) as i64 ) ,
17+ MysqlType :: Tiny => Value :: Int ( i8 :: from_be_bytes ( [ bind[ 0 ] ] ) as i64 ) ,
1618 MysqlType :: Short => Value :: Int ( i16:: from_ne_bytes ( bind. try_into ( ) . unwrap ( ) ) as _ ) ,
1719 MysqlType :: Long => Value :: Int ( i32:: from_ne_bytes ( bind. try_into ( ) . unwrap ( ) ) as _ ) ,
1820 MysqlType :: LongLong => Value :: Int ( i64:: from_ne_bytes ( bind. try_into ( ) . unwrap ( ) ) ) ,
@@ -38,11 +40,11 @@ fn to_value((metadata, bind): (MysqlType, Option<Vec<u8>>)) -> Value {
3840 . expect ( "This does not fail" ) ;
3941 Value :: Time (
4042 time. neg ,
41- time. day as _ ,
42- time. hour as _ ,
43- time. minute as _ ,
44- time. second as _ ,
45- time. second_part as _ ,
43+ time. day ,
44+ time. hour . try_into ( ) . map_err ( cast_helper ) ? ,
45+ time. minute . try_into ( ) . map_err ( cast_helper ) ? ,
46+ time. second . try_into ( ) . map_err ( cast_helper ) ? ,
47+ time. second_part . try_into ( ) . expect ( "Cast does not fail" ) ,
4648 )
4749 }
4850 MysqlType :: Date | MysqlType :: DateTime | MysqlType :: Timestamp => {
@@ -52,13 +54,13 @@ fn to_value((metadata, bind): (MysqlType, Option<Vec<u8>>)) -> Value {
5254 > :: from_sql ( MysqlValue :: new ( & bind, metadata) )
5355 . expect ( "This does not fail" ) ;
5456 Value :: Date (
55- time. year as _ ,
56- time. month as _ ,
57- time. day as _ ,
58- time. hour as _ ,
59- time. minute as _ ,
60- time. second as _ ,
61- time. second_part as _ ,
57+ time. year . try_into ( ) . map_err ( cast_helper ) ? ,
58+ time. month . try_into ( ) . map_err ( cast_helper ) ? ,
59+ time. day . try_into ( ) . map_err ( cast_helper ) ? ,
60+ time. hour . try_into ( ) . map_err ( cast_helper ) ? ,
61+ time. minute . try_into ( ) . map_err ( cast_helper ) ? ,
62+ time. second . try_into ( ) . map_err ( cast_helper ) ? ,
63+ time. second_part . try_into ( ) . expect ( "Cast does not fail" ) ,
6264 )
6365 }
6466 MysqlType :: Numeric
@@ -70,12 +72,19 @@ fn to_value((metadata, bind): (MysqlType, Option<Vec<u8>>)) -> Value {
7072 _ => unreachable ! ( ) ,
7173 } ,
7274 None => Value :: NULL ,
73- }
75+ } ;
76+ Ok ( v)
7477}
7578
76- impl From < ToSqlHelper > for Params {
77- fn from ( ToSqlHelper { metadata, binds } : ToSqlHelper ) -> Self {
78- let values = metadata. into_iter ( ) . zip ( binds) . map ( to_value) . collect ( ) ;
79- Params :: Positional ( values)
79+ impl TryFrom < ToSqlHelper > for Params {
80+ type Error = diesel:: result:: Error ;
81+
82+ fn try_from ( ToSqlHelper { metadata, binds } : ToSqlHelper ) -> Result < Self , Self :: Error > {
83+ let values = metadata
84+ . into_iter ( )
85+ . zip ( binds)
86+ . map ( to_value)
87+ . collect :: < Result < Vec < _ > , Self :: Error > > ( ) ?;
88+ Ok ( Params :: Positional ( values) )
8089 }
8190}
0 commit comments