forgejo-federation/vendor/github.com/go-xorm/builder/cond_and.go

62 lines
1 KiB
Go
Raw Normal View History

// Copyright 2016 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2016-11-04 03:46:01 +05:30
package builder
import "fmt"
type condAnd []Cond
var _ Cond = condAnd{}
// And generates AND conditions
2016-11-04 03:46:01 +05:30
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)
2018-01-27 20:50:59 +05:30
_, isExpr := cond.(expr)
wrap := isOr || isExpr
if wrap {
2016-11-04 03:46:01 +05:30
fmt.Fprint(w, "(")
}
err := cond.WriteTo(w)
if err != nil {
return err
}
2018-01-27 20:50:59 +05:30
if wrap {
2016-11-04 03:46:01 +05:30
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
}