From 91d88930c28cbe898076cbd1f030111a3cd4105e Mon Sep 17 00:00:00 2001 From: what Date: Wed, 8 May 2024 22:36:43 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E8=A7=84=E8=8C=83=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=20NewCondtionExpr=20=3D>=20NewExpr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- condition_expr.go | 2 +- contracts/contracts.go | 74 ++++++++++++++++++++++++++++++++++++++++++ engine/engine_test.go | 8 ++--- 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 contracts/contracts.go diff --git a/condition_expr.go b/condition_expr.go index 6b04435..d67ffdf 100644 --- a/condition_expr.go +++ b/condition_expr.go @@ -200,7 +200,7 @@ func (this ConditionExpr) IsIgnoreEmptyParma(m contracts.ConditionTokenValue) bo return false } -func NewConditionExpr(rResource, rField, token string, operator contracts.ConditionOperator, tType contracts.ConditionTokenType, ignoreEmptyParma bool, fn, fnParam string) contracts.ConditionExpr { +func NewExpr(rResource, rField, token string, operator contracts.ConditionOperator, tType contracts.ConditionTokenType, ignoreEmptyParma bool, fn, fnParam string) contracts.ConditionExpr { return &ConditionExpr{ operator: operator, field: rField, diff --git a/contracts/contracts.go b/contracts/contracts.go new file mode 100644 index 0000000..75ad2e7 --- /dev/null +++ b/contracts/contracts.go @@ -0,0 +1,74 @@ +package contracts + +import ( + "git.fsdpf.net/go/db" + "git.fsdpf.net/go/req" +) + +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 + IsNotEmpty() bool + IsAlwaysRight() bool + ToSql(ConditionTokenValue) db.Expression + AppendTo(Condition) + SetExpr(ConditionExpr) Condition + SetCondition(Condition) Condition + SetMatchPrefix(string) Condition + GetFields(operator ConditionOperator, types ...ConditionTokenType) map[string]string + GetFieldsValue(m ConditionTokenValue, isWithResource bool) map[string]any +} + +type ConditionExpr interface { + GetField() string + GetFieldResource() string + GetOperator() ConditionOperator + + SetMatchPrefix(string) ConditionExpr + + AppendTo(Condition) + ToSql(m ConditionTokenValue) db.Expression + GetTokenName() string + GetTokenType() ConditionTokenType + GetTokenValue(ConditionTokenValue) any + GetTokenSqlValue(ConditionTokenValue) string + IsIgnoreEmptyParma(ConditionTokenValue) bool +} + +type ConditionTokenValue interface { + GetParam(k string) req.GlobalParams + GetGlobalParamsUser() req.User +} diff --git a/engine/engine_test.go b/engine/engine_test.go index bfb6a0f..33d118f 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -26,7 +26,7 @@ func TestEngine(t *testing.T) { cond1 := condition.New(contracts.AND, "条件1") - cond1.SetExpr(condition.NewConditionExpr( + cond1.SetExpr(condition.NewExpr( "TestTable", "age", "age", contracts.EQ, contracts.PARAM, false, "", "", @@ -68,7 +68,7 @@ func TestRelationEngine(t *testing.T) { } cond1 := condition.New(contracts.AND, "条件1"). - SetExpr(condition.NewConditionExpr( + SetExpr(condition.NewExpr( "TestTable", "age", "age", contracts.EQ, contracts.PARAM, false, "", "", @@ -80,12 +80,12 @@ func TestRelationEngine(t *testing.T) { }) cond2 := condition.New(contracts.OR, "条件2"). - SetExpr(condition.NewConditionExpr( + SetExpr(condition.NewExpr( "TestTableA", "age", "age", contracts.EQ, contracts.PARAM, false, "", "", )). - SetExpr(condition.NewConditionExpr( + SetExpr(condition.NewExpr( "TestTable", "age", "age", contracts.EQ, contracts.PARAM, false, "", "",