forked from gluesql/gluesql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_world.rs
105 lines (90 loc) · 2.93 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
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
#[cfg(feature = "sled-storage")]
mod hello_world {
use {
gluesql::{
prelude::{Glue, Payload},
sled_storage::SledStorage,
},
std::fs,
};
struct GreetTable {
rows: Vec<GreetRow>,
}
struct GreetRow {
name: String,
}
impl TryFrom<Payload> for GreetTable {
type Error = &'static str;
fn try_from(payload: Payload) -> Result<Self, Self::Error> {
match payload {
Payload::Select { labels: _, rows } => {
let rows = rows
.into_iter()
.map(|mut row| {
let name = row.remove(0);
GreetRow {
name: String::from(name),
}
})
.collect::<Vec<_>>();
Ok(Self { rows })
}
Payload::SelectMap(rows) => {
let rows = rows
.into_iter()
.map(|row| {
let name = row.get("name").unwrap();
GreetRow {
name: String::from(name),
}
})
.collect::<Vec<_>>();
Ok(Self { rows })
}
_ => Err("unexpected payload, expected a select query result"),
}
}
}
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 table: GreetTable = result.remove(0).try_into().unwrap();
assert_eq!(table.rows.len(), 1);
println!("Hello {}!", table.rows[0].name); // Will always output "Hello World!"
}
}
fn main() {
#[cfg(feature = "sled-storage")]
futures::executor::block_on(hello_world::run());
}