-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.go
68 lines (58 loc) · 1.26 KB
/
database.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
package main
import (
"database/sql"
"fmt"
"os"
)
const setupStatement string = `
CREATE TABLE IF NOT EXISTS articles (
id TEXT NOT NULL UNIQUE,
content TEXT,
modified TEXT NOT NULL,
title TEXT NOT NULL,
uri TEXT NOT NULL
);
CREATE VIRTUAL TABLE articles_fts USING fts5(
id,
content,
modified,
title,
uri,
content="articles"
);
CREATE TRIGGER fts_update AFTER INSERT ON articles
BEGIN
INSERT INTO articles_fts (
id,
content,
modified,
title,
uri
)
VALUES (
new.id,
new.content,
new.modified,
new.title,
new.uri
);
END;
`
// Set up the database and schema. Assumed that the output folder exists.
func makeDatabase(config *BockConfig) *sql.DB {
dbPath := config.outputFolder + "/" + DATABASE_NAME
fmt.Println("Creating database", dbPath)
// Recreate the database from scratch. TODO: Do this intelligently.
os.Remove(dbPath)
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
fmt.Println("ERROR: Could not open", dbPath, ":", err)
os.Exit(EXIT_DATABASE_ERROR)
}
_, err = db.Exec(setupStatement)
if err != nil {
fmt.Printf("ERROR: Could not set up database: %q\n", err)
os.Exit(EXIT_DATABASE_ERROR)
}
return db
}