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
use std::{path::Path};
use std::fs::create_dir_all;
use rusqlite::{Connection, Result};
#[macro_export]
macro_rules! crate_version {
() => {
env!("CARGO_PKG_VERSION")
};
}
fn get_conn() -> Result<Connection> {
let dir = &shellexpand::tilde("~/.dbdir").into_owned();
create_dir_all(dir).expect("Create storage dir");
let path = &shellexpand::tilde("~/.dbdir/bookstore.db").into_owned();
let path = Path::new(path);
Connection::open(path)
}
pub fn init_storage() -> Result<()> {
println!(" at ~/.dbdir/bookstore.db");
let conn = get_conn()?;
conn.execute(
"Create table if not exists book_marks (
id integer primary key,
tag text not null unique,
mark text not null unique
)",
[]
)?;
Ok(())
}
pub fn read_tags() -> Result<Vec<String>> {
let conn = get_conn()?;
let mut stmt = conn.prepare("SELECT tag from book_marks ;")?;
let rows = stmt.query_map([], |row| Ok(row.get(0)))?;
let mut tags = Vec::new();
for row in rows {
tags.push(row.unwrap()?);
}
Ok(tags)
}
pub fn read_mark(tag: &str) -> Result<String> {
let conn = get_conn()?;
let mut stmt = conn.prepare(&format!(
"SELECT mark from book_marks
where tag = '{}';",
&tag
))?;
stmt.query_row([], |row| row.get(0))
}
pub fn add_mark(tag: &str, mark: &str) -> Result<()> {
let conn = get_conn()?;
conn.execute(
"INSERT INTO book_marks (tag, mark) values (?1, ?2)",
&[&tag, &mark],
)?;
Ok(())
}
pub fn del_mark(tag: &str) -> Result<()> {
let conn = get_conn()?;
conn.execute(
"delete from book_marks where tag = ?1 ;",
&[&tag],
)?;
Ok(())
}
pub fn update_mark(tag: &str, newtag: &str) -> Result<()> {
let conn = get_conn()?;
conn.execute(
"update book_marks set tag = ?2 where tag = ?1;",
&[&tag, &newtag],
)?;
Ok(())
}