view 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
line wrap: on
line source

// This is Free Software under GNU Affero General Public License v >= 3.0
// without warranty, see README.md and license for details.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// License-Filename: LICENSES/AGPL-3.0.txt
//
// Copyright (C) 2019 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>

package controllers

import (
	"fmt"
	"strings"
)

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) and(format string, v ...interface{}) {
	if fb.hasCond {
		fb.stmt.WriteString(" AND ")
	} else {
		fb.hasCond = true
	}
	fb.arg(format, v...)
}