计划升级or语句支持

This commit is contained in:
白茶清欢 2024-08-09 17:24:47 +08:00
parent ce2f962784
commit 88f9e831d8
2 changed files with 10 additions and 30 deletions

22
base.go
View File

@ -192,11 +192,6 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
tx = tx.Model(o.Model)
}
// 设置where条件
if nil != o.Where && len(o.Where) > 0 {
tx = tx.Where(o.Where)
}
// 设置 limit offset
if o.Limit > 0 {
tx = tx.Limit(o.Limit)
@ -205,6 +200,11 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
}
}
// 设置where条件
if nil != o.Where && len(o.Where) > 0 {
tx = tx.Where(o.Where)
}
// in 语句
if nil != o.In {
tx = tx.Where(o.In)
@ -251,17 +251,7 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
}
// or 语句
if nil != o.OR {
expression := ""
valList := make([]any, 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...)
if len(o.OR) > 0 {
}
return tx
}

View File

@ -18,16 +18,6 @@ import (
// Date : 11:46 2022/5/15
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 any `json:"value"` // 绑定值
}
// Option 扩展选项
//
// Author : go_developer@163.com<白茶清欢>
@ -47,7 +37,7 @@ type Option struct {
NotLike map[string]string `json:"not_like"` // not like 语句
NotEqual map[string]any `json:"not_equal"` // != 语句
Order []string `json:"order"` // 排序规则
OR []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
}
// WithModel ...
@ -356,12 +346,12 @@ func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:03 2022/7/23
func WithOR(orConditionList ...*ORCondition) SetOption {
func WithOR(orConditionList ...SetOption) SetOption {
return func(o *Option) {
if nil == o.OR {
o.OR = make([]*ORCondition, 0)
o.OR = make([][]SetOption, 0)
}
o.OR = append(o.OR, orConditionList...)
o.OR = append(o.OR, orConditionList)
}
}