api2sql基础的能力 #8
39
option.go
39
option.go
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user