升级sql构建, 支持or语句
This commit is contained in:
96
option.go
96
option.go
@ -8,7 +8,10 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.zhangdeman.cn/zhangdeman/op_type"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SetOption 设置选项
|
||||
@ -393,3 +396,96 @@ func WithOrderAsc(field string) SetOption {
|
||||
o.Order = append(o.Order, field+" asc")
|
||||
}
|
||||
}
|
||||
|
||||
// newOption 生成新的option
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:46 2024/8/9
|
||||
func newOption(setOptionList ...SetOption) *Option {
|
||||
o := &Option{}
|
||||
for _, item := range setOptionList {
|
||||
item(o)
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
// optionToSql 基于 option 配置生成sql
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:46 2024/8/9
|
||||
func optionToSql(o *Option) (sqlBuildResult string, bindValue []any) {
|
||||
bindValue = make([]any, 0)
|
||||
sqlBuildResultBlockList := make([]string, 0)
|
||||
// 设置where条件
|
||||
for fieldName, fieldValue := range o.Where {
|
||||
if nil == fieldValue {
|
||||
continue
|
||||
}
|
||||
if reflect.TypeOf(fieldValue).Kind() == reflect.Slice {
|
||||
// 传入数组, in语句
|
||||
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` in (?)")
|
||||
byteData, _ := json.Marshal(fieldValue)
|
||||
bindValue = append(bindValue, strings.TrimRight(strings.TrimLeft(string(byteData), "["), "]"))
|
||||
} else {
|
||||
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` = ?")
|
||||
bindValue = append(bindValue, fieldValue)
|
||||
}
|
||||
}
|
||||
// 设置不等于
|
||||
for fieldName, fieldValue := range o.NotEqual {
|
||||
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` != ?")
|
||||
bindValue = append(bindValue, fieldValue)
|
||||
}
|
||||
|
||||
// in 语句
|
||||
// 传入数组, in语句
|
||||
for fieldName, fieldValue := range o.In {
|
||||
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` in (?)")
|
||||
byteData, _ := json.Marshal(fieldValue)
|
||||
bindValue = append(bindValue, strings.TrimRight(strings.TrimLeft(string(byteData), "["), "]"))
|
||||
}
|
||||
|
||||
// not in 语句
|
||||
for fieldName, fieldValue := range o.NotIn {
|
||||
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` NOT IN (?)")
|
||||
byteData, _ := json.Marshal(fieldValue)
|
||||
bindValue = append(bindValue, strings.TrimRight(strings.TrimLeft(string(byteData), "["), "]"))
|
||||
}
|
||||
|
||||
// like 语句
|
||||
for fieldName, fieldValue := range o.Like {
|
||||
if len(fieldValue) == 0 {
|
||||
continue
|
||||
}
|
||||
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` LIKE \"%?%\"")
|
||||
bindValue = append(bindValue, fieldValue)
|
||||
}
|
||||
|
||||
// NOT LIKE 语句
|
||||
for fieldName, fieldValue := range o.NotLike {
|
||||
if len(fieldValue) == 0 {
|
||||
continue
|
||||
}
|
||||
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` NOT LIKE \"%?%\"")
|
||||
bindValue = append(bindValue, fieldValue)
|
||||
}
|
||||
|
||||
// >=
|
||||
for fieldName, fieldValue := range o.Start {
|
||||
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` >= ?")
|
||||
bindValue = append(bindValue, fieldValue)
|
||||
}
|
||||
|
||||
// <
|
||||
for fieldName, fieldValue := range o.End {
|
||||
sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` < ?")
|
||||
bindValue = append(bindValue, fieldValue)
|
||||
}
|
||||
|
||||
if len(bindValue) > 0 {
|
||||
sqlBuildResult = strings.Join(sqlBuildResultBlockList, " AND ")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user