-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttrs.go
126 lines (104 loc) · 3.1 KB
/
Attrs.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
117
118
119
120
121
122
123
124
125
126
package main
import (
"database/sql"
"errors"
"github.com/firefox-boxes/boxes-ipc/logging"
_ "github.com/mattn/go-sqlite3"
)
func SetDefaultBox(database *sql.DB) {
statement, _ := database.Prepare("INSERT OR IGNORE INTO metadata (id, data) VALUES ('default-box', '<nobody>')")
_, err := statement.Exec()
if err != nil {
logging.Info(err.Error())
}
}
type AttrDB struct {
db *sql.DB
}
func InitAttrDB(dbPath string) AttrDB {
database, _ := sql.Open("sqlite3", "file:" + dbPath + "?cache=shared&mode=rwc")
statement, _ := database.Prepare("CREATE TABLE IF NOT EXISTS boxes (id TEXT PRIMARY KEY, icon TEXT, name TEXT, exec TEXT)")
_, err := statement.Exec()
if err != nil {
logging.Info(err.Error())
}
statement, _ = database.Prepare("CREATE TABLE IF NOT EXISTS metadata (id TEXT PRIMARY KEY, data TEXT)")
_, err = statement.Exec()
if err != nil {
logging.Info(err.Error())
}
SetDefaultBox(database)
return AttrDB{database}
}
func (attrDB *AttrDB) AddBox(id string, icon string, name string, exec string) {
statement, _ := attrDB.db.Prepare("INSERT INTO boxes (id, icon, name, exec) VALUES (?, ?, ?, ?)")
_, err := statement.Exec(id, icon, name, exec)
if err != nil {
logging.Info(err.Error())
}
}
type Attrs struct {
Id string
Icon string
Name string
Exec string
}
func (attrDB *AttrDB) GetBoxAttrs(id string) (Attrs, error) {
rows, _ := attrDB.db.Query("SELECT id, icon, name, exec FROM boxes WHERE id=?", id)
defer rows.Close()
if rows.Next() {
var a Attrs
rows.Scan(&a.Id, &a.Icon, &a.Name, &a.Exec)
return a, nil
} else {
return Attrs{}, errors.New("id not found")
}
}
func (attrDB *AttrDB) GetAllBoxes() []Attrs {
rows, _ := attrDB.db.Query("SELECT * FROM boxes")
defer rows.Close()
attrsList := make([]Attrs, 0)
for rows.Next() {
var a Attrs
rows.Scan(&a.Id, &a.Icon, &a.Name, &a.Exec)
attrsList = append(attrsList, a)
}
return attrsList
}
func (attrDB *AttrDB) SetBoxAttrs(id, icon, name, exec string) {
statement, _ := attrDB.db.Prepare("UPDATE boxes SET icon=?, name=?, exec=? WHERE id=?")
statement.Exec(icon, name, exec, id)
}
func (attrDB *AttrDB) SetIcon(id string, icon string) {
statement, _ := attrDB.db.Prepare("UPDATE boxes SET icon=? WHERE id=?")
statement.Exec(icon, id)
}
func (attrDB *AttrDB) SetName(id string, name string) {
statement, _ := attrDB.db.Prepare("UPDATE boxes SET name=? WHERE id=?")
statement.Exec(name, id)
}
func (attrDB *AttrDB) SetExec(id string, exec string) {
statement, _ := attrDB.db.Prepare("UPDATE boxes SET exec=? WHERE id=?")
statement.Exec(exec, id)
}
func (attrDB *AttrDB) DeleteBox(id string) {
statement, _ := attrDB.db.Prepare("DELETE FROM boxes WHERE id=?")
statement.Exec(id)
}
func (attrDB *AttrDB) GetDefault() string {
rows, _ := attrDB.db.Query("SELECT data FROM metadata WHERE id='default-box'")
if (rows.Next()) {
data := ""
rows.Scan(&data)
rows.Close()
return data
}
return "<nobody>"
}
func (attrDB *AttrDB) SetDefault(data string) {
statement, _ := attrDB.db.Prepare("UPDATE metadata SET data=? WHERE id='default-box'")
statement.Exec(data)
}
func (attrDB *AttrDB) Close() {
attrDB.db.Close()
}