forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cql-time-types.rs
121 lines (96 loc) · 3.84 KB
/
cql-time-types.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// An example showing how to use time related types in queries
// Date, Time, Timestamp
use anyhow::Result;
use chrono::{Duration, NaiveDate};
use scylla::frame::response::result::CqlValue;
use scylla::frame::value::{Date, Time, Timestamp};
use scylla::transport::session::{IntoTypedRows, Session};
use scylla::SessionBuilder;
use std::env;
#[tokio::main]
async fn main() -> Result<()> {
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
println!("Connecting to {} ...", uri);
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
// Date
// Date is a year, month and day in the range -5877641-06-23 to -5877641-06-23
session
.query(
"CREATE TABLE IF NOT EXISTS ks.dates (d date primary key)",
&[],
)
.await?;
// Dates in the range -262145-1-1 to 262143-12-31 can be represented using chrono::NaiveDate
let example_date: NaiveDate = NaiveDate::from_ymd_opt(2020, 2, 20).unwrap();
session
.query("INSERT INTO ks.dates (d) VALUES (?)", (example_date,))
.await?;
if let Some(rows) = session.query("SELECT d from ks.dates", &[]).await?.rows {
for row in rows.into_typed::<(NaiveDate,)>() {
let (read_date,): (NaiveDate,) = match row {
Ok(read_date) => read_date,
Err(_) => continue, // We might read a date that does not fit in NaiveDate, skip it
};
println!("Read a date: {:?}", read_date);
}
}
// Dates outside this range must be represented in the raw form - an u32 describing days since -5877641-06-23
let example_big_date: Date = Date(u32::MAX);
session
.query("INSERT INTO ks.dates (d) VALUES (?)", (example_big_date,))
.await?;
if let Some(rows) = session.query("SELECT d from ks.dates", &[]).await?.rows {
for row in rows {
let read_days: u32 = match row.columns[0] {
Some(CqlValue::Date(days)) => days,
_ => panic!("oh no"),
};
println!("Read a date as raw days: {}", read_days);
}
}
// Time - nanoseconds since midnight in range 0..=86399999999999
let example_time: Duration = Duration::hours(1);
session
.query(
"CREATE TABLE IF NOT EXISTS ks.times (t time primary key)",
&[],
)
.await?;
// Time as bound value must be wrapped in value::Time to differentiate from Timestamp
session
.query("INSERT INTO ks.times (t) VALUES (?)", (Time(example_time),))
.await?;
if let Some(rows) = session.query("SELECT t from ks.times", &[]).await?.rows {
for row in rows.into_typed::<(Duration,)>() {
let (read_time,): (Duration,) = row?;
println!("Read a time: {:?}", read_time);
}
}
// Timestamp - milliseconds since unix epoch - 1970-01-01
let example_timestamp: Duration = Duration::hours(1); // This will describe 1970-01-01 01:00:00
session
.query(
"CREATE TABLE IF NOT EXISTS ks.timestamps (t timestamp primary key)",
&[],
)
.await?;
// Timestamp as bound value must be wrapped in value::Timestamp to differentiate from Time
session
.query(
"INSERT INTO ks.timestamps (t) VALUES (?)",
(Timestamp(example_timestamp),),
)
.await?;
if let Some(rows) = session
.query("SELECT t from ks.timestamps", &[])
.await?
.rows
{
for row in rows.into_typed::<(Duration,)>() {
let (read_time,): (Duration,) = row?;
println!("Read a timestamp: {:?}", read_time);
}
}
Ok(())
}