-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
285 lines (250 loc) · 6.52 KB
/
helpers.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
package main
import (
"bufio"
"database/sql"
"fmt"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
func getDefaultDBPath() string {
path, err := os.Getwd()
check(err)
return filepath.Join(path, "orunmila.db")
}
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
// Convert tags' ids from the db into a comma separated string
func TagsToIdsInString() string {
var a []string
for _, id := range Tags {
log.Debugf("[TagsToIdsInString] id => %d\n", id)
a = append(a, fmt.Sprint(id))
}
return strings.Join(a, ",")
}
// Gets the ID of a given tag
func getTagId(db *sql.DB, tag string) int64 {
var id int64
err := db.QueryRow("select id from tags where name = ?", tag).Scan(&id)
if err != nil {
id = -1
}
return id
}
// Gets the ID of a given word
func getWordId(db *sql.DB, word string) int64 {
var id int64
err := db.QueryRow("select id from words where name = ?", word).Scan(&id)
if err != nil {
id = -1
}
return id
}
// Creates the database schema
func createDB(dbname string) error {
db, err := sql.Open("sqlite3", dbname)
check(err)
defer db.Close()
sqlStmt := `
create table IF NOT EXISTS words (id integer not null primary key AUTOINCREMENT, name text NOT NULL UNIQUE);
create table IF NOT EXISTS tags (id integer not null primary key AUTOINCREMENT, name text NOT NULL UNIQUE);
create table IF NOT EXISTS wt (word_id integer not null , tag_id integer not null, FOREIGN KEY(word_id) REFERENCES words(id),FOREIGN KEY(tag_id) REFERENCES tags(id),PRIMARY KEY(word_id,tag_id));
create table IF NOT EXISTS sysconfig(name text not null primary key, val text);
insert or ignore into sysconfig(name,val) values ("version","0.0.0"),("dbname","default");
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return err
}
return nil
}
// Split a string into a HASH map of the form Array[word]=-1
func stringToArray(inString string) map[string]int64 {
// explode tags by comma
var wordsArray = strings.Split(inString, ",")
var _tMap = make(map[string]int64)
// loop through unique items
for _, s := range wordsArray {
s = strings.TrimSpace(s)
if s != "" && _tMap[s] != -1 {
_tMap[s] = -1
}
}
return _tMap
}
// Populate Tags map array with their corresponding id
// Tags[tag_name]=tag_id
func populateTagIds(db *sql.DB) {
for tag, id := range Tags {
if id <= 0 {
// fetch tag id from database if exists and assign it
Tags[tag] = getTagId(db, tag)
}
}
}
// Removes tags that have not been assigned an ID
// Tags[tag_name]=tag_id
func removeEmptyTags() {
for tag, id := range Tags {
if id <= 0 {
log.Debugln("Removing not found tag:", tag)
delete(Tags, tag)
}
}
}
// Import the tags into the database and populate Tags with tag_id
func importTags(db *sql.DB) {
tx, err := db.Begin()
check(err)
tagsStmt, err := tx.Prepare("insert or ignore into tags(name) values(?)")
check(err)
defer tagsStmt.Close()
for tag, id := range Tags {
if id <= 0 {
id = getTagId(db, tag)
}
if id <= 0 {
result, err := tagsStmt.Exec(tag)
check(err)
id, _ = result.LastInsertId()
}
Tags[tag] = id
log.Println("Found tag id:", id)
}
err = tx.Commit()
check(err)
}
// Import the words from a given filename into the database
func importFileWords(db *sql.DB, filename string) {
importTags(db)
log.Println(Tags)
file, err := os.Open(filename)
check(err)
defer file.Close()
tx, err := db.Begin()
check(err)
wordsStmt, err := tx.Prepare("insert or ignore into words(name) values(?)")
check(err)
defer wordsStmt.Close()
wtStmt, err := tx.Prepare("insert or ignore into wt(word_id,tag_id) values(?,?)")
check(err)
defer wtStmt.Close()
scanner := bufio.NewScanner(file)
var lines = 0
for scanner.Scan() {
word := strings.TrimSpace(scanner.Text())
var word_id int64
if word != "" {
log.Debugln("importing word:", word)
if word_id = getWordId(db, word); word_id <= 0 {
result, err := wordsStmt.Exec(word)
check(err)
word_id, err = result.LastInsertId()
check(err)
if word_id <= 0 {
log.Debugf("word %s already exists, fetching", word)
word_id = getWordId(db, word)
log.Println("Found word id:", word_id)
}
}
//
log.Debugf("word: %s => id: %d\n", word, word_id)
for tag, tag_id := range Tags {
log.Debugf("adding wt(%d,%d) // %s %s", word_id, tag_id, word, tag)
_, err = wtStmt.Exec(word_id, tag_id)
if err != nil {
log.Error(err)
}
}
word_id = 0
}
if lines%4000 == 0 {
log.Info("Lines:", lines)
err = tx.Commit()
check(err)
tx, err = db.Begin()
check(err)
wordsStmt, err = tx.Prepare("insert or ignore into words(name) values(?)")
check(err)
defer wordsStmt.Close()
wtStmt, err = tx.Prepare("insert or ignore into wt(word_id,tag_id) values(?,?)")
check(err)
defer wtStmt.Close()
}
lines++
}
err = tx.Commit()
check(err)
}
// Search for the needle in the haystack
func containsValue(haystack map[string]int64, needle string) bool {
for tagname := range haystack {
if tagname == needle {
return true
}
}
return false
}
//
// Search for words matching tags
//
func searchWordsByTagIds(db *sql.DB, tags string, showTags bool) {
queryStr := `select t1.name,(select group_concat(name,',') from tags where id in (select tag_id from wt where word_id=t1.id)) as tagged from words as t1`
populateTagIds(db)
removeEmptyTags()
if len(tags) > 0 {
userTags := strings.Split(tags, ",")
log.Infoln("Using tags:", userTags)
for _, CurrentUserTag := range userTags {
if !containsValue(Tags, CurrentUserTag) {
log.Warnf("tag %q not found in the db\n", CurrentUserTag)
}
}
queryStr = fmt.Sprintf(queryStr+` left join wt as t2 on t2.word_id=t1.id WHERE t2.tag_id IN (%s) group by t1.id`, TagsToIdsInString())
} else {
log.Infoln("No tags were given")
}
rows, err := db.Query(queryStr)
check(err)
defer rows.Close()
for rows.Next() {
var name string
var tags string
err = rows.Scan(&name, &tags)
if err != nil {
log.Fatal(err)
}
if showTags {
fmt.Println(name, tags)
} else {
fmt.Println(name)
}
}
err = rows.Err()
check(err)
}
// check if file exists
func isFileExists(filename string) bool {
info, err := os.Stat(filename)
if err == nil && !info.Mode().IsRegular() {
log.Fatalf("%q is not a regular file", filename)
} else if os.IsNotExist(err) {
return false
} else if err != nil {
log.Fatal(err)
}
return true
}
// create the db file if it doesn't exists
func createDbFileifNotExists(dbPtr string) {
if !isFileExists(dbPtr) {
log.Debugln("database does not exist, creating...")
createDB(dbPtr)
}
}