forked from AlexPikalov/cdrs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud_operations.rs
More file actions
133 lines (115 loc) · 4.11 KB
/
crud_operations.rs
File metadata and controls
133 lines (115 loc) · 4.11 KB
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
#[macro_use]
extern crate cdrs;
#[macro_use]
extern crate cdrs_helpers_derive;
#[macro_use]
extern crate maplit;
use std::collections::HashMap;
use cdrs::authenticators::StaticPasswordAuthenticator;
use cdrs::cluster::session::{new as new_session, Session};
use cdrs::cluster::{ClusterTcpConfig, NodeTcpConfigBuilder, TcpConnectionPool};
use cdrs::load_balancing::RoundRobin;
use cdrs::query::*;
use cdrs::frame::IntoBytes;
use cdrs::types::from_cdrs::FromCDRSByName;
use cdrs::types::prelude::*;
type CurrentSession = Session<RoundRobin<TcpConnectionPool<StaticPasswordAuthenticator>>>;
fn main() {
let user = "user";
let password = "password";
let auth = StaticPasswordAuthenticator::new(&user, &password);
let node = NodeTcpConfigBuilder::new("localhost:9042", auth).build();
let cluster_config = ClusterTcpConfig(vec![node]);
let no_compression: CurrentSession =
new_session(&cluster_config, RoundRobin::new()).expect("session should be created");
create_keyspace(&no_compression);
create_udt(&no_compression);
create_table(&no_compression);
insert_struct(&no_compression);
select_struct(&no_compression);
update_struct(&no_compression);
delete_struct(&no_compression);
}
#[derive(Clone, Debug, IntoCDRSValue, TryFromRow, PartialEq)]
struct RowStruct {
key: i32,
user: User,
map: HashMap<String, User>,
list: Vec<User>,
}
impl RowStruct {
fn into_query_values(self) -> QueryValues {
query_values!("key" => self.key, "user" => self.user, "map" => self.map, "list" => self.list)
}
}
#[derive(Debug, Clone, PartialEq, IntoCDRSValue, TryFromUDT)]
struct User {
username: String,
}
fn create_keyspace(session: &CurrentSession) {
let create_ks: &'static str = "CREATE KEYSPACE IF NOT EXISTS test_ks WITH REPLICATION = { \
'class' : 'SimpleStrategy', 'replication_factor' : 1 };";
session.query(create_ks).expect("Keyspace creation error");
}
fn create_udt(session: &CurrentSession) {
let create_type_cql = "CREATE TYPE IF NOT EXISTS test_ks.user (username text)";
session
.query(create_type_cql)
.expect("Keyspace creation error");
}
fn create_table(session: &CurrentSession) {
let create_table_cql =
"CREATE TABLE IF NOT EXISTS test_ks.my_test_table (key int PRIMARY KEY, \
user frozen<test_ks.user>, map map<text, frozen<test_ks.user>>, list list<frozen<test_ks.user>>);";
session
.query(create_table_cql)
.expect("Table creation error");
}
fn insert_struct(session: &CurrentSession) {
let row = RowStruct {
key: 3i32,
user: User {
username: "John".to_string(),
},
map: hashmap! { "John".to_string() => User { username: "John".to_string() } },
list: vec![User {
username: "John".to_string(),
}],
};
let insert_struct_cql = "INSERT INTO test_ks.my_test_table \
(key, user, map, list) VALUES (?, ?, ?, ?)";
session
.query_with_values(insert_struct_cql, row.into_query_values())
.expect("insert");
}
fn select_struct(session: &CurrentSession) {
let select_struct_cql = "SELECT * FROM test_ks.my_test_table";
let rows = session
.query(select_struct_cql)
.expect("query")
.get_body()
.expect("get body")
.into_rows()
.expect("into rows");
for row in rows {
let my_row: RowStruct = RowStruct::try_from_row(row).expect("into RowStruct");
println!("struct got: {:?}", my_row);
}
}
fn update_struct(session: &CurrentSession) {
let update_struct_cql = "UPDATE test_ks.my_test_table SET user = ? WHERE key = ?";
let upd_user = User {
username: "Marry".to_string(),
};
let user_key = 1i32;
session
.query_with_values(update_struct_cql, query_values!(upd_user, user_key))
.expect("update");
}
fn delete_struct(session: &CurrentSession) {
let delete_struct_cql = "DELETE FROM test_ks.my_test_table WHERE key = ?";
let user_key = 1i32;
session
.query_with_values(delete_struct_cql, query_values!(user_key))
.expect("delete");
}