@@ -6,73 +6,12 @@ use futures_intrusive::sync::Mutex;
66
77use crate :: error:: Error ;
88use crate :: odbc:: {
9- OdbcArgumentValue , OdbcColumn , OdbcConnectOptions , OdbcQueryResult , OdbcRow , OdbcTypeInfo , OdbcDataType ,
9+ OdbcArgumentValue , OdbcColumn , OdbcConnectOptions , OdbcQueryResult , OdbcRow , OdbcTypeInfo ,
1010} ;
1111#[ allow( unused_imports) ]
1212use crate :: row:: Row as SqlxRow ;
1313use either:: Either ;
14- use odbc_api:: { Cursor , CursorRow , IntoParameter , ResultSetMetadata , DataType } ;
15-
16- /// Map ODBC API DataType to our OdbcDataType
17- fn map_odbc_data_type ( data_type : DataType ) -> OdbcTypeInfo {
18- let odbc_data_type = match data_type {
19- DataType :: BigInt => OdbcDataType :: BigInt ,
20- DataType :: Binary { .. } => OdbcDataType :: Binary ,
21- DataType :: Bit => OdbcDataType :: Bit ,
22- DataType :: Char { .. } => OdbcDataType :: Char ,
23- DataType :: Date => OdbcDataType :: Date ,
24- DataType :: Decimal { .. } => OdbcDataType :: Decimal ,
25- DataType :: Double => OdbcDataType :: Double ,
26- DataType :: Float { .. } => OdbcDataType :: Float ,
27- DataType :: Integer => OdbcDataType :: Integer ,
28- DataType :: LongVarbinary { .. } => OdbcDataType :: LongVarbinary ,
29- DataType :: LongVarchar { .. } => OdbcDataType :: LongVarchar ,
30- DataType :: Numeric { .. } => OdbcDataType :: Numeric ,
31- DataType :: Real => OdbcDataType :: Real ,
32- DataType :: SmallInt => OdbcDataType :: SmallInt ,
33- DataType :: Time { .. } => OdbcDataType :: Time ,
34- DataType :: Timestamp { .. } => OdbcDataType :: Timestamp ,
35- DataType :: TinyInt => OdbcDataType :: TinyInt ,
36- DataType :: Varbinary { .. } => OdbcDataType :: Varbinary ,
37- DataType :: Varchar { .. } => OdbcDataType :: Varchar ,
38- DataType :: WChar { .. } => OdbcDataType :: WChar ,
39- DataType :: WLongVarchar { .. } => OdbcDataType :: WLongVarchar ,
40- DataType :: WVarchar { .. } => OdbcDataType :: WVarchar ,
41- DataType :: Other { .. } => OdbcDataType :: Unknown ,
42- DataType :: Unknown => OdbcDataType :: Unknown ,
43- } ;
44-
45- // Extract precision, scale, and length information where available
46- match data_type {
47- DataType :: Decimal { precision, scale } => {
48- OdbcTypeInfo :: with_precision_and_scale ( odbc_data_type, precision as u32 , scale as u16 )
49- } ,
50- DataType :: Numeric { precision, scale } => {
51- OdbcTypeInfo :: with_precision_and_scale ( odbc_data_type, precision as u32 , scale as u16 )
52- } ,
53- DataType :: Char { length } | DataType :: Varchar { length } | DataType :: WChar { length } | DataType :: WVarchar { length } => {
54- if let Some ( len) = length {
55- OdbcTypeInfo :: with_length ( odbc_data_type, len. get ( ) as u32 )
56- } else {
57- OdbcTypeInfo :: new ( odbc_data_type)
58- }
59- } ,
60- DataType :: Binary { length } | DataType :: Varbinary { length } => {
61- if let Some ( len) = length {
62- OdbcTypeInfo :: with_length ( odbc_data_type, len. get ( ) as u32 )
63- } else {
64- OdbcTypeInfo :: new ( odbc_data_type)
65- }
66- } ,
67- DataType :: Float { precision } => {
68- OdbcTypeInfo :: with_precision ( odbc_data_type, precision as u32 )
69- } ,
70- DataType :: Time { precision } | DataType :: Timestamp { precision } => {
71- OdbcTypeInfo :: with_precision ( odbc_data_type, precision as u32 )
72- } ,
73- _ => OdbcTypeInfo :: new ( odbc_data_type) ,
74- }
75- }
14+ use odbc_api:: { Cursor , CursorRow , IntoParameter , ResultSetMetadata } ;
7615
7716#[ derive( Debug ) ]
7817pub ( crate ) struct ConnectionWorker {
@@ -384,7 +323,7 @@ where
384323 let name = String :: from_utf8 ( cd. name ) . unwrap_or_else ( |_| format ! ( "col{}" , i - 1 ) ) ;
385324 columns. push ( OdbcColumn {
386325 name,
387- type_info : map_odbc_data_type ( cd. data_type ) ,
326+ type_info : OdbcTypeInfo :: new ( cd. data_type ) ,
388327 ordinal : ( i - 1 ) as usize ,
389328 } ) ;
390329 }
@@ -403,7 +342,7 @@ where
403342 loop {
404343 match cursor. next_row ( ) {
405344 Ok ( Some ( mut row) ) => {
406- let values = collect_row_values ( & mut row, columns. len ( ) ) ?;
345+ let values = collect_row_values ( & mut row, columns) ?;
407346 let _ = tx. send ( Ok ( Either :: Right ( OdbcRow {
408347 columns : columns. to_vec ( ) ,
409348 values,
@@ -418,31 +357,20 @@ where
418357
419358fn collect_row_values (
420359 row : & mut CursorRow < ' _ > ,
421- num_cols : usize ,
360+ columns : & [ OdbcColumn ] ,
422361) -> Result < Vec < ( OdbcTypeInfo , Option < Vec < u8 > > ) > , Error > {
423- let mut values: Vec < ( OdbcTypeInfo , Option < Vec < u8 > > ) > = Vec :: with_capacity ( num_cols) ;
424- for i in 1 ..=num_cols {
362+ let mut values: Vec < ( OdbcTypeInfo , Option < Vec < u8 > > ) > = Vec :: with_capacity ( columns. len ( ) ) ;
363+ for ( i, column) in columns. iter ( ) . enumerate ( ) {
364+ let col_idx = ( i + 1 ) as u16 ;
425365 let mut buf = Vec :: new ( ) ;
426- match row. get_text ( i as u16 , & mut buf) {
427- Ok ( true ) => values. push ( (
428- OdbcTypeInfo :: VARCHAR ,
429- Some ( buf) ,
430- ) ) ,
431- Ok ( false ) => values. push ( (
432- OdbcTypeInfo :: VARCHAR ,
433- None ,
434- ) ) ,
366+ match row. get_text ( col_idx, & mut buf) {
367+ Ok ( true ) => values. push ( ( column. type_info . clone ( ) , Some ( buf) ) ) ,
368+ Ok ( false ) => values. push ( ( column. type_info . clone ( ) , None ) ) ,
435369 Err ( _) => {
436370 let mut bin = Vec :: new ( ) ;
437- match row. get_binary ( i as u16 , & mut bin) {
438- Ok ( true ) => values. push ( (
439- OdbcTypeInfo :: VARBINARY ,
440- Some ( bin) ,
441- ) ) ,
442- Ok ( false ) => values. push ( (
443- OdbcTypeInfo :: VARBINARY ,
444- None ,
445- ) ) ,
371+ match row. get_binary ( col_idx, & mut bin) {
372+ Ok ( true ) => values. push ( ( column. type_info . clone ( ) , Some ( bin) ) ) ,
373+ Ok ( false ) => values. push ( ( column. type_info . clone ( ) , None ) ) ,
446374 Err ( e) => return Err ( Error :: from ( e) ) ,
447375 }
448376 }
0 commit comments