-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.go
106 lines (84 loc) · 1.98 KB
/
sqlite.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
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"go.uber.org/zap"
"sync"
)
type SQLite struct {
//Address string
log *zap.SugaredLogger
db *sql.DB
sync.Mutex
}
func (s *SQLite) Init(logger *zap.SugaredLogger) (err error) {
s.log = logger.Named("sqlite")
log := s.log.Named("Init")
log.Infow("starting")
//s.Address = "file:ndi-router.db?_mutex=full"
// create db file
s.Open()
_, err = s.db.Exec(`create table if not exists matrix ("id" integer, "output" string, "input" string, primary key (id))`)
if err != nil {
log.Errorw("db.Exec(create table)", "err", err)
return err
}
return nil
}
func (s *SQLite) Open() {
log := s.log.Named("Open")
var err error
s.db, err = sql.Open("sqlite3", conf.SqliteDsn)
if err != nil {
log.Errorw("sql.Open", "err", err)
}
}
func (s *SQLite) Close() {
log := s.log.Named("Close")
err := s.db.Close()
if err != nil {
log.Errorw("sql.Close", "err", err)
}
}
func (s *SQLite) UpdateMatrix(output string, input string) {
log := s.log.Named("UpdateMatrix()")
// delete old config
del, err := s.db.Prepare("delete from matrix where output = ?")
_, err = del.Exec(output)
if err != nil {
log.Errorw("del", "err", err)
return
}
// save new config
insert, err := s.db.Prepare("insert into matrix (output, input) values (?, ?)")
res, err := insert.Exec(output, input)
if err != nil {
log.Errorw("insert", "err", err)
return
}
n, err := res.RowsAffected()
if err != nil {
log.Errorw("res.RowsAffected", "err", err)
return
}
log.Infow("saved", "rows", n)
}
func (s *SQLite) GetMatrix(output string) (input string) {
log := s.log.Named("GetMatrix()")
query, err := s.db.Prepare("select input from matrix where output = ?")
rows, err := query.Query(output)
if err != nil {
log.Errorw("select", "err", err)
return ""
}
defer rows.Close()
for rows.Next() {
err = rows.Scan(&input)
if err != nil {
log.Errorw("can't Scan", "err", err)
continue
}
return input
}
return ""
}