-
Notifications
You must be signed in to change notification settings - Fork 4
/
cond_builder.go
264 lines (237 loc) · 5.78 KB
/
cond_builder.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2020 io.xream.sqlxb
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlxb
import "time"
type CondBuilder struct {
bbs []Bb
}
type BoolFunc func() bool
func subCondBuilder() *CondBuilder {
c := new(CondBuilder)
c.bbs = []Bb{}
return c
}
func (cb *CondBuilder) doIn(p string, k string, vs ...interface{}) *CondBuilder {
if vs == nil || len(vs) == 0 {
return cb
}
if len(vs) == 1 && (vs[0] == nil || vs[0] == "") {
return cb
}
ss := []string{}
length := len(vs)
for i := 0; i < length; i++ {
v := vs[i]
if v == nil {
continue
}
switch v.(type) {
case string:
s := "'"
s += v.(string)
s += "'"
ss = append(ss, s)
case uint64, uint, int, int64, int32, int16, int8, byte, float64, float32:
s := N2s(v)
if s == "0" {
continue
}
ss = append(ss, s)
case *uint64, *uint, *int, *int64, *int32, *int16, *int8, *byte, *float64, *float32:
s, isOK := Np2s(v)
if !isOK {
continue
}
ss = append(ss, s)
case interface{}:
panic("Builder.doIn(ke, (obj), ([]arr) ? ...")
default:
panic("Builder.doIn(ke, (*obj)), (*[]arr) ? ...")
}
}
bb := Bb{
op: p,
key: k,
value: &ss,
}
cb.bbs = append(cb.bbs, bb)
return cb
}
func (cb *CondBuilder) doLike(p string, k string, v string) *CondBuilder {
bb := Bb{
op: p,
key: k,
value: v,
}
cb.bbs = append(cb.bbs, bb)
return cb
}
func (cb *CondBuilder) doGLE(p string, k string, v interface{}) *CondBuilder {
switch v.(type) {
case string:
if v.(string) == "" {
return cb
}
case uint64, uint, int64, int, int32, int16, int8, bool, byte, float64, float32:
if v == 0 {
return cb
}
case *uint64, *uint, *int64, *int, *int32, *int16, *int8, *bool, *byte, *float64, *float32:
isNil, n := NilOrNumber(v)
if isNil {
return cb
}
return cb.addBb(p, k, n)
case time.Time:
ts := v.(time.Time).Format("2006-01-02 15:04:05")
return cb.addBb(p, k, ts)
case interface{}:
panic("Builder.doGLE(ke, obj, [] ? ...")
default:
if v == nil {
return cb
}
}
return cb.addBb(p, k, v)
}
func (cb *CondBuilder) addBb(op string, key string, v interface{}) *CondBuilder {
bb := Bb{
op: op,
key: key,
value: v,
}
cb.bbs = append(cb.bbs, bb)
return cb
}
func (cb *CondBuilder) null(op string, k string) *CondBuilder {
bb := Bb{
op: op,
key: k,
}
cb.bbs = append(cb.bbs, bb)
return cb
}
func (cb *CondBuilder) orAndSub(orAnd string, f func(cb *CondBuilder)) *CondBuilder {
c := subCondBuilder()
f(c)
if c.bbs == nil || len(c.bbs) == 0 {
return cb
}
bb := Bb{
op: orAnd,
key: orAnd,
subs: c.bbs,
}
cb.bbs = append(cb.bbs, bb)
return cb
}
func (cb *CondBuilder) orAnd(orAnd string) *CondBuilder {
length := len(cb.bbs)
if length == 0 {
return cb
}
pre := cb.bbs[length-1]
if pre.op == OR {
return cb
}
bb := Bb{
op: orAnd,
}
cb.bbs = append(cb.bbs, bb)
return cb
}
func (cb *CondBuilder) And(f func(cb *CondBuilder)) *CondBuilder {
return cb.orAndSub(AND_SUB, f)
}
func (cb *CondBuilder) Or(f func(cb *CondBuilder)) *CondBuilder {
return cb.orAndSub(OR_SUB, f)
}
func (cb *CondBuilder) OR() *CondBuilder {
return cb.orAnd(OR)
}
func (cb *CondBuilder) Bool(preCond BoolFunc, f func(cb *CondBuilder)) *CondBuilder {
if preCond == nil {
panic("CondBuilder.Bool para of BoolFunc can not nil")
}
if !preCond() {
return cb
}
if f == nil {
panic("CondBuilder.Bool para of func(k string, vs... interface{}) can not nil")
}
f(cb)
return cb
}
func (cb *CondBuilder) Eq(k string, v interface{}) *CondBuilder {
return cb.doGLE(EQ, k, v)
}
func (cb *CondBuilder) Ne(k string, v interface{}) *CondBuilder {
return cb.doGLE(NE, k, v)
}
func (cb *CondBuilder) Gt(k string, v interface{}) *CondBuilder {
return cb.doGLE(GT, k, v)
}
func (cb *CondBuilder) Lt(k string, v interface{}) *CondBuilder {
return cb.doGLE(LT, k, v)
}
func (cb *CondBuilder) Gte(k string, v interface{}) *CondBuilder {
return cb.doGLE(GTE, k, v)
}
func (cb *CondBuilder) Lte(k string, v interface{}) *CondBuilder {
return cb.doGLE(LTE, k, v)
}
// Like sql: LIKE %value%, Like() default has double %
func (cb *CondBuilder) Like(k string, v string) *CondBuilder {
if v == "" {
return cb
}
return cb.doLike(LIKE, k, "%"+v+"%")
}
func (cb *CondBuilder) NotLike(k string, v string) *CondBuilder {
if v == "" {
return cb
}
return cb.doLike(NOT_LIKE, k, "%"+v+"%")
}
// LikeLeft sql: LIKE value%, Like() default has double %, then LikeLeft() remove left %
func (cb *CondBuilder) LikeLeft(k string, v string) *CondBuilder {
if v == "" {
return cb
}
return cb.doLike(LIKE, k, v+"%")
}
func (cb *CondBuilder) In(k string, vs ...interface{}) *CondBuilder {
return cb.doIn(IN, k, vs...)
}
func (cb *CondBuilder) Nin(k string, vs ...interface{}) *CondBuilder {
return cb.doIn(NIN, k, vs...)
}
func (cb *CondBuilder) IsNull(key string) *CondBuilder {
return cb.null(IS_NULL, key)
}
func (cb *CondBuilder) NonNull(key string) *CondBuilder {
return cb.null(NON_NULL, key)
}
func (cb *CondBuilder) X(k string, vs ...interface{}) *CondBuilder {
bb := Bb{
op: XX,
key: k,
value: vs,
}
cb.bbs = append(cb.bbs, bb)
return cb
}