-
Notifications
You must be signed in to change notification settings - Fork 49
/
hello_world.rs
47 lines (40 loc) · 1.2 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
use fnck_sql::db::{DataBaseBuilder, ResultIter};
use fnck_sql::errors::DatabaseError;
use fnck_sql::implement_from_tuple;
use fnck_sql::types::value::DataValue;
#[derive(Default, Debug, PartialEq)]
struct MyStruct {
pub c1: i32,
pub c2: String,
}
implement_from_tuple!(
MyStruct, (
c1: i32 => |inner: &mut MyStruct, value| {
if let DataValue::Int32(val) = value {
inner.c1 = val;
}
},
c2: String => |inner: &mut MyStruct, value| {
if let DataValue::Utf8 { value, .. } = value {
inner.c2 = value;
}
}
)
);
#[cfg(feature = "macros")]
fn main() -> Result<(), DatabaseError> {
let database = DataBaseBuilder::path("./hello_world").build()?;
database
.run("create table if not exists my_struct (c1 int primary key, c2 int)")?
.done()?;
database
.run("insert into my_struct values(0, 0), (1, 1)")?
.done()?;
let iter = database.run("select * from my_struct")?;
let schema = iter.schema().clone();
for tuple in iter {
println!("{:?}", MyStruct::from((&schema, tuple?)));
}
database.run("drop table my_struct")?.done()?;
Ok(())
}