-
Notifications
You must be signed in to change notification settings - Fork 1
/
operator.go
133 lines (101 loc) · 3.67 KB
/
operator.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package sqlbuilder
func In(expr AsExpr, v ...AsExpr) *InOperatorExpr { return InOperator("IN", expr, v...) }
func NotIn(expr AsExpr, v ...AsExpr) *InOperatorExpr { return InOperator("NOT IN", expr, v...) }
type InOperatorExpr struct {
operator string
expr AsExpr
elements []AsExpr
}
func InOperator(operator string, expr AsExpr, v ...AsExpr) *InOperatorExpr {
return &InOperatorExpr{operator: operator, expr: expr, elements: v}
}
func (o *InOperatorExpr) AsExpr(s *Serializer) {
s.F(o.expr.AsExpr).D(" ").D(o.operator).D(" ")
s.D("(")
for i, e := range o.elements {
s.F(e.AsExpr).DC(", ", i < len(o.elements)-1)
}
s.D(")")
}
func Like(left, right AsExpr) *LikeOperatorExpr {
return &LikeOperatorExpr{left: left, right: right}
}
type LikeOperatorExpr struct {
left, right AsExpr
}
func (o *LikeOperatorExpr) AsExpr(s *Serializer) {
s.F(o.left.AsExpr).D(" LIKE ").F(o.right.AsExpr)
}
func And(left, right AsExpr) *BinaryOperatorExpr { return BinaryOperator("AND", left, right) }
func Or(left, right AsExpr) *BinaryOperatorExpr { return BinaryOperator("OR", left, right) }
type BooleanOperatorExpr struct {
operator string
elements []AsExpr
}
func BooleanOperator(operator string, elements ...AsExpr) *BooleanOperatorExpr {
return &BooleanOperatorExpr{operator: operator, elements: elements}
}
func (b *BooleanOperatorExpr) AsExpr(s *Serializer) {
if len(b.elements) == 1 {
s.F(b.elements[0].AsExpr)
return
}
s.D("(")
for i, e := range b.elements {
s.F(e.AsExpr).DC(" "+b.operator+" ", i < len(b.elements)-1)
}
s.D(")")
}
func Eq(left, right AsExpr) *BinaryOperatorExpr { return BinaryOperator("=", left, right) }
func Ne(left, right AsExpr) *BinaryOperatorExpr { return BinaryOperator("!=", left, right) }
func Gt(left, right AsExpr) *BinaryOperatorExpr { return BinaryOperator(">", left, right) }
func Lt(left, right AsExpr) *BinaryOperatorExpr { return BinaryOperator("<", left, right) }
func Gte(left, right AsExpr) *BinaryOperatorExpr { return BinaryOperator(">=", left, right) }
func Lte(left, right AsExpr) *BinaryOperatorExpr { return BinaryOperator("<=", left, right) }
type BinaryOperatorExpr struct {
operator string
left, right AsExpr
}
func BinaryOperator(operator string, left, right AsExpr) *BinaryOperatorExpr {
return &BinaryOperatorExpr{operator: operator, left: left, right: right}
}
func (o *BinaryOperatorExpr) AsExpr(s *Serializer) {
s.D("(").F(o.left.AsExpr).D(" " + o.operator + " ").F(o.right.AsExpr).D(")")
}
func Not(expr AsExpr) *PrefixOperatorExpr { return PrefixOperator("NOT", expr) }
type PrefixOperatorExpr struct {
operator string
expr AsExpr
}
func PrefixOperator(operator string, expr AsExpr) *PrefixOperatorExpr {
return &PrefixOperatorExpr{operator: operator, expr: expr}
}
func (o *PrefixOperatorExpr) AsExpr(s *Serializer) {
s.D(o.operator).D(" ").F(o.expr.AsExpr)
}
func IsNull(expr AsExpr) *PostfixOperatorExpr { return PostfixOperator(expr, "IS NULL") }
func IsNotNull(expr AsExpr) *PostfixOperatorExpr { return PostfixOperator(expr, "IS NOT NULL") }
type PostfixOperatorExpr struct {
expr AsExpr
operator string
}
func PostfixOperator(expr AsExpr, operator string) *PostfixOperatorExpr {
return &PostfixOperatorExpr{expr: expr, operator: operator}
}
func (o *PostfixOperatorExpr) AsExpr(s *Serializer) {
s.F(o.expr.AsExpr).D(" ").D(o.operator)
}
type InfixOperatorExpr struct {
operator string
exprs []AsExpr
}
func InfixOperator(operator string, exprs ...AsExpr) *InfixOperatorExpr {
return &InfixOperatorExpr{operator: operator, exprs: exprs}
}
func (o *InfixOperatorExpr) AsExpr(s *Serializer) {
s.D("(")
for i, e := range o.exprs {
s.F(e.AsExpr).DC(" "+o.operator+" ", i < len(o.exprs)-1)
}
s.D(")")
}