This commit is contained in:
shoopea 2019-05-11 15:11:42 +08:00
parent 0e471b2e5d
commit 6ffc3778f1

24
sql.go
View File

@ -1225,23 +1225,34 @@ func resetMsgParsingRules() {
}
func putUnprocessedMsg(m ChatWarsMessage) (int64, error) {
res, err := db.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
tx, err := db.Begin()
logOnError(err, "putUnprocessedMsg : start transaction")
if err != nil {
return 0, err
}
res, err := tx.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
VALUES (` + strconv.Itoa(objTypeMessage) + `,` + strconv.Itoa(objSubTypeMessageUnknown) + `);`)
logOnError(err, "putUnprocessedMsg : exec insert obj")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "putUnprocessedMsg : rollback insert obj")
return 0, err
}
objId, err := res.LastInsertId()
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "putUnprocessedMsg : rollback get lastInsertId")
return 0, err
}
stmt, err := db.Prepare(`INSERT INTO obj_msg (obj_id, msg_id, chat_id, user_id, sender_user_id, date, text)
stmt, err := tx.Prepare(`INSERT INTO obj_msg (obj_id, msg_id, chat_id, user_id, sender_user_id, date, text)
VALUES (?, ?, ?, ?, ?, ?, ?);`)
logOnError(err, "putUnprocessedMsg : prepare insert obj_msg")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "putUnprocessedMsg : rollback prepare insert obj_msg")
return 0, err
}
defer stmt.Close()
@ -1249,9 +1260,16 @@ func putUnprocessedMsg(m ChatWarsMessage) (int64, error) {
_, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.UserID64, m.SenderUserID64, m.Date, m.Text)
logOnError(err, "putUnprocessedMsg : exec insert obj_msg")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "putUnprocessedMsg : rollback exec insert obj")
return 0, err
}
err = tx.Commit()
logOnError(err, "putUnprocessedMsg : commit")
if err != nil {
return 0, err
}
return objId, nil
}