-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
116 lines (102 loc) · 2.33 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
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
package gists
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"os"
"path/filepath"
"strings"
)
// CreateDatabase reads gists from a channel and writes them
// to an SQLite3 database.
func CreateDatabase(filename string) error {
// Delete the output database file, if it exists
if FileExists(filename) {
os.Remove(filename)
}
// Open the database for output
db, err := sql.Open("sqlite3", filename)
if err != nil {
return err
}
defer db.Close()
// Begin a transaction
tx, err := db.Begin()
if err != nil {
return err
}
createTablesSQL := `
CREATE TABLE gists (
id TEXT,
url TEXT,
description TEXT,
created_at TEXT
);
CREATE TABLE gistfiles (
id TEXT,
filename TEXT,
language TEXT,
size TEXT,
contents BLOB
);
CREATE VIEW joined AS
SELECT g.id, f.filename, f.language, g.description, f.contents
FROM gistfiles f
LEFT JOIN gists g
ON f.id = g.id
ORDER BY 4
;
`
// Create the tables
_, err = tx.Exec(createTablesSQL)
if err != nil {
return err
}
// Commit the transaction
err = tx.Commit()
if err != nil {
return err
}
return nil
}
// FileExists returns true if the specified file exists
func FileExists(filename string) bool {
if strings.HasPrefix(filename, "~/") {
simpleName := strings.TrimPrefix(filename, "~/")
dirname, _ := os.UserHomeDir()
filename = filepath.Join(dirname, simpleName)
}
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}
// LoadDatabase writes a gist to the database
func LoadDatabase(db *sql.DB, gist Gist) error {
// Begin a transaction
tx, err := db.Begin()
if err != nil {
return err
}
// Write gists to tables
insertSQL := `INSERT INTO gists (id, url, description, created_at) VALUES(?, ?, ?, ?)`
_, err = tx.Exec(insertSQL, gist.ID, gist.URL, gist.Description, gist.CreatedAt)
if err != nil {
return err
}
for _, file := range gist.Files {
blob, err := file.GetContents()
if err != nil {
return err
}
insertSQL = `INSERT INTO gistfiles (id, filename, language, size, contents) VALUES(?, ?, ?, ?, ?)`
_, err = tx.Exec(insertSQL, gist.ID, file.Filename, file.Language, file.Size, blob)
if err != nil {
return err
}
}
// Commit the transaction
err = tx.Commit()
if err != nil {
return err
}
// No error
return nil
}