查询条件支持覆盖默认表名

This commit is contained in:
白茶清欢 2023-03-22 21:02:24 +08:00
parent 5808a650dd
commit f2a4e10eb2
2 changed files with 25 additions and 8 deletions

21
base.go
View File

@ -26,7 +26,7 @@ type BaseDao struct {
// //
// Date : 8:06 下午 2021/8/8 // Date : 8:06 下午 2021/8/8
func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}) error { func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}) error {
return dbInstance.Table(b.TableName).Create(data).Error return dbInstance.Create(data).Error
} }
// Update 更新数据 // Update 更新数据
@ -36,7 +36,7 @@ func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}) error {
// Date : 8:12 下午 2021/8/8 // Date : 8:12 下午 2021/8/8
func (b *BaseDao) Update(dbInstance *gorm.DB, updateDate interface{}, optionFuncList ...SetOption) (int64, error) { func (b *BaseDao) Update(dbInstance *gorm.DB, updateDate interface{}, optionFuncList ...SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
r := dbInstance.Table(b.TableName).Updates(updateDate) r := dbInstance.Updates(updateDate)
return r.RowsAffected, r.Error 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 // Date : 8:14 下午 2021/8/8
func (b *BaseDao) List(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error { func (b *BaseDao) List(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
return dbInstance.Table(b.TableName).Find(result).Error return dbInstance.Find(result).Error
} }
// Delete 删除数据, 硬删除, 对应 delete语句 // Delete 删除数据, 硬删除, 对应 delete语句
@ -56,7 +56,7 @@ func (b *BaseDao) List(dbInstance *gorm.DB, result interface{}, optionFuncList .
// //
// Date : 11:46 2023/2/9 // Date : 11:46 2023/2/9
func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel interface{}, optionFuncList ...SetOption) (int64, error) { 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 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 // Date : 8:25 下午 2021/8/8
func (b *BaseDao) Detail(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error { func (b *BaseDao) Detail(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
return dbInstance.Table(b.TableName).First(result).Error return dbInstance.First(result).Error
} }
// IsNotFound 增加结果是否为数据不存在的判断 // IsNotFound 增加结果是否为数据不存在的判断
@ -87,7 +87,7 @@ func (b *BaseDao) IsNotFound(err error) bool {
func (b *BaseDao) Count(dbInstance *gorm.DB, optionFuncList ...SetOption) (int64, error) { func (b *BaseDao) Count(dbInstance *gorm.DB, optionFuncList ...SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
var cnt int64 var cnt int64
return cnt, dbInstance.Table(b.TableName).Count(&cnt).Error return cnt, dbInstance.Count(&cnt).Error
} }
// Tx 执行事务 // Tx 执行事务
@ -140,8 +140,6 @@ func (b *BaseDao) Rollback(db *gorm.DB) error {
// //
// Date : 17:38 2022/5/15 // 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 ...SetOption) *gorm.DB {
// 指定查询的表
tx = tx.Table(b.TableName)
// 构建查询条件 // 构建查询条件
o := &Option{} o := &Option{}
@ -149,6 +147,13 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
fn(o) fn(o)
} }
// 指定查询的表
if len(o.Table) > 0 {
tx.Table(o.Table)
} else {
tx = tx.Table(b.TableName)
}
// 设置where条件 // 设置where条件
if nil != o.Where && len(o.Where) > 0 { if nil != o.Where && len(o.Where) > 0 {
tx = tx.Where(o.Where) tx = tx.Where(o.Where)

View File

@ -30,6 +30,7 @@ type ORCondition struct {
// //
// Date : 8:05 下午 2021/8/8 // Date : 8:05 下午 2021/8/8
type Option struct { type Option struct {
Table string `json:"table"` // 查询的数据表
Limit int `json:"limit"` // 限制数量 Limit int `json:"limit"` // 限制数量
Offset int `json:"offset"` // 偏移量 Offset int `json:"offset"` // 偏移量
In map[string]interface{} `json:"in"` // in语句 In map[string]interface{} `json:"in"` // in语句
@ -44,6 +45,17 @@ type Option struct {
OR []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系 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条件 // WithWhere 设置where条件
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>