-
Notifications
You must be signed in to change notification settings - Fork 2
/
cnd.go
318 lines (280 loc) · 6.63 KB
/
cnd.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package sqlc
import (
"github.com/godaddy-x/jorm/dialect"
)
/**
* @author shadow
* @createby 2018.10.10
*/
// 数据库操作类型枚举
const (
EQ_ = iota
NOT_EQ_
LT_
LTE_
GT_
GTE_
IS_NULL_
IS_NOT_NULL_
BETWEEN_
NOT_BETWEEN_
IN_
NOT_IN_
LIKE_
NO_TLIKE_
OR_
ORDER_BY_
ASC_
DESC_
LEFT_
RIGHT_
INNER_
SUM_
AVG_
MIN_
MAX_
CNT_
)
var (
Id = "Id"
Ignore = "ignore"
Bson = "bson"
Json = "json"
Mg = "mg"
True = "true"
BsonId = "id"
Date = "date"
)
// 数据库操作逻辑条件对象
type Condition struct {
Logic int
Key string
Value interface{}
Values []interface{}
Alias string
}
// 连接表条件对象
type JoinCond struct {
Type int
Table string
Alias string
On string
}
// 主表条件对象
type FromCond struct {
Table string
Alias string
}
// 数据库操作汇总逻辑条件对象
type Cnd struct {
Conditions []Condition
AnyFields []string
Distincts []string
Groupbys []string
Orderbys []Condition
Summaries map[string]int
UpdateKV map[string]interface{}
Model interface{}
Pagination dialect.Dialect
FromCond FromCond
JoinCond []JoinCond
CacheConfig CacheConfig
Aggregates []Condition
}
// 缓存结果集参数
type CacheConfig struct {
Open bool
Prefix string
Key string
Expire int
}
// args[0]=对象类型 args[1]=数据表别名
func M(model interface{}) *Cnd {
return &Cnd{
Conditions: make([]Condition, 0),
AnyFields: make([]string, 0),
Distincts: make([]string, 0),
Groupbys: make([]string, 0),
Orderbys: make([]Condition, 0),
Summaries: make(map[string]int),
UpdateKV: make(map[string]interface{}),
Model: model,
Aggregates: make([]Condition, 0),
}
}
// 保存基础命令操作
func addDefaultCondit(cnd *Cnd, condit Condition) *Cnd {
cnd.Conditions = append(cnd.Conditions, condit)
return cnd
}
// =
func (self *Cnd) Eq(key string, value interface{}) *Cnd {
condit := Condition{EQ_, key, value, nil, ""}
return addDefaultCondit(self, condit)
}
// <>
func (self *Cnd) NotEq(key string, value interface{}) *Cnd {
condit := Condition{NOT_EQ_, key, value, nil, ""}
return addDefaultCondit(self, condit)
}
// <
func (self *Cnd) Lt(key string, value interface{}) *Cnd {
condit := Condition{LT_, key, value, nil, ""}
return addDefaultCondit(self, condit)
}
// <=
func (self *Cnd) Lte(key string, value interface{}) *Cnd {
condit := Condition{LTE_, key, value, nil, ""}
return addDefaultCondit(self, condit)
}
// >
func (self *Cnd) Gt(key string, value interface{}) *Cnd {
condit := Condition{GT_, key, value, nil, ""}
return addDefaultCondit(self, condit)
}
// >=
func (self *Cnd) Gte(key string, value interface{}) *Cnd {
condit := Condition{GTE_, key, value, nil, ""}
return addDefaultCondit(self, condit)
}
// is null
func (self *Cnd) IsNull(key string) *Cnd {
condit := Condition{IS_NULL_, key, nil, nil, ""}
return addDefaultCondit(self, condit)
}
// is not null
func (self *Cnd) IsNotNull(key string) *Cnd {
condit := Condition{IS_NOT_NULL_, key, nil, nil, ""}
return addDefaultCondit(self, condit)
}
// between
func (self *Cnd) Between(key string, value1 interface{}, value2 interface{}) *Cnd {
condit := Condition{BETWEEN_, key, nil, []interface{}{value1, value2}, ""}
return addDefaultCondit(self, condit)
}
// not between
func (self *Cnd) NotBetween(key string, value1 interface{}, value2 interface{}) *Cnd {
condit := Condition{NOT_BETWEEN_, key, nil, []interface{}{value1, value2}, ""}
return addDefaultCondit(self, condit)
}
// in
func (self *Cnd) In(key string, values ...interface{}) *Cnd {
condit := Condition{IN_, key, nil, values, ""}
return addDefaultCondit(self, condit)
}
// not in
func (self *Cnd) NotIn(key string, values ...interface{}) *Cnd {
condit := Condition{NOT_IN_, key, nil, values, ""}
return addDefaultCondit(self, condit)
}
// summary
func (self *Cnd) Summary(logic int, key string) *Cnd {
self.Summaries[key] = logic
return self
}
// like
func (self *Cnd) Like(key string, value interface{}) *Cnd {
condit := Condition{LIKE_, key, value, nil, ""}
return addDefaultCondit(self, condit)
}
// not like
func (self *Cnd) NotLike(key string, value interface{}) *Cnd {
condit := Condition{NO_TLIKE_, key, value, nil, ""}
return addDefaultCondit(self, condit)
}
// or
func (self *Cnd) Or(cnds ...interface{}) *Cnd {
condit := Condition{OR_, "", nil, cnds, ""}
return addDefaultCondit(self, condit)
}
// 复杂查询设定首个from table as
func (self *Cnd) From(fromTable string) *Cnd {
self.FromCond = FromCond{fromTable, ""}
return self
}
// left join
func (self *Cnd) Join(join int, table string, on string) *Cnd {
if len(table) == 0 || len(on) == 0 {
return self
}
self.JoinCond = append(self.JoinCond, JoinCond{join, table, "", on})
return self
}
// limit,以页数跨度查询
func (self *Cnd) Limit(pageNo int64, pageSize int64) *Cnd {
if pageNo <= 0 {
pageNo = 1
}
if pageSize <= 0 || pageSize > 5000 {
pageSize = 10
}
self.Pagination = dialect.Dialect{pageNo, pageSize, 0, 0, true, false}
return self
}
// offset,以下标跨度查询
func (self *Cnd) Offset(offset int64, limit int64) *Cnd {
if offset <= 0 {
offset = 0
}
if limit <= 0 || limit > 5000 {
limit = 10
}
self.Pagination = dialect.Dialect{offset, limit, 0, 0, true, true}
return self
}
// 筛选字段去重
func (self *Cnd) Distinct(keys ...string) *Cnd {
for e := range keys {
self.Distincts = append(self.Distincts, keys[e])
}
return self
}
// 按字段分组
func (self *Cnd) Groupby(keys ...string) *Cnd {
for e := range keys {
self.Groupbys = append(self.Groupbys, keys[e])
}
return self
}
// 聚合函数
func (self *Cnd) Agg(logic int, key string, alias ...string) *Cnd {
if len(key) == 0 {
return self
}
ali := key
if alias != nil || len(alias) > 0 {
ali = alias[0]
}
self.Aggregates = append(self.Aggregates, Condition{Logic: logic, Key: key, Alias: ali})
return self
}
// 按字段排序
func (self *Cnd) Orderby(key string, sortby int) *Cnd {
condit := Condition{ORDER_BY_, key, sortby, nil, ""}
self.Orderbys = append(self.Orderbys, condit)
return self
}
// 筛选指定字段查询
func (self *Cnd) Fields(keys ...string) *Cnd {
for e := range keys {
self.AnyFields = append(self.AnyFields, keys[e])
}
return self
}
// 缓存指定结果集
func (self *Cnd) Cache(config CacheConfig) *Cnd {
self.CacheConfig = config
self.CacheConfig.Open = true
return self
}
// 指定更新字段
func (self *Cnd) UpdateKeyValue(keys []string, values ...interface{}) *Cnd {
if len(keys) == 0 || len(keys) != len(values) {
println("keys和values参数下标不对等")
return self
}
for i := 0; i < len(keys); i++ {
self.UpdateKV[keys[i]] = values[i]
}
return self
}