From 0b6480328d777386662e0d0e83662d6fcfc64e86 Mon Sep 17 00:00:00 2001 From: richardxz Date: Thu, 11 Oct 2018 02:34:44 -0400 Subject: [PATCH] avoid incorrect result when list resource with search conditions --- pkg/models/resources.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/models/resources.go b/pkg/models/resources.go index ed9e1377c..8ace3ba28 100644 --- a/pkg/models/resources.go +++ b/pkg/models/resources.go @@ -220,6 +220,8 @@ func ListResource(resourceName, conditonSrt, pagingStr, order string) (*Resource } func generateConditionStr(conditions *searchConditions) string { + shouldUseAnd := false + shouldUseBrackets := false conditionStr := "" if conditions == nil { @@ -242,11 +244,21 @@ func generateConditionStr(conditions *searchConditions) string { } } + if len(conditionStr) > 0 { + shouldUseAnd = true + } + for k, v := range conditions.matchOr { if len(conditionStr) == 0 { conditionStr = fmt.Sprintf("%s = \"%s\" ", k, v) } else { - conditionStr = fmt.Sprintf("%s OR %s = \"%s\" ", conditionStr, k, v) + if shouldUseAnd { + conditionStr = fmt.Sprintf("%s And (%s = \"%s\" ", conditionStr, k, v) + shouldUseBrackets = true + shouldUseAnd = false + } else { + conditionStr = fmt.Sprintf("%s OR %s = \"%s\" ", conditionStr, k, v) + } } } @@ -254,10 +266,20 @@ func generateConditionStr(conditions *searchConditions) string { if len(conditionStr) == 0 { conditionStr = fmt.Sprintf("%s like '%%%s%%' ", k, v) } else { - conditionStr = fmt.Sprintf("%s OR %s like '%%%s%%' ", conditionStr, k, v) + if shouldUseAnd { + conditionStr = fmt.Sprintf("%s And (%s like '%%%s%%' ", conditionStr, k, v) + shouldUseAnd = false + shouldUseBrackets = true + } else { + conditionStr = fmt.Sprintf("%s OR %s like '%%%s%%' ", conditionStr, k, v) + } } } + if shouldUseBrackets { + conditionStr = fmt.Sprintf("%s )", conditionStr) + } + return conditionStr }