-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.go
160 lines (144 loc) · 3.83 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package onlineconfbot
import (
"context"
"database/sql"
"net"
"strings"
"github.com/go-sql-driver/mysql"
)
type SubscriptionStorage interface {
Subscribe(context.Context, string, bool) error
Unsubscribe(context.Context, string) error
Subscribers(context.Context) ([]Subscription, error)
}
type database struct {
*sql.DB
}
type transaction struct {
*sql.Tx
}
func openDatabase() (*database, error) {
defaultName := strings.ReplaceAll(commandName, "-", "_")
mysqlConfig := mysql.NewConfig()
mysqlConfig.User = config.GetString("/database/user", defaultName)
mysqlConfig.Passwd = config.GetString("/database/pass", "")
mysqlConfig.Net = "tcp"
mysqlConfig.Addr = net.JoinHostPort(config.GetString("/database/host", ""), config.GetString("/database/port", "3306"))
mysqlConfig.DBName = config.GetString("/database/base", defaultName)
mysqlConfig.Params = map[string]string{
"charset": "utf8mb4",
"collation": "utf8mb4_general_ci",
}
db, err := sql.Open("mysql", mysqlConfig.FormatDSN())
if err != nil {
return nil, err
}
return &database{db}, nil
}
func (db *database) BeginTx(ctx context.Context) (*transaction, error) {
tx, err := db.DB.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
return &transaction{tx}, nil
}
func (tx *transaction) GetLastID(ctx context.Context) (int, error) {
row := tx.QueryRowContext(ctx, "SELECT Value FROM lastid WHERE ID = 0 FOR UPDATE")
var lastID int
err := row.Scan(&lastID)
if err != nil {
return 0, err
}
return lastID, nil
}
func (tx *transaction) SetLastID(ctx context.Context, lastID int) error {
_, err := tx.ExecContext(ctx, "UPDATE lastid SET Value = ? WHERE ID = 0", lastID)
return err
}
func (db *database) Subscribe(ctx context.Context, user string, wo bool) error {
_, err := db.ExecContext(ctx, "INSERT INTO subscribe (User, WO) VALUES (?, ?) ON DUPLICATE KEY UPDATE WO=VALUES(WO)", user, wo)
return err
}
func (db *database) Unsubscribe(ctx context.Context, user string) error {
_, err := db.ExecContext(ctx, "DELETE FROM subscribe WHERE User = ?", user)
return err
}
type Subscription struct {
User string
WO bool
}
func (db *database) Subscribers(ctx context.Context) ([]Subscription, error) {
rows, err := db.QueryContext(ctx, "SELECT User, WO FROM subscribe ORDER BY User")
if err != nil {
return nil, err
}
defer rows.Close()
subscriptions := []Subscription{}
for rows.Next() {
var subscription Subscription
err := rows.Scan(&subscription.User, &subscription.WO)
if err != nil {
return nil, err
}
subscriptions = append(subscriptions, subscription)
}
return subscriptions, nil
}
func (db *database) FilterSubscribed(ctx context.Context, users map[string]string) ([]string, error) {
write := []string{}
read := []string{}
for user, access := range users {
switch access {
case "rw":
write = append(write, user)
case "ro":
read = append(read, user)
}
}
query := strings.Builder{}
query.WriteString("SELECT User FROM subscribe WHERE ")
bind := []interface{}{}
if len(write) > 0 {
query.WriteString("User IN (")
for i, user := range write {
query.WriteString("?")
if i+1 != len(write) {
query.WriteString(", ")
}
bind = append(bind, user)
}
query.WriteString(")")
}
if len(read) > 0 {
if len(bind) > 0 {
query.WriteString(" OR ")
}
query.WriteString("(User IN (")
for i, user := range read {
query.WriteString("?")
if i+1 != len(read) {
query.WriteString(", ")
}
bind = append(bind, user)
}
query.WriteString(") AND NOT WO)")
}
if query.Len() == 0 {
return []string{}, nil
}
rows, err := db.QueryContext(ctx, query.String(), bind...)
if err != nil {
return nil, err
}
defer rows.Close()
notifyUsers := []string{}
for rows.Next() {
var user string
err := rows.Scan(&user)
if err != nil {
return nil, err
}
notifyUsers = append(notifyUsers, user)
}
return notifyUsers, nil
}