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
|
use anyhow::Result;
use rusqlite::{params, Connection};
use rusqlite_migration::{Migrations, M};
use crate::model::event::Event;
pub fn init() -> Result<Connection> {
let mut conn = Connection::open("database.db")?;
let migrations = Migrations::new(vec![M::up(include_str!("migrations/1-init.sql"))]);
migrations.to_latest(&mut conn)?;
Ok(conn)
}
pub fn insert(conn: &Connection, event: &Event) -> Result<()> {
conn.execute(
"INSERT INTO events (date, start, end, name, created, updated) VALUES (?, ?, ?, ?, datetime(), datetime())",
params![event.date, event.start, event.end, event.name]
)?;
Ok(())
}
pub fn list(conn: &Connection) -> Result<Vec<Event>> {
let mut stmt = conn.prepare("SELECT date, start, end, name FROM events")?;
let iter = stmt.query_map([], |row|
Ok(Event {
date: row.get(0)?,
start: row.get(1)?,
end: row.get(2)?,
name: row.get(3)?,
})
)?;
Ok(iter.map(|r| r.unwrap()).collect())
}
|