This commit is contained in:
shoopea 2020-02-05 11:05:34 +08:00
parent 585cb289a9
commit 5ae46dca5f
2 changed files with 44 additions and 3 deletions

26
obj.go
View File

@ -343,6 +343,11 @@ func addObjMsg(m ChatWarsMessage) (int64, error) {
}
func getObjMsg(objId int64) (*ChatWarsMessage, error) {
var (
name string
data []byte
)
if v, ok := cacheObjMsg.Load(objId); ok {
m := v.(ChatWarsMessage)
return &m, nil
@ -357,6 +362,7 @@ func getObjMsg(objId int64) (*ChatWarsMessage, error) {
defer stmt.Close()
m = new(ChatWarsMessage)
m.Callbacks = []ChatWarsCallback{}
err = stmt.QueryRow(objId).Scan(&m.ID64, &m.ChatID64, &m.TGUserID64, &m.TGSenderUserID64, &m.Date, &m.Text)
if err != nil {
@ -365,6 +371,24 @@ func getObjMsg(objId int64) (*ChatWarsMessage, error) {
m.ObjID64 = objId
stmt2, err := db.Prepare(`SELECT omc.name, omc.data FROM obj_msg_callback omv WHERE omc.obj_id = ?`)
if err != nil {
return m, err
}
defer stmt2.Close()
rows, err := stmt2.Query(objId)
logOnError(err, "getObjMsg : Query callbacks")
for rows.Next() {
err = rows.Scan(&name, &data)
logOnError(err, "getObjMsg : scan next val")
cb := ChatWarsCallback{
Name: name,
Data: data,
}
m.Callbacks = append(m.Callbacks, cb)
}
cacheObjMsg.Store(objId, *m)
return m, nil
@ -814,7 +838,7 @@ func loadObjJob() error {
cacheObjJob = make(map[int64]Job)
muxObjJob.Unlock()
jobs, err := db.Query(`SELECT o.id, o.obj_sub_type_id, oj.trigger_id, oj.timeout, oj.user_id, oj.zipped, oj.payload FROM obj o, obj_job oj WHERE o.id = oj.obj_id;;`)
jobs, err := db.Query(`SELECT o.id, o.obj_sub_type_id, oj.trigger_id, oj.timeout, oj.user_id, oj.zipped, oj.payload FROM obj o, obj_job oj WHERE o.id = oj.obj_id;`)
if err != nil {
return err
}

21
sql.go
View File

@ -541,6 +541,24 @@ func initDBViews() {
failOnError(err, "initDBViews : create view obj_msg_vault_v")
log.Println("initDBViews : obj_msg_vault_v created ...")
_, err = db.Exec(`CREATE VIEW obj_msg_callback_v AS
SELECT om.obj_id
,o.obj_sub_type_id AS msg_type_id
,cost.intl_id COLLATE utf8mb4_unicode_ci AS msg_type
,om.msg_id
,om.chat_id
,omc.name COLLATE utf8mb4_unicode_ci AS name
,CAST(omc.data as CHAR) data
FROM obj_msg om
,obj o
,code_obj_sub_type cost
,obj_msg_callback omc
WHERE om.obj_id = o.id
AND cost.id = o.obj_sub_type_id
AND omc.obj_id = o.id;`)
failOnError(err, "initDBViews : create view obj_msg_callback_v")
log.Println("initDBViews : obj_msg_callback_v created ...")
log.Println("initDBViews : Views set up")
}
@ -738,8 +756,7 @@ func getSQLListID64(q string) []int64 {
)
rows, err := db.Query(q)
s := fmt.Sprintf("getSQLListID64 : Query(%s)", q)
logOnError(err, s)
logOnError(err, "getSQLListID64 : "+q)
for rows.Next() {
err = rows.Scan(&id)