alfred_workflow_rust_project/
workflow_database.rs

1use rusqlite::{Connection, Params, params, Row, MappedRows, Result};
2use crate::workflow::AlfredWorkflow;
3
4
5impl AlfredWorkflow {
6    pub fn open(path: &str) -> Connection {
7        Connection::open(path).unwrap()
8    }
9
10    pub fn query<P: Params, F, T>(db: &Connection, sql: &str, param: P, map_fun: F) -> Vec<T>
11        where
12            P: Params,
13            F: FnMut(&Row<'_>) -> Result<T>,
14    {
15        let mut stmt = db.prepare(sql).unwrap();
16        let rows = stmt.query_map(param, map_fun).unwrap();
17        let mut rts = Vec::new();
18        for row in rows {
19            rts.push(row.unwrap())
20        }
21        rts
22    }
23
24    pub fn close(db: Connection) {
25        db.close().unwrap();
26    }
27}