-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcurd.rs
75 lines (65 loc) · 1.88 KB
/
curd.rs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use chrono::{NaiveDateTime, Utc};
use clickhouse_http_client::clickhouse_format::{
input::JsonCompactEachRowInput, output::JsonCompactEachRowWithNamesAndTypesOutput,
};
use serde::Deserialize;
use serde_json::Value;
use super::helpers::*;
#[derive(Deserialize, Debug)]
pub struct Event {
#[serde(rename = "event_id")]
pub id: u32,
#[serde(deserialize_with = "clickhouse_data_value::datetime::deserialize")]
pub created_at: NaiveDateTime,
}
#[tokio::test]
async fn simple() -> Result<(), Box<dyn std::error::Error>> {
init_logger();
let client = get_client()?;
client
.execute(
r#"
CREATE TABLE t_testing_events
(
event_id UInt32,
created_at Datetime('UTC')
) ENGINE=Memory
"#,
None,
)
.await?;
let rows: Vec<Vec<Value>> = vec![
vec![1.into(), Utc::now().timestamp().into()],
vec![2.into(), Utc::now().timestamp().into()],
];
client
.insert_with_format(
"INSERT INTO t_testing_events (event_id, created_at)",
JsonCompactEachRowInput::new(rows),
None,
)
.await?;
let (events, info) = client
.select_with_format(
"SELECT * FROM t_testing_events",
JsonCompactEachRowWithNamesAndTypesOutput::<Event>::new(),
None,
)
.await?;
println!("{events:?}");
println!("{info:?}");
assert_eq!(events.len(), 2);
let event = events.first().unwrap();
assert_eq!(event.id, 1);
let (events, info) = client
.select_with_format(
"SELECT * FROM t_testing_events",
JsonCompactEachRowWithNamesAndTypesOutput::<Event>::new(),
vec![("date_time_output_format", "iso")],
)
.await?;
println!("{events:?}");
println!("{info:?}");
client.execute("DROP TABLE t_testing_events", None).await?;
Ok(())
}