yjp 1 жил өмнө
parent
commit
67c55c8c70

+ 20 - 21
convenient/domain/query_rule/query_rule_parser.go

@@ -100,21 +100,6 @@ func (r Rule) checkAsLeaf() error {
 		return errors.New("操作符不支持")
 	}
 
-	if r.Value == nil {
-		switch r.FieldType {
-		case rule.TypeString:
-			r.Value = ""
-		case rule.TypeTime:
-			r.Value = ""
-		case rule.TypeNumber:
-			r.Value = 0
-		case rule.TypeBool:
-			r.Value = false
-		default:
-			return errors.New("字段类型不支持")
-		}
-	}
-
 	return nil
 }
 
@@ -137,6 +122,16 @@ func HasRule(dbSchema string, scope string, domainName string, i *infrastructure
 	})
 }
 
+// GetRulesAndFormConditionClause 获取规则并返回条件语句
+// 参数:
+// - dbSchema: 数据库schema
+// - scope: 范围
+// - domainName: 领域名称
+// - i: 基础设施
+// - ruleParams: 规则参数
+// 返回值:
+// - 语句接口,当错误为nil时,语句接口也可能是nil,代表该规则并没有被赋值,所以是没有条件的
+// - 错误
 func GetRulesAndFormConditionClause(dbSchema string, scope string, domainName string, i *infrastructure.Infrastructure, ruleParams map[string]any) (clause.Clause, error) {
 	r, err := getRule(dbSchema, scope, domainName, i)
 	if err != nil {
@@ -172,13 +167,17 @@ func getRule(dbSchema string, scope string, domainName string, i *infrastructure
 }
 
 func formConditionClause(r Rule, ruleParams map[string]any) (clause.Clause, error) {
-	ruleValue := r.Value
-	ruleParamValue, ok := ruleParams[r.ColumnName]
-	if ok {
-		ruleValue = ruleParamValue
-	}
-
 	if strutils.IsStringEmpty(r.LogicalOperator) {
+		ruleValue := r.Value
+		ruleParamValue, ok := ruleParams[r.ColumnName]
+		if ok {
+			ruleValue = ruleParamValue
+		}
+
+		if ruleValue == nil {
+			return nil, nil
+		}
+
 		conditions := clause.NewConditions()
 
 		switch r.Operator {

+ 6 - 0
framework/core/infrastructure/database/clause/common.go

@@ -1,5 +1,7 @@
 package clause
 
+import "git.sxidc.com/go-tools/utils/strutils"
+
 type CommonClause struct {
 	clause string
 	args   []any
@@ -13,6 +15,10 @@ func NewCommonClause(clause string, args ...any) CommonClause {
 }
 
 func (clause CommonClause) Clause() (string, error) {
+	if strutils.IsStringEmpty(clause.clause) {
+		return "", nil
+	}
+
 	return string(clause.clause + "\n"), nil
 }
 

+ 21 - 2
framework/core/infrastructure/database/clause/condition_join.go

@@ -25,6 +25,18 @@ func (clause *ConditionJoin) Or() *OrJoin {
 }
 
 func (clause *ConditionJoin) formClause(conditionOperator string) (string, error) {
+	if clause.left == nil || clause.right == nil {
+		if clause.left != nil {
+			return clause.left.Clause()
+		}
+
+		if clause.right != nil {
+			return clause.right.Clause()
+		}
+
+		return "", nil
+	}
+
 	leftClause, err := clause.left.Clause()
 	if err != nil {
 		return "", err
@@ -40,8 +52,15 @@ func (clause *ConditionJoin) formClause(conditionOperator string) (string, error
 
 func (clause *ConditionJoin) formArgs() []any {
 	args := make([]any, 0)
-	args = append(args, clause.left.Args()...)
-	args = append(args, clause.right.Args()...)
+
+	if clause.left != nil {
+		args = append(args, clause.left.Args()...)
+	}
+
+	if clause.right != nil {
+		args = append(args, clause.right.Args()...)
+	}
+
 	return args
 }
 

+ 4 - 0
framework/core/infrastructure/database/clause/from.go

@@ -13,6 +13,10 @@ func NewFrom(otherClauses []Clause) *From {
 }
 
 func (clause *From) Clause() (string, error) {
+	if clause.otherClauses == nil || len(clause.otherClauses) == 0 {
+		return "", nil
+	}
+
 	otherClauseBuilder := strings.Builder{}
 	for _, otherClause := range clause.otherClauses {
 		otherClauseStr, err := otherClause.Clause()

+ 6 - 0
framework/core/infrastructure/database/clause/group_by.go

@@ -1,5 +1,7 @@
 package clause
 
+import "git.sxidc.com/go-tools/utils/strutils"
+
 type GroupBy struct {
 	columnName string
 }
@@ -11,6 +13,10 @@ func NewGroupBy(columnName string) *GroupBy {
 }
 
 func (clause *GroupBy) Clause() (string, error) {
+	if strutils.IsStringEmpty(clause.columnName) {
+		return "", nil
+	}
+
 	return "GROUP BY " + clause.columnName + "\n", nil
 }
 

+ 8 - 1
framework/core/infrastructure/database/clause/join.go

@@ -1,6 +1,9 @@
 package clause
 
-import "strings"
+import (
+	"git.sxidc.com/go-tools/utils/strutils"
+	"strings"
+)
 
 type Join struct {
 	joinType  string
@@ -32,6 +35,10 @@ func (clause *Join) Inner() *Join {
 }
 
 func (clause *Join) Clause() (string, error) {
+	if strutils.IsStringEmpty(clause.joinType) || strutils.IsStringEmpty(clause.tableName) || strutils.IsStringEmpty(clause.on) {
+		return "", nil
+	}
+
 	clauseBuilder := strings.Builder{}
 	clauseBuilder.WriteString(clause.joinType)
 	clauseBuilder.WriteString(" " + clause.tableName)

+ 6 - 0
framework/core/infrastructure/database/clause/order_by.go

@@ -1,5 +1,7 @@
 package clause
 
+import "git.sxidc.com/go-tools/utils/strutils"
+
 type OrderBy struct {
 	columnName string
 	aes        bool
@@ -13,6 +15,10 @@ func NewOrderBy(columnName string, aes bool) *OrderBy {
 }
 
 func (clause *OrderBy) Clause() (string, error) {
+	if strutils.IsStringEmpty(clause.columnName) {
+		return "", nil
+	}
+
 	order := " AES"
 	if !clause.aes {
 		order = " DESC"

+ 8 - 0
framework/core/infrastructure/database/clause/table_name.go

@@ -1,8 +1,16 @@
 package clause
 
+import (
+	"git.sxidc.com/go-tools/utils/strutils"
+)
+
 type TableName string
 
 func (s TableName) Clause() (string, error) {
+	if strutils.IsStringEmpty(string(s)) {
+		return "", nil
+	}
+
 	return string(s + "\n"), nil
 }
 

+ 4 - 0
framework/core/infrastructure/database/clause/where.go

@@ -13,6 +13,10 @@ func NewWhere(condition Clause) *Where {
 }
 
 func (clause *Where) Clause() (string, error) {
+	if clause.condition == nil {
+		return "", nil
+	}
+
 	conditionClause, err := clause.condition.Clause()
 	if err != nil {
 		return "", err