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
|
package main
import (
"database/sql"
"errors"
"log"
"os"
_ "github.com/mattn/go-sqlite3"
)
func initDB(path string) *sql.DB {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
log.Printf("Creating %s...\n", path)
file, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
file.Close()
db, _ := sql.Open("sqlite3", path)
initTables(db)
return db
} else {
db, err := sql.Open("sqlite3", path)
if err != nil {
log.Fatal(err)
}
return db
}
}
func initTables(db *sql.DB) {
q := `CREATE TABLE IF NOT EXISTS urls (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
url VARCHAR(500) NOT NULL,
author_id VARCHAR(18),
guild_id VARCHAR(18),
channel_id VARCHAR(18),
status_code INTEGER
);
CREATE UNIQUE INDEX idx_urls_url ON urls(url);`
s, err := db.Prepare(q)
if err != nil {
log.Fatal(err)
}
defer s.Close()
s.Exec()
}
func addArchived(db *sql.DB, m *Message, status_code int) {
q := `INSERT OR IGNORE INTO urls(url, author_id, guild_id, channel_id, status_code) VALUES (?, ?, ?, ?, ?)`
s, err := db.Prepare(q)
if err != nil {
log.Fatal(err)
}
defer s.Close()
_, err = s.Exec(m.URL, m.Author, m.Guild, m.Channel, status_code)
if err != nil {
log.Fatal(err)
}
}
func isCached(db *sql.DB, url string) (bool, int) {
var status_code int
err := db.QueryRow("SELECT status_code FROM urls WHERE url = ?",
url).Scan(&status_code)
switch {
case err == sql.ErrNoRows:
return false, status_code
case err != nil:
log.Fatal(err)
}
return true, status_code
}
|