forked from gluesql/gluesql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_world.rs
76 lines (62 loc) · 1.88 KB
/
hello_world.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
#[cfg(feature = "sled-storage")]
mod hello_world {
use {
gluesql::{prelude::Glue, sled_storage::SledStorage},
std::fs,
};
struct GreetRow {
name: String,
}
pub async fn run() {
/*
Initiate a connection
*/
/*
Open a Sled database, this will create one if one does not yet exist
*/
let sled_dir = "/tmp/gluesql/hello_world";
fs::remove_dir_all(sled_dir).unwrap_or(());
let storage = SledStorage::new(sled_dir).expect("Something went wrong!");
/*
Wrap the Sled database with Glue
*/
let mut glue = Glue::new(storage);
/*
Create table then insert a row
Write queries as a string
*/
let queries = "
CREATE TABLE greet (name TEXT);
INSERT INTO greet VALUES ('World');
";
glue.execute(queries).await.expect("Execution failed");
/*
Select inserted row
*/
let queries = "
SELECT name FROM greet
";
let mut result = glue.execute(queries).await.expect("Failed to execute");
/*
Query results are wrapped into a payload enum, on the basis of the query type
*/
assert_eq!(result.len(), 1);
let payload = result.remove(0);
let rows = payload
.select()
.unwrap()
.map(|map| {
let name = *map.get("name").unwrap();
let name = name.into();
GreetRow { name }
})
.collect::<Vec<_>>();
assert_eq!(rows.len(), 1);
assert_eq!(&rows[0].name, "World");
println!("Hello {}!", rows[0].name); // Will always output "Hello World!"
}
}
fn main() {
#[cfg(feature = "sled-storage")]
futures::executor::block_on(hello_world::run());
}