-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_nb.py
1921 lines (1789 loc) · 46.9 KB
/
proto_nb.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ===========
from zekell import *
import datetime as dt
# -----------
# > Testing
# ===========
db.conn.close()
# -----------
# ===========
db_path = Path('test.db')
# -----------
# ===========
db_path.unlink()
# -----------
# ===========
db = db_connection(db_path, True)
# # -----------
# >> Files table with FTS
# ===========
db_init(db)
# -----------
# ===========
db.ex('select name from sqlite_master where type = "table"')
# -----------
# >> Testing Batch ex
# ===========
db.ex([
'''insert into tags(tag, parent_id) values('test', NULL )''',
'insert into tags(tag, parent_id) values("topics", NULL)',
])
# -----------
# ===========
db.ex([
'insert into tags(tag, parent_id) values("code", 2)',
'insert into tags(tag, parent_id) values("sql", 2)',
'insert into tags(tag, parent_id) values("joins", 4)',
'insert into tags(tag, parent_id) values("functions", 3)'
])
# -----------
# ===========
db.ex('insert into tags(tag, parent_id) values("rollbacktest", 2)')
# -----------
# ===========
db.ex([
'insert into tags(tag, parent_id) values("rollbacktest2", 2)',
'insert into tags(tag, parent_id) values("anothertest", 4)',
'insert into tags(tag, parent_id) values("functions", 3)'
])
# -----------
# ===========
db.ex('select * from tags')
# -----------
# cols: id, title, metadata, body, m-date
# ===========
import datetime as dt
# -----------
# ===========
dt.datetime.utcnow().strftime('%Y%m%d%H%M%S')
# -----------
# ===========
# -----------
# ===========
db.ex('''
create table notes (
id integer primary key,
title text,
metadata text,
body text,
mod_time text
)
''')
# -----------
# ===========
db.ex('''
insert into notes
values(?, ?, ?, ?, ?)
''',
(
int(dt.datetime.utcnow().strftime('%Y%m%d%H%M%S')),
'My first note',
'''---
title: My first note
tags: first, demo
---''',
'''This is a demo note
Not much more to day''',
dt.datetime.utcnow().timestamp()
))
# -----------
# ===========
db.ex('select * from notes')
# -----------
# ===========
db.ex('''
create virtual table notes_fts using fts5(
title,
body,
content='notes',
content_rowid='id'
)
''')
# -----------
# ===========
db.ex('select * from notes_fts')
# -----------
# ===========
# shouldn't work yet ... not FTS content
db.ex("select * from notes_fts where notes_fts match 'demo'")
# -----------
# ===========
# create triggers
# ... slightly different for FTS with special notes_fts column and 'delete' values
db.ex('''
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''')
db.ex('''
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
''')
db.ex('''
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
''')
# -----------
# ===========
db.ex('''
insert into notes
values(?, ?, ?, ?, ?)
''',
(
int(dt.datetime.utcnow().timestamp()),
'My second note',
'''---
title: My second note
tags: second, demo
---''',
'''This is another demo note
Not much more to day''',
dt.datetime.utcnow().timestamp()
))
# -----------
# ===========
db.ex('select * from notes_fts')
# -----------
# ===========
db.ex("select * from notes_fts where notes_fts match 'demo'")
# -----------
# ===========
db.ex("select rowid,* from notes_fts where notes_fts match 'demo'")
# -----------
# ===========
db.ex("delete from notes")
# -----------
# ===========
db.ex("delete from notes where id = 20210829110444")
# -----------
# ===========
db.ex("select rowid,* from notes_fts where notes_fts match 'demo'")
# -----------
# playing with FTS queries
# ===========
db.ex("select highlight(notes_fts, 1, '>>', '<<') from notes_fts where notes_fts match 'demo'")
# -----------
# ===========
db.ex("select snippet(notes_fts, 1, '>>', '<<', '...', 6) from notes_fts where notes_fts match 'demo'")
# -----------
# >> Note Links
# ===========
db.ex("select * from notes")
# -----------
# ===========
create_note_links_table(db)
# -----------
# ===========
db.ex('select * from notes')
db.ex('select * from note_links')
# -----------
# ===========
db.ex("""
create table test_fk(
id integer primary key,
ref_id integer,
cite_id integer,
foreign key (ref_id) references notes (id),
foreign key (cite_id) references notes (id)
)
""")
# -----------
# ===========
db.ex("""
insert into test_fk(ref_id, cite_id) values(20210904100714, 163071404)
""")
# -----------
# ===========
db.ex('select * from test_fk')
# -----------
# ===========
db.ex("""
insert into
note_links(parent_note_id, child_note_id)
values(1630714042, 20210904100714)
""")
# -----------
# ===========
db.ex("""
insert into
note_links(parent_note_id, child_note_id)
values(163071404, 20210904100714)
""")
# -----------
# ===========
db.ex("""
insert into
note_links(parent_note_id, child_note_id)
values(1630714042, 2021090410071)
""")
# -----------
# # ===========
create_tags_table(db)
# # -----------
# ===========
add_tag(db, 'test', None)
# -----------
# ===========
add_tag(db, 'test', 1)
# -----------
# ===========
db.ex('select * from tags where tag = "test" and parent_id IS NULL')
# -----------
# ===========
db.ex('select * from tags')
# -----------
# # ===========
pprint(db.ex("select * from sqlite_master where type='table'"))
# # -----------
# # ===========
db_path.unlink()
# # -----------
# > Instantiating from schema file
# ===========
db = db_connection(Path('test.db'), True)
# -----------
# ===========
schema = Path('schema.sql').read_text()
print(schema)
# -----------
# ===========
db.ex('select * from sqlite_master')
# -----------
# ===========
db.cursor.executescript(schema)
# -----------
# ===========
db.ex('select name from sqlite_master where type="table"')
# -----------
# ===========
db.ex('pragma foreign_keys')
# -----------
# ===========
import datetime as dt
db.ex(
'''
insert into notes
values(?, ?, ?, ?, ?)
''',
(
123456,
'My first note',
'''---
title: My first note
tags: first, demo
---''',
'''This is a demo note
Not much more to day''',
dt.datetime.utcnow().timestamp()
))
# -----------
# ===========
db.ex('select id,* from notes')
# -----------
# > note_tags_table
# ===========
db = db_connection(Path('test.db'), True)
# -----------
# ===========
db.ex('select name from sqlite_master')
# -----------
# ===========
db.ex('drop table note_tags')
# -----------
# ===========
query = '''
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)
);
'''
# -----------
# ===========
db.ex(query)
# -----------
# ===========
db.ex('''
insert into note_tags values (1, 1)
''')
# -----------
# ===========
db_init(db)
# -----------
# ===========
db.ex('select name from sqlite_master')
# -----------
# ===========
file_id = int(dt.datetime.utcnow().strftime('%Y%m%d%H%M%S'))
db.ex('''
insert into notes
values(?, ?, ?, ?, ?)
''',
(
file_id,
'My first note',
'''---
title: My first note
tags: first, demo
---''',
'''This is a demo note
Not much more to day''',
dt.datetime.utcnow().timestamp()
))
# -----------
# ===========
file_id
# -----------
# ===========
db.ex('select * from notes')
# -----------
# ===========
add_tag(db, 'test')
# -----------
# ===========
db.ex('select * from tags')
# -----------
# ===========
db.ex('insert into note_tags values (?, ?)', (file_id, 1))
# -----------
# ===========
db.ex('select * from note_tags')
# -----------
# ===========
# shouldn't work because of foreign key constraint
db.ex('insert into note_tags values (?, ?)', (file_id, 2))
# -----------
# ===========
db.ex('select id from tags')[0][0]
# -----------
# > Tags and auto parent tables
# >> reset tags
# ===========
db.ex('drop table if exists tags')
db.ex('drop table if exists full_tag_paths')
# -----------
# ===========
db.ex('select * from tags')
# -----------
# >> add tags
# ===========
db.ex('''
insert into tags(tag, parent_id) values(
'test',
NULL
)
''')
# -----------
# ===========
db.ex('insert into tags(tag, parent_id) values("topics", NULL)')
# -----------
# ===========
db.ex('''select * from tags''')
# -----------
# ===========
db.ex('insert into tags(tag, parent_id) values("code", 2)')
db.ex('insert into tags(tag, parent_id) values("sql", 2)')
db.ex('insert into tags(tag, parent_id) values("joins", 4)')
db.ex('insert into tags(tag, parent_id) values("functions", 3)')
# -----------
# ===========
# should fail with foreign key constraint
db.ex('insert into tags(tag, parent_id) values("code", 22)')
# -----------
# ===========
db.ex('''
select id from tags where tag == 'topics'
''')
# -----------
# >> join parents
db.ex('select * from tags')
db.ex('select * from full_tag_paths')
# ===========
db.ex('insert into tags(tag, parent_id) values(?,?)', ['space', 3])
# -----------
# ===========
db.ex(['insert into tags(tag, parent_id) values(?,?)']*3, [
['clock', 3],
['business', 7],
['or', 9]
])
# -----------
# ===========
db.ex(['insert into tags(tag, parent_id) values(?,?)']*3, [
['star', 15],
['planet', 15],
['face', 16]
])
# -----------
# ===========
db.ex('''
select m.id, p.tag parent, m.tag, p.id parentid
from tags m
left join tags p
on m.parent_id = p.id
''')
# -----------
# ===========
db.ex('''
with recursive tag_children(id, tag, parent_id) as (
select a.id id, a.tag tag, a.parent_id parent_id
from tags a
left join tags b
on a.parent_id = b.id
)
select * from tag_children
where parent_id = 3
'''
)
# -----------
# ===========
db.ex('''
with recursive tag_children(id, tag, parent_id) as (
select id, tag, parent_id
from tags
where parent_id = ?
),
all_tag_children(id, tag, parent_id) as (
select id, tag, parent_id from tag_children
union
select a.id id, a.tag tag, a.parent_id parent_id
from tags a
inner join all_tag_children b on a.parent_id = b.id
)
select id from all_tag_children
''',
[3])
# -----------
# ===========
db.ex('''
select note_id from note_tags
where tag_id in (
with recursive tag_children(id, tag, parent_id) as (
select id, tag, parent_id
from tags
where parent_id = ?
),
all_tag_children(id, tag, parent_id) as (
select id, tag, parent_id from tag_children
union
select a.id id, a.tag tag, a.parent_id parent_id
from tags a
inner join all_tag_children b on a.parent_id = b.id
)
select id from all_tag_children
)
''',
[3])
# -----------
# ===========
db.ex('''
with recursive all_tag_children(id, tag, parent_id) as (
select id, tag, parent_id from tags
where id = ?
union
select id, tag, parent_id from tags
where parent_id = ?
union
select a.id id, a.tag tag, a.parent_id parent_id
from tags a
inner join all_tag_children b on a.parent_id = b.id
),
tag_children_notes(note_id) as (
select note_id from note_tags
where tag_id in (
select id from all_tag_children
)
)
select note_id from tag_children_notes
''',
[3, 3])
# -----------
db.ex('select note_id from note_tags where tag_id = 3')
db.ex('insert into note_tags(note_id, tag_id) values(20211031083086, 16)')
# >> full tag paths proto
# ===========
db.ex('''
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
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
''')
# -----------
# ===========
db.ex('''
create table 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,
ifnull(pnts.id_path, '-') || '/' || pnts.id as id_path,
ifnull(pnts.tag_path, '-') || '/' || pnts.tag as tag_path
from tags_parents m
join pnts
on pnts.id = m.parent_id
order by m.parent_id desc
)
select
id, tag,
id_path || '/' || id as full_path,
tag_path || '/' || tag as full_tag_path
from pnts
''')
# -----------
# >> Test full tag paths
# ===========
db.ex('select * from full_tag_paths')
# -----------
db.cursor.description
# ===========
db.ex('''
update full_tag_paths
set id = id_ud, tag = tag_ud, full_path = full_path_ud, full_tag_path = full_tag_path_ud
from (
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,
ifnull(pnts.id_path, '-') || '/' || pnts.id as id_path,
ifnull(pnts.tag_path, '-') || '/' || pnts.tag as tag_path
from tags_parents m
join pnts
on pnts.id = m.parent_id
order by m.parent_id desc
)
select
id as id_ud, tag as tag_ud,
id_path || '/' || id as full_path_ud,
tag_path || '/' || tag as full_tag_path_ud
from pnts
)
where id = id_ud
''')
# -----------
# ===========
db.ex('select * from full_tag_paths')
# -----------
# ===========
db.ex('delete from full_tag_paths')
# -----------
# ===========
db.ex('select * from full_tag_paths')
# -----------
# ===========
db.ex('insert into tags(tag, parent_id) values("trigger", 4)')
# -----------
# ===========
db.ex('select * from tags')
# -----------
# ===========
db.ex('''
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,
ifnull(pnts.id_path, '-') || '/' || pnts.id as id_path,
ifnull(pnts.tag_path, '-') || '/' || pnts.tag as tag_path
from tags_parents m
join pnts
on pnts.id = m.parent_id
order by m.parent_id desc
)
select
id as id_ud, tag as tag_ud,
id_path || '/' || id as full_path_ud,
tag_path || '/' || tag as full_tag_path_ud
from pnts
''')
# -----------
# >> Testing full_tag_path triggers
# ===========
db.ex('select * from tags')
# -----------
# ===========
db.ex('select * from full_tag_paths')
# -----------
# test insert trigger
# ===========
db.ex('insert into tags(tag, parent_id) values("triggers", 4)')
# -----------
# ===========
db.ex('select * from tags')
db.ex('select * from full_tag_paths')
# -----------
# test delete trigger
# ===========
# error from foreign key constraint ... ?
db.ex('delete from tags where id = 2')
# -----------
# ===========
db.ex('delete from tags where tag = "triggers"')
# -----------
# ===========
db.ex('select * from tags')
db.ex('select * from full_tag_paths')
# -----------
# test update trigger
# ===========
db.ex('update tags set tag = "new functions" where tag = "functions"')
db.ex('update tags set tag = "new topics" where tag = "topics"')
# -----------
# ===========
db.ex('select * from tags')
db.ex('select * from full_tag_paths')
# -----------
# > Parsing notes
# ===========
import re
# -----------
# ===========
proto_note = (
Path('./prototype/20200904105632 Python.mdzk')
.read_text()
)
# -----------
# ===========
print(proto_note)
# -----------
# ===========
front_matter_pattern = re.compile(r'(?s)^---\n(.+)\n---')
# -----------
# ===========
# -----------
# ===========
front_matter = front_matter_pattern.match(proto_note)
front_matter_data = front_matter.group(1)
meta_data = {}
for line in front_matter_data.splitlines():
key, value = [token.strip() for token in line.split(':')]
if key == 'tags':
meta_data[key] = [token.strip() for token in value.split(',')]
else:
meta_data[key] = value
meta_data
# -----------
# ===========
link_pattern = re.compile(r'\[(.*)\]\(\/(\d{12,14})\)')
# -----------
# ===========
links = link_pattern.findall(proto_note)
links
# -----------
# ===========
link_ids = set(link[1] for link in links)
link_ids
# -----------
# ===========
note_path = Path('prototype/20200904105632 Python.mdzk')
note = parse_note(note_path)
# -----------
# ===========
note._asdict()
# -----------
# ===========
db.ex('pragma table_info(notes)')
# -----------
# ===========
db.ex('''
insert into notes(id, title, frontmatter, body, mod_time)
values(?, ?, ?, ?, ?)''',
params=[note.id, note.title, note.frontmatter, note.body, dt.datetime.utcnow().timestamp()]
)
# -----------
# ===========
db.ex('select * from notes')
# -----------
# > Adding Notes
# ===========
db.ex('select * from notes')
# -----------
# ===========
make_new_note(db, 'first_note')
# -----------
# ===========
db.ex('select * from notes')
# -----------
# ===========
ls ./prototype
# -----------
# ===========
make_new_note(db, '')
# -----------
# ===========
ls ./prototype
# -----------
# ===========
new_note_path = Path('prototype/20211022003326 first_note.md')
# -----------
# ===========
update_note(db, new_note_path)
# -----------
# ===========
db.ex('select * from notes')
# -----------
# ===========
delete_note(db, Path('prototype/20211019103824 first_note.md'))
# -----------
# >> Adding LInks
# Unique links?
# ===========
make_new_note(db, 'first')
# -----------
# ===========
make_new_note(db, 'second')
make_new_note(db, 'third')
make_new_note(db, 'fourth')
# -----------
# ===========
db.ex('select * from notes')
# -----------
# ===========
db.ex('select * from note_links')
# -----------
# ===========
db.ex(
'insert into note_links(parent_note_id, child_note_id) values(?, ?)',
[20211020035331, 20211020035334])
db.ex(
'insert into note_links(parent_note_id, child_note_id) values(?, ?)',
[20211020035331, 20211020035334])
# -----------
# ===========
# duplicates!?
db.ex('select * from note_links')
# -----------
# ===========
# ignore uniqueness constraing error and skip
db.ex(
'insert or ignore into note_links(parent_note_id, child_note_id) values(?, ?)',
[20211020035331, 20211020035334])
# -----------
# ===========
# BUT ... doesn't ignore foreign key constraints
db.ex(
'insert or ignore into note_links(parent_note_id, child_note_id) values(?, ?)',
[20211020035331, 123123123123])
# -----------
# >> Update note with links
# ===========
db.ex('select * from note_links')
# -----------
# ===========
note_path = Path('prototype/20211022010525 first.md')
update_note(db, note_path)
# -----------
# ===========
db.ex('select * from notes')
# -----------
# ===========
db.ex('select * from note_links')
# -----------
# ===========
parse_note_body(note_path.read_text())
# -----------
note_path.read_text()
# ===========
link_pattern.findall(note_path.read_text())
# -----------
# >> Adding Tags
# ===========
# get list of paths
paths = [
t[0] for t in
db.ex('select full_tag_path from full_tag_paths')
]
paths
# -----------
# ===========
paths = ['test',
'topics',
'topics/code',
'topics/code/functions',
'topics/sql',
'topics/sql/joins']
# -----------
# ===========
new_path = 'topics/sql/recursive/theory'
# -----------
# ===========
tag_path_pattern = re.compile(r'[a-zA-Z_][a-zA-Z_\/]*[a-zA-Z_]')
def is_valid_tag_path(tag_path: str):
return tag_path_pattern.fullmatch(tag_path)
def check_valid_tag_path(tag_path: str):
if not is_valid_tag_path(tag_path):
raise NoteError('tag path ({}) is not valid (must fullmatch with {})'.format(
tag_path, tag_path_pattern.pattern))
def get_tag_id(db: DB, tag: str, parent_id: Optional[int]) -> int:
# sqlite: "is" works with null, "=" doesn't
result = db.ex(
'select id from tags where tag = ? and parent_id is ?',
[tag, parent_id]
)
return result[0][0]
def get_tag_path_id(db: DB, tag_path: str) -> int:
check_valid_tag_path(tag_path)
result = db.ex(
'select id from full_tag_paths where full_tag_path = ?',
[tag_path])
return result[0][0]
def get_all_full_tag_path(db: DB) -> list:
paths = [
tp[0] for tp
in db.ex('select full_tag_path from full_tag_paths')
]
return paths
# -----------
# ===========
def add_new_tag_path(db: DB, new_tag_path: str):
"""Add tags necessary for new_tag_path to refer to an extant tag
return None if necessary tags already exist
return id of newly created tag (leaf) if new tag(s) created
"""
check_valid_tag_path(new_tag_path)
paths = get_all_full_tag_path(db)
new_path_parents = [ # get longest match from all tag_paths
tag_path for tag_path
in paths
if tag_path in new_tag_path
]
if new_tag_path in paths:
# shouldn't happen, but just in case
return
new_path_parent = max(new_path_parents, default=None)
# no match or match does not start from beginning or is not SUB-string (ie too long)
# then new path is all new tags
if not new_path_parent or not (new_tag_path.find(new_path_parent) == 0):
new_path_parent_id = None
# whole path is new!
new_tags = new_tag_path.split('/')