-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcustom_types.rs
More file actions
75 lines (66 loc) · 1.84 KB
/
Copy pathcustom_types.rs
File metadata and controls
75 lines (66 loc) · 1.84 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
use skytable::{
query,
query::SQParam,
response::{FromResponse, Response},
ClientResult, Config,
};
#[derive(Debug, PartialEq, Clone)]
struct User {
username: String,
password: String,
followers: u64,
email: Option<String>,
}
impl User {
fn new(username: String, password: String, followers: u64, email: Option<String>) -> Self {
Self {
username,
password,
followers,
email,
}
}
}
impl SQParam for User {
fn append_param(&self, q: &mut Vec<u8>) -> usize {
self.username.append_param(q)
+ self.password.append_param(q)
+ self.followers.append_param(q)
+ self.email.append_param(q)
}
}
impl FromResponse for User {
fn from_response(resp: Response) -> ClientResult<Self> {
let (username, password, followers, email) = FromResponse::from_response(resp)?;
Ok(Self::new(username, password, followers, email))
}
}
fn main() {
let mut db = Config::new_default("username", "password")
.connect()
.unwrap();
// set up schema
// create space
db.query_parse::<()>(&query!("create space myspace"))
.unwrap();
// create model
db.query_parse::<()>(&query!(
"create model myspace.mymodel(username: string, password: string, followers: uint64, null email: string)"
))
.unwrap();
// insert data
let our_user = User::new("myuser".into(), "pass123".into(), 0, None);
db.query_parse::<()>(&query!(
"insert into myspace.mymodel(?, ?, ?, ?)",
our_user.clone()
))
.unwrap();
// select data
let ret_user: User = db
.query_parse(&query!(
"select * from myspace.mymodel WHERE username = ?",
&our_user.username
))
.unwrap();
assert_eq!(our_user, ret_user);
}