-
Notifications
You must be signed in to change notification settings - Fork 0
/
dblayer.go
85 lines (68 loc) · 1.7 KB
/
dblayer.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
package db
import (
"fmt"
"log"
"strings"
)
type Data struct {
URL string
Title string
Content string
Links []string
Last_parsed int64
}
func Insert(d []Data) {
if len(d) < 1 {
fmt.Println("DIdn't receive enough data")
return
}
stmt := "insert or replace into documents (url, title, content, links, last_parsed) values %s"
valueStrings := []string{}
valueArgs := []interface{}{}
linkStrings := []string{}
linkArgs := []interface{}{}
for _, v := range d {
valueStrings = append(valueStrings, "(?, ?, ?, ?, ?)")
links := strings.Join(v.Links, ";")
valueArgs = append(valueArgs, v.URL)
valueArgs = append(valueArgs, v.Title)
valueArgs = append(valueArgs, v.Content)
valueArgs = append(valueArgs, links)
valueArgs = append(valueArgs, v.Last_parsed)
for _, v1 := range v.Links {
linkStrings = append(linkStrings, "(?)")
linkArgs = append(linkArgs, v1)
}
}
stmt = fmt.Sprintf(stmt, strings.Join(valueStrings, ","))
// fmt.Println(stmt)
_, err := db.Exec(stmt, valueArgs...)
if err != nil {
log.Fatalf("Couldn't insert into SQLite: %v", err)
}
stmt = "insert or ignore into documents (url) values %s"
stmt = fmt.Sprintf(stmt, strings.Join(linkStrings, ","))
_, err = db.Exec(stmt, linkArgs...)
if err != nil {
log.Fatalf("Couldn't insert into SQLite: %v", err)
}
}
func GetLinks() ([]string, error) {
var links []string
rows, err := db.Query("select url from documents where title is null limit 50")
if err != nil {
return nil, err
}
for rows.Next() {
var link string
err = rows.Scan(&link)
if err != nil {
return nil, err
}
links = append(links, link)
}
if rows.Err() != nil {
return nil, rows.Err()
}
return links, nil
}