changeset 2670:d7b1dd25f91f import-overview-rework

Made filter building a bit more reusable.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 14 Mar 2019 16:20:16 +0100
parents 831129a27536
children 8f3facf902dd
files pkg/controllers/importqueue.go
diffstat 1 files changed, 37 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/importqueue.go	Thu Mar 14 16:08:17 2019 +0100
+++ b/pkg/controllers/importqueue.go	Thu Mar 14 16:20:16 2019 +0100
@@ -127,52 +127,54 @@
 	return &ta
 }
 
+type filterBuilder struct {
+	stmt    strings.Builder
+	args    []interface{}
+	hasCond bool
+}
+
+func (fb *filterBuilder) arg(format string, v ...interface{}) {
+	indices := make([]interface{}, len(v))
+	for i := range indices {
+		indices[i] = len(fb.args) + i + 1
+	}
+	fmt.Fprintf(&fb.stmt, format, indices...)
+	fb.args = append(fb.args, v...)
+}
+
+func (fb *filterBuilder) cond(format string, v ...interface{}) {
+	if fb.hasCond {
+		fb.stmt.WriteString(" AND ")
+	} else {
+		fb.hasCond = true
+	}
+	fb.arg(format, v...)
+}
+
 func queryImportListStmt(
 	conn *sql.Conn,
 	req *http.Request,
 ) (*sql.Rows, error) {
 
-	var (
-		stmt    strings.Builder
-		args    []interface{}
-		hasCond bool
-	)
+	var fb filterBuilder
 
-	arg := func(format string, v ...interface{}) {
-		indices := make([]interface{}, len(v))
-		for i := range indices {
-			indices[i] = len(args) + i + 1
-		}
-		fmt.Fprintf(&stmt, format, indices...)
-		args = append(args, v...)
-	}
+	fb.stmt.WriteString(selectImportsSQL)
 
-	cond := func(format string, v ...interface{}) {
-		if hasCond {
-			stmt.WriteString(" AND ")
-		} else {
-			hasCond = true
-		}
-		arg(format, v...)
-	}
-
-	stmt.WriteString(selectImportsSQL)
-
-	stmt.WriteString(" WHERE ")
+	fb.stmt.WriteString(" WHERE ")
 
 	if st := req.FormValue("states"); st != "" {
 		states := toTextArray(st, imports.ImportStateNames)
-		cond(" state = ANY($%d) ", states)
+		fb.cond(" state = ANY($%d) ", states)
 	}
 
 	if ks := req.FormValue("kinds"); ks != "" {
 		kinds := toTextArray(ks, imports.ImportKindNames())
-		cond(" kind = ANY($%d) ", kinds)
+		fb.cond(" kind = ANY($%d) ", kinds)
 	}
 
 	if idss := req.FormValue("ids"); idss != "" {
 		ids := toInt8Array(idss)
-		cond(" id = ANY($%d) ", ids)
+		fb.cond(" id = ANY($%d) ", ids)
 	}
 
 	if from := req.FormValue("from"); from != "" {
@@ -180,7 +182,7 @@
 		if err != nil {
 			return nil, err
 		}
-		cond(" enqueued >= $%d ", fromTime)
+		fb.cond(" enqueued >= $%d ", fromTime)
 	}
 
 	if to := req.FormValue("to"); to != "" {
@@ -188,21 +190,21 @@
 		if err != nil {
 			return nil, err
 		}
-		cond(" enqueued <= $%d ", toTime)
+		fb.cond(" enqueued <= $%d ", toTime)
 	}
 
 	switch warn := strings.ToLower(req.FormValue("warnings")); warn {
 	case "1", "t", "true":
-		cond(" id IN (SELECT id FROM warned) ")
+		fb.cond(" id IN (SELECT id FROM warned) ")
 	}
 
-	if !hasCond {
-		stmt.WriteString(" TRUE ")
+	if !fb.hasCond {
+		fb.stmt.WriteString(" TRUE ")
 	}
 
-	stmt.WriteString(" ORDER BY enqueued DESC ")
+	fb.stmt.WriteString(" ORDER BY enqueued DESC ")
 
-	return conn.QueryContext(req.Context(), stmt.String(), args...)
+	return conn.QueryContext(req.Context(), fb.stmt.String(), fb.args...)
 }
 
 func enqueued(ctx context.Context, conn *sql.Conn, what, query string) *models.ImportTime {