完成一版 setTxCondition

This commit is contained in:
白茶清欢 2022-05-15 18:04:06 +08:00
parent 43588655ef
commit 670c7c7af9
1 changed files with 79 additions and 27 deletions

106
base.go
View File

@ -5,26 +5,19 @@
// Date : 8:04 下午 2021/8/8
package mysql
import "gorm.io/gorm"
import (
"gorm.io/gorm"
)
type BaseDao struct {
}
// CreateNewData 创建新的数据
// CreateNewRecord 创建新的数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:06 下午 2021/8/8
func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, table string, data interface{}, o *Option) error {
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
func (b *BaseDao) CreateNewRecord(dbInstance *gorm.DB, table string, data interface{}) error {
return dbInstance.Table(table).Create(data).Error
}
@ -33,18 +26,8 @@ func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, table string, data interfac
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:12 下午 2021/8/8
func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, data interface{}, condition interface{}, o *Option) error {
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
return dbInstance.Table(table).Where(condition).Updates(data).Error
func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, updateDate interface{}, optionFuncList ...SetOption) error {
return dbInstance.Table(table).Updates(updateDate).Error
}
// Select 查询数据列表
@ -52,7 +35,7 @@ func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, data interface{}
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:14 下午 2021/8/8
func (b *BaseDao) Select(dbInstance *gorm.DB, table string,condition interface{}, result interface{}, o *Option) error {
func (b *BaseDao) Select(dbInstance *gorm.DB, table string, condition interface{}, result interface{}, o *Option) error {
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
@ -70,7 +53,7 @@ func (b *BaseDao) Select(dbInstance *gorm.DB, table string,condition interface{}
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Detail(dbInstance *gorm.DB,table string, condition interface{}, result interface{}, o *Option) error {
func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, condition interface{}, result interface{}, o *Option) error {
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
@ -88,7 +71,7 @@ func (b *BaseDao) Detail(dbInstance *gorm.DB,table string, condition interface{}
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Count(dbInstance *gorm.DB, table string,condition interface{}, o *Option) (int64, error) {
func (b *BaseDao) Count(dbInstance *gorm.DB, table string, condition interface{}, o *Option) (int64, error) {
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
@ -128,3 +111,72 @@ func (b *BaseDao) Commit(db *gorm.DB) error {
func (b *BaseDao) Rollback(db *gorm.DB) error {
return db.Rollback().Error
}
// setTxCondition 设置查询条件
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:38 2022/5/15
func (b *BaseDao) setTxCondition(tx *gorm.DB, table string, optionFuncList ...SetOption) {
// 指定查询的表
tx.Table(table)
// 构建查询条件
o := &Option{}
for _, fn := range optionFuncList {
fn(o)
}
// 设置where条件
if nil != o.Where {
tx.Where(o.Where)
}
// 设置 limit offset
if o.Limit > 0 {
tx.Limit(o.Limit)
}
if o.Offset >= 0 {
tx.Offset(o.Offset)
}
// in 语句
if nil != o.In {
tx.Where(o.In)
}
// not in 语句
if nil != o.NotIn {
for field, value := range o.NotIn {
tx.Where(field+" NOT IN ? ", value)
}
}
// like 语句
if nil != o.Like {
for field, value := range o.NotIn {
tx.Where(field+" LIKE ? ", value)
}
}
// NOT LIKE 语句
if nil != o.NotLike {
for field, value := range o.NotLike {
tx.Where(field+" NOT LIKE ? ", value)
}
}
// >=
if nil != o.Start {
for field, value := range o.Start {
tx.Where(field+" >= ?", value)
}
}
// <
if nil != o.End {
for field, value := range o.End {
tx.Where(field+" < ?", value)
}
}
}