-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbfunc.go
1406 lines (1206 loc) · 34.4 KB
/
dbfunc.go
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
package main
import (
"database/sql"
"fmt"
"os"
"strconv"
"strings"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/slzatz/vimango/vim"
)
func getId() int {
return org.rows[org.fr].id
}
/***********************************new*********************************/
func entryTidFromId(id int) int {
var tid int
_ = db.QueryRow("SELECT tid FROM task WHERE id=?;", id).Scan(&tid)
return tid
}
func keywordTidFromId(id int) int {
var tid int
_ = db.QueryRow("SELECT tid FROM keyword WHERE id=?;", id).Scan(&tid)
return tid
}
/***********************************new*********************************/
func timeDelta(t string) string {
var t0 time.Time
if strings.Contains(t, "T") {
t0, _ = time.Parse("2006-01-02T15:04:05Z", t)
} else {
t0, _ = time.Parse("2006-01-02 15:04:05", t)
}
diff := time.Since(t0)
diff = diff / 1000000000
if diff <= 120 {
return fmt.Sprintf("%d seconds ago", diff)
} else if diff <= 60*120 {
return fmt.Sprintf("%d minutes ago", diff/60) // <120 minutes we report minute
} else if diff <= 48*60*60 {
return fmt.Sprintf("%d hours ago", diff/3600) // <48 hours report hours
} else if diff <= 24*60*60*60 {
return fmt.Sprintf("%d days ago", diff/3600/24) // <60 days report days
} else if diff <= 24*30*24*60*60 {
return fmt.Sprintf("%d months ago", diff/3600/24/30) // <24 months rep
} else {
return fmt.Sprintf("%d years ago", diff/3600/24/30/12)
}
}
func keywordId(title string) int {
var id int
_ = db.QueryRow("SELECT id FROM keyword WHERE title=?;", title).Scan(&id)
return id
}
func keywordExists(title string) (int, bool) {
var tid sql.NullInt64
err := db.QueryRow("SELECT tid FROM keyword WHERE title=?;", title).Scan(&tid)
if err != nil {
return 0, false
}
return int(tid.Int64), true
}
func entryTid(id int) int {
var tid int
_ = db.QueryRow("SELECT tid FROM task WHERE id=?;", id).Scan(&tid)
return tid
}
/*
func keywordName(id int) string {
row := db.QueryRow("SELECT name FROM keyword WHERE id=?;", id)
var name string
err := row.Scan(&name)
if err != nil {
return ""
}
return name
}
*/
func filterTitle(filter string, tid int) string {
row := db.QueryRow(fmt.Sprintf("SELECT title FROM %s WHERE tid=?;", filter), tid)
var title string
err := row.Scan(&title)
if err != nil {
return ""
}
return title
}
func contextExists(title string) (int, bool) {
var tid sql.NullInt64
err := db.QueryRow("SELECT tid FROM context WHERE title=?;", title).Scan(&tid)
if err != nil {
return 0, false
}
return int(tid.Int64), true
}
func folderExists(title string) (int, bool) {
var tid sql.NullInt64
err := db.QueryRow("SELECT tid FROM folder WHERE title=?;", title).Scan(&tid)
if err != nil {
return 0, false
}
return int(tid.Int64), true
}
func contextList() map[string]struct{} {
rows, _ := db.Query("SELECT title FROM context;")
defer rows.Close()
contexts := make(map[string]struct{})
for rows.Next() {
var title string
_ = rows.Scan(&title)
contexts[title] = struct{}{}
}
return contexts
}
func folderList() map[string]struct{} {
rows, _ := db.Query("SELECT title FROM folder;")
defer rows.Close()
folders := make(map[string]struct{})
for rows.Next() {
var title string
_ = rows.Scan(&title)
folders[title] = struct{}{}
}
return folders
}
func keywordList() map[string]struct{} {
rows, _ := db.Query("SELECT title FROM keyword;")
defer rows.Close()
keywords := make(map[string]struct{})
for rows.Next() {
var title string
_ = rows.Scan(&title)
keywords[title] = struct{}{}
}
return keywords
}
func toggleStar() {
id := getId()
s := fmt.Sprintf("UPDATE %s SET star=?, modified=datetime('now') WHERE id=?;",
org.view) //table
_, err := db.Exec(s, !org.rows[org.fr].star, id)
if err != nil {
sess.showOrgMessage("Error in toggleStar for id %d: %v", id, err)
}
org.rows[org.fr].star = !org.rows[org.fr].star
sess.showOrgMessage("Toggle star succeeded")
}
func toggleDeleted() {
id := getId()
s := fmt.Sprintf("UPDATE %s SET deleted=?, modified=datetime('now') WHERE id=?;", org.view)
_, err := db.Exec(s, !org.rows[org.fr].deleted, id)
if err != nil {
sess.showOrgMessage("Error toggling %s id %d to deleted: %v", org.view, id, err)
return
}
org.rows[org.fr].deleted = !org.rows[org.fr].deleted
sess.showOrgMessage("Toggle deleted for %s id %d succeeded", org.view, id)
}
func toggleArchived() {
id := getId()
org.rows[org.fr].archived = !org.rows[org.fr].archived
_, err := db.Exec("UPDATE task SET archived=?, modified=datetime('now') WHERE id=?;",
org.rows[org.fr].archived, id)
if err != nil {
sess.showOrgMessage("Error toggling archived for entry id %d: %v", id, err)
return
}
sess.showOrgMessage("Toggle archived for entry %d succeeded", id)
}
func updateTaskContextByTid(tid, id int) {
_, err := db.Exec("UPDATE task SET context_tid=?, modified=datetime('now') WHERE id=?;",
tid, id)
if err != nil {
sess.showOrgMessage("Error updating context for entry %d to tid %d: %v", id, tid, err)
return
}
}
func updateTaskFolderByTid(tid, id int) {
_, err := db.Exec("UPDATE task SET folder_tid=?, modified=datetime('now') WHERE id=?;",
tid, id)
if err != nil {
sess.showOrgMessage("Error updating folder for entry %d to tid %d: %v", id, tid, err)
return
}
}
func updateNote(id int, text string) {
var nullableText sql.NullString
if len(text) != 0 {
nullableText.String = text
nullableText.Valid = true
}
_, err := db.Exec("UPDATE task SET note=?, modified=datetime('now') WHERE id=?;",
nullableText, id)
if err != nil {
sess.showOrgMessage("Error in updateNote for entry with id %d: %v", id, err)
return
}
/***************fts virtual table update*********************/
entry_tid := entryTidFromId(id) /////////////////////////////////////////////////////
//_, err = fts_db.Exec("UPDATE fts SET note=? WHERE lm_id=?;", text, id)
_, err = fts_db.Exec("UPDATE fts SET note=? WHERE tid=?;", text, entry_tid)
if err != nil {
sess.showOrgMessage("Error in updateNote updating fts for entry with id %d: %v", id, err)
}
sess.showOrgMessage("Updated note and fts entry for entry %d", id)
}
func getSyncItems(max int) {
rows, err := db.Query(fmt.Sprintf("SELECT id, title, %s FROM sync_log ORDER BY %s DESC LIMIT %d", org.sort, org.sort, max))
if err != nil {
sess.showOrgMessage("Error in getSyncItems: %v", err)
return
}
defer rows.Close()
org.rows = nil
for rows.Next() {
var row Row
var sort string
err = rows.Scan(&row.id, &row.title, &sort)
if err != nil {
sess.showOrgMessage("Error in getSyncItems: %v", err)
return
}
row.sort = timeDelta(sort)
org.rows = append(org.rows, row)
}
}
func deleteSyncItem(id int) {
_, err := db.Exec("DELETE FROM sync_log WHERE id=?;", id)
if err != nil {
sess.showOrgMessage("Error deleting sync_log entry with id %d: %v", id, err)
return
}
sess.showOrgMessage("Deleted sync_log entry with id %d", id)
}
func filterEntries(taskView int, filter interface{}, showDeleted bool, sort string, sortPriority bool, max int) []Row {
s := fmt.Sprintf("SELECT task.id, task.title, task.star, task.deleted, task.archived, task.%s FROM task ", sort)
switch taskView {
case BY_CONTEXT:
s += "JOIN context ON context.tid=task.context_tid WHERE context.title=?"
case BY_FOLDER:
s += "JOIN folder ON folder.tid = task.folder_tid WHERE folder.title=?"
case BY_KEYWORD:
s += "JOIN task_keyword ON task.tid=task_keyword.task_tid " +
"JOIN keyword ON keyword.tid=task_keyword.keyword_tid " +
"WHERE task.tid = task_keyword.task_tid AND " +
"task_keyword.keyword_tid = keyword.tid AND keyword.title=?"
case BY_RECENT:
//s += "WHERE 1=1"
s += "WHERE 1=?"
filter = 1
default:
sess.showOrgMessage("You asked for an unsupported db query")
return []Row{}
}
if !showDeleted {
s += " AND task.archived=false AND task.deleted=false"
}
if sortPriority {
s += fmt.Sprintf(" ORDER BY task.star DESC, task.%s DESC LIMIT %d;", sort, max)
} else {
s += fmt.Sprintf(" ORDER BY task.%s DESC LIMIT %d;", sort, max) //01162022
}
var rows *sql.Rows
var err error
/*
if filter == "" { //Recent
rows, err = db.Query(s)
} else {
rows, err = db.Query(s, filter)
}
*/
rows, err = db.Query(s, filter)
if err != nil {
sess.showOrgMessage("Error in getItems: %v", err)
return []Row{}
}
defer rows.Close()
var orgRows []Row
for rows.Next() {
var row Row
var sort sql.NullString
err = rows.Scan(
&row.id,
&row.title,
&row.star,
&row.deleted,
&row.archived,
&sort,
)
if err != nil {
sess.showOrgMessage("Error in filterEntries: %v", err)
return orgRows
}
if sort.Valid {
row.sort = timeDelta(sort.String)
} else {
row.sort = ""
}
orgRows = append(orgRows, row)
}
return orgRows
}
func (o *Organizer) readRowsIntoBuffer() {
var ss []string
for _, row := range o.rows {
ss = append(ss, row.title)
}
vim.BufferSetLines(o.vbuf, 0, -1, ss, len(ss))
vim.BufferSetCurrent(o.vbuf)
}
func updateTitle() {
row := org.rows[org.fr]
if row.id == -1 {
// send pointer to insertRowinDB because updating row struct with new id
err := insertRowInDB(&org.rows[org.fr])
if err != nil {
sess.showOrgMessage("Error inserting into DB: %v", err)
} else {
sess.showOrgMessage("New entry written to db with id: %d", row.id)
}
return
}
_, err := db.Exec("UPDATE task SET title=?, modified=datetime('now') WHERE id=?", row.title, row.id)
if err != nil {
sess.showOrgMessage("Error in updateTitle for id %d: %v", row.id, err)
return
}
/***************fts virtual table update*********************/
entry_tid := entryTidFromId(row.id) /////////////////////////////////////////////////////
//_, err = fts_db.Exec("UPDATE fts SET title=? WHERE lm_id=?;", row.title, row.id)
_, err = fts_db.Exec("UPDATE fts SET title=? WHERE tid=?;", row.title, entry_tid)
if err != nil {
sess.showOrgMessage("Error in updateTitle update fts for id %d: %v", row.id, err)
return
}
}
func updateRows() {
var updated_rows []int
for fr, row := range org.rows {
if !row.dirty {
continue
}
if row.id == -1 {
// send pointer to insertRowinDB because updating row struct with new id
err := insertRowInDB(&org.rows[fr])
if err != nil {
sess.showOrgMessage("Error inserting into DB: %v", err) //could be overwritten
} else {
updated_rows = append(updated_rows, row.id)
}
continue
}
_, err := db.Exec("UPDATE task SET title=?, modified=datetime('now') WHERE id=?", row.title, row.id)
if err != nil {
sess.showOrgMessage("Error in updateRows for id %d: %v", row.id, err)
return
}
row.dirty = false
updated_rows = append(updated_rows, row.id)
}
if len(updated_rows) == 0 {
sess.showOrgMessage("There were no rows to update")
return
}
sess.showOrgMessage("These ids were updated: %v", updated_rows)
}
func insertRowInDB(row *Row) error { // should return err
folder_tid := 1
context_tid := 1
// if org.taskview is BY_KEYWORD or BY_RECENT then new task gets context=1, folder=1
switch org.taskview {
case BY_CONTEXT:
context_tid, _ = contextExists(org.filter)
case BY_FOLDER:
folder_tid, _ = folderExists(org.filter)
}
var id int
err := db.QueryRow("INSERT INTO task (title, folder_tid, context_tid, star, added) "+
"VALUES (?, ?, ?, ?, datetime('now')) RETURNING id;",
row.title, folder_tid, context_tid, row.star).Scan(&id)
if err != nil {
sess.showOrgMessage("Error inserting into DB: %v", err)
//return -1
return err
}
row.id = id
row.dirty = false
return nil
/***************fts virtual table update*********************/
/*
// this is an issue; in current mode can't index in fts until sync and get a tid
entry_tid := entryTidFromId(row.id) /////////////////////////////////////////////////////
_, err = fts_db.Exec("INSERT INTO fts (title, note, tag, tid) VALUES (?, ?, ?, ?);", row.title, "", "", entry_tid)
if err != nil {
sess.showOrgMessage("Error in insertRowInDB inserting into fts for %s: %v", row.title, err)
return row.id
}
sess.showOrgMessage("Successfully inserted new row with id %d and indexed it (new version)", row.id)
//return row.id
return nil
*/
}
func insertSyncEntry(title, note string) {
_, err := db.Exec("INSERT INTO sync_log (title, note, modified) VALUES (?, ?, datetime('now'));",
title, note)
if err != nil {
sess.showOrgMessage("Error inserting sync log into db: %v", err)
} else {
sess.showOrgMessage("Wrote sync log to db")
}
}
func readNoteIntoString(id int) string {
if id == -1 {
return "" // id given to new and unsaved entries
}
row := db.QueryRow("SELECT note FROM task WHERE id=?;", id)
var note sql.NullString
err := row.Scan(¬e)
if err != nil {
sess.showOrgMessage("Error retrieving note for id %d: %v", id, err)
return ""
}
return note.String
}
func readNoteIntoBuffer(e *Editor, id int) {
if id == -1 {
return // id given to new and unsaved entries
}
row := db.QueryRow("SELECT note FROM task WHERE id=?;", id)
var note sql.NullString
err := row.Scan(¬e)
if err != nil {
sess.showOrgMessage("Error opening note for editing: %v", err)
return
}
e.ss = strings.Split(note.String, "\n")
//e.ss = strings.Split(note, "\n")
e.vbuf = vim.BufferNew(0)
vim.BufferSetCurrent(e.vbuf)
vim.BufferSetLines(e.vbuf, 0, -1, e.ss, len(e.ss))
}
func readSyncLogIntoAltRows(id int) {
row := db.QueryRow("SELECT note FROM sync_log WHERE id=?;", id)
var note string
err := row.Scan(¬e)
if err != nil {
return
}
org.altRows = nil
for _, line := range strings.Split(note, "\n") {
var r AltRow
r.title = line
org.altRows = append(org.altRows, r)
}
}
func readSyncLog(id int) string {
row := db.QueryRow("SELECT note FROM sync_log WHERE id=?;", id)
var note string
err := row.Scan(¬e)
if err != nil {
return ""
}
return note
}
func getEntryInfo(id int) NewEntry {
if id == -1 {
return NewEntry{}
}
row := db.QueryRow("SELECT id, tid, title, folder_tid, context_tid, star, added, archived, deleted, modified FROM task WHERE id=?;", id)
var e NewEntry
var tid sql.NullInt64
err := row.Scan(
&e.id,
&tid,
&e.title,
&e.folder_tid,
&e.context_tid,
&e.star,
&e.added,
&e.archived,
&e.deleted,
&e.modified,
)
e.tid = int(tid.Int64)
if err != nil {
sess.showOrgMessage("Error in getEntryInfo for id %d: %v", id, err)
return NewEntry{}
}
return e
}
func taskFolder(id int) string {
//row := db.QueryRow("SELECT folder.title FROM folder JOIN task on task.folder_tid = folder.tid WHERE task.id=?;", id)
// below seems better because where clause is on task
row := db.QueryRow("SELECT folder.title FROM task JOIN folder on task.folder_tid = folder.tid WHERE task.id=?;", id)
var title string
err := row.Scan(&title)
if err != nil {
return ""
}
return title
}
func taskContext(id int) string {
row := db.QueryRow("SELECT context.title FROM task JOIN context on task.context_tid = context.tid WHERE task.id=?;", id)
var title string
err := row.Scan(&title)
if err != nil {
return ""
}
return title
}
/*
func getContextTid(id int) int {
row := db.QueryRow("SELECT context_tid FROM task WHERE id=?;", id)
var tid int
err := row.Scan(&tid)
if err != nil {
return -1
}
return tid
}
*/
func getTitle(id int) string {
row := db.QueryRow("SELECT title FROM task WHERE id=?;", id)
var title string
err := row.Scan(&title)
if err != nil {
return ""
}
return title
}
func getTaskKeywords(id int) string {
entry_tid := entryTidFromId(id) /////////////////////////////////////////////////////
//rows, err := db.Query("SELECT keyword.name FROM task_keyword LEFT OUTER JOIN keyword ON "+
// "keyword.id=task_keyword.keyword_id WHERE task_keyword.task_id=?;", id)
rows, err := db.Query("SELECT keyword.title FROM task_keyword LEFT OUTER JOIN keyword ON "+
"keyword.tid=task_keyword.keyword_tid WHERE task_keyword.task_tid=?;", entry_tid)
if err != nil {
sess.showOrgMessage("Error in getTaskKeywords for entry id %d: %v", id, err)
return ""
}
defer rows.Close()
kk := []string{}
for rows.Next() {
var title string
err = rows.Scan(&title)
kk = append(kk, title)
}
if len(kk) == 0 {
return ""
}
return strings.Join(kk, ",")
}
func getTaskKeywordTids(id int) []int {
entry_tid := entryTidFromId(id) /////////////////////////////////////////////////////
keyword_tids := []int{}
rows, err := db.Query("SELECT keyword_tid FROM task_keyword LEFT OUTER JOIN keyword ON "+
"keyword.tid=task_keyword.keyword_tid WHERE task_keyword.task_tid=?;", entry_tid)
if err != nil {
sess.showOrgMessage("Error in getTaskKeywordIds for entry id %d: %v", id, err)
return keyword_tids
}
defer rows.Close()
for rows.Next() {
var tid int
err = rows.Scan(&tid)
keyword_tids = append(keyword_tids, tid)
}
return keyword_tids
}
func searchEntries(st string, showDeleted, help bool) []Row {
rows, err := fts_db.Query("SELECT tid, highlight(fts, 0, '\x1b[48;5;31m', '\x1b[49m') "+
"FROM fts WHERE fts MATCH ? ORDER BY bm25(fts, 2.0, 1.0, 5.0);", st)
defer rows.Close()
var ftsTids []int
var ftsTitles = make(map[int]string)
for rows.Next() {
var ftsTid int
var ftsTitle string
err = rows.Scan(
&ftsTid,
&ftsTitle,
)
if err != nil {
sess.showOrgMessage("Error trying to retrieve search info from fts_db - term: %s; %v", st, err)
return []Row{}
}
ftsTids = append(ftsTids, ftsTid)
ftsTitles[ftsTid] = ftsTitle
}
if len(ftsTids) == 0 {
return []Row{}
}
// As noted above, if the item is deleted (gone) from the db it's id will not be found if it's still in fts
stmt := fmt.Sprintf("SELECT task.id, task.tid, task.title, task.star, task.deleted, task.archived, task.%s FROM task WHERE ", org.sort)
if help {
stmt += "task.context_tid = 16 and task.tid IN ("
} else {
stmt += "task.tid IN ("
}
max := len(ftsTids) - 1
for i := 0; i < max; i++ {
stmt += strconv.Itoa(ftsTids[i]) + ", "
}
stmt += strconv.Itoa(ftsTids[max]) + ")"
if showDeleted {
stmt += " ORDER BY "
} else {
stmt += " AND task.archived=false AND task.deleted=false ORDER BY "
}
for i := 0; i < max; i++ {
stmt += "task.tid = " + strconv.Itoa(ftsTids[i]) + " DESC, "
}
stmt += "task.tid = " + strconv.Itoa(ftsTids[max]) + " DESC"
rows, err = db.Query(stmt)
if err != nil {
sess.showOrgMessage("Error in Find query %q: %v", stmt[:10], err)
return []Row{}
}
var orgRows []Row
for rows.Next() {
var row Row
var sort string
err = rows.Scan(
&row.id,
&row.tid,
&row.title,
&row.star,
&row.deleted,
&row.archived,
&sort,
)
if err != nil {
sess.showOrgMessage("Error in searchEntries reading rows")
return []Row{}
}
row.sort = timeDelta(sort)
row.ftsTitle = ftsTitles[row.tid]
orgRows = append(orgRows, row)
}
return orgRows
}
func getContainers() {
org.rows = nil
org.sort = "modified" //only time column that all containers have
/*
var table string
var columns string
var orderBy string //only needs to be change for keyword
switch org.view {
case CONTEXT:
table = "context"
columns = "id, title, star, deleted, modified"
orderBy = "title"
case FOLDER:
table = "folder"
columns = "id, title, star, deleted, modified"
orderBy = "title"
case KEYWORD:
table = "keyword"
columns = "id, title, star, deleted, modified"
orderBy = "titltitltitle"
default:
sess.showOrgMessage("Somehow you are in a view I can't handle")
return
}
*/
//stmt := fmt.Sprintf("SELECT %s FROM %s ORDER BY %s COLLATE NOCASE ASC;", columns, table, orderBy)
stmt := fmt.Sprintf("SELECT id, title, star, deleted, modified FROM %s ORDER BY title COLLATE NOCASE ASC;", org.view)
rows, err := db.Query(stmt)
if err != nil {
sess.showOrgMessage("Error SELECTING id, title, star, deleted, modified FROM %s", org.view)
return
}
defer rows.Close()
for rows.Next() {
var r Row
//var modified string
var sort string
rows.Scan(
&r.id,
&r.title,
&r.star,
&r.deleted,
//&modified,
&sort,
)
//r.modified = timeDelta(modified)
r.sort = timeDelta(sort)
org.rows = append(org.rows, r)
}
if len(org.rows) == 0 {
sess.showOrgMessage("No results were returned")
org.mode = NO_ROWS
}
// below should be somewhere else
org.fc, org.fr, org.rowoff = 0, 0, 0
org.filter = ""
}
func getAltContainers() {
org.altRows = nil
/*
var table string
var columns string
var orderBy string //only needs to be change for keyword
switch org.altView {
case CONTEXT:
table = "context"
//columns = "id, title, \"default\""
columns = "id, title, star"
orderBy = "title"
case FOLDER:
table = "folder"
//columns = "id, title, private"
columns = "id, title, star"
orderBy = "title"
case KEYWORD:
table = "keyword"
columns = "id, title, star"
orderBy = "title"
default:
sess.showOrgMessage("Somehow you are in a view I can't handle")
return
}
*/
//stmt := fmt.Sprintf("SELECT %s FROM %s ORDER BY %s COLLATE NOCASE ASC;", columns, table, orderBy)
stmt := fmt.Sprintf("SELECT id, title, star FROM %s ORDER BY title COLLATE NOCASE ASC;", org.altView)
rows, err := db.Query(stmt)
if err != nil {
sess.showOrgMessage("Error SELECTING id, title, star FROM %s", org.altView)
return
}
defer rows.Close()
for rows.Next() {
var r AltRow
rows.Scan(
&r.id,
&r.title,
&r.star,
)
org.altRows = append(org.altRows, r)
}
/*
if len(org.altRows) == 0 {
sess.showOrgMessage("No results were returned")
}
*/
// below should ? be somewhere else
org.altFr = 0
}
func getContainerInfo(id int) Container {
/*
type Container struct {
id int
tid int
title string
star bool
created string
deleted bool
modified string
count int
}
*/
if id == -1 {
return Container{}
}
var countQuery string
switch org.view {
case CONTEXT:
//table = "context"
// Note: the join for context and folder is on the context/folder *tid*
countQuery = "SELECT COUNT(*) FROM task JOIN context ON context.tid = task.context_tid WHERE context.id=?;"
//columns = "id, tid, title, star, created, deleted, modified"
case FOLDER:
//table = "folder"
countQuery = "SELECT COUNT(*) FROM task JOIN folder ON folder.tid = task.folder_tid WHERE folder.id=?;"
//columns = "id, tid, title, star, created, deleted, modified"
case KEYWORD:
//table = "keyword"
//countQuery = "SELECT COUNT(*) FROM task_keyword WHERE keyword_tid=?;"
countQuery = "SELECT COUNT(*) FROM task_keyword WHERE keyword_tid=(SELECT tid FROM keyword WHERE id=?);"
//columns = "id, tid, name, star, deleted, modified"
default:
sess.showOrgMessage("Somehow you are in a view I can't handle")
return Container{}
}
var c Container
row := db.QueryRow(countQuery, id)
err := row.Scan(&c.count)
if err != nil {
sess.showOrgMessage("Error in getContainerInfo: %v", err)
return Container{}
}
//stmt := fmt.Sprintf("SELECT %s FROM %s WHERE id=?;", columns, table)
stmt := fmt.Sprintf("SELECT id, tid, title, star, deleted, modified FROM %s WHERE id=?;", org.view)
row = db.QueryRow(stmt, id)
var tid sql.NullInt64
err = row.Scan(
&c.id,
&tid,
&c.title,
&c.star,
&c.deleted,
&c.modified,
)
c.tid = int(tid.Int64)
if err != nil {
sess.showOrgMessage("Error in getContainerInfo: %v", err)
return Container{}
}
return c
}
func addTaskKeywordByTid(keyword_tid, entry_id int, update_fts bool) {
entry_tid := entryTidFromId(entry_id) /////////////////////////////////////////////////////
_, err := db.Exec("INSERT OR IGNORE INTO task_keyword (task_tid, keyword_tid) VALUES (?, ?);",
entry_tid, keyword_tid)
if err != nil {
sess.showOrgMessage("Error in addTaskKeywordByTid = INSERT or IGNORE INTO task_keyword: %v", err)
return
}
_, err = db.Exec("UPDATE task SET modified = datetime('now') WHERE id=?;", entry_id)
if err != nil {
sess.showOrgMessage("Error in addTaskKeywordByTid - Update task modified: %v", err)
return
}
// *************fts virtual table update**********************
if !update_fts {
return
}
s := getTaskKeywords(entry_id)
//_, err = fts_db.Exec("UPDATE fts SET tag=? WHERE lm_id=?;", s, entry_id)
_, err = fts_db.Exec("UPDATE fts SET tag=? WHERE tid=?;", s, entry_tid)
if err != nil {
sess.showOrgMessage("Error in addTaskKeywordByTid - fts Update: %v", err)
}
}
// not in use but worked
func getNoteSearchPositions__(id int) [][]int {
row := fts_db.QueryRow("SELECT rowid FROM fts WHERE lm_id=?;", id)
var rowid int
err := row.Scan(&rowid)
if err != nil {
return [][]int{}
}
var word_positions [][]int
for i, term := range strings.Split(sess.fts_search_terms, " ") {
word_positions = append(word_positions, []int{})
rows, err := fts_db.Query("SELECT offset FROM fts_v WHERE doc=? AND term=? AND col='note';", rowid, term)
if err != nil {
sess.showOrgMessage("Error in getNoteSearchPositions - 'SELECT offset FROM fts_v': %v", err)
return [][]int{}
}
defer rows.Close()
for rows.Next() {