dangogh closed pull request #2110: TO go: allow orderby to be multiple columns
separated by ","
URL: https://github.com/apache/trafficcontrol/pull/2110
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
index 0c8774b30..26c3ac8af 100644
--- a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
+++ b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
@@ -34,12 +34,9 @@ type WhereColumnInfo struct {
Checker func(string) error
}
-const baseWhere = "\nWHERE"
-const baseOrderBy = "\nORDER BY"
-
func BuildWhereAndOrderBy(parameters map[string]string, queryParamsToSQLCols
map[string]WhereColumnInfo) (string, string, map[string]interface{}, []error) {
- whereClause := baseWhere
- orderBy := baseOrderBy
+ var whereClause string
+ var orderBy string
var criteria string
var queryValues map[string]interface{}
var errs []error
@@ -52,20 +49,24 @@ func BuildWhereAndOrderBy(parameters map[string]string,
queryParamsToSQLCols map
return "", "", queryValues, errs
}
- if orderby, ok := parameters["orderby"]; ok {
- log.Debugln("orderby: ", orderby)
- if colInfo, ok := queryParamsToSQLCols[orderby]; ok {
- log.Debugln("orderby column ", colInfo)
- orderBy += " " + colInfo.Column
- } else {
- log.Debugln("Incorrect name for orderby: ", orderby)
+ if names, ok := parameters["orderby"]; ok {
+ var columns []string
+ log.Debugln("orderby: ", names)
+ for _, n := range strings.Split(names, ",") {
+ if colInfo, ok := queryParamsToSQLCols[n]; ok {
+ log.Debugln("orderby column ", colInfo)
+ columns = append(columns, colInfo.Column)
+ } else {
+ log.Debugln("Incorrect name for orderby: ", n)
+ }
}
+ orderBy = strings.Join(columns, ",")
}
- if whereClause == baseWhere {
- whereClause = ""
+ if whereClause != "" {
+ whereClause = "\nWHERE " + whereClause
}
- if orderBy == baseOrderBy {
- orderBy = ""
+ if orderBy != "" {
+ whereClause = "\nORDER BY " + whereClause
}
log.Debugf("\n--\n Where: %s \n Order By: %s", whereClause, orderBy)
return whereClause, orderBy, queryValues, errs
diff --git a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers_test.go
b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers_test.go
index 5607dfc44..9d06c43fa 100644
--- a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers_test.go
+++ b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers_test.go
@@ -35,7 +35,7 @@ func stripAllWhitespace(s string) string {
}
func TestBuildQuery(t *testing.T) {
- v := map[string]string{"param1": "queryParamv1", "param2":
"queryParamv2"}
+ v := map[string]string{"param1": "queryParamv1", "param2":
"queryParamv2", "orderby": "param1,param2"}
selectStmt := `SELECT
t.col1,
@@ -62,12 +62,16 @@ FROM table t
}
if strings.Contains(actualQuery, expectedV1) {
- t.Errorf("expected: %v error, actual: %v", actualQuery,
expectedV1)
+ t.Errorf("expected: %v error, actual: %v", expectedV1,
actualQuery)
}
expectedV2 := v["param2"]
if strings.Contains(actualQuery, expectedV2) {
- t.Errorf("expected: %v error, actual: %v", actualQuery,
expectedV2)
+ t.Errorf("expected: %v error, actual: %v", expectedV2,
actualQuery)
}
+ expectedOrderby := queryParamsToSQLCols["param1"].Column + "," +
queryParamsToSQLCols["param2"].Column
+ if expectedOrderby != orderBy {
+ t.Errorf("expected: %v error, actual: %v", expectedOrderby,
orderBy)
+ }
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services