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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
extern crate linked_hash_set;
extern crate regex;
extern crate rusqlite;
extern crate serde;
extern crate serde_json;
use self::rusqlite::types::{FromSql, ToSql};
use self::rusqlite::{Connection, MappedRows, NO_PARAMS};
use super::make_tags;
use std::collections::HashMap;
use {KVStringI64, Note, Tags};
pub fn select_by_day(conn: &Connection) -> String {
let mut stmt = conn
.prepare(
"SELECT substr(created_at, 0, 11) as dt, count(1) as n
FROM note
group by dt
order by dt",
)
.unwrap();
let result_iter = stmt
.query_map(NO_PARAMS, |row| KVStringI64 {
k: row.get(0),
v: row.get(1),
})
.unwrap();
let mut d = "[ ".to_owned();
for r in result_iter {
let mut r = r.unwrap();
d.push_str(&serde_json::to_string(&r).unwrap());
d.push_str(",");
}
d.pop();
d.push_str("]");
d
}
pub fn select_by_tag(conn: &Connection) -> String {
let mut stmt = conn
.prepare(
"SELECT tags
FROM note",
)
.unwrap();
let mut tag_count_map: HashMap<String, i64> = HashMap::new();
let result_iter = stmt
.query_map(NO_PARAMS, |row| Tags { tags: row.get(0) })
.unwrap();
for r in result_iter {
let r = r.unwrap();
let tag_set = r.tags.split(',');
for t in tag_set {
if let Some(&v) = &tag_count_map.get(t) {
tag_count_map.insert(t.to_lowercase(), v + 1);
} else {
tag_count_map.insert(t.to_lowercase(), 1);
}
}
}
let mut d = "[ ".to_owned();
for (tag, &count) in &tag_count_map {
let item = KVStringI64 {
k: tag.to_string(),
v: count,
};
d.push_str(&serde_json::to_string(&item).unwrap());
d.push_str(",");
}
d.pop();
d.push_str("]");
d
}
pub fn select_count(conn: &Connection) -> u32 {
let mut stmt = conn.prepare("SELECT count(1) FROM note").unwrap();
let rs = stmt.query_row(NO_PARAMS, |row| row.get(0)).unwrap();
rs
}
pub fn select(conn: &Connection, limit: &u32, offset: &u32) -> String {
let mut stmt = conn
.prepare(
"SELECT rowid, title, url, tags, description, comments
, hex(annotations)
, created_at, is_public
FROM note
order by created_at desc limit :limit offset :offset",
)
.unwrap();
let note_iter = stmt
.query_map_named(
&[(":limit", limit as &ToSql), (":offset", offset as &ToSql)],
|row| Note {
rowid: row.get(0),
title: row.get(1),
url: row.get(2),
tags: row.get(3),
description: row.get(4),
comments: row.get(5),
annotations: super::utils::make_data_url(row),
created_at: row.get(7),
is_public: row.get(8),
},
)
.unwrap();
let mut j = "[ ".to_owned();
for note in note_iter {
let mut note = note.unwrap();
note.tags = make_tags(¬e.tags);
j.push_str(&serde_json::to_string(¬e).unwrap());
j.push_str(",");
}
j.pop();
j.push_str("]");
j
}