@@ -53,13 +53,16 @@ fn decode_raw<'a, T: Decode<'a, sqlx::any::Any> + Default>(
5353}
5454
5555pub fn sql_nonnull_to_json < ' r > ( mut get_ref : impl FnMut ( ) -> sqlx:: any:: AnyValueRef < ' r > ) -> Value {
56+ use AnyTypeInfoKind :: { Mssql , MySql } ;
5657 let raw_value = get_ref ( ) ;
5758 let type_info = raw_value. type_info ( ) ;
5859 let type_name = type_info. name ( ) ;
5960 log:: trace!( "Decoding a value of type {type_name:?} (type info: {type_info:?})" ) ;
61+ let AnyTypeInfo ( ref db_type) = * type_info;
6062 match type_name {
61- "REAL" | "FLOAT" | "FLOAT4" | "FLOAT8" | "DOUBLE" | "NUMERIC" | "DECIMAL" | "MONEY"
62- | "SMALLMONEY" => decode_raw :: < f64 > ( raw_value) . into ( ) ,
63+ "REAL" | "FLOAT" | "FLOAT4" | "FLOAT8" | "DOUBLE" | "NUMERIC" | "DECIMAL" => {
64+ decode_raw :: < f64 > ( raw_value) . into ( )
65+ }
6366 "INT8" | "BIGINT" | "SERIAL8" | "BIGSERIAL" | "IDENTITY" | "INT64" | "INTEGER8"
6467 | "BIGINT SIGNED" => decode_raw :: < i64 > ( raw_value) . into ( ) ,
6568 "INT" | "INT4" | "INTEGER" | "MEDIUMINT" | "YEAR" => decode_raw :: < i32 > ( raw_value) . into ( ) ,
@@ -69,15 +72,11 @@ pub fn sql_nonnull_to_json<'r>(mut get_ref: impl FnMut() -> sqlx::any::AnyValueR
6972 decode_raw :: < u32 > ( raw_value) . into ( )
7073 }
7174 "BOOL" | "BOOLEAN" => decode_raw :: < bool > ( raw_value) . into ( ) ,
72- "BIT" if matches ! ( * type_info, AnyTypeInfo ( AnyTypeInfoKind :: Mssql ( _) ) ) => {
73- decode_raw :: < bool > ( raw_value) . into ( )
74- }
75- "BIT" if matches ! ( * type_info, AnyTypeInfo ( AnyTypeInfoKind :: MySql ( ref mysql_type) ) if mysql_type. max_size( ) == Some ( 1 ) ) => {
75+ "BIT" if matches ! ( db_type, Mssql ( _) ) => decode_raw :: < bool > ( raw_value) . into ( ) ,
76+ "BIT" if matches ! ( db_type, MySql ( mysql_type) if mysql_type. max_size( ) == Some ( 1 ) ) => {
7677 decode_raw :: < bool > ( raw_value) . into ( )
7778 }
78- "BIT" if matches ! ( * type_info, AnyTypeInfo ( AnyTypeInfoKind :: MySql ( _) ) ) => {
79- decode_raw :: < u64 > ( raw_value) . into ( )
80- }
79+ "BIT" if matches ! ( db_type, MySql ( _) ) => decode_raw :: < u64 > ( raw_value) . into ( ) ,
8180 "DATE" => decode_raw :: < chrono:: NaiveDate > ( raw_value)
8281 . to_string ( )
8382 . into ( ) ,
@@ -93,6 +92,9 @@ pub fn sql_nonnull_to_json<'r>(mut get_ref: impl FnMut() -> sqlx::any::AnyValueR
9392 . format ( "%FT%T%.f" )
9493 . to_string ( )
9594 . into ( ) ,
95+ "MONEY" | "SMALLMONEY" if matches ! ( db_type, Mssql ( _) ) => {
96+ decode_raw :: < f64 > ( raw_value) . into ( )
97+ }
9698 "JSON" | "JSON[]" | "JSONB" | "JSONB[]" => decode_raw :: < Value > ( raw_value) ,
9799 // Deserialize as a string by default
98100 _ => decode_raw :: < String > ( raw_value) . into ( ) ,
@@ -212,7 +214,7 @@ mod tests {
212214 "jsonb" : { "key" : "value" } ,
213215 "age_interval" : "2 mons 13 days" ,
214216 "justified_interval" : "1 year 2 mons 3 days" ,
215- "money_val" : 0.0 // TODO: fix this. This should be 1234.56
217+ "money_val" : "$1,234.56"
216218 } ) ,
217219 ) ;
218220 Ok ( ( ) )
0 commit comments