Skip to content

Commit 87df409

Browse files
committed
Support NULL, BOOL, TIMESTAMP
1 parent 9654125 commit 87df409

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

src/column.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::*;
22
use std::rc::Rc;
33
use std::slice::Iter;
44

5-
// https://crossdb.org/client/api-c/#xdb_type_t
5+
// https://github.com/crossdb-org/crossdb/blob/main/include/crossdb.h
66
#[derive(Debug, Clone, Copy)]
77
pub enum DataType {
88
Null,
@@ -21,6 +21,7 @@ pub enum DataType {
2121
Binary,
2222
VChar,
2323
VBinary,
24+
Bool,
2425
Max,
2526
}
2627

@@ -43,6 +44,7 @@ impl Display for DataType {
4344
DataType::Binary => write!(f, "BINARY"),
4445
DataType::VChar => write!(f, "VCHAR"),
4546
DataType::VBinary => write!(f, "VBINARY"),
47+
DataType::Bool => write!(f, "BOOL"),
4648
DataType::Max => write!(f, "MAX"),
4749
}
4850
}
@@ -69,6 +71,7 @@ impl DataType {
6971
xdb_type_t_XDB_TYPE_BINARY => Self::Binary,
7072
xdb_type_t_XDB_TYPE_VCHAR => Self::VChar,
7173
xdb_type_t_XDB_TYPE_VBINARY => Self::VBinary,
74+
xdb_type_t_XDB_TYPE_BOOL => Self::Bool,
7275
xdb_type_t_XDB_TYPE_MAX => Self::Max,
7376
_ => unreachable!(),
7477
}

src/de.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> {
8989
Value::I64(v) => visitor.visit_i64(v),
9090
Value::F32(v) => visitor.visit_f32(v),
9191
Value::F64(v) => visitor.visit_f64(v),
92-
Value::Char(v) => visitor.visit_str(v),
92+
// TODO
93+
Value::Timestamp(v) => visitor.visit_i64(v),
94+
Value::String(v) => visitor.visit_str(v),
9395
// TODO: Deserialize Binary to Vec<u8>
9496
Value::Binary(_) => unimplemented!(),
97+
Value::Bool(v) => visitor.visit_bool(v),
9598
}
9699
}
97100

@@ -144,8 +147,8 @@ mod tests {
144147
de(&Value::I16(1), Some(1i16));
145148
de(&Value::I16(1), Some(1_u128));
146149

147-
de(&Value::Char("Hello"), String::from("Hello"));
148-
de(&Value::Char("Hello"), Some(String::from("Hello")));
150+
de(&Value::String("Hello"), String::from("Hello"));
151+
de(&Value::String("Hello"), Some(String::from("Hello")));
149152
}
150153

151154
#[test]

src/value.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ pub enum Value<'a> {
99
I64(i64),
1010
F32(f32),
1111
F64(f64),
12-
Char(&'a str),
12+
Timestamp(i64),
13+
String(&'a str),
1314
Binary(&'a [u8]),
15+
Bool(bool),
1416
}
1517

1618
impl Display for Value<'_> {
@@ -23,8 +25,10 @@ impl Display for Value<'_> {
2325
Value::I64(v) => write!(f, "{}", v),
2426
Value::F32(v) => write!(f, "{}", v),
2527
Value::F64(v) => write!(f, "{}", v),
26-
Value::Char(v) => write!(f, "{}", v),
28+
Value::Timestamp(v) => write!(f, "{}", v),
29+
Value::String(v) => write!(f, "{}", v),
2730
Value::Binary(v) => write!(f, "{:?}", v),
31+
Value::Bool(v) => write!(f, "{}", v),
2832
}
2933
}
3034
}
@@ -45,14 +49,19 @@ impl<'a> Value<'a> {
4549
DataType::BigInt => Value::I64(xdb_column_int64(meta, row, col)),
4650
DataType::Float => Value::F32(xdb_column_float(meta, row, col)),
4751
DataType::Double => Value::F64(xdb_column_double(meta, row, col)),
52+
DataType::Timestamp => Value::Timestamp(xdb_column_int64(meta, row, col)),
4853
DataType::Char | DataType::VChar => {
49-
let s = CStr::from_ptr(xdb_column_str(meta, row, col));
50-
Value::Char(s.to_str().unwrap())
54+
let ptr = xdb_column_str(meta, row, col);
55+
if ptr.is_null() {
56+
return Value::Null;
57+
}
58+
Value::String(CStr::from_ptr(ptr).to_str().unwrap())
5159
}
5260
DataType::Binary | DataType::VBinary => {
5361
// xdb_column_blob(meta, row, col, pLen);
5462
todo!()
5563
}
64+
DataType::Bool => Value::Bool(xdb_column_int(meta, row, col) == 1),
5665
_ => unimplemented!(),
5766
}
5867
}

0 commit comments

Comments
 (0)