1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::{types::Type, KlickhouseError, Result, Value};
mod std_deserialize;
mod std_serialize;
pub trait ToSql {
fn to_sql(self) -> Result<Value>;
}
impl ToSql for Value {
fn to_sql(self) -> Result<Value> {
Ok(self)
}
}
pub fn unexpected_type(type_: &Type) -> KlickhouseError {
KlickhouseError::DeserializeError(format!("unexpected type: {}", type_))
}
pub trait FromSql: Sized {
fn from_sql(type_: &Type, value: Value) -> Result<Self>;
}
impl FromSql for Value {
fn from_sql(_type_: &Type, value: Value) -> Result<Self> {
Ok(value)
}
}
pub trait Row: Sized {
fn deserialize_row(map: Vec<(&str, &Type, Value)>) -> Result<Self>;
fn serialize_row(self) -> Result<Vec<(&'static str, Value)>>;
}
pub struct UnitValue<T: FromSql + ToSql>(pub T);
impl<T: FromSql + ToSql> Row for UnitValue<T> {
fn deserialize_row(map: Vec<(&str, &Type, Value)>) -> Result<Self> {
if map.is_empty() {
return Err(KlickhouseError::MissingField("<unit>"));
}
let item = map.into_iter().next().unwrap();
T::from_sql(item.1, item.2).map(UnitValue)
}
fn serialize_row(self) -> Result<Vec<(&'static str, Value)>> {
Ok(vec![("_", self.0.to_sql()?)])
}
}