[feat] 新增 New Option 可选项
This commit is contained in:
parent
e54629237c
commit
8270326a7b
26
condition.go
26
condition.go
@ -213,6 +213,28 @@ func (this Condition) GetFields(operator ConditionOperator, types ...ConditionTo
|
||||
return result
|
||||
}
|
||||
|
||||
func New(typ ConditionType, describe string) *Condition {
|
||||
return &Condition{typ: typ, describe: describe}
|
||||
type Option func(option *Condition)
|
||||
|
||||
func Type(v ConditionType) Option {
|
||||
return func(option *Condition) {
|
||||
option.typ = v
|
||||
}
|
||||
}
|
||||
|
||||
func Describe(v string) Option {
|
||||
return func(option *Condition) {
|
||||
option.describe = v
|
||||
}
|
||||
}
|
||||
|
||||
func New(opts ...Option) *Condition {
|
||||
cond := &Condition{typ: AND}
|
||||
|
||||
if l := len(opts); l > 0 {
|
||||
for i := 0; i < l; i++ {
|
||||
opts[i](cond)
|
||||
}
|
||||
}
|
||||
|
||||
return cond
|
||||
}
|
||||
|
@ -17,26 +17,26 @@ type ConditionTokenType string
|
||||
|
||||
const (
|
||||
SQL ConditionTokenType = "sql"
|
||||
FUNC = "func"
|
||||
PARAM = "param"
|
||||
STRING = "string"
|
||||
FUNC ConditionTokenType = "func"
|
||||
PARAM ConditionTokenType = "param"
|
||||
STRING ConditionTokenType = "string"
|
||||
)
|
||||
|
||||
const (
|
||||
IS_NULL ConditionOperator = "IS NULL"
|
||||
IS_NOT_NULL = "IS NOT NULL"
|
||||
EQ = "="
|
||||
NE = "!="
|
||||
GT = ">"
|
||||
GE = ">="
|
||||
LT = "<"
|
||||
LE = "<="
|
||||
LIKE = "LIKE"
|
||||
NOT_LIKE = "NOT LIKE"
|
||||
IN = "IN"
|
||||
NOT_IN = "NOT IN"
|
||||
REGEXP = "REGEXP"
|
||||
NOT_REGEXP = "NOT REGEXP"
|
||||
IS_NOT_NULL ConditionOperator = "IS NOT NULL"
|
||||
EQ ConditionOperator = "="
|
||||
NE ConditionOperator = "!="
|
||||
GT ConditionOperator = ">"
|
||||
GE ConditionOperator = ">="
|
||||
LT ConditionOperator = "<"
|
||||
LE ConditionOperator = "<="
|
||||
LIKE ConditionOperator = "LIKE"
|
||||
NOT_LIKE ConditionOperator = "NOT LIKE"
|
||||
IN ConditionOperator = "IN"
|
||||
NOT_IN ConditionOperator = "NOT IN"
|
||||
REGEXP ConditionOperator = "REGEXP"
|
||||
NOT_REGEXP ConditionOperator = "NOT REGEXP"
|
||||
)
|
||||
|
||||
type ConditionTokenValue interface {
|
||||
@ -232,15 +232,50 @@ func (this ConditionExpr) IsIgnoreEmptyParma(m ConditionTokenValue) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func NewExpr(rResource, rField, token string, operator ConditionOperator, tType ConditionTokenType, ignoreEmptyParma bool, fn, fnParam string) *ConditionExpr {
|
||||
return &ConditionExpr{
|
||||
operator: operator,
|
||||
type ExprOption func(option *ConditionExpr)
|
||||
|
||||
func Operator(v ConditionOperator) ExprOption {
|
||||
return func(option *ConditionExpr) {
|
||||
option.operator = v
|
||||
}
|
||||
}
|
||||
|
||||
func TokenType(v ConditionTokenType) ExprOption {
|
||||
return func(option *ConditionExpr) {
|
||||
option.tokenType = v
|
||||
}
|
||||
}
|
||||
|
||||
func IgnoreEmptyParma(v bool) ExprOption {
|
||||
return func(option *ConditionExpr) {
|
||||
option.ignoreEmptyParma = v
|
||||
}
|
||||
}
|
||||
|
||||
func FieldSqlFn(fn, fnParam string) ExprOption {
|
||||
return func(option *ConditionExpr) {
|
||||
option.fieldSqlFunc = fn
|
||||
option.fieldSqlFuncParam = fnParam
|
||||
}
|
||||
}
|
||||
|
||||
func NewExpr(rResource, rField, token string, opts ...ExprOption) *ConditionExpr {
|
||||
expr := &ConditionExpr{
|
||||
field: rField,
|
||||
fieldResource: rResource,
|
||||
fieldSqlFunc: fn,
|
||||
fieldSqlFuncParam: fnParam,
|
||||
ignoreEmptyParma: ignoreEmptyParma,
|
||||
tokenType: tType,
|
||||
token: string(token),
|
||||
token: token,
|
||||
tokenType: STRING,
|
||||
operator: EQ,
|
||||
ignoreEmptyParma: false,
|
||||
fieldSqlFunc: "",
|
||||
fieldSqlFuncParam: "",
|
||||
}
|
||||
|
||||
if l := len(opts); l > 0 {
|
||||
for i := 0; i < l; i++ {
|
||||
opts[i](expr)
|
||||
}
|
||||
}
|
||||
|
||||
return expr
|
||||
}
|
||||
|
@ -23,12 +23,11 @@ func TestEngine(t *testing.T) {
|
||||
g: req.NewGlobalParam(`{"age": 30}`, nil),
|
||||
}
|
||||
|
||||
cond1 := condition.New(condition.AND, "条件1")
|
||||
cond1 := condition.New(condition.Describe("条件1"))
|
||||
|
||||
cond1.SetExpr(condition.NewExpr(
|
||||
"TestTable", "age", "age",
|
||||
condition.EQ, condition.PARAM,
|
||||
false, "", "",
|
||||
condition.TokenType(condition.PARAM),
|
||||
))
|
||||
|
||||
engine.Case(cond1, func(data any, g req.GlobalParams) error {
|
||||
@ -66,11 +65,10 @@ func TestRelationEngine(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
cond1 := condition.New(condition.AND, "条件1").
|
||||
cond1 := condition.New(condition.Describe("条件1")).
|
||||
SetExpr(condition.NewExpr(
|
||||
"TestTable", "age", "age",
|
||||
condition.EQ, condition.PARAM,
|
||||
false, "", "",
|
||||
condition.TokenType(condition.PARAM),
|
||||
))
|
||||
|
||||
engine.Case(cond1, func(data any, g req.GlobalParams) error {
|
||||
@ -78,16 +76,14 @@ func TestRelationEngine(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
cond2 := condition.New(condition.OR, "条件2").
|
||||
cond2 := condition.New(condition.Type(condition.OR), condition.Describe("条件2")).
|
||||
SetExpr(condition.NewExpr(
|
||||
"TestTableA", "age", "age",
|
||||
condition.EQ, condition.PARAM,
|
||||
false, "", "",
|
||||
condition.TokenType(condition.PARAM),
|
||||
)).
|
||||
SetExpr(condition.NewExpr(
|
||||
"TestTable", "age", "age",
|
||||
condition.EQ, condition.PARAM,
|
||||
false, "", "",
|
||||
condition.TokenType(condition.PARAM),
|
||||
))
|
||||
|
||||
engine.Case(cond2, func(data any, g req.GlobalParams) error {
|
||||
|
Loading…
Reference in New Issue
Block a user