summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/go-xorm/builder/cond_and.go
blob: 5063c79bf8b36bab7fcf2187da40acf3bcd4857f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
}