增加 WithAnyCondition

This commit is contained in:
白茶清欢 2024-08-23 15:22:44 +08:00
parent e5233b4fae
commit 15b1d9e6b0

View File

@ -9,7 +9,11 @@ package database
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/op_type" "git.zhangdeman.cn/zhangdeman/op_type"
"git.zhangdeman.cn/zhangdeman/serialize"
"reflect" "reflect"
"strings" "strings"
) )
@ -508,3 +512,38 @@ func parseInSql(fieldValue any) (string, []any) {
} }
return strings.Join(placeholderList, ","), dataList return strings.Join(placeholderList, ","), dataList
} }
// WithAnyCondition 设置任意查询条件, 仅 where 子句 in / not in / like / not like / == / !=
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:48 2024/8/23
func WithAnyCondition(column string, operate string, value any) (SetOption, error) {
if nil == value {
return nil, errors.New("value is nil")
}
switch operate {
case consts.WhereOperateEqual:
return WithWhere(map[string]any{column: value}), nil
case consts.WhereOperateNotEqual:
return WithNotEqual(column, value), nil
case consts.WhereOperateIn:
var target []any
if err := serialize.JSON.Transition(value, &target); nil != err {
return nil, err
}
return WithIn[[]any](column, target), nil
case consts.WhereOperateNotIn:
var target []any
if err := serialize.JSON.Transition(value, &target); nil != err {
return nil, err
}
return WithNotIn[[]any](column, target), nil
case consts.WhereOperateLike:
return WithLike(column, fmt.Sprintf("%v", value)), nil
case consts.WhereOperateNotLike:
return WithNotLike(column, fmt.Sprintf("%v", value)), nil
default:
return nil, errors.New(operate + " : operate is not support")
}
}