comparison pkg/controllers/importqueue.go @ 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 0fcf80a413a2
children 8f3facf902dd
comparison
equal deleted inserted replaced
2669:831129a27536 2670:d7b1dd25f91f
125 return nil 125 return nil
126 } 126 }
127 return &ta 127 return &ta
128 } 128 }
129 129
130 type filterBuilder struct {
131 stmt strings.Builder
132 args []interface{}
133 hasCond bool
134 }
135
136 func (fb *filterBuilder) arg(format string, v ...interface{}) {
137 indices := make([]interface{}, len(v))
138 for i := range indices {
139 indices[i] = len(fb.args) + i + 1
140 }
141 fmt.Fprintf(&fb.stmt, format, indices...)
142 fb.args = append(fb.args, v...)
143 }
144
145 func (fb *filterBuilder) cond(format string, v ...interface{}) {
146 if fb.hasCond {
147 fb.stmt.WriteString(" AND ")
148 } else {
149 fb.hasCond = true
150 }
151 fb.arg(format, v...)
152 }
153
130 func queryImportListStmt( 154 func queryImportListStmt(
131 conn *sql.Conn, 155 conn *sql.Conn,
132 req *http.Request, 156 req *http.Request,
133 ) (*sql.Rows, error) { 157 ) (*sql.Rows, error) {
134 158
135 var ( 159 var fb filterBuilder
136 stmt strings.Builder 160
137 args []interface{} 161 fb.stmt.WriteString(selectImportsSQL)
138 hasCond bool 162
139 ) 163 fb.stmt.WriteString(" WHERE ")
140
141 arg := func(format string, v ...interface{}) {
142 indices := make([]interface{}, len(v))
143 for i := range indices {
144 indices[i] = len(args) + i + 1
145 }
146 fmt.Fprintf(&stmt, format, indices...)
147 args = append(args, v...)
148 }
149
150 cond := func(format string, v ...interface{}) {
151 if hasCond {
152 stmt.WriteString(" AND ")
153 } else {
154 hasCond = true
155 }
156 arg(format, v...)
157 }
158
159 stmt.WriteString(selectImportsSQL)
160
161 stmt.WriteString(" WHERE ")
162 164
163 if st := req.FormValue("states"); st != "" { 165 if st := req.FormValue("states"); st != "" {
164 states := toTextArray(st, imports.ImportStateNames) 166 states := toTextArray(st, imports.ImportStateNames)
165 cond(" state = ANY($%d) ", states) 167 fb.cond(" state = ANY($%d) ", states)
166 } 168 }
167 169
168 if ks := req.FormValue("kinds"); ks != "" { 170 if ks := req.FormValue("kinds"); ks != "" {
169 kinds := toTextArray(ks, imports.ImportKindNames()) 171 kinds := toTextArray(ks, imports.ImportKindNames())
170 cond(" kind = ANY($%d) ", kinds) 172 fb.cond(" kind = ANY($%d) ", kinds)
171 } 173 }
172 174
173 if idss := req.FormValue("ids"); idss != "" { 175 if idss := req.FormValue("ids"); idss != "" {
174 ids := toInt8Array(idss) 176 ids := toInt8Array(idss)
175 cond(" id = ANY($%d) ", ids) 177 fb.cond(" id = ANY($%d) ", ids)
176 } 178 }
177 179
178 if from := req.FormValue("from"); from != "" { 180 if from := req.FormValue("from"); from != "" {
179 fromTime, err := time.Parse(models.ImportTimeFormat, from) 181 fromTime, err := time.Parse(models.ImportTimeFormat, from)
180 if err != nil { 182 if err != nil {
181 return nil, err 183 return nil, err
182 } 184 }
183 cond(" enqueued >= $%d ", fromTime) 185 fb.cond(" enqueued >= $%d ", fromTime)
184 } 186 }
185 187
186 if to := req.FormValue("to"); to != "" { 188 if to := req.FormValue("to"); to != "" {
187 toTime, err := time.Parse(models.ImportTimeFormat, to) 189 toTime, err := time.Parse(models.ImportTimeFormat, to)
188 if err != nil { 190 if err != nil {
189 return nil, err 191 return nil, err
190 } 192 }
191 cond(" enqueued <= $%d ", toTime) 193 fb.cond(" enqueued <= $%d ", toTime)
192 } 194 }
193 195
194 switch warn := strings.ToLower(req.FormValue("warnings")); warn { 196 switch warn := strings.ToLower(req.FormValue("warnings")); warn {
195 case "1", "t", "true": 197 case "1", "t", "true":
196 cond(" id IN (SELECT id FROM warned) ") 198 fb.cond(" id IN (SELECT id FROM warned) ")
197 } 199 }
198 200
199 if !hasCond { 201 if !fb.hasCond {
200 stmt.WriteString(" TRUE ") 202 fb.stmt.WriteString(" TRUE ")
201 } 203 }
202 204
203 stmt.WriteString(" ORDER BY enqueued DESC ") 205 fb.stmt.WriteString(" ORDER BY enqueued DESC ")
204 206
205 return conn.QueryContext(req.Context(), stmt.String(), args...) 207 return conn.QueryContext(req.Context(), fb.stmt.String(), fb.args...)
206 } 208 }
207 209
208 func enqueued(ctx context.Context, conn *sql.Conn, what, query string) *models.ImportTime { 210 func enqueued(ctx context.Context, conn *sql.Conn, what, query string) *models.ImportTime {
209 if what == "" { 211 if what == "" {
210 return nil 212 return nil