forked from slicebit/qb
/
combiner.go
51 lines (42 loc) · 1.42 KB
/
combiner.go
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
package qb
import (
"strings"
"fmt"
)
// buildCombiners generats and or statements and join them appropriately
func buildCombiners(dialect Dialect, combiner string, conditionals []Conditional) (string, []interface{}) {
sqls := []string{}
bindings := []interface{}{}
for _, c := range conditionals {
sql, values := c.Build(dialect)
sqls = append(sqls, sql)
bindings = append(bindings, values...)
}
return strings.Join(sqls, fmt.Sprintf(" %s ", combiner)), bindings
}
// And generates an AndClause given conditional clauses
func And(conditions ...Conditional) AndClause {
return AndClause{conditions}
}
// AndClause is the base struct to keep and within the where clause
// It satisfies the Clause interface
type AndClause struct {
conditions []Conditional
}
// Build compiles the and clause, joins the sql, returns sql and bindings
func (c AndClause) Build(dialect Dialect) (string, []interface{}) {
return buildCombiners(dialect, "AND", c.conditions)
}
// Or generates an OrClause given conditional clauses
func Or(conditions ...Conditional) OrClause {
return OrClause{conditions}
}
// OrClause is the base struct to keep or within the where clause
// It satisfies the Clause interface
type OrClause struct {
conditions []Conditional
}
// Build compiles the or clause, joins the sql, returns sql and bindings
func (c OrClause) Build(dialect Dialect) (string, []interface{}) {
return buildCombiners(dialect, "OR", c.conditions)
}