1 Star 0 Fork 0

liuxuezhan / mylib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DB.go 13.11 KB
一键复制 编辑 原始数据 按行查看 历史
liuxuezhan 提交于 2020-10-14 10:09 . 'new mylib'
package GameDB
import (
"fmt"
"gitee.com/liuxuezhan/mylib/Utils/wlog"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const (
panicMode = false //panic模式
configCollectionName = "Config"
mailIdCollectionName = "mail_id"
mailsCollectionName = "mails"
everyoneMailCollectionName = "everyone_mails"
worldDataIdCollectionName = "world_data_id"
worldDataCollectionName = "world_data"
characteristicCollectionName = "characteristic"
leagueIdCollectionName = "league_id"
leagueDataCollectionName = "league_data"
globalActionIdCollectionName = "global_action_id"
globalActionCollectionName = "global_action"
userPowerRankingCollectionName = "user_power_ranking"
leaguePowerRankingCollectionName = "league_power_ranking"
leagueKilledRankingCollectionName = "league_killed_ranking"
commanderKilledRankingCollectionName = "commander_killed_ranking"
commanderAirShipStarRankingCollectionName = "commander_air_ship_star_ranking"
commanderWorldSituationLeagueRankingCollectionName = "world_situation_league_ranking"
commanderLevelRankingCollectionName = "commander_level_ranking"
mainCityLevelRankingCollectionName = "main_city_level_ranking"
bountyRankingCollectionName = "bounty_ranking"
worldChatIdCollectionName = "world_chat_id"
worldChatCollectionName = "world_chat"
hotWorldChatCollectionName = "hot_world_chat"
sessionCollectionName = "session_collection"
leagueChatCollectionName = "league_chat"
worldChatWXCollectionName = "world_chat_wx"
reportCollectionName = "report"
leagueBusinessIncomeHistoryCollectionName = "league_business_income_history"
leagueBusinessOutcomeHistoryCollectionName = "league_business_outcome_history"
activityCollectionName = "activity"
miracleWarCollectionName = "miracle_war"
WarCityCollectionName = "war_city"
leagueCityCollectionName = "league_city_activity"
airdropCollectionName = "airdrop_info"
boonCenterCollectionName = "boon_configs"
chaptersCollectionName = "user_chapters"
cardPoolCollectionName = "card_pool"
prisonMessageCollectionName = "prison_message"
offlineUserCollectionName = "offline_user"
serverShoutdownTime = "server_shoutdown_time"
DBGolbal = "DBGolbal"
battleInfoCollectionName = "battle_info"
dropActivtyColectionName = "drop_activity_rank"
LeagueCupCollectionName = "LeagueCup"
strongestCommanderCollectionName = "strongest_commander"
worldSituationCollectionName = "world_situation"
worldSituationRankSnapshotCollectionName = "world_situation_rank_snapshot"
strongestCommanderCollectionRankName = "strongest_commander_rank"
compensationCollectionName = "compensation"
expeditionShopCollectionName = "expedition_shop"
characteristicSimpleCollectionName = "characteristic_simple"
heroKilledRankingCollectionName = "hero_killed_ranking"
gatherResourceRankingCollectionName = "gather_resource_ranking"
serverListCollectionName = "server_list"
starPioneerCollectionName = "star_pioneer"
monsterAtkCityCollectionName = "monster_atk_city"
leagueBattleLogCollectionName = "league_battle_log"
macPlayerRankingCollectionName = "mac_player_ranking"
macLeagueRankingCollectionName = "mac_league_ranking"
prepareForWarCollectionName = "prepare_for_war"
prepareForWarCollectionRankName = "prepare_for_war_rank"
transferBattleCollectionName = "transfer_battle"
transferBattleUserSubCollectionName = "transfer_battle_user_sub"
transferBattleUserTotalCollectionName = "transfer_battle_user_total"
transferBattleLeagueSubCollectionName = "transfer_battle_league_sub"
transferBattleLeagueTotalCollectionName = "transfer_battle_league_total"
transferBattleUserBackupCollectionName = "transfer_battle_user_backup"
)
var (
globalSession *mgo.Session //全局会话
)
//配置
type ConfigDocument struct {
Id uint64 "_id"
ServerId uint32 "server_id"
}
//id
type IdDocument struct {
Id uint64 "_id"
NextId uint32 "next_id"
}
//数据库父类
type DBObject struct {
dbName string
Config ConfigDocument //配置
}
////>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//获取集合
func (object *DBObject) withCollection(collectionName string, callback func(collection *mgo.Collection)) {
sessionCopy := globalSession.Clone()
defer sessionCopy.Close()
callback(sessionCopy.DB(object.dbName).C(collectionName))
}
//创建索引
func (object *DBObject) ensureIndex(collectionName string, indexes []mgo.Index) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
for _, index := range indexes {
if ret = collection.EnsureIndex(index); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v) ensure index error: %v", collectionName, index, ret.Error()))
}
break
}
}
})
return ret
}
//添加
func (object *DBObject) add(collectionName string, doc interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if ret = collection.Insert(doc); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v) add error: %v", collectionName, doc, ret.Error()))
}
}
})
return ret
}
//获取
func (object *DBObject) Get(collectionName string, query, res interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if ret = collection.Find(query).One(res); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v, %v) get error: %v", collectionName, query, res, ret.Error()))
}
}
})
return ret
}
//通过id获取
func (object *DBObject) getById(collectionName string, id, res interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if ret = collection.FindId(id).One(res); nil != ret {
if panicMode {
panic(ret)
} else {
//wlog.Error(fmt.Sprintf("(%v, %v, %v) get by id error: %v", collectionName, id, res, ret.Error()))
}
}
})
return ret
}
//获取所有
func (object *DBObject) getAll(collectionName, sort string, skip, limit int, query, selector, res interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if nil == query {
query = bson.M{}
}
q := collection.Find(query).Select(selector)
if 0 != len(sort) {
q = q.Sort(sort)
}
if ret = q.Skip(skip).Limit(limit).All(res); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v, %v, %v, %v, %v, %v) get all error: %v",
collectionName, sort, skip, limit, query, selector, res, ret.Error()))
}
}
})
return ret
}
//计数
func (object *DBObject) count(collectionName string, query interface{}) (error, int) {
var count int = 0
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if nil == query {
count, ret = collection.Count()
} else {
count, ret = collection.Find(query).Count()
}
if nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v) count error: %v", collectionName, query, ret.Error()))
}
}
})
return ret, count
}
//查找并修改
func (object *DBObject) findAndModify(collectionName string, id interface{}, change mgo.Change, doc interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if _, ret = collection.FindId(id).Apply(change, doc); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v, %v, %v) find and modify error: %v", collectionName, id, change, doc, ret.Error()))
}
}
})
return ret
}
//更新
func (object *DBObject) update(collectionName string, query, update interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if ret = collection.Update(query, update); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v, %v) update error: %v", collectionName, query, update, ret.Error()))
}
}
})
return ret
}
//更新
func (object *DBObject) updateById(collectionName string, id uint64, update interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if ret = collection.UpdateId(id, update); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v, %v) update error: %v", collectionName, id, update, ret))
}
}
})
return ret
}
//更新所有
func (object *DBObject) updateAll(collectionName string, query, update interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if _, ret = collection.UpdateAll(query, update); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v, %v) update all error: %v", collectionName, query, update, ret.Error()))
}
}
})
return ret
}
//通过id更新或插入
func (object *DBObject) upsertById(collectionName string, id, update interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if _, ret = collection.UpsertId(id, update); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v, %v) upsert by id error: %v", collectionName, id, update, ret.Error()))
}
}
})
return ret
}
//更新或插入
func (object *DBObject) upsert(collectionName string, query, update interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if _, ret = collection.Upsert(query, update); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v, %v) upsert error: %v", collectionName, query, update, ret.Error()))
}
}
})
return ret
}
//设置新的置
func (object *DBObject) upsertKeyValue(collectionName string, id interface{}, key string, value interface{}) error {
update := bson.M{"$set": bson.M{key: value}}
return object.upsertById(collectionName, id, update)
}
//删除文档
func (object *DBObject) removeById(collectionName string, id interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if ret = collection.RemoveId(id); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v) remove by id error: %v", collectionName, id, ret.Error()))
}
}
})
return ret
}
//删除文档
func (object *DBObject) removeAll(collectionName string, query interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if _, ret = collection.RemoveAll(query); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v) remove all error: %v", collectionName, query, ret.Error()))
}
}
})
return ret
}
//删除文档
func (object *DBObject) remove(collectionName string, query interface{}) error {
var ret error = nil
object.withCollection(collectionName, func(collection *mgo.Collection) {
if ret = collection.Remove(query); nil != ret {
if panicMode {
panic(ret)
} else {
wlog.Error(fmt.Sprintf("(%v, %v) remove error: %v", collectionName, query, ret.Error()))
}
}
})
return ret
}
//获取迭代器
func (object *DBObject) foreach(collectionName string, query interface{}, sort string, doc interface{}, callback func(error, bool, interface{})) {
object.withCollection(collectionName, func(collection *mgo.Collection) {
if nil == query {
query = bson.M{}
}
q := collection.Find(query)
if nil == q {
return
}
if 0 != len(sort) {
q = q.Sort(sort)
}
iterator := q.Iter()
if nil == iterator {
return
}
for iterator.Next(doc) {
callback(nil, true, doc)
}
iterator.Close()
})
}
1
https://gitee.com/liuxuezhan/mylib.git
git@gitee.com:liuxuezhan/mylib.git
liuxuezhan
mylib
mylib
v1.1.3

搜索帮助