comparison pkg/controllers/common.go @ 3190:54a3e40cfbed

controllers: moved filter builder to a separate file.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 08 May 2019 10:50:14 +0200
parents
children eeff2cc4ff9d
comparison
equal deleted inserted replaced
3189:6f8fb2053881 3190:54a3e40cfbed
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2019 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package controllers
15
16 import (
17 "fmt"
18 "strings"
19 )
20
21 type filterBuilder struct {
22 stmt strings.Builder
23 args []interface{}
24 hasCond bool
25 }
26
27 func (fb *filterBuilder) arg(format string, v ...interface{}) {
28 indices := make([]interface{}, len(v))
29 for i := range indices {
30 indices[i] = len(fb.args) + i + 1
31 }
32 fmt.Fprintf(&fb.stmt, format, indices...)
33 fb.args = append(fb.args, v...)
34 }
35
36 func (fb *filterBuilder) and(format string, v ...interface{}) {
37 if fb.hasCond {
38 fb.stmt.WriteString(" AND ")
39 } else {
40 fb.hasCond = true
41 }
42 fb.arg(format, v...)
43 }