@@ -28,7 +28,8 @@ impl Encode<'_, Mssql> for i8 {
28
28
29
29
impl Decode < ' _ , Mssql > for i8 {
30
30
fn decode ( value : MssqlValueRef < ' _ > ) -> Result < Self , BoxDynError > {
31
- decode_integer ( value)
31
+ let i64_val = <i64 as Decode < Mssql > >:: decode ( value) ?;
32
+ convert_integer :: < Self > ( i64_val)
32
33
}
33
34
}
34
35
@@ -55,7 +56,8 @@ impl Encode<'_, Mssql> for i16 {
55
56
56
57
impl Decode < ' _ , Mssql > for i16 {
57
58
fn decode ( value : MssqlValueRef < ' _ > ) -> Result < Self , BoxDynError > {
58
- decode_integer ( value)
59
+ let i64_val = <i64 as Decode < Mssql > >:: decode ( value) ?;
60
+ convert_integer :: < Self > ( i64_val)
59
61
}
60
62
}
61
63
@@ -79,7 +81,8 @@ impl Encode<'_, Mssql> for i32 {
79
81
80
82
impl Decode < ' _ , Mssql > for i32 {
81
83
fn decode ( value : MssqlValueRef < ' _ > ) -> Result < Self , BoxDynError > {
82
- decode_integer ( value)
84
+ let i64_val = <i64 as Decode < Mssql > >:: decode ( value) ?;
85
+ convert_integer :: < Self > ( i64_val)
83
86
}
84
87
}
85
88
@@ -114,7 +117,43 @@ impl Encode<'_, Mssql> for i64 {
114
117
115
118
impl Decode < ' _ , Mssql > for i64 {
116
119
fn decode ( value : MssqlValueRef < ' _ > ) -> Result < Self , BoxDynError > {
117
- decode_integer ( value)
120
+ let ty = value. type_info . 0 . ty ;
121
+ let precision = value. type_info . 0 . precision ;
122
+ let scale = value. type_info . 0 . scale ;
123
+
124
+ match ty {
125
+ DataType :: SmallInt
126
+ | DataType :: Int
127
+ | DataType :: TinyInt
128
+ | DataType :: BigInt
129
+ | DataType :: IntN => {
130
+ let mut buf = [ 0u8 ; 8 ] ;
131
+ let bytes_val = value. as_bytes ( ) ?;
132
+ let len = bytes_val. len ( ) ;
133
+
134
+ if len > buf. len ( ) {
135
+ return Err ( err_protocol ! (
136
+ "Decoding {:?} as a i64 failed because type {:?} has more than {} bytes" ,
137
+ value,
138
+ ty,
139
+ buf. len( )
140
+ )
141
+ . into ( ) ) ;
142
+ }
143
+
144
+ buf[ ..len] . copy_from_slice ( & bytes_val) ;
145
+ Ok ( i64:: from_le_bytes ( buf) )
146
+ }
147
+ DataType :: Numeric | DataType :: NumericN | DataType :: Decimal | DataType :: DecimalN => {
148
+ decode_numeric ( value. as_bytes ( ) ?, precision, scale)
149
+ }
150
+ _ => Err ( err_protocol ! (
151
+ "Decoding {:?} as a i64 failed because type {:?} is not implemented" ,
152
+ value,
153
+ ty
154
+ )
155
+ . into ( ) ) ,
156
+ }
118
157
}
119
158
}
120
159
@@ -132,69 +171,18 @@ fn decode_numeric(bytes: &[u8], _precision: u8, mut scale: u8) -> Result<i64, Bo
132
171
Ok ( n * if negative { -1 } else { 1 } )
133
172
}
134
173
135
- fn decode_integer < T > ( value : MssqlValueRef < ' _ > ) -> Result < T , BoxDynError >
174
+ fn convert_integer < T > ( i64_val : i64 ) -> Result < T , BoxDynError >
136
175
where
137
176
T : TryFrom < i64 > ,
138
177
T :: Error : std:: error:: Error + Send + Sync + ' static ,
139
178
{
140
- let ty = value. type_info . 0 . ty ;
141
- let precision = value. type_info . 0 . precision ;
142
- let scale = value. type_info . 0 . scale ;
143
-
144
- let type_name = type_name :: < T > ( ) ;
145
-
146
- match ty {
147
- DataType :: SmallInt
148
- | DataType :: Int
149
- | DataType :: TinyInt
150
- | DataType :: BigInt
151
- | DataType :: IntN => {
152
- let mut buf = [ 0u8 ; 8 ] ;
153
- let bytes_val = value. as_bytes ( ) ?;
154
- let len = bytes_val. len ( ) ;
155
-
156
- if len > buf. len ( ) {
157
- return Err ( err_protocol ! (
158
- "Decoding {:?} as a {} failed because type {:?} has more than {} bytes" ,
159
- value,
160
- type_name,
161
- ty,
162
- buf. len( )
163
- )
164
- . into ( ) ) ;
165
- }
166
-
167
- buf[ ..len] . copy_from_slice ( & bytes_val) ;
168
-
169
- let i64_val = i64:: from_le_bytes ( buf) ;
170
- T :: try_from ( i64_val) . map_err ( |_| {
171
- err_protocol ! (
172
- "Decoding {:?} as a {} failed because value {} is out of range" ,
173
- value,
174
- type_name,
175
- i64_val
176
- )
177
- . into ( )
178
- } )
179
- }
180
- DataType :: Numeric | DataType :: NumericN | DataType :: Decimal | DataType :: DecimalN => {
181
- let n = decode_numeric ( value. as_bytes ( ) ?, precision, scale) ?;
182
- T :: try_from ( n) . map_err ( |_| {
183
- err_protocol ! (
184
- "Decoding {:?} as a {} failed because value {} is out of range" ,
185
- value,
186
- type_name,
187
- n
188
- )
189
- . into ( )
190
- } )
191
- }
192
- _ => Err ( err_protocol ! (
193
- "Decoding {:?} as a {} failed because type {:?} is not implemented" ,
194
- value,
195
- type_name,
196
- ty
179
+ T :: try_from ( i64_val) . map_err ( |err| {
180
+ err_protocol ! (
181
+ "Converting {} to {} failed: {}" ,
182
+ i64_val,
183
+ type_name:: <T >( ) ,
184
+ err
197
185
)
198
- . into ( ) ) ,
199
- }
186
+ . into ( )
187
+ } )
200
188
}
0 commit comments