-
Notifications
You must be signed in to change notification settings - Fork 5
/
where.go
67 lines (57 loc) · 1.21 KB
/
where.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package orm
import (
"fmt"
"strings"
)
type binaryOp string
const (
Eq = "="
GT = ">"
LT = "<"
GE = ">="
LE = "<="
NE = "!="
Between = "BETWEEN"
Like = "LIKE"
In = "IN"
)
type Cond struct {
PlaceHolderGenerator func(n int) []string
Lhs string
Op binaryOp
Rhs interface{}
}
func (b Cond) ToSql() (string, []interface{}) {
var phs []string
if b.Op == In {
phs = b.PlaceHolderGenerator(len(b.Rhs.([]interface{})))
return fmt.Sprintf("%s IN (%s)", b.Lhs, strings.Join(phs, ",")), b.Rhs.([]interface{})
} else {
phs = b.PlaceHolderGenerator(1)
return fmt.Sprintf("%s %s %s", b.Lhs, b.Op, pop(&phs)), []interface{}{b.Rhs}
}
}
const (
nextType_AND = "AND"
nextType_OR = "OR"
)
type whereClause struct {
PlaceHolderGenerator func(n int) []string
nextTyp string
next *whereClause
Cond
}
func (w whereClause) ToSql() (string, []interface{}) {
w.Cond.PlaceHolderGenerator = w.PlaceHolderGenerator
base, args := w.Cond.ToSql()
if w.next == nil {
return base, args
}
if w.next != nil {
next, nextArgs := w.next.ToSql()
base += " " + w.nextTyp + " " + next
args = append(args, nextArgs)
return base, args
}
return base, args
}