11use crate :: * ;
22use cidr:: { IpInet , Ipv4Inet , Ipv6Inet } ;
33use mac_address:: MacAddress ;
4- use std:: ffi:: c_char;
54use std:: net:: { Ipv4Addr , Ipv6Addr } ;
6- use std:: ptr:: read;
75
86#[ derive( Debug , Clone , PartialEq ) ]
97pub enum Value < ' a > {
108 Null ,
11- I8 ( i8 ) ,
12- I16 ( i16 ) ,
139 I32 ( i32 ) ,
1410 I64 ( i64 ) ,
1511 F32 ( f32 ) ,
@@ -27,8 +23,6 @@ impl Display for Value<'_> {
2723 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
2824 match self {
2925 Value :: Null => write ! ( f, "NULL" ) ,
30- Value :: I8 ( v) => write ! ( f, "{}" , v) ,
31- Value :: I16 ( v) => write ! ( f, "{}" , v) ,
3226 Value :: I32 ( v) => write ! ( f, "{}" , v) ,
3327 Value :: I64 ( v) => write ! ( f, "{}" , v) ,
3428 Value :: F32 ( v) => write ! ( f, "{}" , v) ,
@@ -44,57 +38,62 @@ impl Display for Value<'_> {
4438}
4539
4640impl Value < ' _ > {
47- pub ( crate ) unsafe fn from_ptr ( ptr : * const c_void , t : DataType ) -> Self {
48- if ptr. is_null ( ) {
41+ pub ( crate ) unsafe fn from_ptr (
42+ res : * mut xdb_res_t ,
43+ row : * mut xdb_row_t ,
44+ i : u16 ,
45+ t : DataType ,
46+ ) -> Self {
47+ if xdb_column_null ( res, row, i) {
4948 return Self :: Null ;
5049 }
5150 match t {
5251 DataType :: Null => Self :: Null ,
53- DataType :: TinyInt => Self :: I8 ( read ( ptr as * const i8 ) ) ,
52+ DataType :: TinyInt => Self :: I32 ( xdb_column_int ( res, row, i) ) ,
53+ DataType :: SmallInt => Self :: I32 ( xdb_column_int ( res, row, i) ) ,
54+ DataType :: Int => Self :: I32 ( xdb_column_int ( res, row, i) ) ,
55+ DataType :: BigInt => Self :: I64 ( xdb_column_int64 ( res, row, i) ) ,
5456 DataType :: UTinyInt => todo ! ( ) ,
55- DataType :: SmallInt => Self :: I16 ( read ( ptr as * const i16 ) ) ,
5657 DataType :: USmallInt => todo ! ( ) ,
57- DataType :: Int => Self :: I32 ( read ( ptr as * const i32 ) ) ,
5858 DataType :: UInt => todo ! ( ) ,
59- DataType :: BigInt => Self :: I64 ( read ( ptr as * const i64 ) ) ,
6059 DataType :: UBigInt => todo ! ( ) ,
61- DataType :: Float => Self :: F32 ( read ( ptr as * const f32 ) ) ,
62- DataType :: Double => Self :: F64 ( read ( ptr as * const f64 ) ) ,
63- DataType :: Timestamp => Self :: Timestamp ( read ( ptr as * const i64 ) ) ,
60+ DataType :: Float => Self :: F32 ( xdb_column_float ( res , row , i ) ) ,
61+ DataType :: Double => Self :: F64 ( xdb_column_double ( res , row , i ) ) ,
62+ DataType :: Timestamp => Self :: Timestamp ( xdb_column_int64 ( res , row , i ) ) ,
6463 DataType :: Char | DataType :: VChar => {
65- let str = CStr :: from_ptr ( ptr as * const c_char ) . to_str ( ) . unwrap ( ) ;
64+ let ptr = xdb_column_str ( res, row, i) ;
65+ let str = CStr :: from_ptr ( ptr) . to_str ( ) . unwrap ( ) ;
6666 Self :: String ( str)
6767 }
6868 DataType :: Binary | DataType :: VBinary => {
69- let len = read ( ( ptr as * const u8 ) . offset ( -2 ) as * const u16 ) ;
69+ let mut len = 0_i32 ;
70+ let ptr = xdb_column_blob ( res, row, i, & mut len) ;
71+ if len <= 0 {
72+ return Self :: Null ;
73+ }
7074 let data = from_raw_parts ( ptr as * const u8 , len as usize ) ;
7175 Self :: Binary ( data)
7276 }
73- DataType :: Bool => Self :: Bool ( * ( ptr as * const i8 ) == 1 ) ,
77+ DataType :: Bool => Self :: Bool ( xdb_column_bool ( res , row , i ) ) ,
7478 DataType :: Inet => {
75- let bytes = from_raw_parts ( ptr as * const u8 , 18 ) ;
76- let mask = bytes[ 0 ] ;
77- let family = bytes[ 1 ] ;
78- match family {
79+ let inet = * xdb_column_inet ( res, row, i) ;
80+ match inet. family {
7981 4 => {
8082 let mut buf = [ 0 ; 4 ] ;
81- buf. copy_from_slice ( & bytes [ 2 .. 6 ] ) ;
82- let net = Ipv4Inet :: new ( Ipv4Addr :: from ( buf) , mask) . unwrap ( ) ;
83+ buf. copy_from_slice ( & inet . addr [ 0 .. 4 ] ) ;
84+ let net = Ipv4Inet :: new ( Ipv4Addr :: from ( buf) , inet . mask ) . unwrap ( ) ;
8385 Self :: Inet ( IpInet :: V4 ( net) )
8486 }
8587 6 => {
86- let mut buf = [ 0 ; 16 ] ;
87- buf. copy_from_slice ( & bytes[ 2 ..18 ] ) ;
88- let net = Ipv6Inet :: new ( Ipv6Addr :: from ( buf) , mask) . unwrap ( ) ;
88+ let net = Ipv6Inet :: new ( Ipv6Addr :: from ( inet. addr ) , inet. mask ) . unwrap ( ) ;
8989 Self :: Inet ( IpInet :: V6 ( net) )
9090 }
9191 _ => unreachable ! ( ) ,
9292 }
9393 }
9494 DataType :: Mac => {
95- let bytes = from_raw_parts ( ptr as * const u8 , 6 ) ;
96- let address =
97- MacAddress :: new ( [ bytes[ 0 ] , bytes[ 1 ] , bytes[ 2 ] , bytes[ 3 ] , bytes[ 4 ] , bytes[ 5 ] ] ) ;
95+ let mac = * xdb_column_mac ( res, row, i) ;
96+ let address = MacAddress :: new ( mac. addr ) ;
9897 Self :: Mac ( address)
9998 }
10099 DataType :: Array => todo ! ( ) ,
0 commit comments