|
|
@@ -5,13 +5,13 @@ import (
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/domain"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
|
|
|
+ "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/clause"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/tag/rule"
|
|
|
"git.sxidc.com/go-tools/utils/reflectutils"
|
|
|
"git.sxidc.com/go-tools/utils/strutils"
|
|
|
"github.com/pkg/errors"
|
|
|
"reflect"
|
|
|
- "strings"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
@@ -126,18 +126,18 @@ func HasRule(dbSchema string, scope string, domainName string, i *infrastructure
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-func GetRulesAndFormConditionClause(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (string, []any, error) {
|
|
|
+func GetRulesAndFormConditionClause(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (clause.Clause, error) {
|
|
|
r, err := getRule(dbSchema, scope, domainName, i)
|
|
|
if err != nil {
|
|
|
- return "", nil, err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- clause, args, err := formConditionClause(r)
|
|
|
+ c, err := formConditionClause(r)
|
|
|
if err != nil {
|
|
|
- return "", nil, err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- return clause, args, nil
|
|
|
+ return c, nil
|
|
|
}
|
|
|
|
|
|
func getRule(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure) (Rule, error) {
|
|
|
@@ -160,9 +160,9 @@ func getRule(dbSchema string, scope string, domainName string, i *infrastructure
|
|
|
return *r, nil
|
|
|
}
|
|
|
|
|
|
-func formConditionClause(r Rule) (string, []any, error) {
|
|
|
+func formConditionClause(r Rule) (clause.Clause, error) {
|
|
|
if strutils.IsStringEmpty(r.LogicalOperator) {
|
|
|
- conditions := sql.NewConditions()
|
|
|
+ conditions := clause.NewConditions()
|
|
|
|
|
|
switch r.Operator {
|
|
|
case opEqual:
|
|
|
@@ -179,70 +179,60 @@ func formConditionClause(r Rule) (string, []any, error) {
|
|
|
conditions.GreaterThanAndEqual(r.ColumnName, r.Value)
|
|
|
case opIn:
|
|
|
if reflect.TypeOf(r.Value).Kind() != reflect.Slice {
|
|
|
- return "", nil, errors.New("使用\"包含在列表\"操作符必须使用列表")
|
|
|
+ return nil, errors.New("使用\"包含在列表\"操作符必须使用列表")
|
|
|
}
|
|
|
|
|
|
conditions.In(r.FieldName, r.Value)
|
|
|
case opNotIn:
|
|
|
if reflect.TypeOf(r.Value).Kind() != reflect.Slice {
|
|
|
- return "", nil, errors.New("使用\"不包含在列表\"操作符必须使用列表")
|
|
|
+ return nil, errors.New("使用\"不包含在列表\"操作符必须使用列表")
|
|
|
}
|
|
|
|
|
|
conditions.NotIn(r.FieldName, r.Value)
|
|
|
case opPrefix:
|
|
|
strValue, err := reflectutils.ToString(r.Value)
|
|
|
if err != nil {
|
|
|
- return "", nil, err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
conditions.Like(r.FieldName, strValue+"%")
|
|
|
case opSuffix:
|
|
|
strValue, err := reflectutils.ToString(r.Value)
|
|
|
if err != nil {
|
|
|
- return "", nil, err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
conditions.Like(r.FieldName, "%"+strValue)
|
|
|
case opContains:
|
|
|
strValue, err := reflectutils.ToString(r.Value)
|
|
|
if err != nil {
|
|
|
- return "", nil, err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
conditions.Like(r.FieldName, "%"+strValue+"%")
|
|
|
default:
|
|
|
- return "", nil, errors.Errorf("不支持的操作符%v", r.Operator)
|
|
|
+ return nil, errors.Errorf("不支持的操作符%v", r.Operator)
|
|
|
}
|
|
|
|
|
|
- return conditions.And(), conditions.Args(), nil
|
|
|
+ return conditions.And(), nil
|
|
|
}
|
|
|
|
|
|
- leftClause, leftArgs, err := formConditionClause(*r.Left)
|
|
|
+ leftClause, err := formConditionClause(*r.Left)
|
|
|
if err != nil {
|
|
|
- return "", nil, err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- rightClause, rightArgs, err := formConditionClause(*r.Right)
|
|
|
+ rightClause, err := formConditionClause(*r.Right)
|
|
|
if err != nil {
|
|
|
- return "", nil, err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- clauseBuilder := strings.Builder{}
|
|
|
-
|
|
|
- clauseBuilder.WriteString("(" + leftClause + ")")
|
|
|
-
|
|
|
switch r.LogicalOperator {
|
|
|
case LogicalOperatorAnd:
|
|
|
- clauseBuilder.WriteString(" AND ")
|
|
|
+ return clause.NewConditionJoin(leftClause, rightClause).And(), nil
|
|
|
case LogicalOperatorOr:
|
|
|
- clauseBuilder.WriteString(" OR ")
|
|
|
+ return clause.NewConditionJoin(leftClause, rightClause).Or(), nil
|
|
|
+ default:
|
|
|
+ return clause.NewConditionJoin(leftClause, rightClause).And(), nil
|
|
|
}
|
|
|
-
|
|
|
- clauseBuilder.WriteString("(" + rightClause + ")")
|
|
|
-
|
|
|
- args := make([]any, 0)
|
|
|
- args = append(args, leftArgs...)
|
|
|
- args = append(args, rightArgs...)
|
|
|
-
|
|
|
- return clauseBuilder.String(), args, nil
|
|
|
}
|