[feat] 调整 接口文件
This commit is contained in:
parent
91d88930c2
commit
50a0a1a85c
21
condition.go
21
condition.go
@ -10,6 +10,11 @@ import (
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
const (
|
||||
OR contracts.ConditionType = "OR"
|
||||
AND = "AND"
|
||||
)
|
||||
|
||||
type Condition struct {
|
||||
parent contracts.Condition
|
||||
typ contracts.ConditionType // 分组类型
|
||||
@ -50,19 +55,19 @@ func (this Condition) IsAlwaysRight() bool {
|
||||
flag := []bool{}
|
||||
|
||||
for _, expr := range this.exprs {
|
||||
if expr.GetOperator() != contracts.EQ {
|
||||
if expr.GetOperator() != EQ {
|
||||
flag = append(flag, false)
|
||||
continue
|
||||
}
|
||||
|
||||
if expr.GetTokenType() != contracts.SQL {
|
||||
if expr.GetTokenType() != SQL {
|
||||
flag = append(flag, false)
|
||||
continue
|
||||
}
|
||||
|
||||
eRight := fmt.Sprintf("%s.%s", expr.GetFieldResource(), expr.GetField()) == strings.ReplaceAll(expr.GetTokenName(), "`", "")
|
||||
|
||||
if this.typ == contracts.OR && eRight {
|
||||
if this.typ == OR && eRight {
|
||||
return true
|
||||
}
|
||||
|
||||
@ -72,7 +77,7 @@ func (this Condition) IsAlwaysRight() bool {
|
||||
for _, cond := range this.childrens {
|
||||
cRight := cond.IsAlwaysRight()
|
||||
|
||||
if this.typ == contracts.OR && cRight {
|
||||
if this.typ == OR && cRight {
|
||||
return true
|
||||
}
|
||||
|
||||
@ -106,16 +111,16 @@ func (this Condition) ToSql(m contracts.ConditionTokenValue) db.Expression {
|
||||
sql := strings.Join(conditions, " "+string(this.Type())+" ")
|
||||
|
||||
if sql == "" {
|
||||
if this.parent == nil && this.Type() == contracts.OR {
|
||||
if this.parent == nil && this.Type() == OR {
|
||||
return db.Raw("false")
|
||||
} else if this.parent == nil && this.Type() == contracts.AND {
|
||||
} else if this.parent == nil && this.Type() == AND {
|
||||
return db.Raw("true")
|
||||
}
|
||||
return db.Raw("")
|
||||
}
|
||||
|
||||
// 包裹 SQL, 避免语法表达错误
|
||||
if this.parent == nil || this.Type() == contracts.OR {
|
||||
if this.parent == nil || this.Type() == OR {
|
||||
sql = "(" + sql + ")"
|
||||
}
|
||||
|
||||
@ -158,7 +163,7 @@ func (this Condition) GetFieldsValue(m contracts.ConditionTokenValue, isWithReso
|
||||
|
||||
// 表达式
|
||||
for _, item := range this.exprs {
|
||||
if item.GetOperator() != contracts.EQ {
|
||||
if item.GetOperator() != EQ {
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,30 @@ import (
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
const (
|
||||
SQL contracts.ConditionTokenType = "sql"
|
||||
FUNC = "func"
|
||||
PARAM = "param"
|
||||
STRING = "string"
|
||||
)
|
||||
|
||||
const (
|
||||
IS_NULL contracts.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"
|
||||
)
|
||||
|
||||
type ConditionExpr struct {
|
||||
parent contracts.Condition
|
||||
operator contracts.ConditionOperator
|
||||
@ -65,24 +89,24 @@ func (this ConditionExpr) ToSql(m contracts.ConditionTokenValue) db.Expression {
|
||||
//
|
||||
secondary := ""
|
||||
switch operator {
|
||||
case contracts.IS_NULL:
|
||||
case contracts.IS_NOT_NULL:
|
||||
case IS_NULL:
|
||||
case IS_NOT_NULL:
|
||||
|
||||
secondary = ""
|
||||
case contracts.EQ, contracts.NE,
|
||||
contracts.GT, contracts.GE,
|
||||
contracts.LT, contracts.LE,
|
||||
contracts.REGEXP, contracts.NOT_REGEXP:
|
||||
case EQ, NE,
|
||||
GT, GE,
|
||||
LT, LE,
|
||||
REGEXP, NOT_REGEXP:
|
||||
|
||||
if this.GetTokenType() == contracts.SQL {
|
||||
if this.GetTokenType() == SQL {
|
||||
secondary = value
|
||||
} else {
|
||||
secondary = "'" + strings.Trim(value, "'") + "'"
|
||||
}
|
||||
|
||||
case contracts.LIKE, contracts.NOT_LIKE:
|
||||
case LIKE, NOT_LIKE:
|
||||
secondary = "'%" + strings.Trim(value, "'") + "%'"
|
||||
case contracts.IN, contracts.NOT_IN:
|
||||
case IN, NOT_IN:
|
||||
secondary = "(" + lo.Ternary(value == "", "''", value) + ")"
|
||||
}
|
||||
|
||||
@ -118,7 +142,7 @@ func (this ConditionExpr) GetTokenType() contracts.ConditionTokenType {
|
||||
}
|
||||
|
||||
func (this *ConditionExpr) GetTokenSqlValue(m contracts.ConditionTokenValue) string {
|
||||
if this.GetTokenType() == contracts.SQL {
|
||||
if this.GetTokenType() == SQL {
|
||||
return this.token
|
||||
}
|
||||
|
||||
@ -141,8 +165,8 @@ func (this *ConditionExpr) GetTokenSqlValue(m contracts.ConditionTokenValue) str
|
||||
}
|
||||
|
||||
// 强制使用 in
|
||||
if this.operator == contracts.EQ {
|
||||
this.operator = contracts.IN
|
||||
if this.operator == EQ {
|
||||
this.operator = IN
|
||||
}
|
||||
|
||||
return strings.Join(aStr, ", ")
|
||||
@ -156,14 +180,14 @@ func (this *ConditionExpr) GetTokenSqlValue(m contracts.ConditionTokenValue) str
|
||||
|
||||
func (this ConditionExpr) GetTokenValue(m contracts.ConditionTokenValue) any {
|
||||
switch this.GetTokenType() {
|
||||
case contracts.PARAM:
|
||||
case PARAM:
|
||||
if this.matchPrefix != "" {
|
||||
return m.GetParam(fmt.Sprintf("%s.%s", this.matchPrefix, this.token)).Value()
|
||||
}
|
||||
return m.GetParam(this.token).Value()
|
||||
case contracts.STRING:
|
||||
case STRING:
|
||||
return this.token
|
||||
case contracts.FUNC:
|
||||
case FUNC:
|
||||
switch this.token {
|
||||
case "UserID":
|
||||
return m.GetGlobalParamsUser().ID()
|
||||
@ -187,7 +211,7 @@ func (this ConditionExpr) IsIgnoreEmptyParma(m contracts.ConditionTokenValue) bo
|
||||
return false
|
||||
}
|
||||
|
||||
if this.tokenType != contracts.PARAM {
|
||||
if this.tokenType != PARAM {
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -9,35 +9,6 @@ type ConditionType string
|
||||
type ConditionOperator string
|
||||
type ConditionTokenType string
|
||||
|
||||
const (
|
||||
OR ConditionType = "OR"
|
||||
AND ConditionType = "AND"
|
||||
)
|
||||
|
||||
const (
|
||||
SQL ConditionTokenType = "sql"
|
||||
FUNC ConditionTokenType = "func"
|
||||
PARAM ConditionTokenType = "param"
|
||||
STRING ConditionTokenType = "string"
|
||||
)
|
||||
|
||||
const (
|
||||
IS_NULL ConditionOperator = "IS NULL"
|
||||
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 Condition interface {
|
||||
Type() ConditionType
|
||||
IsEmpty() bool
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"git.fsdpf.net/go/condition"
|
||||
"git.fsdpf.net/go/condition/contracts"
|
||||
"git.fsdpf.net/go/req"
|
||||
)
|
||||
|
||||
@ -24,11 +23,11 @@ func TestEngine(t *testing.T) {
|
||||
g: req.NewGlobalParam(`{"age": 30}`, nil),
|
||||
}
|
||||
|
||||
cond1 := condition.New(contracts.AND, "条件1")
|
||||
cond1 := condition.New(condition.AND, "条件1")
|
||||
|
||||
cond1.SetExpr(condition.NewExpr(
|
||||
"TestTable", "age", "age",
|
||||
contracts.EQ, contracts.PARAM,
|
||||
condition.EQ, condition.PARAM,
|
||||
false, "", "",
|
||||
))
|
||||
|
||||
@ -67,10 +66,10 @@ func TestRelationEngine(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
cond1 := condition.New(contracts.AND, "条件1").
|
||||
cond1 := condition.New(condition.AND, "条件1").
|
||||
SetExpr(condition.NewExpr(
|
||||
"TestTable", "age", "age",
|
||||
contracts.EQ, contracts.PARAM,
|
||||
condition.EQ, condition.PARAM,
|
||||
false, "", "",
|
||||
))
|
||||
|
||||
@ -79,15 +78,15 @@ func TestRelationEngine(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
cond2 := condition.New(contracts.OR, "条件2").
|
||||
cond2 := condition.New(condition.OR, "条件2").
|
||||
SetExpr(condition.NewExpr(
|
||||
"TestTableA", "age", "age",
|
||||
contracts.EQ, contracts.PARAM,
|
||||
condition.EQ, condition.PARAM,
|
||||
false, "", "",
|
||||
)).
|
||||
SetExpr(condition.NewExpr(
|
||||
"TestTable", "age", "age",
|
||||
contracts.EQ, contracts.PARAM,
|
||||
condition.EQ, condition.PARAM,
|
||||
false, "", "",
|
||||
))
|
||||
|
||||
|
@ -12,9 +12,9 @@ func NewConditionByRes(items []base.ResCondition, describe string) (root contrac
|
||||
for _, item := range items {
|
||||
switch item.Type {
|
||||
case "and":
|
||||
conditions[item.Id] = &Condition{typ: contracts.AND}
|
||||
conditions[item.Id] = &Condition{typ: AND}
|
||||
case "or":
|
||||
conditions[item.Id] = &Condition{typ: contracts.OR}
|
||||
conditions[item.Id] = &Condition{typ: OR}
|
||||
case "expr":
|
||||
conditions[item.Id] = &ConditionExpr{
|
||||
field: item.Column,
|
||||
|
Loading…
Reference in New Issue
Block a user