/
sqlite.go
335 lines (303 loc) · 11.1 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package wife
import (
"strconv"
"sync"
"time"
sql "github.com/FloatTech/sqlite"
)
// BlackListStruct (Example: blacklist_1292581422)
type BlackListStruct struct {
BlackList int64 `db:"blacklist"`
}
// DisabledListStruct (Example: disabled_1292581422)
type DisabledListStruct struct {
DisabledList int64 `db:"disabledlist"`
}
// OrderListStruct (Example: orderlist_1145141919180)
type OrderListStruct struct {
OrderPerson int64 `db:"order"`
TargerPerson int64 `db:"target"`
Time string `db:"time"`
}
// GlobalDataStruct (Example: grouplist_1292581422 )
type GlobalDataStruct struct {
PairKey string `db:"pairkey"`
UserID int64 `db:"userid"`
TargetID int64 `db:"targetid"`
Time string `db:"time"`
}
// PairKeyStruct pairkey is used to check the list.
type PairKeyStruct struct {
PairKey string `db:"pairkey"`
StatusID int64 `db:"statusid"`
}
// ChosenList User Refer To Theirs username and save it by using the quick additon (table: chosenList) be careful that it's global.
type ChosenList struct {
IdentifyID string `db:"id"`
UserID int64 `db:"userid"`
TargetName string `db:"targetname"`
TargetID int64 `db:"targetid"`
}
var (
marryList = &sql.Sqlite{}
marryLocker = sync.Mutex{}
)
func init() {
// init UserDataBase
marryList.DBPath = engine.DataFolder() + "data.db"
err := marryList.Open(time.Hour * 24)
if err != nil {
panic(err)
}
}
/*
SQL HANDLER
*/
// FormatInsertUserGlobalMarryList Format Insert
func FormatInsertUserGlobalMarryList(UserID int64, targetID int64, PairKeyRaw string) *GlobalDataStruct {
return &GlobalDataStruct{PairKey: PairKeyRaw, UserID: UserID, TargetID: targetID, Time: strconv.FormatInt(time.Now().Unix(), 10)}
}
// FormatPairKey Format PairKey
func FormatPairKey(PairKeyRaw string, statusID int64) *PairKeyStruct {
return &PairKeyStruct{PairKey: PairKeyRaw, StatusID: statusID}
}
// FormatBlackList Format BlackList
func FormatBlackList(blockID int64) *BlackListStruct {
return &BlackListStruct{BlackList: blockID}
}
// FormatDisabledList Format DisabledList
func FormatDisabledList(disabledID int64) *DisabledListStruct {
return &DisabledListStruct{DisabledList: disabledID}
}
// FormatOrderList Format OrderList
func FormatOrderList(orderPersonal int64, targetPersonal int64, time string) *OrderListStruct {
return &OrderListStruct{OrderPerson: orderPersonal, TargerPerson: targetPersonal, Time: time}
}
// InsertUserGlobalMarryList no check,use it with caution // Only insert and do nothing.
func InsertUserGlobalMarryList(db *sql.Sqlite, groupID int64, UserID int64, targetID int64, StatusID int64, PairKeyRaw string) error {
marryLocker.Lock()
defer marryLocker.Unlock()
formatList := FormatInsertUserGlobalMarryList(UserID, targetID, PairKeyRaw)
err := db.Insert("grouplist_"+strconv.FormatInt(groupID, 10), formatList)
if err != nil {
_ = db.Create("grouplist_"+strconv.FormatInt(groupID, 10), &GlobalDataStruct{})
_ = db.Insert("grouplist_"+strconv.FormatInt(groupID, 10), formatList)
}
// throw key
err = db.Insert("pairkey_"+strconv.FormatInt(groupID, 10), FormatPairKey(PairKeyRaw, StatusID))
if err != nil {
_ = db.Create("pairkey_"+strconv.FormatInt(groupID, 10), &PairKeyStruct{})
_ = db.Insert("pairkey_"+strconv.FormatInt(groupID, 10), FormatPairKey(PairKeyRaw, StatusID))
}
return err
}
// RemoveUserGlobalMarryList just remove it,it will persist the key and change it to Type4.
func RemoveUserGlobalMarryList(db *sql.Sqlite, pairKey string, groupID int64) bool {
marryLocker.Lock()
defer marryLocker.Unlock()
// first find the key list.
var pairKeyNeed PairKeyStruct
err := db.Find("pairkey_"+strconv.FormatInt(groupID, 10), &pairKeyNeed, "where pairkey is '"+pairKey+"'")
if err != nil {
// cannnot find, don't need to remove.
return false
}
// if found.
getThisKey := pairKeyNeed.PairKey
err = db.Del("pairkey_"+strconv.FormatInt(groupID, 10), "where pairkey is '"+getThisKey+"'")
if err != nil {
// cannnot find, don't need to remove.
return false
}
err = db.Del("grouplist_"+strconv.FormatInt(groupID, 10), "where pairkey is '"+getThisKey+"'")
if err != nil {
// cannnot find, don't need to remove.
return false
}
err = db.Insert("pairkey_"+strconv.FormatInt(groupID, 10), FormatPairKey(pairKey, 4))
return err == nil
// store? || persist this key and check the next Time.
}
// CustomRemoveUserGlobalMarryList just remove it,it will persist the key (referKeyStatus)
func CustomRemoveUserGlobalMarryList(db *sql.Sqlite, pairKey string, groupID int64, statusID int64) bool {
marryLocker.Lock()
defer marryLocker.Unlock()
// first find the key list.
var pairKeyNeed PairKeyStruct
err := db.Find("pairkey_"+strconv.FormatInt(groupID, 10), &pairKeyNeed, "where pairkey is '"+pairKey+"'")
if err != nil {
// cannnot find, don't need to remove.
return false
}
// if found.
getThisKey := pairKeyNeed.PairKey
_ = db.Del("pairkey_"+strconv.FormatInt(groupID, 10), "where pairkey is '"+getThisKey+"'")
_ = db.Del("grouplist_"+strconv.FormatInt(groupID, 10), "where pairkey is '"+getThisKey+"'")
// store? || persist this key and check the next Time.
_ = db.Insert("pairkey_"+strconv.FormatInt(groupID, 10), FormatPairKey(pairKey, statusID))
return err == nil
}
/*
// CheckThisKeyStatus check this key status.
func CheckThisKeyStatus(db *sql.Sqlite, pairKey string, groupID int64) int64 {
marryLocker.Lock()
defer marryLocker.Unlock()
// first find the key list.
var pairKeyNeed PairKeyStruct
err := db.Find("pairkey_"+strconv.FormatInt(groupID, 10), &pairKeyNeed, "where pairkey is "+pairKey)
if err != nil {
// cannnot find, don't need to remove.
return -1
}
return pairKeyNeed.StatusID
}
*/
// AddBlackList add blacklist
func AddBlackList(db *sql.Sqlite, userID int64, targetID int64) error {
marryLocker.Lock()
defer marryLocker.Unlock()
var blackListNeed BlackListStruct
err := db.Find("blacklist_"+strconv.FormatInt(userID, 10), &blackListNeed, "where blacklist is '"+strconv.FormatInt(targetID, 10)+"'")
if err != nil {
// add it, not sure then init this and add.
_ = db.Create("blacklist_"+strconv.FormatInt(userID, 10), &BlackListStruct{})
err = db.Insert("blacklist_"+strconv.FormatInt(userID, 10), FormatBlackList(targetID))
return err
}
// find this so don't do it.
return err
}
// DeleteBlackList delete blacklist.
func DeleteBlackList(db *sql.Sqlite, userID int64, targetID int64) error {
marryLocker.Lock()
defer marryLocker.Unlock()
var blackListNeed BlackListStruct
err := db.Find("blacklist_"+strconv.FormatInt(userID, 10), &blackListNeed, "where blacklist is '"+strconv.FormatInt(targetID, 10)+"'")
if err != nil {
// not init or didn't find.
return err
}
_ = db.Del("blacklist_"+strconv.FormatInt(userID, 10), "where blacklist is '"+strconv.FormatInt(targetID, 10)+"'")
return err
}
// CheckTheBlackListIsExistedToThisPerson check the person is blocked.
func CheckTheBlackListIsExistedToThisPerson(db *sql.Sqlite, userID int64, targetID int64) bool {
marryLocker.Lock()
defer marryLocker.Unlock()
var blackListNeed BlackListStruct
err := db.Find("blacklist_"+strconv.FormatInt(userID, 10), &blackListNeed, "where blacklist is '"+strconv.FormatInt(targetID, 10)+"'")
if err != nil {
return true
}
if blackListNeed.BlackList == targetID {
return false
}
return true
}
// AddDisabledList add disabledList
func AddDisabledList(db *sql.Sqlite, userID int64, groupID int64) error {
marryLocker.Lock()
defer marryLocker.Unlock()
var disabledListNeed DisabledListStruct
err := db.Find("disabled_"+strconv.FormatInt(userID, 10), &disabledListNeed, "where disabledlist is '"+strconv.FormatInt(groupID, 10)+"'")
if err != nil {
// add it, not sure then init this and add.
_ = db.Create("disabled_"+strconv.FormatInt(userID, 10), &DisabledListStruct{})
_ = db.Insert("disabled_"+strconv.FormatInt(userID, 10), FormatDisabledList(groupID))
return err
}
// find this so don't do it.
return err
}
// DeleteDisabledList delete disabledlist.
func DeleteDisabledList(db *sql.Sqlite, userID int64, groupID int64) error {
marryLocker.Lock()
defer marryLocker.Unlock()
var disabledListNeed DisabledListStruct
err := db.Find("disabled_"+strconv.FormatInt(userID, 10), &disabledListNeed, "where disabledlist is '"+strconv.FormatInt(groupID, 10)+"'")
if err != nil {
// not init or didn't find.
return err
}
_ = db.Del("disabled_"+strconv.FormatInt(userID, 10), "where disabledlist is '"+strconv.FormatInt(groupID, 10)+"'")
return err
}
// CheckDisabledListIsExistedInThisGroup Check this group.
func CheckDisabledListIsExistedInThisGroup(db *sql.Sqlite, userID int64, groupID int64) bool {
marryLocker.Lock()
defer marryLocker.Unlock()
var disabledListNeed DisabledListStruct
err := db.Find("disabled_"+strconv.FormatInt(userID, 10), &disabledListNeed, "where disabledlist is '"+strconv.FormatInt(groupID, 10)+"'")
if err != nil {
// not init or didn't find.
return true
}
if disabledListNeed.DisabledList == groupID {
return false
}
return true
}
// AddOrderToList add order.
func AddOrderToList(db *sql.Sqlite, userID int64, targetID int64, time string, groupID int64) error {
marryLocker.Lock()
defer marryLocker.Unlock()
var addOrderListNeed OrderListStruct
err := db.Find("orderlist_"+strconv.FormatInt(groupID, 10), &addOrderListNeed, "where order is '"+strconv.FormatInt(userID, 10)+"'")
if err != nil {
// create and insert.
_ = db.Create("orderlist_"+strconv.FormatInt(groupID, 10), &OrderListStruct{})
_ = db.Insert("orderlist_"+strconv.FormatInt(groupID, 10), FormatOrderList(userID, targetID, time))
return err
}
// find it.
return err
}
// RemoveOrderToList remove it.
func RemoveOrderToList(db *sql.Sqlite, userID int64, groupID int64) error {
marryLocker.Lock()
defer marryLocker.Unlock()
var addOrderListNeed OrderListStruct
err := db.Find("orderlist_"+strconv.FormatInt(groupID, 10), &addOrderListNeed, "where order is '"+strconv.FormatInt(userID, 10)+"'")
if err != nil {
return err
}
err = db.Del("orderlist_"+strconv.FormatInt(groupID, 10), "where order is '"+strconv.FormatInt(userID, 10)+"'")
return err
}
/*
// CheckThisOrderList getList
func CheckThisOrderList(db *sql.Sqlite, userID int64, groupID int64) (OrderUser int64, TargetUSer int64, time string) {
marryLocker.Lock()
defer marryLocker.Unlock()
var addOrderListNeed OrderListStruct
err := db.Find("orderlist_"+strconv.FormatInt(groupID, 10), &addOrderListNeed, "where order is "+strconv.FormatInt(userID, 10))
if err != nil {
return -1, -1, ""
}
OrderUser = addOrderListNeed.OrderPerson
TargetUSer = addOrderListNeed.OrderPerson
time = addOrderListNeed.Time
return OrderUser, TargetUSer, time
}
*/
// GetTheGroupList Get this group.
func GetTheGroupList(gid int64) (list [][2]string, num int) {
marryLocker.Lock()
defer marryLocker.Unlock()
gidStr := strconv.FormatInt(gid, 10)
getNum, _ := marryList.Count("grouplist_" + gidStr)
if getNum == 0 {
return nil, 0
}
var info GlobalDataStruct
list = make([][2]string, 0, num)
_ = marryList.FindFor("grouplist_"+gidStr, &info, "GROUP BY userid", func() error {
getInfoSlice := [2]string{
strconv.FormatInt(info.UserID, 10),
strconv.FormatInt(info.TargetID, 10),
}
list = append(list, getInfoSlice)
return nil
})
return list, getNum
}