1
1
use diesel:: mysql:: data_types:: MysqlTime ;
2
2
use diesel:: mysql:: MysqlType ;
3
3
use diesel:: mysql:: MysqlValue ;
4
+ use diesel:: QueryResult ;
4
5
use mysql_async:: { Params , Value } ;
5
6
use std:: convert:: TryInto ;
6
7
@@ -9,10 +10,11 @@ pub(super) struct ToSqlHelper {
9
10
pub ( super ) binds : Vec < Option < Vec < u8 > > > ,
10
11
}
11
12
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 {
14
16
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 ) ,
16
18
MysqlType :: Short => Value :: Int ( i16:: from_ne_bytes ( bind. try_into ( ) . unwrap ( ) ) as _ ) ,
17
19
MysqlType :: Long => Value :: Int ( i32:: from_ne_bytes ( bind. try_into ( ) . unwrap ( ) ) as _ ) ,
18
20
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 {
38
40
. expect ( "This does not fail" ) ;
39
41
Value :: Time (
40
42
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" ) ,
46
48
)
47
49
}
48
50
MysqlType :: Date | MysqlType :: DateTime | MysqlType :: Timestamp => {
@@ -52,13 +54,13 @@ fn to_value((metadata, bind): (MysqlType, Option<Vec<u8>>)) -> Value {
52
54
> :: from_sql ( MysqlValue :: new ( & bind, metadata) )
53
55
. expect ( "This does not fail" ) ;
54
56
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" ) ,
62
64
)
63
65
}
64
66
MysqlType :: Numeric
@@ -70,12 +72,19 @@ fn to_value((metadata, bind): (MysqlType, Option<Vec<u8>>)) -> Value {
70
72
_ => unreachable ! ( ) ,
71
73
} ,
72
74
None => Value :: NULL ,
73
- }
75
+ } ;
76
+ Ok ( v)
74
77
}
75
78
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) )
80
89
}
81
90
}
0 commit comments