1
1
use crate :: * ;
2
2
use cidr:: { IpInet , Ipv4Inet , Ipv6Inet } ;
3
3
use mac_address:: MacAddress ;
4
- use std:: ffi:: c_char;
5
4
use std:: net:: { Ipv4Addr , Ipv6Addr } ;
6
- use std:: ptr:: read;
7
5
8
6
#[ derive( Debug , Clone , PartialEq ) ]
9
7
pub enum Value < ' a > {
10
8
Null ,
11
- I8 ( i8 ) ,
12
- I16 ( i16 ) ,
13
9
I32 ( i32 ) ,
14
10
I64 ( i64 ) ,
15
11
F32 ( f32 ) ,
@@ -27,8 +23,6 @@ impl Display for Value<'_> {
27
23
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
28
24
match self {
29
25
Value :: Null => write ! ( f, "NULL" ) ,
30
- Value :: I8 ( v) => write ! ( f, "{}" , v) ,
31
- Value :: I16 ( v) => write ! ( f, "{}" , v) ,
32
26
Value :: I32 ( v) => write ! ( f, "{}" , v) ,
33
27
Value :: I64 ( v) => write ! ( f, "{}" , v) ,
34
28
Value :: F32 ( v) => write ! ( f, "{}" , v) ,
@@ -44,57 +38,62 @@ impl Display for Value<'_> {
44
38
}
45
39
46
40
impl 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) {
49
48
return Self :: Null ;
50
49
}
51
50
match t {
52
51
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) ) ,
54
56
DataType :: UTinyInt => todo ! ( ) ,
55
- DataType :: SmallInt => Self :: I16 ( read ( ptr as * const i16 ) ) ,
56
57
DataType :: USmallInt => todo ! ( ) ,
57
- DataType :: Int => Self :: I32 ( read ( ptr as * const i32 ) ) ,
58
58
DataType :: UInt => todo ! ( ) ,
59
- DataType :: BigInt => Self :: I64 ( read ( ptr as * const i64 ) ) ,
60
59
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 ) ) ,
64
63
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 ( ) ;
66
66
Self :: String ( str)
67
67
}
68
68
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
+ }
70
74
let data = from_raw_parts ( ptr as * const u8 , len as usize ) ;
71
75
Self :: Binary ( data)
72
76
}
73
- DataType :: Bool => Self :: Bool ( * ( ptr as * const i8 ) == 1 ) ,
77
+ DataType :: Bool => Self :: Bool ( xdb_column_bool ( res , row , i ) ) ,
74
78
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 {
79
81
4 => {
80
82
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 ( ) ;
83
85
Self :: Inet ( IpInet :: V4 ( net) )
84
86
}
85
87
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 ( ) ;
89
89
Self :: Inet ( IpInet :: V6 ( net) )
90
90
}
91
91
_ => unreachable ! ( ) ,
92
92
}
93
93
}
94
94
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 ) ;
98
97
Self :: Mac ( address)
99
98
}
100
99
DataType :: Array => todo ! ( ) ,
0 commit comments