1
1
use crate :: * ;
2
+ use std:: rc:: Rc ;
3
+ use std:: slice:: Iter ;
2
4
3
5
// https://crossdb.org/client/api-c/#xdb_type_t
4
6
#[ derive( Debug , Clone , Copy ) ]
5
- pub enum ColumnType {
7
+ pub enum DataType {
6
8
Null ,
7
9
TinyInt ,
8
10
SmallInt ,
@@ -22,65 +24,120 @@ pub enum ColumnType {
22
24
Max ,
23
25
}
24
26
25
- impl From < u32 > for ColumnType {
26
- #[ allow( non_upper_case_globals) ]
27
- fn from ( value : u32 ) -> Self {
28
- match value {
29
- xdb_type_t_XDB_TYPE_NULL => ColumnType :: Null ,
30
- xdb_type_t_XDB_TYPE_TINYINT => ColumnType :: TinyInt ,
31
- xdb_type_t_XDB_TYPE_SMALLINT => ColumnType :: SmallInt ,
32
- xdb_type_t_XDB_TYPE_INT => ColumnType :: Int ,
33
- xdb_type_t_XDB_TYPE_BIGINT => ColumnType :: BigInt ,
34
- xdb_type_t_XDB_TYPE_UTINYINT => ColumnType :: UTinyInt ,
35
- xdb_type_t_XDB_TYPE_USMALLINT => ColumnType :: USmallInt ,
36
- xdb_type_t_XDB_TYPE_UINT => ColumnType :: UInt ,
37
- xdb_type_t_XDB_TYPE_UBIGINT => ColumnType :: UBigInt ,
38
- xdb_type_t_XDB_TYPE_FLOAT => ColumnType :: Float ,
39
- xdb_type_t_XDB_TYPE_DOUBLE => ColumnType :: Double ,
40
- xdb_type_t_XDB_TYPE_TIMESTAMP => ColumnType :: Timestamp ,
41
- xdb_type_t_XDB_TYPE_CHAR => ColumnType :: Char ,
42
- xdb_type_t_XDB_TYPE_BINARY => ColumnType :: Binary ,
43
- xdb_type_t_XDB_TYPE_VCHAR => ColumnType :: VChar ,
44
- xdb_type_t_XDB_TYPE_VBINARY => ColumnType :: VBinary ,
45
- xdb_type_t_XDB_TYPE_MAX => ColumnType :: Max ,
46
- _ => unreachable ! ( ) ,
27
+ impl Display for DataType {
28
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
29
+ match self {
30
+ DataType :: Null => write ! ( f, "NULL" ) ,
31
+ DataType :: TinyInt => write ! ( f, "TINYINT" ) ,
32
+ DataType :: SmallInt => write ! ( f, "SMALLINT" ) ,
33
+ DataType :: Int => write ! ( f, "INT" ) ,
34
+ DataType :: BigInt => write ! ( f, "BIGINT" ) ,
35
+ DataType :: UTinyInt => write ! ( f, "UTINYINT" ) ,
36
+ DataType :: USmallInt => write ! ( f, "USMALLINT" ) ,
37
+ DataType :: UInt => write ! ( f, "UINT" ) ,
38
+ DataType :: UBigInt => write ! ( f, "UBIGINT" ) ,
39
+ DataType :: Float => write ! ( f, "FLOAT" ) ,
40
+ DataType :: Double => write ! ( f, "DOUBLE" ) ,
41
+ DataType :: Timestamp => write ! ( f, "TIMESTAMP" ) ,
42
+ DataType :: Char => write ! ( f, "CHAR" ) ,
43
+ DataType :: Binary => write ! ( f, "BINARY" ) ,
44
+ DataType :: VChar => write ! ( f, "VCHAR" ) ,
45
+ DataType :: VBinary => write ! ( f, "VBINARY" ) ,
46
+ DataType :: Max => write ! ( f, "MAX" ) ,
47
47
}
48
48
}
49
49
}
50
50
51
- impl Display for ColumnType {
52
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
53
- match self {
54
- ColumnType :: Null => write ! ( f, "NULL" ) ,
55
- ColumnType :: TinyInt => write ! ( f, "TINYINT" ) ,
56
- ColumnType :: SmallInt => write ! ( f, "SMALLINT" ) ,
57
- ColumnType :: Int => write ! ( f, "INT" ) ,
58
- ColumnType :: BigInt => write ! ( f, "BIGINT" ) ,
59
- ColumnType :: UTinyInt => write ! ( f, "UTINYINT" ) ,
60
- ColumnType :: USmallInt => write ! ( f, "USMALLINT" ) ,
61
- ColumnType :: UInt => write ! ( f, "UINT" ) ,
62
- ColumnType :: UBigInt => write ! ( f, "UBIGINT" ) ,
63
- ColumnType :: Float => write ! ( f, "FLOAT" ) ,
64
- ColumnType :: Double => write ! ( f, "DOUBLE" ) ,
65
- ColumnType :: Timestamp => write ! ( f, "TIMESTAMP" ) ,
66
- ColumnType :: Char => write ! ( f, "CHAR" ) ,
67
- ColumnType :: Binary => write ! ( f, "BINARY" ) ,
68
- ColumnType :: VChar => write ! ( f, "VCHAR" ) ,
69
- ColumnType :: VBinary => write ! ( f, "VBINARY" ) ,
70
- ColumnType :: Max => write ! ( f, "MAX" ) ,
51
+ impl DataType {
52
+ #[ allow( non_upper_case_globals) ]
53
+ unsafe fn from_meta ( meta : u64 , col : u16 ) -> Self {
54
+ let t = xdb_column_type ( meta, col) ;
55
+ match t {
56
+ xdb_type_t_XDB_TYPE_NULL => Self :: Null ,
57
+ xdb_type_t_XDB_TYPE_TINYINT => Self :: TinyInt ,
58
+ xdb_type_t_XDB_TYPE_SMALLINT => Self :: SmallInt ,
59
+ xdb_type_t_XDB_TYPE_INT => Self :: Int ,
60
+ xdb_type_t_XDB_TYPE_BIGINT => Self :: BigInt ,
61
+ xdb_type_t_XDB_TYPE_UTINYINT => Self :: UTinyInt ,
62
+ xdb_type_t_XDB_TYPE_USMALLINT => Self :: USmallInt ,
63
+ xdb_type_t_XDB_TYPE_UINT => Self :: UInt ,
64
+ xdb_type_t_XDB_TYPE_UBIGINT => Self :: UBigInt ,
65
+ xdb_type_t_XDB_TYPE_FLOAT => Self :: Float ,
66
+ xdb_type_t_XDB_TYPE_DOUBLE => Self :: Double ,
67
+ xdb_type_t_XDB_TYPE_TIMESTAMP => Self :: Timestamp ,
68
+ xdb_type_t_XDB_TYPE_CHAR => Self :: Char ,
69
+ xdb_type_t_XDB_TYPE_BINARY => Self :: Binary ,
70
+ xdb_type_t_XDB_TYPE_VCHAR => Self :: VChar ,
71
+ xdb_type_t_XDB_TYPE_VBINARY => Self :: VBinary ,
72
+ xdb_type_t_XDB_TYPE_MAX => Self :: Max ,
73
+ _ => unreachable ! ( ) ,
71
74
}
72
75
}
73
76
}
74
77
75
- impl ColumnType {
76
- pub ( crate ) fn all ( res : & xdb_res_t ) -> Vec < Self > {
77
- let mut vec = Vec :: with_capacity ( res. col_count as usize ) ;
78
- for i in 0 ..vec. capacity ( ) {
78
+ #[ derive( Debug , Clone ) ]
79
+ pub struct Columns {
80
+ inner : Rc < Vec < ( String , DataType ) > > ,
81
+ }
82
+
83
+ impl Columns {
84
+ pub ( crate ) unsafe fn from_res ( ptr : * mut xdb_res_t ) -> Self {
85
+ let res = * ptr;
86
+ let mut columns = Vec :: with_capacity ( res. col_count as usize ) ;
87
+ for i in 0 ..res. col_count {
79
88
unsafe {
80
- let t = xdb_column_type ( res. col_meta , i as u16 ) ;
81
- vec. push ( Self :: from ( t) ) ;
89
+ let name = CStr :: from_ptr ( xdb_column_name ( res. col_meta , i) )
90
+ . to_str ( )
91
+ . unwrap ( )
92
+ . to_string ( ) ;
93
+ let datatype = DataType :: from_meta ( res. col_meta , i) ;
94
+ columns. push ( ( name, datatype) ) ;
82
95
}
83
96
}
84
- vec
97
+ Self {
98
+ inner : Rc :: new ( columns) ,
99
+ }
100
+ }
101
+
102
+ pub fn len ( & self ) -> usize {
103
+ self . inner . len ( )
104
+ }
105
+
106
+ pub fn name ( & self , i : usize ) -> & str {
107
+ self . inner [ i] . 0 . as_str ( )
108
+ }
109
+
110
+ pub fn datatype ( & self , i : usize ) -> DataType {
111
+ self . inner [ i] . 1
112
+ }
113
+
114
+ pub fn iter ( & self ) -> ColumnsIter {
115
+ ColumnsIter {
116
+ inner : self . inner . iter ( ) ,
117
+ }
118
+ }
119
+
120
+ pub fn into_inner ( self ) -> Option < Vec < ( String , DataType ) > > {
121
+ Rc :: into_inner ( self . inner )
122
+ }
123
+ }
124
+
125
+ pub struct ColumnsIter < ' a > {
126
+ inner : Iter < ' a , ( String , DataType ) > ,
127
+ }
128
+
129
+ impl < ' a > Iterator for ColumnsIter < ' a > {
130
+ type Item = & ' a ( String , DataType ) ;
131
+ fn next ( & mut self ) -> Option < Self :: Item > {
132
+ self . inner . next ( )
133
+ }
134
+ }
135
+
136
+ impl < ' a > IntoIterator for & ' a Columns {
137
+ type Item = & ' a ( String , DataType ) ;
138
+ type IntoIter = ColumnsIter < ' a > ;
139
+
140
+ fn into_iter ( self ) -> Self :: IntoIter {
141
+ self . iter ( )
85
142
}
86
143
}
0 commit comments