forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-defined-type.rs
60 lines (48 loc) · 1.7 KB
/
user-defined-type.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
use anyhow::Result;
use scylla::cql_to_rust::FromCqlVal;
use scylla::macros::{FromUserType, IntoUserType};
use scylla::{IntoTypedRows, Session, 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' : 'SimpleStrategy', 'replication_factor' : 1}", &[]).await?;
session
.query(
"CREATE TYPE IF NOT EXISTS ks.my_type (int_val int, text_val text)",
&[],
)
.await?;
session
.query(
"CREATE TABLE IF NOT EXISTS ks.udt_tab (k int, my my_type, primary key (k))",
&[],
)
.await?;
// Define custom struct that matches User Defined Type created earlier
// wrapping field in Option will gracefully handle null field values
#[derive(Debug, IntoUserType, FromUserType)]
struct MyType {
int_val: i32,
text_val: Option<String>,
}
let to_insert = MyType {
int_val: 17,
text_val: Some("Some string".to_string()),
};
// It can be inserted like a normal value
session
.query("INSERT INTO ks.udt_tab (k, my) VALUES (5, ?)", (to_insert,))
.await?;
// And read like any normal value
if let Some(rows) = session.query("SELECT my FROM ks.udt_tab", &[]).await?.rows {
for row in rows.into_typed::<(MyType,)>() {
let (my_val,) = row?;
println!("{:?}", my_val)
}
}
println!("Ok.");
Ok(())
}