-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
63 lines (51 loc) · 1.31 KB
/
db.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
// Responsible for interacting with the SQLite3 'data.db'.
//
// Does not follow ORM style. Contains just optimized methods for fast interaction with database.
package db
import (
"database/sql"
"log"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
"github.com/tofa-project/client-daemon/glob"
)
// json map type (map[string]interface{})
type j = glob.J
// One instance to rule them all
var Instance *sql.DB
// Initializes the database system
func Init() {
log.Print("Loading db... ")
d, err := sql.Open("sqlite3", filepath.FromSlash(glob.V_DATA_DIR+glob.C_DB_DIR))
if err != nil {
panic(err)
}
Instance = d
// Checks for default tables to be present
r := Query(` SELECT name FROM sqlite_master WHERE type='table' AND name='apps'; `)
if !r.Next() {
makeAppsTable()
}
r.Close()
r = Query(` SELECT name FROM sqlite_master WHERE type='table' AND name='logs'; `)
if !r.Next() {
makeLogsTable()
}
r.Close()
log.Print("OK\n")
}
// Default query method. Returns only the rows. Handles errors automatically
func Query(q string, args ...interface{}) *sql.Rows {
r, err := Instance.Query(q, args...)
if err != nil {
panic(err)
}
return r
}
// Default exec method. Handles errors automatically
func Exec(q string, args ...interface{}) {
_, err := Instance.Exec(q, args...)
if err != nil {
panic(err)
}
}