-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
282 lines (245 loc) · 7.52 KB
/
schema.sql
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
pragma foreign_keys = ON;
-- > Meta data
-- Maybe add trigger on all insert,update,delete to update last_mod_time?
create table if not exists
meta_data(
last_mod_time text,
version text
);
-- > Notes
create table if not exists
notes (
id integer primary key,
title text,
frontmatter text,
body text,
mod_time text
);
-- >> Staged Notes
-- Notes added but not yet properly updated
create table if not exists
staged_notes (
id integer primary key,
title text,
note_path text,
add_time text
);
-- >> Full Text Search over notes
create virtual table if not exists
notes_fts using fts5(
title,
body,
content='notes',
content_rowid='id'
);
-- >> FTS Sync Triggers
create trigger if not exists
notes_ai
after insert on notes
begin
insert into notes_fts (rowid, title, body)
values (new.id, new.title, new.body);
end;
create trigger if not exists
notes_ad
after delete on notes
begin
insert into notes_fts(notes_fts, rowid, title, body)
values('delete', old.id, old.title, old.body);
end;
create trigger if not exists
notes_au
after update on notes
begin
insert into notes_fts(notes_fts, rowid, title, body)
values('delete', old.id, old.title, old.body);
insert into notes_fts (rowid, title, body)
values (new.id, new.title, new.body);
end;
-- > Note Links
create table if not exists
note_links (
id integer primary key,
parent_note_id integer,
child_note_id integer,
foreign key (parent_note_id)
references notes (id),
foreign key (child_note_id)
references notes (id),
unique (
parent_note_id, child_note_id)
on conflict abort
);
-- > Tags Table
create table if not exists
tags (
id integer primary key autoincrement,
tag text,
parent_id integer,
check (tag != ''),
foreign key (parent_id) references tags (id),
unique (
tag, parent_id
)
on conflict abort
);
-- >> Full Paths Table
create table if not exists full_tag_paths as
with recursive tags_parents(id, tag, parent_id, parent) as
(
select a.id, a.tag, a.parent_id, b.tag as parent
from tags a
left join tags b
on a.parent_id = b.id
),
pnts(id, tag, parent_id, id_path, tag_path) as
(
select
id, tag, parent_id,
ifnull(parent_id, '-') as id_path, ifnull(parent, '-') as tag_path
from tags_parents
where parent_id is NULL
union
select
m.id, m.tag, m.parent_id,
pnts.id_path || '/' || pnts.id as id_path,
pnts.tag_path || '/' || pnts.tag as tag_path
from tags_parents m
inner join pnts
on pnts.id = m.parent_id
order by m.parent_id desc
)
select
id, tag,
ltrim(id_path || '/' || id, '-/') as full_path,
ltrim(tag_path || '/' || tag, '-/') as full_tag_path
from pnts;
-- >>> Update on insert
-- need to repeat for delete and update ...
-- Just copy paste the recursive query from above ... sighs ... what alternatives??
create trigger if not exists tag_path_update_insert
after insert on tags
begin
delete from full_tag_paths;
insert into full_tag_paths
with recursive tags_parents(id, tag, parent_id, parent) as
(
select a.id, a.tag, a.parent_id, b.tag as parent
from tags a
left join tags b
on a.parent_id = b.id
),
pnts(id, tag, parent_id, id_path, tag_path) as
(
select
id, tag, parent_id,
ifnull(parent_id, '-') as id_path, ifnull(parent, '-') as tag_path
from tags_parents
where parent_id is NULL
union
select
m.id, m.tag, m.parent_id,
pnts.id_path || '/' || pnts.id as id_path,
pnts.tag_path || '/' || pnts.tag as tag_path
from tags_parents m
inner join pnts
on pnts.id = m.parent_id
order by m.parent_id desc
)
select
id as id, tag as tag,
ltrim(id_path || '/' || id, '-/') as full_path,
ltrim(tag_path || '/' || tag, '-/') as full_tag_path
from pnts;
end;
-- >>> Update on delete
create trigger if not exists tag_path_update_delete
after delete on tags
begin
delete from full_tag_paths;
insert into full_tag_paths
with recursive tags_parents(id, tag, parent_id, parent) as
(
select a.id, a.tag, a.parent_id, b.tag as parent
from tags a
left join tags b
on a.parent_id = b.id
),
pnts(id, tag, parent_id, id_path, tag_path) as
(
select
id, tag, parent_id,
ifnull(parent_id, '-') as id_path, ifnull(parent, '-') as tag_path
from tags_parents
where parent_id is NULL
union
select
m.id, m.tag, m.parent_id,
pnts.id_path || '/' || pnts.id as id_path,
pnts.tag_path || '/' || pnts.tag as tag_path
from tags_parents m
inner join pnts
on pnts.id = m.parent_id
order by m.parent_id desc
)
select
id as id, tag as tag,
ltrim(id_path || '/' || id, '-/') as full_path,
ltrim(tag_path || '/' || tag, '-/') as full_tag_path
from pnts;
end;
-- >>> Update on update
create trigger if not exists tag_path_update_update
after update on tags
begin
delete from full_tag_paths;
insert into full_tag_paths
with recursive tags_parents(id, tag, parent_id, parent) as
(
select a.id, a.tag, a.parent_id, b.tag as parent
from tags a
left join tags b
on a.parent_id = b.id
),
pnts(id, tag, parent_id, id_path, tag_path) as
(
select
id, tag, parent_id,
ifnull(parent_id, '-') as id_path, ifnull(parent, '-') as tag_path
from tags_parents
where parent_id is NULL
union
select
m.id, m.tag, m.parent_id,
pnts.id_path || '/' || pnts.id as id_path,
pnts.tag_path || '/' || pnts.tag as tag_path
from tags_parents m
inner join pnts
on pnts.id = m.parent_id
order by m.parent_id desc
)
select
id as id, tag as tag,
ltrim(id_path || '/' || id, '-/') as full_path,
ltrim(tag_path || '/' || tag, '-/') as full_tag_path
from pnts;
end;
-- >> Note tags table
-- create table if not exists
-- note_tags (
-- note_id integer,
-- tag_id integer,
-- foreign key (note_id) references notes (id),
-- foreign key (tag_id) references tags (id)
-- );
-- >>> With unique constraint and index
-- Check functionallity (and maybe performance)?
create table if not exists
note_tags (
note_id integer,
tag_id integer,
foreign key (note_id) references notes (id),
foreign key (tag_id) references tags (id),
unique (note_id, tag_id)
on conflict abort
);