-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
db.go
447 lines (399 loc) · 9.41 KB
/
db.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
package db
import (
"bytes"
"database/sql"
"fmt"
"os/user"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"
"github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
)
const (
DbPath = "~/Library/Group Containers/9K33E3U3T4.net.shinyfrog.bear/Application Data/database.sqlite"
TitleKey = "ZTITLE"
TextKey = "ZTEXT"
TagsKey = "TAGS"
NoteIDKey = "ZUNIQUEIDENTIFIER"
RECENT_NOTES = `
SELECT
note.ZUNIQUEIDENTIFIER,
note.ZTITLE,
GROUP_CONCAT(tag.ZTITLE) AS TAGS
FROM
ZSFNOTE note
LEFT JOIN
Z_5TAGS nTag ON note.Z_PK = nTag.Z_5NOTES
LEFT JOIN
ZSFNOTETAG tag ON nTag.Z_13TAGS = tag.Z_PK
WHERE
note.ZARCHIVED = 0
AND note.ZTRASHED = 0
GROUP BY
note.ZUNIQUEIDENTIFIER,
note.ZTITLE
ORDER BY
note.ZMODIFICATIONDATE DESC
LIMIT 25
`
NOTES_BY_QUERY = `
SELECT
note.ZUNIQUEIDENTIFIER,
note.ZTITLE,
GROUP_CONCAT(tag.ZTITLE) AS TAGS
FROM
ZSFNOTE note
LEFT JOIN
Z_5TAGS nTag ON note.Z_PK = nTag.Z_5NOTES
LEFT JOIN
ZSFNOTETAG tag ON nTag.Z_13TAGS = tag.Z_PK
WHERE
note.ZUNIQUEIDENTIFIER IN (
SELECT
note.ZUNIQUEIDENTIFIER
FROM
ZSFNOTE note
LEFT JOIN
ZSFNOTEFILE images ON images.ZNOTE = note.Z_PK
WHERE
note.ZARCHIVED = 0
AND note.ZTRASHED = 0
AND note.ZTEXT IS NOT NULL
AND (
utflower(note.ZTITLE) LIKE utflower('%'||$1||'%') OR
utflower(note.ZTEXT) LIKE utflower('%'||$1||'%') OR
images.ZSEARCHTEXT LIKE utflower('%'||$1||'%')
)
)
GROUP BY
note.ZUNIQUEIDENTIFIER,
note.ZTITLE
ORDER BY
CASE WHEN utflower(note.ZTITLE) LIKE utflower('%'||$1||'%') THEN 0 ELSE 1 END,
note.ZMODIFICATIONDATE DESC
LIMIT 200
`
NOTES_BY_TAGS_AND_QUERY = `
WITH
joined AS (
SELECT
note.ZUNIQUEIDENTIFIER,
note.ZTITLE,
note.ZTEXT,
note.ZMODIFICATIONDATE,
tag.ZTITLE AS TAG_TITLE,
images.ZSEARCHTEXT
FROM
ZSFNOTE note
INNER JOIN
Z_5TAGS nTag ON note.Z_PK = nTag.Z_5NOTES
INNER JOIN
ZSFNOTETAG tag ON nTag.Z_13TAGS = tag.Z_PK
LEFT JOIN
ZSFNOTEFILE images ON images.ZNOTE = note.Z_PK
WHERE
note.ZARCHIVED = 0
AND note.ZTRASHED = 0
AND note.ZTEXT IS NOT NULL
),
hasSearchedTags AS (
{{ .TagIntersection}}
)
SELECT
ZUNIQUEIDENTIFIER,
ZTITLE,
GROUP_CONCAT(DISTINCT TAG_TITLE) AS TAGS
FROM
joined
WHERE
ZUNIQUEIDENTIFIER IN hasSearchedTags
AND (
utflower(ZTITLE) LIKE utflower('%{{ .Text}}%') OR
utflower(ZTEXT) LIKE utflower('%{{ .Text}}%') OR
ZSEARCHTEXT LIKE utflower('%{{ .Text}}%')
)
GROUP BY
ZUNIQUEIDENTIFIER,
ZTITLE
ORDER BY
CASE WHEN utflower(ZTITLE) LIKE utflower('%{{ .Text}}%') THEN 0 ELSE 1 END,
ZMODIFICATIONDATE DESC
LIMIT 200
`
TAGS_BY_TITLE = `
SELECT
DISTINCT t.ZTITLE
FROM
ZSFNOTE n
INNER JOIN
Z_5TAGS nt ON n.Z_PK = nt.Z_5NOTES
INNER JOIN
ZSFNOTETAG t ON nt.Z_13TAGS = t.Z_PK
WHERE
n.ZARCHIVED = 0
AND n.ZTRASHED = 0
AND UTFLOWER(t.ZTITLE) LIKE UTFLOWER('%%%s%%')
ORDER BY
t.ZMODIFICATIONDATE DESC
LIMIT 25
`
NOTE_TITLE_BY_ID = `
SELECT
DISTINCT ZTITLE
FROM
ZSFNOTE
WHERE
ZARCHIVED = 0
AND ZTRASHED = 0
AND ZUNIQUEIDENTIFIER = '%s'
ORDER BY
ZMODIFICATIONDATE DESC
LIMIT 25
`
NOTE_TEXT_BY_ID = `
SELECT
DISTINCT ZTEXT
FROM
ZSFNOTE
WHERE
ZARCHIVED = 0
AND ZTRASHED = 0
AND ZUNIQUEIDENTIFIER = '%s'
ORDER BY
ZMODIFICATIONDATE DESC
LIMIT 25
`
)
type TagQueryArg struct {
Text string
TagIntersection string
}
func TemplateToString(templateStr string, data any) (string, error) {
var buffer bytes.Buffer
t := template.Must(template.New("").Parse(templateStr))
err := t.Execute(&buffer, data)
return buffer.String(), errors.WithStack(err)
}
type Note map[string]string
func Expanduser(path string) string {
usr, _ := user.Current()
dir := usr.HomeDir
if path[:2] == "~/" {
path = filepath.Join(dir, path[2:])
}
return path
}
type LiteDB struct {
db *sql.DB
}
func NewLiteDB(path string) (LiteDB, error) {
sql.Register("sqlite3_custom", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
return conn.RegisterFunc("utflower", utfLower, true)
},
})
db, err := sql.Open("sqlite3_custom", path)
litedb := LiteDB{db}
return litedb, err
}
func utfLower(s string) string {
return strings.ToLower(s)
}
func NewBearDB() (LiteDB, error) {
path := Expanduser(DbPath)
litedb, err := NewLiteDB(path)
return litedb, err
}
func (litedb LiteDB) Query(q string, args ...interface{}) ([]Note, error) {
results := []Note{}
rows, err := litedb.db.Query(q, args...)
if err != nil {
return results, errors.WithStack(err)
}
cols, err := rows.Columns()
if err != nil {
return results, errors.WithStack(err)
}
for rows.Next() {
m := Note{}
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i := range columns {
columnPointers[i] = &columns[i]
}
if err := rows.Scan(columnPointers...); err != nil {
return results, errors.WithStack(err)
}
for i, colName := range cols {
val := columnPointers[i].(*interface{})
s, ok := (*val).(string)
if ok {
m[colName] = s
} else {
m[colName] = ""
}
}
results = append(results, m)
}
err = rows.Close()
if err != nil {
return results, errors.WithStack(err)
}
err = rows.Err()
if err != nil {
return results, errors.WithStack(err)
}
return results, errors.WithStack(err)
}
func escape(s string) string {
return strings.Replace(s, "'", "''", -1)
}
func containsOrderedWords(text string, words []string) bool {
prev := 0
for _, w := range words {
i := strings.Index(text, w)
if i == -1 || i < prev {
return false
}
prev = i
}
return true
}
func containsWords(text string, words []string) bool {
for _, w := range words {
if !strings.Contains(text, w) {
return false
}
}
return true
}
func (litedb LiteDB) queryNotesByTextAndTagConjunction(text, tagIntersection string, tags []string) ([]Note, error) {
tagQueryArg := TagQueryArg{
Text: escape(text),
TagIntersection: tagIntersection,
}
query, err := TemplateToString(NOTES_BY_TAGS_AND_QUERY, tagQueryArg)
if err != nil {
return nil, err
}
return litedb.Query(query)
}
func RemoveTagHashes(tag string) string {
tag = tag[1:]
if strings.HasSuffix(tag, "#") {
tag = tag[:len(tag)-1]
}
return tag
}
func (litedb LiteDB) QueryNotesByTextAndTags(text string, tags []string) ([]Note, error) {
var selectStatements []string
for _, t := range tags {
s := fmt.Sprintf("SELECT ZUNIQUEIDENTIFIER FROM joined WHERE utflower(TAG_TITLE) = utflower('%s')", RemoveTagHashes(t))
selectStatements = append(selectStatements, s)
}
tagIntersection := strings.Join(selectStatements, "\nINTERSECT\n")
wordQuery := func(word string) ([]Note, error) {
return litedb.queryNotesByTextAndTagConjunction(word, tagIntersection, tags)
}
return multiWordQuery(text, wordQuery)
}
func (litedb LiteDB) QueryNotesByText(text string) ([]Note, error) {
wordQuery := func(word string) ([]Note, error) {
word = escape(word)
return litedb.Query(NOTES_BY_QUERY, word)
}
return multiWordQuery(text, wordQuery)
}
func splitSpacesOrQuoted(s string) []string {
r := regexp.MustCompile(`([^\s"']+)|"([^"]*)"`)
matches := r.FindAllStringSubmatch(s, -1)
var split []string
for _, v := range matches {
match := v[1]
if match == "" {
match = v[2]
}
split = append(split, match)
}
return split
}
type noteRecord struct {
note Note
contains bool
containsOrderedWords bool
containsWords bool
originalIndex int
}
func NewNoteRecord(i int, note Note, lowerText string) *noteRecord {
title := strings.ToLower(note[TitleKey])
words := strings.Split(lowerText, " ")
record := noteRecord{
originalIndex: i,
note: note,
contains: strings.Contains(title, lowerText),
containsOrderedWords: containsOrderedWords(title, words),
containsWords: containsWords(title, words),
}
return &record
}
func multiWordQuery(text string, wordQuery func(string) ([]Note, error)) ([]Note, error) {
lowerText := strings.ToLower(text)
words := splitSpacesOrQuoted(lowerText)
var records []*noteRecord
fullMatch := map[string]bool{}
notes, err := wordQuery(lowerText)
if err != nil {
return nil, err
}
for i, note := range notes {
noteId := note[NoteIDKey]
record := NewNoteRecord(i, note, lowerText)
record.originalIndex = i
records = append(records, record)
fullMatch[noteId] = true
}
var multiRecords []*noteRecord
count := map[string]int{}
for _, word := range words {
notes, err := wordQuery(word)
if err != nil {
return nil, err
}
for i, note := range notes {
noteId := note[NoteIDKey]
if count[noteId] == 0 && !fullMatch[noteId] {
record := NewNoteRecord(i, note, lowerText)
record.originalIndex = i
multiRecords = append(multiRecords, record)
}
count[noteId]++
}
}
for _, record := range multiRecords {
if count[record.note[NoteIDKey]] == len(words) || record.containsWords {
records = append(records, record)
}
}
sort.Slice(records, func(i, j int) bool {
iRecord := records[i]
jRecord := records[j]
if iRecord.contains != jRecord.contains {
return iRecord.contains
}
if iRecord.containsOrderedWords != jRecord.containsOrderedWords {
return iRecord.containsOrderedWords
}
if iRecord.containsWords != jRecord.containsWords {
return iRecord.containsWords
}
return iRecord.originalIndex < jRecord.originalIndex
})
var finalRows []Note
for _, noteRecord := range records {
finalRows = append(finalRows, noteRecord.note)
}
return finalRows, nil
}