-
Notifications
You must be signed in to change notification settings - Fork 1
/
libgrizzly.go
262 lines (243 loc) · 6.49 KB
/
libgrizzly.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
package grizzly
import (
"database/sql"
"fmt"
"log"
"os/user"
"regexp"
"strings"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
_ "github.com/mattn/go-sqlite3"
)
type NoteTag struct {
Id int
Title string
Tags []string
Identifier string
CreationDate time.Time `gorm:"type:timestamp"`
}
type Note struct {
Id int
Title string
Text string
Tags []string
Identifier string
}
func OpenDB() *gorm.DB {
homeDirStr := homeDir() + "/Library/Group Containers/9K33E3U3T4.net.shinyfrog.bear/Application Data/database.sqlite"
db, err := gorm.Open("sqlite3", homeDirStr)
if err != nil {
log.Fatal(err)
}
return db
}
type NoteDuplicate struct {
Id int
Title string
Count int
}
func GetAllNotes(db *gorm.DB, notes *[]Note) {
rows, err := db.Raw(`
select n.Z_PK as id,
n.ZTEXT as text,
n.ZTITLE as title,
n.ZUNIQUEIDENTIFIER as identifier,
group_concat(t.ZTITLE) as tags
from ZSFNOTE as n left join Z_7TAGS as tn on n.Z_PK=tn.Z_7NOTES
left join ZSFNOTETAG as t on tn.Z_14TAGS=t.Z_PK
group by id;`).Rows()
defer rows.Close()
if err != nil {
log.Fatal("Could not process query")
}
for rows.Next() {
var note Note
var text sql.NullString
var tagStr sql.NullString
err = rows.Scan(¬e.Id, &text, ¬e.Title, ¬e.Identifier, &tagStr)
if text.Valid {
note.Text = text.String
}
if tagStr.Valid {
note.Tags = strings.Split(tagStr.String, ",")
}
*notes = append(*notes, note)
if err != nil {
log.Fatal(err)
}
}
}
func GetAllWithTags(db *gorm.DB, notes *[]NoteTag) {
rows, err := db.Raw(`
select n.Z_PK as id,
n.ZTITLE as title,
n.ZUNIQUEIDENTIFIER as identifier,
group_concat(t.ZTITLE) as tags,
n.ZCREATIONDATE
from ZSFNOTE as n left join Z_7TAGS as tn on n.Z_PK=tn.Z_7NOTES
left join ZSFNOTETAG as t on tn.Z_14TAGS=t.Z_PK
where t.ZTITLE is not null
group by id;`).Rows()
defer rows.Close()
if err != nil {
log.Fatal("Could not process query")
}
for rows.Next() {
var note NoteTag
var tagStr sql.NullString
var creationDate sql.NullFloat64
err = rows.Scan(¬e.Id, ¬e.Title, ¬e.Identifier, &tagStr, &creationDate)
if tagStr.Valid {
note.Tags = strings.Split(tagStr.String, ",")
}
if creationDate.Valid {
//note.CreationDate = creationDate.Time
//fmt.Println(creationDate.Time)
seconds, _ := time.ParseDuration(fmt.Sprintf("%fs", creationDate.Float64))
start := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
fmt.Printf("start: %s, seconds: %d, now: %s\n", start, seconds, start.Add(seconds))
note.CreationDate = start.Add(seconds)
}
*notes = append(*notes, note)
if err != nil {
log.Fatal(err)
}
}
}
func GetAllMarked(db *gorm.DB, notes *[]Note) {
db.Raw(`
select n.Z_PK as id,
n.ZTEXT as text,
n.ZTITLE as title,
group_concat(t.ZTITLE) as tags,
n.ZUNIQUEIDENTIFIER as identifier
from ZSFNOTE as n left join Z_7TAGS as tn on n.Z_PK=tn.Z_7NOTES
left join ZSFNOTETAG as t on tn.Z_14TAGS=t.Z_PK
where text like "%::%::%"
group by id;`).Scan(notes)
}
func GetTailWithTags(db *gorm.DB, notes *[]NoteTag, limit int) {
rows, err := db.Raw(`
select n.Z_PK as id,
n.ZTITLE as title,
n.ZUNIQUEIDENTIFIER as identifier,
group_concat(t.ZTITLE) as tags
from ZSFNOTE as n left join Z_7TAGS as tn on n.Z_PK=tn.Z_7NOTES
left join ZSFNOTETAG as t on tn.Z_14TAGS=t.Z_PK
group by id
order by id asc
limit ?;`, limit).Rows()
defer rows.Close()
if err != nil {
log.Fatal("Could not process query")
}
for rows.Next() {
var note NoteTag
var tagStr sql.NullString
err = rows.Scan(¬e.Id, ¬e.Title, ¬e.Identifier, &tagStr)
if tagStr.Valid {
note.Tags = strings.Split(tagStr.String, ",")
}
*notes = append(*notes, note)
if err != nil {
log.Fatal(err)
}
}
}
func GetHeadWithTags(db *gorm.DB, notes *[]NoteTag, limit int) {
rows, err := db.Raw(`
select n.Z_PK as id,
n.ZTITLE as title,
n.ZUNIQUEIDENTIFIER as identifier,
group_concat(t.ZTITLE) as tags
from ZSFNOTE as n left join Z_7TAGS as tn on n.Z_PK=tn.Z_7NOTES
left join ZSFNOTETAG as t on tn.Z_14TAGS=t.Z_PK
group by id
order by id desc
limit ?;`, limit).Rows()
defer rows.Close()
if err != nil {
log.Fatal("Could not process query")
}
for rows.Next() {
var note NoteTag
var tagStr sql.NullString
err = rows.Scan(¬e.Id, ¬e.Title, ¬e.Identifier, &tagStr)
if tagStr.Valid {
note.Tags = strings.Split(tagStr.String, ",")
}
*notes = append(*notes, note)
if err != nil {
log.Fatal(err)
}
}
}
func homeDir() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return usr.HomeDir
}
func GetDuplicates(db *gorm.DB, notes *[]NoteDuplicate) {
db.Raw(`
select Z_PK as id, ZTITLE as title, count(ZTITLE) as count
from ZSFNOTE
group by ZTITLE having ( count > 1 );
`).Scan(notes)
}
func GetUnlinked(db *gorm.DB, unlinked *[]string) {
var allNotes []Note
GetAllNotes(db, &allNotes)
reference := make(map[string][]string)
r, _ := regexp.Compile("\\(bear:\\/\\/x-callback-url\\/open-note?(.*)\\)")
idr, _ := regexp.Compile("bear:\\/\\/x-callback-url\\/open-note\\?id=([^&)]*)?")
for _, note := range allNotes {
reference[note.Identifier] = make([]string, 0)
matches := r.FindAllString(note.Text, -1)
for _, mark := range matches {
idMatches := idr.FindStringSubmatch(mark)
if len(idMatches) >= 2 {
reference[idMatches[1]] = append(reference[idMatches[1]], note.Identifier)
}
}
}
// filter out entries with no backlinks
for note, backlinks := range reference {
if len(backlinks) == 0 {
*unlinked = append(*unlinked, note)
}
}
}
func SearchTitles(db *gorm.DB, partialTitle string, notes *[]NoteTag) {
rows, err := db.Raw(`select n.Z_PK as id,
n.ZTITLE as title,
n.ZUNIQUEIDENTIFIER as identifier,
group_concat(t.ZTITLE) as tags
from ZSFNOTE as n left join Z_7TAGS as tn on n.Z_PK=tn.Z_7NOTES
left join ZSFNOTETAG as t on tn.Z_14TAGS=t.Z_PK
where n.ZTITLE like ?
group by id;`, "%"+partialTitle+"%").Rows()
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var row NoteTag
var tagStr sql.NullString
err = rows.Scan(&row.Id, &row.Title, &row.Identifier, &tagStr)
if tagStr.Valid {
row.Tags = strings.Split(tagStr.String, ",")
}
*notes = append(*notes, row)
if err != nil {
log.Fatal(err)
}
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}