支持OR语句

This commit is contained in:
白茶清欢 2022-07-23 20:11:51 +08:00
parent 2fb098b469
commit 2e2846ddb0
2 changed files with 39 additions and 0 deletions

14
base.go
View File

@ -157,5 +157,19 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, table string, optionFuncList ...Se
tx = tx.Where(field+" < ?", value) tx = tx.Where(field+" < ?", value)
} }
} }
// or 语句
if nil != o.OR {
expression := ""
valList := make([]interface{}, 0)
for _, o := range o.OR {
if len(expression) > 0 {
expression = expression + " OR "
}
expression = expression + " " + o.Express
valList = append(valList, o.Value)
}
tx = tx.Where(expression, valList...)
}
return tx return tx
} }

View File

@ -14,6 +14,16 @@ package mysql
// Date : 11:46 2022/5/15 // Date : 11:46 2022/5/15
type SetOption func(o *Option) type SetOption func(o *Option)
// ORCondition OR 条件
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:07 2022/7/23
type ORCondition struct {
Express string `json:"express"` // 表达式
Value string `json:"value"` // 绑定值
}
// Option 扩展选项 // Option 扩展选项
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
@ -30,6 +40,7 @@ type Option struct {
Like map[string]string `json:"like"` // like 语句 Like map[string]string `json:"like"` // like 语句
NotLike map[string]string `json:"not_like"` // not like 语句 NotLike map[string]string `json:"not_like"` // not like 语句
NotEqual map[string]interface{} `json:"not_equal"` // != 语句 NotEqual map[string]interface{} `json:"not_equal"` // != 语句
OR []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
} }
// WithWhere 设置where条件 // WithWhere 设置where条件
@ -269,3 +280,17 @@ func WithBatchNotEqual(data map[string]interface{}) SetOption {
} }
} }
} }
// WithOR 设置OR语句
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:03 2022/7/23
func WithOR(orConditionList []*ORCondition) SetOption {
return func(o *Option) {
if nil == o.OR {
o.OR = make([]*ORCondition, 0)
}
o.OR = append(o.OR, orConditionList...)
}
}