修复代码BUG + 循环引用

This commit is contained in:
2024-08-23 17:54:48 +08:00
parent f359598109
commit c6b8d29b61
9 changed files with 154 additions and 115 deletions

23
base.go
View File

@ -7,6 +7,7 @@ package database
import (
"errors"
"git.zhangdeman.cn/zhangdeman/database/define"
"gorm.io/gorm"
)
@ -25,8 +26,8 @@ type BaseDao struct {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:06 下午 2021/8/8
func (b *BaseDao) Create(dbInstance *gorm.DB, data any, optionList ...SetOption) error {
o := &Option{}
func (b *BaseDao) Create(dbInstance *gorm.DB, data any, optionList ...define.SetOption) error {
o := &define.Option{}
for _, itemFunc := range optionList {
itemFunc(o)
}
@ -42,7 +43,7 @@ func (b *BaseDao) Create(dbInstance *gorm.DB, data any, optionList ...SetOption)
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:12 下午 2021/8/8
func (b *BaseDao) Update(dbInstance *gorm.DB, updateData any, optionFuncList ...SetOption) (int64, error) {
func (b *BaseDao) Update(dbInstance *gorm.DB, updateData any, optionFuncList ...define.SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
r := dbInstance.Updates(updateData)
return r.RowsAffected, r.Error
@ -53,7 +54,7 @@ func (b *BaseDao) Update(dbInstance *gorm.DB, updateData any, optionFuncList ...
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:05 2024/1/13
func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...SetOption) (int64, error) {
func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...define.SetOption) (int64, error) {
optionFuncList = append(optionFuncList, WithLimit(1, 0))
return b.Update(dbInstance, updateData, optionFuncList...)
}
@ -63,7 +64,7 @@ func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:14 下午 2021/8/8
func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...SetOption) error {
func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...define.SetOption) error {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
return dbInstance.Find(result).Error
}
@ -73,7 +74,7 @@ func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...SetOpt
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:46 2023/2/9
func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...SetOption) (int64, error) {
func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...define.SetOption) (int64, error) {
dbInstance = dbInstance.Model(dataModel)
dbInstance = b.setTxCondition(dbInstance, optionFuncList...).Delete(dataModel)
return dbInstance.RowsAffected, dbInstance.Error
@ -84,7 +85,7 @@ func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...S
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Detail(dbInstance *gorm.DB, result any, optionFuncList ...SetOption) error {
func (b *BaseDao) Detail(dbInstance *gorm.DB, result any, optionFuncList ...define.SetOption) error {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
return dbInstance.First(result).Error
}
@ -118,7 +119,7 @@ func (b *BaseDao) IsNotFound(err error) bool {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Count(dbInstance *gorm.DB, optionFuncList ...SetOption) (int64, error) {
func (b *BaseDao) Count(dbInstance *gorm.DB, optionFuncList ...define.SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
var cnt int64
return cnt, dbInstance.Count(&cnt).Error
@ -173,10 +174,10 @@ func (b *BaseDao) Rollback(db *gorm.DB) error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:38 2022/5/15
func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm.DB {
func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...define.SetOption) *gorm.DB {
// 构建查询条件
o := &Option{}
o := &define.Option{}
for _, fn := range optionFuncList {
fn(o)
}
@ -256,7 +257,7 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
// or 语句
if nil != o.OR {
for _, itemOr := range o.OR {
orOption := &Option{}
orOption := &define.Option{}
for _, fn := range itemOr {
fn(orOption)
}