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