You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
815 B
Go
55 lines
815 B
Go
8 years ago
|
package builder
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
type condAnd []Cond
|
||
|
|
||
|
var _ Cond = condAnd{}
|
||
|
|
||
|
func And(conds ...Cond) Cond {
|
||
|
var result = make(condAnd, 0, len(conds))
|
||
|
for _, cond := range conds {
|
||
|
if cond == nil || !cond.IsValid() {
|
||
|
continue
|
||
|
}
|
||
|
result = append(result, cond)
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func (and condAnd) WriteTo(w Writer) error {
|
||
|
for i, cond := range and {
|
||
|
_, isOr := cond.(condOr)
|
||
|
if isOr {
|
||
|
fmt.Fprint(w, "(")
|
||
|
}
|
||
|
|
||
|
err := cond.WriteTo(w)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if isOr {
|
||
|
fmt.Fprint(w, ")")
|
||
|
}
|
||
|
|
||
|
if i != len(and)-1 {
|
||
|
fmt.Fprint(w, " AND ")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (and condAnd) And(conds ...Cond) Cond {
|
||
|
return And(and, And(conds...))
|
||
|
}
|
||
|
|
||
|
func (and condAnd) Or(conds ...Cond) Cond {
|
||
|
return Or(and, Or(conds...))
|
||
|
}
|
||
|
|
||
|
func (and condAnd) IsValid() bool {
|
||
|
return len(and) > 0
|
||
|
}
|