Refine the DB in context.

This commit is contained in:
zicla 2019-04-27 23:42:44 +08:00
parent 8e11641a7c
commit fa28bbb83f
10 changed files with 90 additions and 85 deletions

View File

@ -15,7 +15,7 @@ var CONTEXT = &Context{}
//上下文管理数据库连接管理所有路由请求管理所有的单例component. //上下文管理数据库连接管理所有路由请求管理所有的单例component.
type Context struct { type Context struct {
//数据库连接 //数据库连接
DB *gorm.DB db *gorm.DB
//session缓存 //session缓存
SessionCache *cache.Table SessionCache *cache.Table
//各类的Bean Map。这里面是包含ControllerMap中所有元素 //各类的Bean Map。这里面是包含ControllerMap中所有元素
@ -50,23 +50,28 @@ func (this *Context) Init() {
} }
//获取数据库对象
func (this *Context) GetDB() *gorm.DB {
return this.db
}
func (this *Context) OpenDb() { func (this *Context) OpenDb() {
var err error = nil 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 { if err != nil {
core.LOGGER.Panic("failed to connect mysql database") core.LOGGER.Panic("failed to connect mysql database")
} }
//是否打开sql日志(在调试阶段可以打开以方便查看执行的SQL) //是否打开sql日志(在调试阶段可以打开以方便查看执行的SQL)
this.DB.LogMode(false) this.db.LogMode(false)
} }
func (this *Context) CloseDb() { func (this *Context) CloseDb() {
if this.DB != nil { if this.db != nil {
err := this.DB.Close() err := this.db.Close()
if err != nil { if err != nil {
core.LOGGER.Error("关闭数据库连接出错 %s", err.Error()) core.LOGGER.Error("关闭数据库连接出错 %s", err.Error())
} }

View File

@ -19,7 +19,7 @@ func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard {
dashboard.CreateTime = time.Now() dashboard.CreateTime = time.Now()
dashboard.UpdateTime = time.Now() dashboard.UpdateTime = time.Now()
dashboard.Sort = time.Now().UnixNano() / 1e6 dashboard.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(dashboard) db := CONTEXT.GetDB().Create(dashboard)
this.PanicError(db.Error) this.PanicError(db.Error)
return dashboard return dashboard
@ -29,7 +29,7 @@ func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard {
func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard { func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard {
dashboard.UpdateTime = time.Now() dashboard.UpdateTime = time.Now()
db := CONTEXT.DB.Save(dashboard) db := CONTEXT.GetDB().Save(dashboard)
this.PanicError(db.Error) this.PanicError(db.Error)
return dashboard return dashboard
@ -38,7 +38,7 @@ func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard {
//删除一条记录 //删除一条记录
func (this *DashboardDao) Delete(dashboard *Dashboard) { func (this *DashboardDao) Delete(dashboard *Dashboard) {
db := CONTEXT.DB.Delete(&dashboard) db := CONTEXT.GetDB().Delete(&dashboard)
this.PanicError(db.Error) this.PanicError(db.Error)
} }
@ -47,7 +47,7 @@ func (this *DashboardDao) FindByDt(dt string) *Dashboard {
// Read // Read
var dashboard Dashboard 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 { if db.Error != nil {
return nil return nil
} }
@ -64,7 +64,7 @@ func (this *DashboardDao) Page(page int, pageSize int, dt string, sortArray []bu
} }
var conditionDB *gorm.DB 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 count := 0
db := conditionDB.Count(&count) db := conditionDB.Count(&count)
@ -89,7 +89,7 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes {
Value: "DESC", Value: "DESC",
}, },
} }
rows, err := CONTEXT.DB.Model(&Footprint{}). rows, err := CONTEXT.GetDB().Model(&Footprint{}).
Select("ip,COUNT(uuid) as times"). Select("ip,COUNT(uuid) as times").
Group("ip"). Group("ip").
Order(this.GetSortString(sortArray)). Order(this.GetSortString(sortArray)).
@ -99,8 +99,8 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes {
this.PanicError(err) this.PanicError(err)
for rows.Next() { for rows.Next() {
var ip string; var ip string
var times int64 = 0; var times int64 = 0
rows.Scan(&ip, &times) rows.Scan(&ip, &times)
item := &DashboardIpTimes{ item := &DashboardIpTimes{
Ip: ip, Ip: ip,
@ -115,6 +115,6 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes {
//执行清理操作 //执行清理操作
func (this *DashboardDao) Cleanup() { func (this *DashboardDao) Cleanup() {
this.logger.Info("[DashboardDao]执行清理清除数据库中所有Dashboard记录。") 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) this.PanicError(db.Error)
} }

View File

@ -14,7 +14,7 @@ func (this *DownloadTokenDao) FindByUuid(uuid string) *DownloadToken {
// Read // Read
var downloadToken = &DownloadToken{} 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 { if db.Error != nil {
return nil return nil
} }
@ -27,7 +27,7 @@ func (this *DownloadTokenDao) CheckByUuid(uuid string) *DownloadToken {
// Read // Read
var downloadToken = &DownloadToken{} 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) this.PanicError(db.Error)
return downloadToken return downloadToken
@ -42,7 +42,7 @@ func (this *DownloadTokenDao) Create(downloadToken *DownloadToken) *DownloadToke
downloadToken.CreateTime = time.Now() downloadToken.CreateTime = time.Now()
downloadToken.UpdateTime = time.Now() downloadToken.UpdateTime = time.Now()
downloadToken.Sort = time.Now().UnixNano() / 1e6 downloadToken.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(downloadToken) db := CONTEXT.GetDB().Create(downloadToken)
this.PanicError(db.Error) this.PanicError(db.Error)
return downloadToken return downloadToken
@ -52,7 +52,7 @@ func (this *DownloadTokenDao) Create(downloadToken *DownloadToken) *DownloadToke
func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken { func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken {
downloadToken.UpdateTime = time.Now() downloadToken.UpdateTime = time.Now()
db := CONTEXT.DB.Save(downloadToken) db := CONTEXT.GetDB().Save(downloadToken)
this.PanicError(db.Error) this.PanicError(db.Error)
return downloadToken return downloadToken
@ -61,6 +61,6 @@ func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken
//执行清理操作 //执行清理操作
func (this *DownloadTokenDao) Cleanup() { func (this *DownloadTokenDao) Cleanup() {
this.logger.Info("[DownloadTokenDao]执行清理清除数据库中所有DownloadToken记录。") 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) this.PanicError(db.Error)
} }

View File

@ -17,7 +17,7 @@ func (this *FootprintDao) FindByUuid(uuid string) *Footprint {
// Read // Read
var footprint Footprint 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 { if db.Error != nil {
return nil return nil
} }
@ -29,7 +29,7 @@ func (this *FootprintDao) CheckByUuid(uuid string) *Footprint {
// Read // Read
var footprint Footprint 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) this.PanicError(db.Error)
return &footprint return &footprint
@ -46,7 +46,7 @@ func (this *FootprintDao) Page(page int, pageSize int, userUuid string, sortArra
} }
var conditionDB *gorm.DB 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 count := 0
db := conditionDB.Count(&count) db := conditionDB.Count(&count)
@ -68,7 +68,7 @@ func (this *FootprintDao) Create(footprint *Footprint) *Footprint {
footprint.CreateTime = time.Now() footprint.CreateTime = time.Now()
footprint.UpdateTime = time.Now() footprint.UpdateTime = time.Now()
footprint.Sort = time.Now().UnixNano() / 1e6 footprint.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(footprint) db := CONTEXT.GetDB().Create(footprint)
this.PanicError(db.Error) this.PanicError(db.Error)
return footprint return footprint
@ -78,7 +78,7 @@ func (this *FootprintDao) Create(footprint *Footprint) *Footprint {
func (this *FootprintDao) Save(footprint *Footprint) *Footprint { func (this *FootprintDao) Save(footprint *Footprint) *Footprint {
footprint.UpdateTime = time.Now() footprint.UpdateTime = time.Now()
db := CONTEXT.DB.Save(footprint) db := CONTEXT.GetDB().Save(footprint)
this.PanicError(db.Error) this.PanicError(db.Error)
return footprint return footprint
@ -87,14 +87,14 @@ func (this *FootprintDao) Save(footprint *Footprint) *Footprint {
//删除一条记录 //删除一条记录
func (this *FootprintDao) Delete(footprint *Footprint) { func (this *FootprintDao) Delete(footprint *Footprint) {
db := CONTEXT.DB.Delete(&footprint) db := CONTEXT.GetDB().Delete(&footprint)
this.PanicError(db.Error) this.PanicError(db.Error)
} }
//获取一段时间中,总的数量 //获取一段时间中,总的数量
func (this *FootprintDao) CountBetweenTime(startTime time.Time, endTime time.Time) int64 { func (this *FootprintDao) CountBetweenTime(startTime time.Time, endTime time.Time) int64 {
var count 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) this.PanicError(db.Error)
return count return count
} }
@ -102,7 +102,7 @@ func (this *FootprintDao) CountBetweenTime(startTime time.Time, endTime time.Tim
//获取一段时间中UV的数量 //获取一段时间中UV的数量
func (this *FootprintDao) UvBetweenTime(startTime time.Time, endTime time.Time) int64 { func (this *FootprintDao) UvBetweenTime(startTime time.Time, endTime time.Time) int64 {
var count 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) this.PanicError(db.Error)
row := db.Row() row := db.Row()
row.Scan(&count) 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 { func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.Time) int64 {
var cost float64 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) this.PanicError(db.Error)
row := db.Row() row := db.Row()
row.Scan(&cost) row.Scan(&cost)
@ -121,13 +121,13 @@ func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.T
//删除某个时刻之前的记录 //删除某个时刻之前的记录
func (this *FootprintDao) DeleteByCreateTimeBefore(createTime time.Time) { 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) this.PanicError(db.Error)
} }
//执行清理操作 //执行清理操作
func (this *FootprintDao) Cleanup() { func (this *FootprintDao) Cleanup() {
this.logger.Info("[FootprintDao]执行清理清除数据库中所有Footprint记录。") 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) this.PanicError(db.Error)
} }

View File

@ -20,7 +20,7 @@ func (this *ImageCacheDao) FindByUuid(uuid string) *ImageCache {
// Read // Read
var imageCache ImageCache 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 { if db.Error != nil {
return nil return nil
} }
@ -32,7 +32,7 @@ func (this *ImageCacheDao) CheckByUuid(uuid string) *ImageCache {
// Read // Read
var imageCache ImageCache 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) this.PanicError(db.Error)
return &imageCache return &imageCache
@ -53,7 +53,7 @@ func (this *ImageCacheDao) FindByMatterUuidAndMode(matterUuid string, mode strin
} }
var imageCache = &ImageCache{} 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 { if db.Error != nil {
return nil return nil
@ -67,7 +67,7 @@ func (this *ImageCacheDao) CheckByUuidAndUserUuid(uuid string, userUuid string)
// Read // Read
var imageCache = &ImageCache{} 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) this.PanicError(db.Error)
return imageCache return imageCache
@ -79,7 +79,7 @@ func (this *ImageCacheDao) ListByUserUuidAndPuuidAndDirAndName(userUuid string)
var imageCaches []*ImageCache var imageCaches []*ImageCache
db := CONTEXT.DB. db := CONTEXT.GetDB().
Where(ImageCache{UserUuid: userUuid}). Where(ImageCache{UserUuid: userUuid}).
Find(&imageCaches) Find(&imageCaches)
this.PanicError(db.Error) this.PanicError(db.Error)
@ -101,7 +101,7 @@ func (this *ImageCacheDao) Page(page int, pageSize int, userUuid string, matterU
} }
var conditionDB *gorm.DB 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 count := 0
db := conditionDB.Count(&count) db := conditionDB.Count(&count)
@ -123,7 +123,7 @@ func (this *ImageCacheDao) Create(imageCache *ImageCache) *ImageCache {
imageCache.CreateTime = time.Now() imageCache.CreateTime = time.Now()
imageCache.UpdateTime = time.Now() imageCache.UpdateTime = time.Now()
imageCache.Sort = time.Now().UnixNano() / 1e6 imageCache.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(imageCache) db := CONTEXT.GetDB().Create(imageCache)
this.PanicError(db.Error) this.PanicError(db.Error)
return imageCache return imageCache
@ -133,7 +133,7 @@ func (this *ImageCacheDao) Create(imageCache *ImageCache) *ImageCache {
func (this *ImageCacheDao) Save(imageCache *ImageCache) *ImageCache { func (this *ImageCacheDao) Save(imageCache *ImageCache) *ImageCache {
imageCache.UpdateTime = time.Now() imageCache.UpdateTime = time.Now()
db := CONTEXT.DB.Save(imageCache) db := CONTEXT.GetDB().Save(imageCache)
this.PanicError(db.Error) this.PanicError(db.Error)
return imageCache return imageCache
@ -160,7 +160,7 @@ func (this *ImageCacheDao) deleteFileAndDir(imageCache *ImageCache) {
//删除一个文件,数据库中删除,物理磁盘上删除。 //删除一个文件,数据库中删除,物理磁盘上删除。
func (this *ImageCacheDao) Delete(imageCache *ImageCache) { func (this *ImageCacheDao) Delete(imageCache *ImageCache) {
db := CONTEXT.DB.Delete(&imageCache) db := CONTEXT.GetDB().Delete(&imageCache)
this.PanicError(db.Error) this.PanicError(db.Error)
this.deleteFileAndDir(imageCache) this.deleteFileAndDir(imageCache)
@ -176,11 +176,11 @@ func (this *ImageCacheDao) DeleteByMatterUuid(matterUuid string) {
//查询出即将删除的图片缓存 //查询出即将删除的图片缓存
var imageCaches []*ImageCache 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) 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) 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 { func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Time) int64 {
var size 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) this.PanicError(db.Error)
row := db.Row() row := db.Row()
row.Scan(&size) row.Scan(&size)
@ -203,6 +203,6 @@ func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Tim
//执行清理操作 //执行清理操作
func (this *ImageCacheDao) Cleanup() { func (this *ImageCacheDao) Cleanup() {
this.logger.Info("[ImageCacheDao]执行清理清除数据库中所有ImageCache记录。") 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) this.PanicError(db.Error)
} }

View File

@ -32,7 +32,7 @@ func (this *MatterDao) FindByUuid(uuid string) *Matter {
// Read // Read
var matter Matter 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 != nil {
if db.Error.Error() == result.DB_ERROR_NOT_FOUND { if db.Error.Error() == result.DB_ERROR_NOT_FOUND {
return nil return nil
@ -111,7 +111,7 @@ func (this *MatterDao) FindByUserUuidAndPuuidAndNameAndDirTrue(userUuid string,
wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{1}}) wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{1}})
var matter = &Matter{} 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 != nil {
return nil return nil
@ -125,7 +125,7 @@ func (this *MatterDao) CheckByUuidAndUserUuid(uuid string, userUuid string) *Mat
// Read // Read
var matter = &Matter{} 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) this.PanicError(db.Error)
return matter return matter
@ -154,7 +154,7 @@ func (this *MatterDao) CountByUserUuidAndPuuidAndDirAndName(userUuid string, puu
wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{dir}}) wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{dir}})
db := CONTEXT.DB. db := CONTEXT.GetDB().
Model(&matter). Model(&matter).
Where(wp.Query, wp.Args...). Where(wp.Query, wp.Args...).
Count(&count) Count(&count)
@ -168,7 +168,7 @@ func (this *MatterDao) ListByUserUuidAndPuuidAndDirAndName(userUuid string, puui
var matters []*Matter var matters []*Matter
db := CONTEXT.DB. db := CONTEXT.GetDB().
Where(Matter{UserUuid: userUuid, Puuid: puuid, Dir: dir, Name: name}). Where(Matter{UserUuid: userUuid, Puuid: puuid, Dir: dir, Name: name}).
Find(&matters) Find(&matters)
this.PanicError(db.Error) 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 { func (this *MatterDao) List(puuid string, userUuid string, sortArray []builder.OrderPair) []*Matter {
var matters []*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) this.PanicError(db.Error)
return matters 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}}) 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 { } else {
conditionDB = CONTEXT.DB.Model(&Matter{}).Where(wp.Query, wp.Args...) conditionDB = CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...)
} }
count := 0 count := 0
@ -248,7 +248,7 @@ func (this *MatterDao) Create(matter *Matter) *Matter {
matter.CreateTime = time.Now() matter.CreateTime = time.Now()
matter.UpdateTime = time.Now() matter.UpdateTime = time.Now()
matter.Sort = time.Now().UnixNano() / 1e6 matter.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(matter) db := CONTEXT.GetDB().Create(matter)
this.PanicError(db.Error) this.PanicError(db.Error)
return matter return matter
@ -258,7 +258,7 @@ func (this *MatterDao) Create(matter *Matter) *Matter {
func (this *MatterDao) Save(matter *Matter) *Matter { func (this *MatterDao) Save(matter *Matter) *Matter {
matter.UpdateTime = time.Now() matter.UpdateTime = time.Now()
db := CONTEXT.DB.Save(matter) db := CONTEXT.GetDB().Save(matter)
this.PanicError(db.Error) this.PanicError(db.Error)
return matter return matter
@ -266,7 +266,7 @@ func (this *MatterDao) Save(matter *Matter) *Matter {
//计数器加一 //计数器加一
func (this *MatterDao) TimesIncrement(matterUuid string) { 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) 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) this.PanicError(db.Error)
//从磁盘中删除该文件夹。 //从磁盘中删除该文件夹。
@ -291,7 +291,7 @@ func (this *MatterDao) Delete(matter *Matter) {
} else { } else {
//删除数据库中文件记录 //删除数据库中文件记录
db := CONTEXT.DB.Delete(&matter) db := CONTEXT.GetDB().Delete(&matter)
this.PanicError(db.Error) 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 { func (this *MatterDao) CountBetweenTime(startTime time.Time, endTime time.Time) int64 {
var count 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) this.PanicError(db.Error)
return count 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 { func (this *MatterDao) SizeBetweenTime(startTime time.Time, endTime time.Time) int64 {
var size 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) this.PanicError(db.Error)
row := db.Row() row := db.Row()
err := row.Scan(&size) 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 wp = &builder.WherePair{Query: "user_uuid = ? AND path = ?", Args: []interface{}{userUuid, path}}
var matter = &Matter{} 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 != nil {
if db.Error.Error() == result.DB_ERROR_NOT_FOUND { 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() { func (this *MatterDao) Cleanup() {
this.logger.Info("[MatterDao]执行清理清除数据库中所有Matter记录。删除磁盘中所有Matter文件。") 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) this.PanicError(db.Error)
err := os.RemoveAll(config.CONFIG.MatterPath) err := os.RemoveAll(config.CONFIG.MatterPath)

View File

@ -15,7 +15,7 @@ func (this *PreferenceDao) Fetch() *Preference {
// Read // Read
var preference = &Preference{} var preference = &Preference{}
db := CONTEXT.DB.First(preference) db := CONTEXT.GetDB().First(preference)
if db.Error != nil { if db.Error != nil {
if db.Error.Error() == result.DB_ERROR_NOT_FOUND { 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.CreateTime = time.Now()
preference.UpdateTime = time.Now() preference.UpdateTime = time.Now()
preference.Sort = time.Now().UnixNano() / 1e6 preference.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(preference) db := CONTEXT.GetDB().Create(preference)
this.PanicError(db.Error) this.PanicError(db.Error)
return preference return preference
@ -49,7 +49,7 @@ func (this *PreferenceDao) Create(preference *Preference) *Preference {
func (this *PreferenceDao) Save(preference *Preference) *Preference { func (this *PreferenceDao) Save(preference *Preference) *Preference {
preference.UpdateTime = time.Now() preference.UpdateTime = time.Now()
db := CONTEXT.DB.Save(preference) db := CONTEXT.GetDB().Save(preference)
this.PanicError(db.Error) this.PanicError(db.Error)
return preference return preference
@ -59,6 +59,6 @@ func (this *PreferenceDao) Save(preference *Preference) *Preference {
func (this *PreferenceDao) Cleanup() { func (this *PreferenceDao) Cleanup() {
this.logger.Info("[PreferenceDao]执行清理清除数据库中所有Preference记录。") 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) this.PanicError(db.Error)
} }

View File

@ -14,7 +14,7 @@ func (this *SessionDao) FindByUuid(uuid string) *Session {
// Read // Read
var session = &Session{} 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 { if db.Error != nil {
return nil return nil
} }
@ -26,7 +26,7 @@ func (this *SessionDao) CheckByUuid(uuid string) *Session {
// Read // Read
var session = &Session{} 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) this.PanicError(db.Error)
return session return session
} }
@ -39,7 +39,7 @@ func (this *SessionDao) Create(session *Session) *Session {
session.CreateTime = time.Now() session.CreateTime = time.Now()
session.UpdateTime = time.Now() session.UpdateTime = time.Now()
session.Sort = time.Now().UnixNano() / 1e6 session.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(session) db := CONTEXT.GetDB().Create(session)
this.PanicError(db.Error) this.PanicError(db.Error)
return session return session
@ -49,7 +49,7 @@ func (this *SessionDao) Create(session *Session) *Session {
func (this *SessionDao) Save(session *Session) *Session { func (this *SessionDao) Save(session *Session) *Session {
session.UpdateTime = time.Now() session.UpdateTime = time.Now()
db := CONTEXT.DB.Save(session) db := CONTEXT.GetDB().Save(session)
this.PanicError(db.Error) this.PanicError(db.Error)
return session return session
@ -60,7 +60,7 @@ func (this *SessionDao) Delete(uuid string) {
session := this.CheckByUuid(uuid) session := this.CheckByUuid(uuid)
session.ExpireTime = time.Now() session.ExpireTime = time.Now()
db := CONTEXT.DB.Delete(session) db := CONTEXT.GetDB().Delete(session)
this.PanicError(db.Error) this.PanicError(db.Error)
@ -69,6 +69,6 @@ func (this *SessionDao) Delete(uuid string) {
//执行清理操作 //执行清理操作
func (this *SessionDao) Cleanup() { func (this *SessionDao) Cleanup() {
this.logger.Info("[SessionDao]执行清理清除数据库中所有Session记录。") 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) this.PanicError(db.Error)
} }

View File

@ -14,7 +14,7 @@ func (this *UploadTokenDao) FindByUuid(uuid string) *UploadToken {
// Read // Read
var uploadToken = &UploadToken{} 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 { if db.Error != nil {
return nil return nil
} }
@ -32,7 +32,7 @@ func (this *UploadTokenDao) Create(uploadToken *UploadToken) *UploadToken {
uploadToken.CreateTime = time.Now() uploadToken.CreateTime = time.Now()
uploadToken.UpdateTime = time.Now() uploadToken.UpdateTime = time.Now()
uploadToken.Sort = time.Now().UnixNano() / 1e6 uploadToken.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(uploadToken) db := CONTEXT.GetDB().Create(uploadToken)
this.PanicError(db.Error) this.PanicError(db.Error)
return uploadToken return uploadToken
@ -42,7 +42,7 @@ func (this *UploadTokenDao) Create(uploadToken *UploadToken) *UploadToken {
func (this *UploadTokenDao) Save(uploadToken *UploadToken) *UploadToken { func (this *UploadTokenDao) Save(uploadToken *UploadToken) *UploadToken {
uploadToken.UpdateTime = time.Now() uploadToken.UpdateTime = time.Now()
db := CONTEXT.DB.Save(uploadToken) db := CONTEXT.GetDB().Save(uploadToken)
this.PanicError(db.Error) this.PanicError(db.Error)
return uploadToken return uploadToken

View File

@ -24,7 +24,7 @@ func (this *UserDao) Create(user *User) *User {
user.LastTime = time.Now() user.LastTime = time.Now()
user.Sort = time.Now().UnixNano() / 1e6 user.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(user) db := CONTEXT.GetDB().Create(user)
this.PanicError(db.Error) this.PanicError(db.Error)
return user return user
@ -35,7 +35,7 @@ func (this *UserDao) FindByUuid(uuid string) *User {
// Read // Read
var user *User = &User{} 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 { if db.Error != nil {
return nil return nil
} }
@ -51,7 +51,7 @@ func (this *UserDao) CheckByUuid(uuid string) *User {
// Read // Read
var user = &User{} 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) this.PanicError(db.Error)
return user return user
} }
@ -60,7 +60,7 @@ func (this *UserDao) CheckByUuid(uuid string) *User {
func (this *UserDao) FindByUsername(username string) *User { func (this *UserDao) FindByUsername(username string) *User {
var user = &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 { if db.Error != nil {
return nil return nil
} }
@ -71,7 +71,7 @@ func (this *UserDao) FindByUsername(username string) *User {
func (this *UserDao) FindByEmail(email string) *User { func (this *UserDao) FindByEmail(email string) *User {
var user *User = &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 { if db.Error != nil {
return nil return nil
} }
@ -100,15 +100,15 @@ func (this *UserDao) Page(page int, pageSize int, username string, email string,
} }
count := 0 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) this.PanicError(db.Error)
var users []*User var users []*User
orderStr := this.GetSortString(sortArray) orderStr := this.GetSortString(sortArray)
if orderStr == "" { 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 { } 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) 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 { func (this *UserDao) CountByUsername(username string) int {
var count int var count int
db := CONTEXT.DB. db := CONTEXT.GetDB().
Model(&User{}). Model(&User{}).
Where("username = ?", username). Where("username = ?", username).
Count(&count) Count(&count)
@ -132,7 +132,7 @@ func (this *UserDao) CountByUsername(username string) int {
//查询某个邮箱是否已经有用户了 //查询某个邮箱是否已经有用户了
func (this *UserDao) CountByEmail(email string) int { func (this *UserDao) CountByEmail(email string) int {
var count int var count int
db := CONTEXT.DB. db := CONTEXT.GetDB().
Model(&User{}). Model(&User{}).
Where("email = ?", email). Where("email = ?", email).
Count(&count) Count(&count)
@ -144,7 +144,7 @@ func (this *UserDao) CountByEmail(email string) int {
func (this *UserDao) Save(user *User) *User { func (this *UserDao) Save(user *User) *User {
user.UpdateTime = time.Now() user.UpdateTime = time.Now()
db := CONTEXT.DB. db := CONTEXT.GetDB().
Save(user) Save(user)
this.PanicError(db.Error) this.PanicError(db.Error)
return user return user
@ -153,6 +153,6 @@ func (this *UserDao) Save(user *User) *User {
//执行清理操作 //执行清理操作
func (this *UserDao) Cleanup() { func (this *UserDao) Cleanup() {
this.logger.Info("[UserDao]执行清理清除数据库中所有User记录。") 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) this.PanicError(db.Error)
} }