Browse Source

优化变量

lxg 3 years ago
parent
commit
ff4528927a
2 changed files with 32 additions and 23 deletions
  1. 9 9
      entity.go
  2. 23 14
      query.go

+ 9 - 9
entity.go

@@ -250,9 +250,9 @@ func (e *Entity) prepareConditions(ctx *http.Context, query *Query, schemas []*S
 		switch schema.Format {
 		case "string", "text", "textarea":
 			if schema.getProperties().Match == MatchExactly {
-				query.AndFilterWhere(NewQueryCondition(schema.Column, formValue))
+				query.AndFilterWhere(NewCond(schema.Column, formValue))
 			} else {
-				query.AndFilterWhere(NewQueryConditionWithOperator("LIKE", schema.Column, formValue))
+				query.AndFilterWhere(NewCond(schema.Column, formValue).WithExpr("LIKE"))
 			}
 		case "date", "time", "datetime":
 			var sep string
@@ -264,23 +264,23 @@ func (e *Entity) prepareConditions(ctx *http.Context, query *Query, schemas []*S
 			}
 			if ss := strings.Split(formValue, sep); len(ss) == 2 {
 				query.AndFilterWhere(
-					NewQueryConditionWithOperator(">=", schema.Column, strings.TrimSpace(ss[0])),
-					NewQueryConditionWithOperator("<=", schema.Column, strings.TrimSpace(ss[1])),
+					NewCond(schema.Column, strings.TrimSpace(ss[0])).WithExpr(">="),
+					NewCond(schema.Column, strings.TrimSpace(ss[1])).WithExpr("<="),
 				)
 			} else {
-				query.AndFilterWhere(NewQueryCondition(schema.Column, formValue))
+				query.AndFilterWhere(NewCond(schema.Column, formValue))
 			}
 		case "duration", "number", "integer", "decimal":
-			query.AndFilterWhere(NewQueryCondition(schema.Column, formValue))
+			query.AndFilterWhere(NewCond(schema.Column, formValue))
 		default:
 			if schema.Type == "string" {
 				if schema.getProperties().Match == MatchExactly {
-					query.AndFilterWhere(NewQueryCondition(schema.Column, formValue))
+					query.AndFilterWhere(NewCond(schema.Column, formValue))
 				} else {
-					query.AndFilterWhere(NewQueryConditionWithOperator("LIKE", schema.Column, formValue))
+					query.AndFilterWhere(NewCond(schema.Column, formValue).WithExpr("LIKE"))
 				}
 			} else {
-				query.AndFilterWhere(NewQueryCondition(schema.Column, formValue))
+				query.AndFilterWhere(NewCond(schema.Column, formValue))
 			}
 		}
 	}

+ 23 - 14
query.go

@@ -25,9 +25,9 @@ type (
 	}
 
 	condition struct {
-		Field    string      `json:"field"`
-		Value    interface{} `json:"value"`
-		Operator string      `json:"operator"`
+		Field string      `json:"field"`
+		Value interface{} `json:"value"`
+		Expr  string      `json:"expr"`
 	}
 
 	join struct {
@@ -37,6 +37,11 @@ type (
 	}
 )
 
+func (cond *condition) WithExpr(v string) *condition {
+	cond.Expr = v
+	return cond
+}
+
 func (query *Query) compile() (*gorm.DB, error) {
 	db := query.db
 	if query.condition != "" {
@@ -105,18 +110,18 @@ func (query *Query) buildConditions(operator string, filter bool, conds ...*cond
 				continue
 			}
 		}
-		if cond.Operator == "" {
-			cond.Operator = "="
+		if cond.Expr == "" {
+			cond.Expr = "="
 		}
-		switch strings.ToUpper(cond.Operator) {
+		switch strings.ToUpper(cond.Expr) {
 		case "=", "<>", ">", "<", ">=", "<=", "!=":
 			if sb.Len() > 0 {
 				sb.WriteString(" " + operator + " ")
 			}
-			if cond.Operator == "=" && cond.Value == nil {
+			if cond.Expr == "=" && cond.Value == nil {
 				sb.WriteString("`" + cond.Field + "` IS NULL")
 			} else {
-				sb.WriteString("`" + cond.Field + "` " + cond.Operator + " ?")
+				sb.WriteString("`" + cond.Field + "` " + cond.Expr + " ?")
 				params = append(params, cond.Value)
 			}
 		case "LIKE":
@@ -342,19 +347,23 @@ func NewQuery(db *gorm.DB) *Query {
 	}
 }
 
+func NewCond(field string, value interface{}) *condition {
+	return NewQueryCondition(field, value)
+}
+
 func NewQueryCondition(field string, value interface{}) *condition {
 	return &condition{
-		Field:    field,
-		Value:    value,
-		Operator: "=",
+		Field: field,
+		Value: value,
+		Expr:  "=",
 	}
 }
 
 func NewQueryConditionWithOperator(operator, field string, value interface{}) *condition {
 	cond := &condition{
-		Field:    field,
-		Value:    value,
-		Operator: operator,
+		Field: field,
+		Value: value,
+		Expr:  operator,
 	}
 	return cond
 }