优化 or 语句 + 支持 between / not between
This commit is contained in:
parent
15b1d9e6b0
commit
1071c7558e
69
base.go
69
base.go
@ -199,7 +199,72 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
|
|||||||
tx = tx.Offset(o.Offset)
|
tx = tx.Offset(o.Offset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sqlBlockList := make([]string, 0)
|
// in 语句
|
||||||
|
if nil != o.In {
|
||||||
|
tx = tx.Where(o.In)
|
||||||
|
}
|
||||||
|
|
||||||
|
// not in 语句
|
||||||
|
if nil != o.NotIn {
|
||||||
|
for field, value := range o.NotIn {
|
||||||
|
tx = tx.Where(field+" NOT IN ? ", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// like 语句
|
||||||
|
if nil != o.Like {
|
||||||
|
for field, value := range o.Like {
|
||||||
|
tx = tx.Where(field+" LIKE ? ", "%"+value+"%")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOT LIKE 语句
|
||||||
|
if nil != o.NotLike {
|
||||||
|
for field, value := range o.NotLike {
|
||||||
|
tx = tx.Where(field+" NOT LIKE ? ", "%"+value+"%")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// >=
|
||||||
|
if nil != o.Start {
|
||||||
|
for field, value := range o.Start {
|
||||||
|
tx = tx.Where(field+" >= ?", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// <
|
||||||
|
if nil != o.End {
|
||||||
|
for field, value := range o.End {
|
||||||
|
tx = tx.Where(field+" < ?", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// between
|
||||||
|
for field, betweenVal := range o.Between {
|
||||||
|
tx = tx.Where("`"+field+"` BETWEEN ? AND ?", betweenVal[0], betweenVal[1])
|
||||||
|
}
|
||||||
|
// not between
|
||||||
|
for field, notBetweenVal := range o.NotBetween {
|
||||||
|
tx = tx.Where("`"+field+"` NOT BETWEEN ? AND ?", notBetweenVal[0], notBetweenVal[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// 排序
|
||||||
|
for _, orderRule := range o.Order {
|
||||||
|
tx = tx.Order(orderRule)
|
||||||
|
}
|
||||||
|
|
||||||
|
// or 语句
|
||||||
|
if nil != o.OR {
|
||||||
|
for _, itemOr := range o.OR {
|
||||||
|
orOption := &Option{}
|
||||||
|
for _, fn := range itemOr {
|
||||||
|
fn(orOption)
|
||||||
|
}
|
||||||
|
orSql, orBindVal := optionToSql(orOption)
|
||||||
|
tx.Or(orSql, orBindVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*sqlBlockList := make([]string, 0)
|
||||||
bindValueList := make([]any, 0)
|
bindValueList := make([]any, 0)
|
||||||
sql, bindVal := optionToSql(o)
|
sql, bindVal := optionToSql(o)
|
||||||
tx.Or(sql, bindVal...)
|
tx.Or(sql, bindVal...)
|
||||||
@ -212,6 +277,6 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
|
|||||||
}
|
}
|
||||||
orSql, orBindVal := optionToSql(orOption)
|
orSql, orBindVal := optionToSql(orOption)
|
||||||
tx.Or(orSql, orBindVal...)
|
tx.Or(orSql, orBindVal...)
|
||||||
}
|
}*/
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
69
option.go
69
option.go
@ -31,20 +31,22 @@ type SetOption func(o *Option)
|
|||||||
//
|
//
|
||||||
// Date : 8:05 下午 2021/8/8
|
// Date : 8:05 下午 2021/8/8
|
||||||
type Option struct {
|
type Option struct {
|
||||||
Model any `json:"-"` // 操作model
|
Model any `json:"-"` // 操作model
|
||||||
Table string `json:"table"` // 查询的数据表
|
Table string `json:"table"` // 查询的数据表
|
||||||
Limit int `json:"limit"` // 限制数量
|
Limit int `json:"limit"` // 限制数量
|
||||||
Offset int `json:"offset"` // 偏移量
|
Offset int `json:"offset"` // 偏移量
|
||||||
In map[string]any `json:"in"` // in语句
|
In map[string]any `json:"in"` // in语句
|
||||||
NotIn map[string]any `json:"not_in"` // not in语句
|
NotIn map[string]any `json:"not_in"` // not in语句
|
||||||
Where map[string]any `json:"where"` // where 条件
|
Where map[string]any `json:"where"` // where 条件
|
||||||
Start map[string]any `json:"start"` // >= 条件
|
Between map[string][2]any `json:"between"` // between 条件
|
||||||
End map[string]any `json:"end"` // < 条件
|
NotBetween map[string][2]any `json:"not_between"` // not between 条件
|
||||||
Like map[string]string `json:"like"` // like 语句
|
Start map[string]any `json:"start"` // >= 条件
|
||||||
NotLike map[string]string `json:"not_like"` // not like 语句
|
End map[string]any `json:"end"` // < 条件
|
||||||
NotEqual map[string]any `json:"not_equal"` // != 语句
|
Like map[string]string `json:"like"` // like 语句
|
||||||
Order []string `json:"order"` // 排序规则
|
NotLike map[string]string `json:"not_like"` // not like 语句
|
||||||
OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
|
NotEqual map[string]any `json:"not_equal"` // != 语句
|
||||||
|
Order []string `json:"order"` // 排序规则
|
||||||
|
OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithModel ...
|
// WithModel ...
|
||||||
@ -488,6 +490,17 @@ func optionToSql(o *Option) (sqlBuildResult string, bindValue []any) {
|
|||||||
bindValue = append(bindValue, fieldValue)
|
bindValue = append(bindValue, fieldValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// between
|
||||||
|
for field, betweenVal := range o.Between {
|
||||||
|
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+field+"` BETWEEN ? AND ?")
|
||||||
|
bindValue = append(bindValue, betweenVal[0], betweenVal[1])
|
||||||
|
}
|
||||||
|
// not between
|
||||||
|
for field, notBetweenVal := range o.NotBetween {
|
||||||
|
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+field+"` NOT BETWEEN ? AND ?")
|
||||||
|
bindValue = append(bindValue, notBetweenVal[0], notBetweenVal[1])
|
||||||
|
}
|
||||||
|
|
||||||
if len(bindValue) > 0 {
|
if len(bindValue) > 0 {
|
||||||
sqlBuildResult = strings.Join(sqlBuildResultBlockList, " AND ")
|
sqlBuildResult = strings.Join(sqlBuildResultBlockList, " AND ")
|
||||||
}
|
}
|
||||||
@ -513,6 +526,34 @@ func parseInSql(fieldValue any) (string, []any) {
|
|||||||
return strings.Join(placeholderList, ","), dataList
|
return strings.Join(placeholderList, ","), dataList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithBetween between 语句
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 15:52 2024/8/23
|
||||||
|
func WithBetween(field string, left any, right any) SetOption {
|
||||||
|
return func(o *Option) {
|
||||||
|
if nil == o.Between {
|
||||||
|
o.Between = map[string][2]any{}
|
||||||
|
}
|
||||||
|
o.Between[field] = [2]any{left, right}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithNotBetween not between 语句
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 15:52 2024/8/23
|
||||||
|
func WithNotBetween(field string, left any, right any) SetOption {
|
||||||
|
return func(o *Option) {
|
||||||
|
if nil == o.NotBetween {
|
||||||
|
o.NotBetween = map[string][2]any{}
|
||||||
|
}
|
||||||
|
o.NotBetween[field] = [2]any{left, right}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithAnyCondition 设置任意查询条件, 仅 where 子句 in / not in / like / not like / == / !=
|
// WithAnyCondition 设置任意查询条件, 仅 where 子句 in / not in / like / not like / == / !=
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
Loading…
Reference in New Issue
Block a user