-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
177 lines (160 loc) · 4.84 KB
/
sql.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package sql
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/jmoiron/sqlx"
"github.com/mattn/go-sqlite3"
)
type Db struct {
db *sqlx.DB
}
func NewDb(filename string) (*Db, error) {
db, err := sqlx.Open("sqlite3", filename+"?_txlock=IMMEDIATE&_journal_mode=WAL")
if err != nil {
return nil, fmt.Errorf("NewDb: %w", err)
}
d := new(Db)
d.db = db
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS danmaku(
danmaku_id INTEGER PRIMARY KEY AUTOINCREMENT,
send_mode INT NOT NULL,
send_font_size INT NOT NULL,
danmaku_color INT NOT NULL,
time INT NOT NULL,
dmid INT NOT NULL,
msg_type INT NOT NULL,
bubble TEXT NOT NULL,
content TEXT NOT NULL,
mid INT NOT NULL,
uname TEXT NOT NULL,
room_admin INT NOT NULL,
vip INT NOT NULL,
svip INT NOT NULL,
rank INT NOT NULL,
mobile_verify INT NOT NULL,
uname_color TEXT NOT NULL,
medal_name TEXT NOT NULL,
up_name TEXT NOT NULL,
medal_level INT NOT NULL,
user_level INT NOT NULL
);
CREATE TABLE IF NOT EXISTS hot(
time INT NOT NULL,
hot INT,
watched INT
);
CREATE TABLE IF NOT EXISTS gift(
id INTEGER PRIMARY KEY AUTOINCREMENT,
uname TEXT NOT NULL,
uid INT NOT NULL,
gift_name TEXT NOT NULL,
gift_num INT NOT NULL,
gift_price INT NOT NULL,
gift_id INT NOT NULL,
action TEXT NOT NULL,
time INT NOT NULL
);
CREATE TABLE IF NOT EXISTS sc(
sc_id INTEGER PRIMARY KEY AUTOINCREMENT,
id INT NOT NULL,
uname TEXT NOT NULL,
uid INT NOT NULL,
time INT NOT NULL,
start_time INT NOT NULL,
message TEXT NOT NULL,
price INT NOT NULL
);
CREATE TABLE IF NOT EXISTS delsc(
delsc_id INTEGER PRIMARY KEY AUTOINCREMENT,
id INT NOT NULL,
time INT NOT NULL
);
CREATE TABLE IF NOT EXISTS viewer(
viewer_id INTEGER PRIMARY KEY AUTOINCREMENT,
UID INT NOT NULL,
Uname TEXT NOT NULL,
time INT NOT NULL,
Score INT NOT NULL,
Dmscore INT NOT NULL,
MedalLevel INT NOT NULL,
MedalName TEXT NOT NULL,
TargetId INT NOT NULL
);
`)
if err != nil {
return nil, fmt.Errorf("NewDb: %w", err)
}
return d, nil
}
func (db *Db) Close() error {
return db.db.Close()
}
func (db *Db) InsertDanmaku(ctx context.Context, t *Danmaku) error {
err := insert(ctx, t, db, `INSERT INTO "danmaku" ("send_mode", "send_font_size", "danmaku_color", "time", "dmid", "msg_type", "bubble", "content", "mid", "uname", "room_admin", "vip", "svip", "rank", "mobile_verify", "uname_color", "medal_name", "up_name", "medal_level", "user_level") VALUES (:send_mode, :send_font_size, :danmaku_color, :time, :dmid, :msg_type, :bubble, :content, :mid, :uname, :room_admin, :vip, :svip, :rank, :mobile_verify, :uname_color, :medal_name, :up_name, :medal_level, :user_level);`)
if err != nil {
return fmt.Errorf("db.InsertDanmaku: %w", err)
}
return nil
}
func (db *Db) InsertHot(ctx context.Context, hot, watched int64) error {
h := Hot{
Time: time.Now().Unix(),
Hot: hot,
Watched: watched,
}
err := insert(ctx, &h, db, `INSERT INTO "hot" ("time", "hot", "watched") VALUES (:time, :hot, :watched);`)
if err != nil {
return fmt.Errorf("db.InsertHot: %w", err)
}
return nil
}
func (db *Db) InsertGift(ctx context.Context, gift *Gift) error {
err := insert(ctx, gift, db, `INSERT INTO gift ("uname", "uid", "gift_name", "gift_num", "gift_price", "time", "action", "gift_id") VALUES (:uname, :uid, :gift_name, :gift_num, :gift_price, :time, :action, :gift_id);`)
if err != nil {
return fmt.Errorf("db.InsertHot: %w", err)
}
return nil
}
func (db *Db) InsertSC(ctx context.Context, sc *Sc) error {
err := insert(ctx, sc, db, `INSERT INTO sc ("id", "uname", "uid", "time", "start_time", "message", "price") VALUES (:id, :uname, :uid, :time, :start_time, :message, :price);`)
if err != nil {
return fmt.Errorf("db.InsertHot: %w", err)
}
return nil
}
func (db *Db) InsertDelSC(ctx context.Context, id int64) error {
err := insert(ctx, map[string]interface{}{"id": id, "time": time.Now().Unix()}, db, `INSERT INTO delsc ("id", "time") VALUES (:id, :time);`)
if err != nil {
return fmt.Errorf("db.InsertHot: %w", err)
}
return nil
}
func (db *Db) InsertViewer(ctx context.Context, viewer *Viewer) error {
err := insert(ctx, viewer, db, `INSERT INTO viewer ("UID", "Uname", "time", "Score", "Dmscore", "MedalLevel", "MedalName", "TargetId") VALUES (:uid, :uname, :time, :score, :dmscore, :medallevel, :medalname, :targetid);`)
if err != nil {
return fmt.Errorf("db.InsertViewer: %w", err)
}
return nil
}
func insert[D any](ctx context.Context, d D, db *Db, query string) error {
_, err := db.db.NamedExecContext(ctx, query, d)
if err != nil {
e := sqlite3.Error{}
if errors.As(err, &e) {
if e.Code == sqlite3.ErrConstraint {
log.Println(err)
return nil
}
if e.Code == sqlite3.ErrBusy || e.Code == sqlite3.ErrLocked {
log.Println(err)
time.Sleep(1 * time.Second)
return insert(ctx, d, db, query)
}
}
return fmt.Errorf("insert: %w", err)
}
return nil
}