42 lines
785 B
Go
42 lines
785 B
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type QueryBuilder struct {
|
|
conditions []string
|
|
args []interface{}
|
|
idx int
|
|
}
|
|
|
|
func NewQueryBuilder(startIdx int) *QueryBuilder {
|
|
return &QueryBuilder{idx: startIdx}
|
|
}
|
|
|
|
func (qb *QueryBuilder) Add(condition string, arg interface{}) {
|
|
qb.conditions = append(qb.conditions, fmt.Sprintf(condition, qb.idx))
|
|
qb.args = append(qb.args, arg)
|
|
qb.idx++
|
|
}
|
|
|
|
func (qb *QueryBuilder) AddLiteral(condition string) {
|
|
qb.conditions = append(qb.conditions, condition)
|
|
}
|
|
|
|
func (qb *QueryBuilder) Where() string {
|
|
if len(qb.conditions) == 0 {
|
|
return ""
|
|
}
|
|
return " WHERE " + strings.Join(qb.conditions, " AND ")
|
|
}
|
|
|
|
func (qb *QueryBuilder) Args() []interface{} {
|
|
return qb.args
|
|
}
|
|
|
|
func (qb *QueryBuilder) Idx() int {
|
|
return qb.idx
|
|
}
|