database/base.go

131 lines
2.8 KiB
Go
Raw Normal View History

2022-05-15 11:27:28 +08:00
// Package mysql ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:04 下午 2021/8/8
package mysql
import "gorm.io/gorm"
type BaseDao struct {
}
// CreateNewData 创建新的数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:06 下午 2021/8/8
2022-05-15 11:40:48 +08:00
func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, table string, data interface{}, o *Option) error {
2022-05-15 11:27:28 +08:00
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
2022-05-15 11:40:48 +08:00
return dbInstance.Table(table).Create(data).Error
2022-05-15 11:27:28 +08:00
}
// UpdateData 更新数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:12 下午 2021/8/8
2022-05-15 11:40:48 +08:00
func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, data interface{}, condition interface{}, o *Option) error {
2022-05-15 11:27:28 +08:00
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
2022-05-15 11:40:48 +08:00
return dbInstance.Table(table).Where(condition).Updates(data).Error
2022-05-15 11:27:28 +08:00
}
// Select 查询数据列表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:14 下午 2021/8/8
2022-05-15 11:40:48 +08:00
func (b *BaseDao) Select(dbInstance *gorm.DB, table string,condition interface{}, result interface{}, o *Option) error {
2022-05-15 11:27:28 +08:00
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
2022-05-15 11:40:48 +08:00
return dbInstance.Table(table).Find(result, condition).Error
2022-05-15 11:27:28 +08:00
}
// Detail 查询详情
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
2022-05-15 11:40:48 +08:00
func (b *BaseDao) Detail(dbInstance *gorm.DB,table string, condition interface{}, result interface{}, o *Option) error {
2022-05-15 11:27:28 +08:00
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
2022-05-15 11:40:48 +08:00
return dbInstance.Table(table).First(result, condition).Error
2022-05-15 11:27:28 +08:00
}
// Count 查询数量
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
2022-05-15 11:40:48 +08:00
func (b *BaseDao) Count(dbInstance *gorm.DB, table string,condition interface{}, o *Option) (int64, error) {
2022-05-15 11:27:28 +08:00
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
var cnt int64
2022-05-15 11:40:48 +08:00
return cnt, dbInstance.Table(table).Where(condition).Count(&cnt).Error
2022-05-15 11:27:28 +08:00
}
// Begin 开启事务
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:09 下午 2021/8/9
func (b *BaseDao) Begin(dbInstance *gorm.DB) *gorm.DB {
return dbInstance.Begin()
}
// Commit 提交事务
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:10 下午 2021/8/9
func (b *BaseDao) Commit(db *gorm.DB) error {
return db.Commit().Error
}
// Rollback 回滚
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:12 下午 2021/8/9
func (b *BaseDao) Rollback(db *gorm.DB) error {
return db.Rollback().Error
}