forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cql-time-types.rs
233 lines (190 loc) · 7.38 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// An example showing how to use time related types in queries
// Date, Time, Timestamp
use anyhow::Result;
use chrono::{DateTime, NaiveDate, NaiveTime, Utc};
use scylla::frame::response::result::CqlValue;
use scylla::frame::value::{CqlDate, CqlTime, CqlTimestamp};
use scylla::transport::session::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 examples_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 examples_ks.dates (d date primary key)",
&[],
)
.await?;
// If 'chrono' feature is enabled, dates in the range -262145-1-1 to 262143-12-31 can be represented using
// chrono::NaiveDate
let chrono_date = NaiveDate::from_ymd_opt(2020, 2, 20).unwrap();
session
.query(
"INSERT INTO examples_ks.dates (d) VALUES (?)",
(chrono_date,),
)
.await?;
let result = session
.query("SELECT d from examples_ks.dates", &[])
.await?;
for row in result.rows_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!("Parsed a date into chrono::NaiveDate: {:?}", read_date);
}
// Alternatively, you can enable 'time' feature and use `time::Date` to represent date. `time::Date` only allows
// dates in range -9999-1-1 to 9999-12-31. Or, if you have 'time/large-dates' feature enabled, this range changes
// to -999999-1-1 to 999999-12-31
let time_date = time::Date::from_calendar_date(2020, time::Month::March, 21).unwrap();
session
.query("INSERT INTO examples_ks.dates (d) VALUES (?)", (time_date,))
.await?;
let result = session
.query("SELECT d from examples_ks.dates", &[])
.await?;
for row in result.rows_typed::<(time::Date,)>()? {
let (read_date,) = match row {
Ok(read_date) => read_date,
Err(_) => continue, // We might read a date that does not fit in time::Date, skip it
};
println!("Parsed a date into time::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: CqlDate = CqlDate(u32::MAX);
session
.query(
"INSERT INTO examples_ks.dates (d) VALUES (?)",
(example_big_date,),
)
.await?;
let result = session
.query("SELECT d from examples_ks.dates", &[])
.await?;
let mut iter = result.rows_typed::<(CqlValue,)>()?;
while let Some((value,)) = iter.next().transpose()? {
let read_days: u32 = match value {
CqlValue::Date(CqlDate(days)) => days,
_ => panic!("oh no"),
};
println!("Read a date as raw days: {}", read_days);
}
// Time
// Time is represented as nanosecond count since midnight in range 0..=86399999999999
session
.query(
"CREATE TABLE IF NOT EXISTS examples_ks.times (t time primary key)",
&[],
)
.await?;
// Time can be represented using 3 different types, chrono::NaiveTime, time::Time and CqlTime. All types support
// full value range
// chrono::NaiveTime
let chrono_time = NaiveTime::from_hms_nano_opt(1, 2, 3, 456_789_012).unwrap();
session
.query(
"INSERT INTO examples_ks.times (t) VALUES (?)",
(chrono_time,),
)
.await?;
let result = session
.query("SELECT t from examples_ks.times", &[])
.await?;
let mut iter = result.rows_typed::<(NaiveTime,)>()?;
while let Some((read_time,)) = iter.next().transpose()? {
println!("Parsed a time into chrono::NaiveTime: {:?}", read_time);
}
// time::Time
let time_time = time::Time::from_hms_nano(2, 3, 4, 567_890_123).unwrap();
session
.query("INSERT INTO examples_ks.times (t) VALUES (?)", (time_time,))
.await?;
let result = session
.query("SELECT t from examples_ks.times", &[])
.await?;
let mut iter = result.rows_typed::<(time::Time,)>()?;
while let Some((read_time,)) = iter.next().transpose()? {
println!("Parsed a time into time::Time: {:?}", read_time);
}
// CqlTime
let time_time = CqlTime(((3 * 60 + 4) * 60 + 5) * 1_000_000_000 + 678_901_234);
session
.query("INSERT INTO examples_ks.times (t) VALUES (?)", (time_time,))
.await?;
let result = session
.query("SELECT t from examples_ks.times", &[])
.await?;
let mut iter = result.rows_typed::<(CqlTime,)>()?;
while let Some((read_time,)) = iter.next().transpose()? {
println!("Read a time as raw nanos: {:?}", read_time);
}
// Timestamp
// Timestamp is represented as milliseconds since unix epoch - 1970-01-01. Negative values are also possible
session
.query(
"CREATE TABLE IF NOT EXISTS examples_ks.timestamps (t timestamp primary key)",
&[],
)
.await?;
// Timestamp can also be represented using 3 different types, chrono::DateTime<chrono::Utc>, time::OffsetDateTime and
// CqlTimestamp. Only CqlTimestamp allows full range.
// chrono::DateTime<chrono::Utc>
let chrono_datetime = Utc::now();
session
.query(
"INSERT INTO examples_ks.timestamps (t) VALUES (?)",
(chrono_datetime,),
)
.await?;
let result = session
.query("SELECT t from examples_ks.timestamps", &[])
.await?;
let mut iter = result.rows_typed::<(DateTime<Utc>,)>()?;
while let Some((read_time,)) = iter.next().transpose()? {
println!(
"Parsed a timestamp into chrono::DateTime<chrono::Utc>: {:?}",
read_time
);
}
// time::OffsetDateTime
let time_datetime = time::OffsetDateTime::now_utc();
session
.query(
"INSERT INTO examples_ks.timestamps (t) VALUES (?)",
(time_datetime,),
)
.await?;
let result = session
.query("SELECT t from examples_ks.timestamps", &[])
.await?;
let mut iter = result.rows_typed::<(time::OffsetDateTime,)>()?;
while let Some((read_time,)) = iter.next().transpose()? {
println!(
"Parsed a timestamp into time::OffsetDateTime: {:?}",
read_time
);
}
// CqlTimestamp
let cql_datetime = CqlTimestamp(1 << 31);
session
.query(
"INSERT INTO examples_ks.timestamps (t) VALUES (?)",
(cql_datetime,),
)
.await?;
let result = session
.query("SELECT t from examples_ks.timestamps", &[])
.await?;
let mut iter = result.rows_typed::<(CqlTimestamp,)>()?;
while let Some((read_time,)) = iter.next().transpose()? {
println!("Read a timestamp as raw millis: {:?}", read_time);
}
Ok(())
}