From fa28bbb83fc037fd4110269c419cf028fcdd82ab Mon Sep 17 00:00:00 2001 From: zicla Date: Sat, 27 Apr 2019 23:42:44 +0800 Subject: [PATCH] Refine the DB in context. --- code/rest/context.go | 15 ++++++++++----- code/rest/dashboard_dao.go | 18 ++++++++--------- code/rest/download_token_dao.go | 10 +++++----- code/rest/footprint_dao.go | 22 ++++++++++----------- code/rest/image_cache_dao.go | 26 ++++++++++++------------- code/rest/matter_dao.go | 34 ++++++++++++++++----------------- code/rest/preference_dao.go | 8 ++++---- code/rest/session_dao.go | 12 ++++++------ code/rest/upload_token_dao.go | 6 +++--- code/rest/user_dao.go | 24 +++++++++++------------ 10 files changed, 90 insertions(+), 85 deletions(-) diff --git a/code/rest/context.go b/code/rest/context.go index 5ef5364..a1294e1 100644 --- a/code/rest/context.go +++ b/code/rest/context.go @@ -15,7 +15,7 @@ var CONTEXT = &Context{} //上下文,管理数据库连接,管理所有路由请求,管理所有的单例component. type Context struct { //数据库连接 - DB *gorm.DB + db *gorm.DB //session缓存 SessionCache *cache.Table //各类的Bean Map。这里面是包含ControllerMap中所有元素 @@ -50,23 +50,28 @@ func (this *Context) Init() { } +//获取数据库对象 +func (this *Context) GetDB() *gorm.DB { + return this.db +} + func (this *Context) OpenDb() { var err error = nil - this.DB, err = gorm.Open("mysql", config.CONFIG.MysqlUrl) + this.db, err = gorm.Open("mysql", config.CONFIG.MysqlUrl) if err != nil { core.LOGGER.Panic("failed to connect mysql database") } //是否打开sql日志(在调试阶段可以打开,以方便查看执行的SQL) - this.DB.LogMode(false) + this.db.LogMode(false) } func (this *Context) CloseDb() { - if this.DB != nil { - err := this.DB.Close() + if this.db != nil { + err := this.db.Close() if err != nil { core.LOGGER.Error("关闭数据库连接出错 %s", err.Error()) } diff --git a/code/rest/dashboard_dao.go b/code/rest/dashboard_dao.go index a0b5a76..47a8e89 100644 --- a/code/rest/dashboard_dao.go +++ b/code/rest/dashboard_dao.go @@ -19,7 +19,7 @@ func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard { dashboard.CreateTime = time.Now() dashboard.UpdateTime = time.Now() dashboard.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.DB.Create(dashboard) + db := CONTEXT.GetDB().Create(dashboard) this.PanicError(db.Error) return dashboard @@ -29,7 +29,7 @@ func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard { func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard { dashboard.UpdateTime = time.Now() - db := CONTEXT.DB.Save(dashboard) + db := CONTEXT.GetDB().Save(dashboard) this.PanicError(db.Error) return dashboard @@ -38,7 +38,7 @@ func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard { //删除一条记录 func (this *DashboardDao) Delete(dashboard *Dashboard) { - db := CONTEXT.DB.Delete(&dashboard) + db := CONTEXT.GetDB().Delete(&dashboard) this.PanicError(db.Error) } @@ -47,7 +47,7 @@ func (this *DashboardDao) FindByDt(dt string) *Dashboard { // Read var dashboard Dashboard - db := CONTEXT.DB.Where(&Dashboard{Dt: dt}).First(&dashboard) + db := CONTEXT.GetDB().Where(&Dashboard{Dt: dt}).First(&dashboard) if db.Error != nil { return nil } @@ -64,7 +64,7 @@ func (this *DashboardDao) Page(page int, pageSize int, dt string, sortArray []bu } var conditionDB *gorm.DB - conditionDB = CONTEXT.DB.Model(&Dashboard{}).Where(wp.Query, wp.Args...) + conditionDB = CONTEXT.GetDB().Model(&Dashboard{}).Where(wp.Query, wp.Args...) count := 0 db := conditionDB.Count(&count) @@ -89,7 +89,7 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes { Value: "DESC", }, } - rows, err := CONTEXT.DB.Model(&Footprint{}). + rows, err := CONTEXT.GetDB().Model(&Footprint{}). Select("ip,COUNT(uuid) as times"). Group("ip"). Order(this.GetSortString(sortArray)). @@ -99,8 +99,8 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes { this.PanicError(err) for rows.Next() { - var ip string; - var times int64 = 0; + var ip string + var times int64 = 0 rows.Scan(&ip, ×) item := &DashboardIpTimes{ Ip: ip, @@ -115,6 +115,6 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes { //执行清理操作 func (this *DashboardDao) Cleanup() { this.logger.Info("[DashboardDao]执行清理:清除数据库中所有Dashboard记录。") - db := CONTEXT.DB.Where("uuid is not null").Delete(Dashboard{}) + db := CONTEXT.GetDB().Where("uuid is not null").Delete(Dashboard{}) this.PanicError(db.Error) } diff --git a/code/rest/download_token_dao.go b/code/rest/download_token_dao.go index 995cc43..42ded18 100644 --- a/code/rest/download_token_dao.go +++ b/code/rest/download_token_dao.go @@ -14,7 +14,7 @@ func (this *DownloadTokenDao) FindByUuid(uuid string) *DownloadToken { // Read var downloadToken = &DownloadToken{} - db := CONTEXT.DB.Where(&DownloadToken{Base: Base{Uuid: uuid}}).First(downloadToken) + db := CONTEXT.GetDB().Where(&DownloadToken{Base: Base{Uuid: uuid}}).First(downloadToken) if db.Error != nil { return nil } @@ -27,7 +27,7 @@ func (this *DownloadTokenDao) CheckByUuid(uuid string) *DownloadToken { // Read var downloadToken = &DownloadToken{} - db := CONTEXT.DB.Where(&DownloadToken{Base: Base{Uuid: uuid}}).First(downloadToken) + db := CONTEXT.GetDB().Where(&DownloadToken{Base: Base{Uuid: uuid}}).First(downloadToken) this.PanicError(db.Error) return downloadToken @@ -42,7 +42,7 @@ func (this *DownloadTokenDao) Create(downloadToken *DownloadToken) *DownloadToke downloadToken.CreateTime = time.Now() downloadToken.UpdateTime = time.Now() downloadToken.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.DB.Create(downloadToken) + db := CONTEXT.GetDB().Create(downloadToken) this.PanicError(db.Error) return downloadToken @@ -52,7 +52,7 @@ func (this *DownloadTokenDao) Create(downloadToken *DownloadToken) *DownloadToke func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken { downloadToken.UpdateTime = time.Now() - db := CONTEXT.DB.Save(downloadToken) + db := CONTEXT.GetDB().Save(downloadToken) this.PanicError(db.Error) return downloadToken @@ -61,6 +61,6 @@ func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken //执行清理操作 func (this *DownloadTokenDao) Cleanup() { this.logger.Info("[DownloadTokenDao]执行清理:清除数据库中所有DownloadToken记录。") - db := CONTEXT.DB.Where("uuid is not null").Delete(DownloadToken{}) + db := CONTEXT.GetDB().Where("uuid is not null").Delete(DownloadToken{}) this.PanicError(db.Error) } diff --git a/code/rest/footprint_dao.go b/code/rest/footprint_dao.go index 963f421..a61931a 100644 --- a/code/rest/footprint_dao.go +++ b/code/rest/footprint_dao.go @@ -17,7 +17,7 @@ func (this *FootprintDao) FindByUuid(uuid string) *Footprint { // Read var footprint Footprint - db := CONTEXT.DB.Where(&Footprint{Base: Base{Uuid: uuid}}).First(&footprint) + db := CONTEXT.GetDB().Where(&Footprint{Base: Base{Uuid: uuid}}).First(&footprint) if db.Error != nil { return nil } @@ -29,7 +29,7 @@ func (this *FootprintDao) CheckByUuid(uuid string) *Footprint { // Read var footprint Footprint - db := CONTEXT.DB.Where(&Footprint{Base: Base{Uuid: uuid}}).First(&footprint) + db := CONTEXT.GetDB().Where(&Footprint{Base: Base{Uuid: uuid}}).First(&footprint) this.PanicError(db.Error) return &footprint @@ -46,7 +46,7 @@ func (this *FootprintDao) Page(page int, pageSize int, userUuid string, sortArra } var conditionDB *gorm.DB - conditionDB = CONTEXT.DB.Model(&Footprint{}).Where(wp.Query, wp.Args...) + conditionDB = CONTEXT.GetDB().Model(&Footprint{}).Where(wp.Query, wp.Args...) count := 0 db := conditionDB.Count(&count) @@ -68,7 +68,7 @@ func (this *FootprintDao) Create(footprint *Footprint) *Footprint { footprint.CreateTime = time.Now() footprint.UpdateTime = time.Now() footprint.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.DB.Create(footprint) + db := CONTEXT.GetDB().Create(footprint) this.PanicError(db.Error) return footprint @@ -78,7 +78,7 @@ func (this *FootprintDao) Create(footprint *Footprint) *Footprint { func (this *FootprintDao) Save(footprint *Footprint) *Footprint { footprint.UpdateTime = time.Now() - db := CONTEXT.DB.Save(footprint) + db := CONTEXT.GetDB().Save(footprint) this.PanicError(db.Error) return footprint @@ -87,14 +87,14 @@ func (this *FootprintDao) Save(footprint *Footprint) *Footprint { //删除一条记录 func (this *FootprintDao) Delete(footprint *Footprint) { - db := CONTEXT.DB.Delete(&footprint) + db := CONTEXT.GetDB().Delete(&footprint) this.PanicError(db.Error) } //获取一段时间中,总的数量 func (this *FootprintDao) CountBetweenTime(startTime time.Time, endTime time.Time) int64 { var count int64 - db := CONTEXT.DB.Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count) + db := CONTEXT.GetDB().Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count) this.PanicError(db.Error) return count } @@ -102,7 +102,7 @@ func (this *FootprintDao) CountBetweenTime(startTime time.Time, endTime time.Tim //获取一段时间中UV的数量 func (this *FootprintDao) UvBetweenTime(startTime time.Time, endTime time.Time) int64 { var count int64 - db := CONTEXT.DB.Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("COUNT(DISTINCT(ip))") + db := CONTEXT.GetDB().Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("COUNT(DISTINCT(ip))") this.PanicError(db.Error) row := db.Row() row.Scan(&count) @@ -112,7 +112,7 @@ func (this *FootprintDao) UvBetweenTime(startTime time.Time, endTime time.Time) //获取一段时间中平均耗时 func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.Time) int64 { var cost float64 - db := CONTEXT.DB.Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("AVG(cost)") + db := CONTEXT.GetDB().Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("AVG(cost)") this.PanicError(db.Error) row := db.Row() row.Scan(&cost) @@ -121,13 +121,13 @@ func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.T //删除某个时刻之前的记录 func (this *FootprintDao) DeleteByCreateTimeBefore(createTime time.Time) { - db := CONTEXT.DB.Where("create_time < ?", createTime).Delete(Footprint{}) + db := CONTEXT.GetDB().Where("create_time < ?", createTime).Delete(Footprint{}) this.PanicError(db.Error) } //执行清理操作 func (this *FootprintDao) Cleanup() { this.logger.Info("[FootprintDao]执行清理:清除数据库中所有Footprint记录。") - db := CONTEXT.DB.Where("uuid is not null").Delete(Footprint{}) + db := CONTEXT.GetDB().Where("uuid is not null").Delete(Footprint{}) this.PanicError(db.Error) } diff --git a/code/rest/image_cache_dao.go b/code/rest/image_cache_dao.go index 5db715e..d553449 100644 --- a/code/rest/image_cache_dao.go +++ b/code/rest/image_cache_dao.go @@ -20,7 +20,7 @@ func (this *ImageCacheDao) FindByUuid(uuid string) *ImageCache { // Read var imageCache ImageCache - db := CONTEXT.DB.Where(&ImageCache{Base: Base{Uuid: uuid}}).First(&imageCache) + db := CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}}).First(&imageCache) if db.Error != nil { return nil } @@ -32,7 +32,7 @@ func (this *ImageCacheDao) CheckByUuid(uuid string) *ImageCache { // Read var imageCache ImageCache - db := CONTEXT.DB.Where(&ImageCache{Base: Base{Uuid: uuid}}).First(&imageCache) + db := CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}}).First(&imageCache) this.PanicError(db.Error) return &imageCache @@ -53,7 +53,7 @@ func (this *ImageCacheDao) FindByMatterUuidAndMode(matterUuid string, mode strin } var imageCache = &ImageCache{} - db := CONTEXT.DB.Model(&ImageCache{}).Where(wp.Query, wp.Args...).First(imageCache) + db := CONTEXT.GetDB().Model(&ImageCache{}).Where(wp.Query, wp.Args...).First(imageCache) if db.Error != nil { return nil @@ -67,7 +67,7 @@ func (this *ImageCacheDao) CheckByUuidAndUserUuid(uuid string, userUuid string) // Read var imageCache = &ImageCache{} - db := CONTEXT.DB.Where(&ImageCache{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(imageCache) + db := CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(imageCache) this.PanicError(db.Error) return imageCache @@ -79,7 +79,7 @@ func (this *ImageCacheDao) ListByUserUuidAndPuuidAndDirAndName(userUuid string) var imageCaches []*ImageCache - db := CONTEXT.DB. + db := CONTEXT.GetDB(). Where(ImageCache{UserUuid: userUuid}). Find(&imageCaches) this.PanicError(db.Error) @@ -101,7 +101,7 @@ func (this *ImageCacheDao) Page(page int, pageSize int, userUuid string, matterU } var conditionDB *gorm.DB - conditionDB = CONTEXT.DB.Model(&ImageCache{}).Where(wp.Query, wp.Args...) + conditionDB = CONTEXT.GetDB().Model(&ImageCache{}).Where(wp.Query, wp.Args...) count := 0 db := conditionDB.Count(&count) @@ -123,7 +123,7 @@ func (this *ImageCacheDao) Create(imageCache *ImageCache) *ImageCache { imageCache.CreateTime = time.Now() imageCache.UpdateTime = time.Now() imageCache.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.DB.Create(imageCache) + db := CONTEXT.GetDB().Create(imageCache) this.PanicError(db.Error) return imageCache @@ -133,7 +133,7 @@ func (this *ImageCacheDao) Create(imageCache *ImageCache) *ImageCache { func (this *ImageCacheDao) Save(imageCache *ImageCache) *ImageCache { imageCache.UpdateTime = time.Now() - db := CONTEXT.DB.Save(imageCache) + db := CONTEXT.GetDB().Save(imageCache) this.PanicError(db.Error) return imageCache @@ -160,7 +160,7 @@ func (this *ImageCacheDao) deleteFileAndDir(imageCache *ImageCache) { //删除一个文件,数据库中删除,物理磁盘上删除。 func (this *ImageCacheDao) Delete(imageCache *ImageCache) { - db := CONTEXT.DB.Delete(&imageCache) + db := CONTEXT.GetDB().Delete(&imageCache) this.PanicError(db.Error) this.deleteFileAndDir(imageCache) @@ -176,11 +176,11 @@ func (this *ImageCacheDao) DeleteByMatterUuid(matterUuid string) { //查询出即将删除的图片缓存 var imageCaches []*ImageCache - db := CONTEXT.DB.Where(wp.Query, wp.Args).Find(&imageCaches) + db := CONTEXT.GetDB().Where(wp.Query, wp.Args).Find(&imageCaches) this.PanicError(db.Error) //删除文件记录 - db = CONTEXT.DB.Where(wp.Query, wp.Args).Delete(ImageCache{}) + db = CONTEXT.GetDB().Where(wp.Query, wp.Args).Delete(ImageCache{}) this.PanicError(db.Error) //删除文件实体 @@ -193,7 +193,7 @@ func (this *ImageCacheDao) DeleteByMatterUuid(matterUuid string) { //获取一段时间中文件总大小 func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Time) int64 { var size int64 - db := CONTEXT.DB.Model(&ImageCache{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)") + db := CONTEXT.GetDB().Model(&ImageCache{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)") this.PanicError(db.Error) row := db.Row() row.Scan(&size) @@ -203,6 +203,6 @@ func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Tim //执行清理操作 func (this *ImageCacheDao) Cleanup() { this.logger.Info("[ImageCacheDao]执行清理:清除数据库中所有ImageCache记录。") - db := CONTEXT.DB.Where("uuid is not null").Delete(ImageCache{}) + db := CONTEXT.GetDB().Where("uuid is not null").Delete(ImageCache{}) this.PanicError(db.Error) } diff --git a/code/rest/matter_dao.go b/code/rest/matter_dao.go index 630c84a..8f17479 100644 --- a/code/rest/matter_dao.go +++ b/code/rest/matter_dao.go @@ -32,7 +32,7 @@ func (this *MatterDao) FindByUuid(uuid string) *Matter { // Read var matter Matter - db := CONTEXT.DB.Where(&Matter{Base: Base{Uuid: uuid}}).First(&matter) + db := CONTEXT.GetDB().Where(&Matter{Base: Base{Uuid: uuid}}).First(&matter) if db.Error != nil { if db.Error.Error() == result.DB_ERROR_NOT_FOUND { return nil @@ -111,7 +111,7 @@ func (this *MatterDao) FindByUserUuidAndPuuidAndNameAndDirTrue(userUuid string, wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{1}}) var matter = &Matter{} - db := CONTEXT.DB.Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) + db := CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) if db.Error != nil { return nil @@ -125,7 +125,7 @@ func (this *MatterDao) CheckByUuidAndUserUuid(uuid string, userUuid string) *Mat // Read var matter = &Matter{} - db := CONTEXT.DB.Where(&Matter{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(matter) + db := CONTEXT.GetDB().Where(&Matter{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(matter) this.PanicError(db.Error) return matter @@ -154,7 +154,7 @@ func (this *MatterDao) CountByUserUuidAndPuuidAndDirAndName(userUuid string, puu wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{dir}}) - db := CONTEXT.DB. + db := CONTEXT.GetDB(). Model(&matter). Where(wp.Query, wp.Args...). Count(&count) @@ -168,7 +168,7 @@ func (this *MatterDao) ListByUserUuidAndPuuidAndDirAndName(userUuid string, puui var matters []*Matter - db := CONTEXT.DB. + db := CONTEXT.GetDB(). Where(Matter{UserUuid: userUuid, Puuid: puuid, Dir: dir, Name: name}). Find(&matters) this.PanicError(db.Error) @@ -180,7 +180,7 @@ func (this *MatterDao) ListByUserUuidAndPuuidAndDirAndName(userUuid string, puui func (this *MatterDao) List(puuid string, userUuid string, sortArray []builder.OrderPair) []*Matter { var matters []*Matter - db := CONTEXT.DB.Where(Matter{UserUuid: userUuid, Puuid: puuid}).Order(this.GetSortString(sortArray)).Find(&matters) + db := CONTEXT.GetDB().Where(Matter{UserUuid: userUuid, Puuid: puuid}).Order(this.GetSortString(sortArray)).Find(&matters) this.PanicError(db.Error) return matters @@ -223,9 +223,9 @@ func (this *MatterDao) Page(page int, pageSize int, puuid string, userUuid strin orWp = orWp.Or(&builder.WherePair{Query: "name LIKE ?", Args: []interface{}{"%." + v}}) } - conditionDB = CONTEXT.DB.Model(&Matter{}).Where(wp.Query, wp.Args...).Where(orWp.Query, orWp.Args...) + conditionDB = CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).Where(orWp.Query, orWp.Args...) } else { - conditionDB = CONTEXT.DB.Model(&Matter{}).Where(wp.Query, wp.Args...) + conditionDB = CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...) } count := 0 @@ -248,7 +248,7 @@ func (this *MatterDao) Create(matter *Matter) *Matter { matter.CreateTime = time.Now() matter.UpdateTime = time.Now() matter.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.DB.Create(matter) + db := CONTEXT.GetDB().Create(matter) this.PanicError(db.Error) return matter @@ -258,7 +258,7 @@ func (this *MatterDao) Create(matter *Matter) *Matter { func (this *MatterDao) Save(matter *Matter) *Matter { matter.UpdateTime = time.Now() - db := CONTEXT.DB.Save(matter) + db := CONTEXT.GetDB().Save(matter) this.PanicError(db.Error) return matter @@ -266,7 +266,7 @@ func (this *MatterDao) Save(matter *Matter) *Matter { //计数器加一 func (this *MatterDao) TimesIncrement(matterUuid string) { - db := CONTEXT.DB.Model(&Matter{}).Where("uuid = ?", matterUuid).Update("times", gorm.Expr("times + 1")) + db := CONTEXT.GetDB().Model(&Matter{}).Where("uuid = ?", matterUuid).Update("times", gorm.Expr("times + 1")) this.PanicError(db.Error) } @@ -282,7 +282,7 @@ func (this *MatterDao) Delete(matter *Matter) { } //删除数据库中文件夹本身 - db := CONTEXT.DB.Delete(&matter) + db := CONTEXT.GetDB().Delete(&matter) this.PanicError(db.Error) //从磁盘中删除该文件夹。 @@ -291,7 +291,7 @@ func (this *MatterDao) Delete(matter *Matter) { } else { //删除数据库中文件记录 - db := CONTEXT.DB.Delete(&matter) + db := CONTEXT.GetDB().Delete(&matter) this.PanicError(db.Error) //删除对应的缓存图片。 @@ -311,7 +311,7 @@ func (this *MatterDao) Delete(matter *Matter) { //获取一段时间中,总的数量 func (this *MatterDao) CountBetweenTime(startTime time.Time, endTime time.Time) int64 { var count int64 - db := CONTEXT.DB.Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count) + db := CONTEXT.GetDB().Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count) this.PanicError(db.Error) return count } @@ -319,7 +319,7 @@ func (this *MatterDao) CountBetweenTime(startTime time.Time, endTime time.Time) //获取一段时间中文件总大小 func (this *MatterDao) SizeBetweenTime(startTime time.Time, endTime time.Time) int64 { var size int64 - db := CONTEXT.DB.Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)") + db := CONTEXT.GetDB().Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)") this.PanicError(db.Error) row := db.Row() err := row.Scan(&size) @@ -333,7 +333,7 @@ func (this *MatterDao) findByUserUuidAndPath(userUuid string, path string) *Matt var wp = &builder.WherePair{Query: "user_uuid = ? AND path = ?", Args: []interface{}{userUuid, path}} var matter = &Matter{} - db := CONTEXT.DB.Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) + db := CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) if db.Error != nil { if db.Error.Error() == result.DB_ERROR_NOT_FOUND { @@ -363,7 +363,7 @@ func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Mat //执行清理操作 func (this *MatterDao) Cleanup() { this.logger.Info("[MatterDao]执行清理:清除数据库中所有Matter记录。删除磁盘中所有Matter文件。") - db := CONTEXT.DB.Where("uuid is not null").Delete(Matter{}) + db := CONTEXT.GetDB().Where("uuid is not null").Delete(Matter{}) this.PanicError(db.Error) err := os.RemoveAll(config.CONFIG.MatterPath) diff --git a/code/rest/preference_dao.go b/code/rest/preference_dao.go index ef4211d..c16817b 100644 --- a/code/rest/preference_dao.go +++ b/code/rest/preference_dao.go @@ -15,7 +15,7 @@ func (this *PreferenceDao) Fetch() *Preference { // Read var preference = &Preference{} - db := CONTEXT.DB.First(preference) + db := CONTEXT.GetDB().First(preference) if db.Error != nil { if db.Error.Error() == result.DB_ERROR_NOT_FOUND { @@ -39,7 +39,7 @@ func (this *PreferenceDao) Create(preference *Preference) *Preference { preference.CreateTime = time.Now() preference.UpdateTime = time.Now() preference.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.DB.Create(preference) + db := CONTEXT.GetDB().Create(preference) this.PanicError(db.Error) return preference @@ -49,7 +49,7 @@ func (this *PreferenceDao) Create(preference *Preference) *Preference { func (this *PreferenceDao) Save(preference *Preference) *Preference { preference.UpdateTime = time.Now() - db := CONTEXT.DB.Save(preference) + db := CONTEXT.GetDB().Save(preference) this.PanicError(db.Error) return preference @@ -59,6 +59,6 @@ func (this *PreferenceDao) Save(preference *Preference) *Preference { func (this *PreferenceDao) Cleanup() { this.logger.Info("[PreferenceDao]执行清理:清除数据库中所有Preference记录。") - db := CONTEXT.DB.Where("uuid is not null").Delete(Preference{}) + db := CONTEXT.GetDB().Where("uuid is not null").Delete(Preference{}) this.PanicError(db.Error) } diff --git a/code/rest/session_dao.go b/code/rest/session_dao.go index db6299b..0f960c4 100644 --- a/code/rest/session_dao.go +++ b/code/rest/session_dao.go @@ -14,7 +14,7 @@ func (this *SessionDao) FindByUuid(uuid string) *Session { // Read var session = &Session{} - db := CONTEXT.DB.Where(&Session{Base: Base{Uuid: uuid}}).First(session) + db := CONTEXT.GetDB().Where(&Session{Base: Base{Uuid: uuid}}).First(session) if db.Error != nil { return nil } @@ -26,7 +26,7 @@ func (this *SessionDao) CheckByUuid(uuid string) *Session { // Read var session = &Session{} - db := CONTEXT.DB.Where(&Session{Base: Base{Uuid: uuid}}).First(session) + db := CONTEXT.GetDB().Where(&Session{Base: Base{Uuid: uuid}}).First(session) this.PanicError(db.Error) return session } @@ -39,7 +39,7 @@ func (this *SessionDao) Create(session *Session) *Session { session.CreateTime = time.Now() session.UpdateTime = time.Now() session.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.DB.Create(session) + db := CONTEXT.GetDB().Create(session) this.PanicError(db.Error) return session @@ -49,7 +49,7 @@ func (this *SessionDao) Create(session *Session) *Session { func (this *SessionDao) Save(session *Session) *Session { session.UpdateTime = time.Now() - db := CONTEXT.DB.Save(session) + db := CONTEXT.GetDB().Save(session) this.PanicError(db.Error) return session @@ -60,7 +60,7 @@ func (this *SessionDao) Delete(uuid string) { session := this.CheckByUuid(uuid) session.ExpireTime = time.Now() - db := CONTEXT.DB.Delete(session) + db := CONTEXT.GetDB().Delete(session) this.PanicError(db.Error) @@ -69,6 +69,6 @@ func (this *SessionDao) Delete(uuid string) { //执行清理操作 func (this *SessionDao) Cleanup() { this.logger.Info("[SessionDao]执行清理:清除数据库中所有Session记录。") - db := CONTEXT.DB.Where("uuid is not null").Delete(Session{}) + db := CONTEXT.GetDB().Where("uuid is not null").Delete(Session{}) this.PanicError(db.Error) } diff --git a/code/rest/upload_token_dao.go b/code/rest/upload_token_dao.go index c21d3bc..e100686 100644 --- a/code/rest/upload_token_dao.go +++ b/code/rest/upload_token_dao.go @@ -14,7 +14,7 @@ func (this *UploadTokenDao) FindByUuid(uuid string) *UploadToken { // Read var uploadToken = &UploadToken{} - db := CONTEXT.DB.Where(&UploadToken{Base: Base{Uuid: uuid}}).First(uploadToken) + db := CONTEXT.GetDB().Where(&UploadToken{Base: Base{Uuid: uuid}}).First(uploadToken) if db.Error != nil { return nil } @@ -32,7 +32,7 @@ func (this *UploadTokenDao) Create(uploadToken *UploadToken) *UploadToken { uploadToken.CreateTime = time.Now() uploadToken.UpdateTime = time.Now() uploadToken.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.DB.Create(uploadToken) + db := CONTEXT.GetDB().Create(uploadToken) this.PanicError(db.Error) return uploadToken @@ -42,7 +42,7 @@ func (this *UploadTokenDao) Create(uploadToken *UploadToken) *UploadToken { func (this *UploadTokenDao) Save(uploadToken *UploadToken) *UploadToken { uploadToken.UpdateTime = time.Now() - db := CONTEXT.DB.Save(uploadToken) + db := CONTEXT.GetDB().Save(uploadToken) this.PanicError(db.Error) return uploadToken diff --git a/code/rest/user_dao.go b/code/rest/user_dao.go index bcdec14..4a319df 100644 --- a/code/rest/user_dao.go +++ b/code/rest/user_dao.go @@ -24,7 +24,7 @@ func (this *UserDao) Create(user *User) *User { user.LastTime = time.Now() user.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.DB.Create(user) + db := CONTEXT.GetDB().Create(user) this.PanicError(db.Error) return user @@ -35,7 +35,7 @@ func (this *UserDao) FindByUuid(uuid string) *User { // Read var user *User = &User{} - db := CONTEXT.DB.Where(&User{Base: Base{Uuid: uuid}}).First(user) + db := CONTEXT.GetDB().Where(&User{Base: Base{Uuid: uuid}}).First(user) if db.Error != nil { return nil } @@ -51,7 +51,7 @@ func (this *UserDao) CheckByUuid(uuid string) *User { // Read var user = &User{} - db := CONTEXT.DB.Where(&User{Base: Base{Uuid: uuid}}).First(user) + db := CONTEXT.GetDB().Where(&User{Base: Base{Uuid: uuid}}).First(user) this.PanicError(db.Error) return user } @@ -60,7 +60,7 @@ func (this *UserDao) CheckByUuid(uuid string) *User { func (this *UserDao) FindByUsername(username string) *User { var user = &User{} - db := CONTEXT.DB.Where(&User{Username: username}).First(user) + db := CONTEXT.GetDB().Where(&User{Username: username}).First(user) if db.Error != nil { return nil } @@ -71,7 +71,7 @@ func (this *UserDao) FindByUsername(username string) *User { func (this *UserDao) FindByEmail(email string) *User { var user *User = &User{} - db := CONTEXT.DB.Where(&User{Email: email}).First(user) + db := CONTEXT.GetDB().Where(&User{Email: email}).First(user) if db.Error != nil { return nil } @@ -100,15 +100,15 @@ func (this *UserDao) Page(page int, pageSize int, username string, email string, } count := 0 - db := CONTEXT.DB.Model(&User{}).Where(wp.Query, wp.Args...).Count(&count) + db := CONTEXT.GetDB().Model(&User{}).Where(wp.Query, wp.Args...).Count(&count) this.PanicError(db.Error) var users []*User orderStr := this.GetSortString(sortArray) if orderStr == "" { - db = CONTEXT.DB.Where(wp.Query, wp.Args...).Offset(page * pageSize).Limit(pageSize).Find(&users) + db = CONTEXT.GetDB().Where(wp.Query, wp.Args...).Offset(page * pageSize).Limit(pageSize).Find(&users) } else { - db = CONTEXT.DB.Where(wp.Query, wp.Args...).Order(orderStr).Offset(page * pageSize).Limit(pageSize).Find(&users) + db = CONTEXT.GetDB().Where(wp.Query, wp.Args...).Order(orderStr).Offset(page * pageSize).Limit(pageSize).Find(&users) } this.PanicError(db.Error) @@ -121,7 +121,7 @@ func (this *UserDao) Page(page int, pageSize int, username string, email string, //查询某个用户名是否已经有用户了 func (this *UserDao) CountByUsername(username string) int { var count int - db := CONTEXT.DB. + db := CONTEXT.GetDB(). Model(&User{}). Where("username = ?", username). Count(&count) @@ -132,7 +132,7 @@ func (this *UserDao) CountByUsername(username string) int { //查询某个邮箱是否已经有用户了 func (this *UserDao) CountByEmail(email string) int { var count int - db := CONTEXT.DB. + db := CONTEXT.GetDB(). Model(&User{}). Where("email = ?", email). Count(&count) @@ -144,7 +144,7 @@ func (this *UserDao) CountByEmail(email string) int { func (this *UserDao) Save(user *User) *User { user.UpdateTime = time.Now() - db := CONTEXT.DB. + db := CONTEXT.GetDB(). Save(user) this.PanicError(db.Error) return user @@ -153,6 +153,6 @@ func (this *UserDao) Save(user *User) *User { //执行清理操作 func (this *UserDao) Cleanup() { this.logger.Info("[UserDao]执行清理:清除数据库中所有User记录。") - db := CONTEXT.DB.Where("uuid is not null and role != ?", USER_ROLE_ADMINISTRATOR).Delete(User{}) + db := CONTEXT.GetDB().Where("uuid is not null and role != ?", USER_ROLE_ADMINISTRATOR).Delete(User{}) this.PanicError(db.Error) }