diff --git a/base.go b/base.go index 3f128b5..491bf61 100644 --- a/base.go +++ b/base.go @@ -26,7 +26,7 @@ type BaseDao struct { // // Date : 8:06 下午 2021/8/8 func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}) error { - return dbInstance.Table(b.TableName).Create(data).Error + return dbInstance.Create(data).Error } // Update 更新数据 @@ -36,7 +36,7 @@ func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}) error { // Date : 8:12 下午 2021/8/8 func (b *BaseDao) Update(dbInstance *gorm.DB, updateDate interface{}, optionFuncList ...SetOption) (int64, error) { dbInstance = b.setTxCondition(dbInstance, optionFuncList...) - r := dbInstance.Table(b.TableName).Updates(updateDate) + r := dbInstance.Updates(updateDate) return r.RowsAffected, r.Error } @@ -47,7 +47,7 @@ func (b *BaseDao) Update(dbInstance *gorm.DB, updateDate interface{}, optionFunc // Date : 8:14 下午 2021/8/8 func (b *BaseDao) List(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error { dbInstance = b.setTxCondition(dbInstance, optionFuncList...) - return dbInstance.Table(b.TableName).Find(result).Error + return dbInstance.Find(result).Error } // Delete 删除数据, 硬删除, 对应 delete语句 @@ -56,7 +56,7 @@ func (b *BaseDao) List(dbInstance *gorm.DB, result interface{}, optionFuncList . // // Date : 11:46 2023/2/9 func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel interface{}, optionFuncList ...SetOption) (int64, error) { - dbInstance = b.setTxCondition(dbInstance, optionFuncList...).Table(b.TableName).Delete(dataModel) + dbInstance = b.setTxCondition(dbInstance, optionFuncList...).Delete(dataModel) return dbInstance.RowsAffected, dbInstance.Error } @@ -67,7 +67,7 @@ func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel interface{}, optionFuncL // Date : 8:25 下午 2021/8/8 func (b *BaseDao) Detail(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error { dbInstance = b.setTxCondition(dbInstance, optionFuncList...) - return dbInstance.Table(b.TableName).First(result).Error + return dbInstance.First(result).Error } // IsNotFound 增加结果是否为数据不存在的判断 @@ -87,7 +87,7 @@ func (b *BaseDao) IsNotFound(err error) bool { func (b *BaseDao) Count(dbInstance *gorm.DB, optionFuncList ...SetOption) (int64, error) { dbInstance = b.setTxCondition(dbInstance, optionFuncList...) var cnt int64 - return cnt, dbInstance.Table(b.TableName).Count(&cnt).Error + return cnt, dbInstance.Count(&cnt).Error } // Tx 执行事务 @@ -140,8 +140,6 @@ func (b *BaseDao) Rollback(db *gorm.DB) error { // // Date : 17:38 2022/5/15 func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm.DB { - // 指定查询的表 - tx = tx.Table(b.TableName) // 构建查询条件 o := &Option{} @@ -149,6 +147,13 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm fn(o) } + // 指定查询的表 + if len(o.Table) > 0 { + tx.Table(o.Table) + } else { + tx = tx.Table(b.TableName) + } + // 设置where条件 if nil != o.Where && len(o.Where) > 0 { tx = tx.Where(o.Where) diff --git a/option.go b/option.go index e8a3ef4..af3052e 100644 --- a/option.go +++ b/option.go @@ -30,6 +30,7 @@ type ORCondition struct { // // Date : 8:05 下午 2021/8/8 type Option struct { + Table string `json:"table"` // 查询的数据表 Limit int `json:"limit"` // 限制数量 Offset int `json:"offset"` // 偏移量 In map[string]interface{} `json:"in"` // in语句 @@ -44,6 +45,17 @@ type Option struct { OR []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系 } +// WithTable 设置查询的表名 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:56 2023/3/22 +func WithTable(tableName string) SetOption { + return func(o *Option) { + o.Table = tableName + } +} + // WithWhere 设置where条件 // // Author : go_developer@163.com<白茶清欢>