This commit is contained in:
shoopea 2019-05-25 17:04:56 +08:00
parent ebb5e021d9
commit 00dce41e9a

94
obj.go
View File

@ -285,100 +285,6 @@ func loadObjGuild() error {
return nil return nil
} }
func addObjGuild(tag string, name string) (int64, error) {
tx, err := db.Begin()
logOnError(err, "addObjGuild : 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(objTypeGuild) + `,` + strconv.Itoa(objSubTypeGuild) + `);`)
logOnError(err, "addObjGuild : exec insert obj")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjGuild : rollback insert obj")
return 0, err
}
objId, err := res.LastInsertId()
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjGuild : rollback get lastInsertId")
return 0, err
}
stmt, err := tx.Prepare(`INSERT INTO obj_guild (obj_id, tag, name, chat_id, deposit_chat_id)
VALUES (?, ?, ?, NULL, NULL);`)
logOnError(err, "addObjGuild : prepare insert obj_guild")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjGuild : rollback prepare insert obj_guild")
return 0, err
}
defer stmt.Close()
_, err = stmt.Exec(objId, tag, name)
logOnError(err, "addObjGuild : exec insert obj_guild")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjGuild : rollback exec insert obj")
return 0, err
}
err = tx.Commit()
logOnError(err, "addObjGuild : commit")
if err != nil {
return 0, err
}
return objId, nil
}
func getObjGuildID(s string) int64 {
if g, ok := cacheObjGuild[s]; ok {
return g.ObjID64
} else {
objID64, err := addObjGuild(s, ``)
logOnError(err, "getObjGuildID")
g := new(ChatWarsGuild)
g.ObjID64 = objID64
g.Tag = s
g.Name = ``
cacheObjGuild[s] = *g
}
return cacheObjGuild[s].ObjID64
}
func loadObjGuild() error {
var (
id int64
tag string
name string
)
cacheObjGuild = make(map[string]ChatWarsGuild)
guilds, err := db.Query(`SELECT og.obj_id, og.tag, og.name FROM obj_guild og;`)
if err != nil {
return err
}
defer guilds.Close()
for guilds.Next() {
err = guilds.Scan(&id, &tag, &name)
if err != nil {
return err
}
g := new(ChatWarsGuild)
g.ObjID64 = id
g.Tag = tag
g.Name = name
cacheObjGuild[tag] = *g
}
return nil
}
func addObjUser(name string) (int64, error) { func addObjUser(name string) (int64, error) {
tx, err := db.Begin() tx, err := db.Begin()
logOnError(err, "addObjUser : start transaction") logOnError(err, "addObjUser : start transaction")