-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_util.go
More file actions
565 lines (484 loc) · 14.5 KB
/
db_util.go
File metadata and controls
565 lines (484 loc) · 14.5 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package common
import (
"context"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gorm.io/gorm"
)
type Direction string
const (
ASCStr Direction = "ASC"
DESCStr Direction = "DESC"
)
const (
ASC = 1
DESC = 2
)
type OrderByParam struct {
Field string
Direction Direction
}
type OrderByParams []OrderByParam
type QueryOptions struct {
SelectFields []string
OmitFields []string
OrderFields OrderByParams
Limit int
Offset int
}
type MysqlOption struct {
Query string //缩小范围,仅支持字符串
Args []any
}
func (o *MysqlOption) GenMysqlWhere(db *gorm.DB) {
db.Where(o.Query, o.Args...)
}
func (o *MysqlOption) GenMongoOption(m bson.M) {
// m[o.Query] = o.Args[0]
}
type Option = Optioner
func O(query string, args ...any) *MysqlOption {
return &MysqlOption{Query: query, Args: args}
}
func W(query string, args ...any) *MysqlOption {
return &MysqlOption{Query: query, Args: args}
}
type SqlQueryOptions struct {
QueryFields []Optioner
SelectFields []string
// OmitFields []string
Joins []*MysqlOption
GroupBy []string
OrderFields OrderByParams
Limit int
Offset int
}
type SqlUpdateOptions struct {
QueryFields []Optioner
Updates any
}
type MongoQueryOptions struct {
QueryFields bson.M
SelectFields bson.M
OrderFields bson.M
Limit int
Offset int
}
type DbOperation struct {
TableName string
Db *gorm.DB
Context context.Context
}
// 带条件查询,
//
// 1. option是查询条件;
// 2. result是查询结果的存放地;
func (op *DbOperation) Query(option *SqlQueryOptions, result any) (err error) {
return op.QueryCV(option, nil, result)
}
// 带条件查询,
//
// 1. option是查询条件;
// 2. total是查询结果的总数;
// 3. result是查询结果的存放地;
func (op *DbOperation) QueryCV(option *SqlQueryOptions, total *int64, result any) (err error) {
tbOrg := op.Db.WithContext(op.Context).Table(op.TableName)
GenMysqlWhere(tbOrg, option.QueryFields)
// for _, where := range option.QueryFields {
// tbOrg.Where(where.Query, where.Args...)
// }
for _, join := range option.Joins {
tbOrg.Joins(join.Query, join.Args...)
}
// total!=nil 表示我想要总数;
// 如果此时Limit为0,表示如果此时的sql语句会查询所有行,就没有必要去count了;
// 此时如果rsult为nil,表示我不想查结果,那就单纯查count。所以又要count了;
if total != nil && (option.Limit > 0 || result == nil) {
tbOrg.Count(total)
if *total == 0 {
return tbOrg.Error
}
}
if result == nil {
return nil
}
for _, group := range option.GroupBy {
tbOrg.Group(group)
}
tbOrg.Select(option.SelectFields)
if option.Limit > 0 {
tbOrg.Limit(option.Limit)
}
if option.Offset > 0 { // offset 0 写不写效果相同,就少写一句,减少gorm拼接sql和数据库服务器的解析工作
tbOrg.Offset(option.Offset)
}
if len(option.OrderFields) > 0 {
for _, order := range option.OrderFields {
if order.Direction == ASCStr {
tbOrg.Order(order.Field + " ASC")
} else {
tbOrg.Order(order.Field + " DESC")
}
}
}
tbOrg.Find(result)
err = tbOrg.Error
return
}
func (op *DbOperation) Update(option *SqlUpdateOptions) (err error) {
tb := op.Db.WithContext(op.Context).Table(op.TableName)
GenMysqlWhere(tb, option.QueryFields)
tb.Updates(option.Updates)
return tb.Error
}
func (op *DbOperation) Delete(option []Optioner) (err error) {
tb := op.Db.WithContext(op.Context).Table(op.TableName)
GenMysqlWhere(tb, option)
tb.Delete(nil)
return tb.Error
}
// Create 新增
func (op *DbOperation) Create(obj any) (err error) {
tb := op.Db.WithContext(op.Context).Table(op.TableName)
tb.Create(obj)
return tb.Error
}
type OptionType int
const (
OptionTypeEq OptionType = iota
OptionTypeNe
)
type TypeOption struct {
Query string //缩小范围,仅支持字符串
Args any
}
type EqOption TypeOption
type NeOption TypeOption
type InOption TypeOption
type NotInOption TypeOption
type GtOption TypeOption
type GteOption TypeOption
type LtOption TypeOption
type LteOption TypeOption
// ExistOption 表示MongoDB的$exists操作符,用于检查字段是否存在
type ExistOption TypeOption
// LikeOption 表示模糊匹配操作符,用于SQL的LIKE查询和MongoDB的正则查询
type LikeOption struct {
Fields []string
Arg string
}
func IdsOption(ids any) []Optioner {
return []Optioner{In("id", ids)}
}
func IdOption(id any) []Optioner {
return []Optioner{Eq("id", id)}
}
func OptIn(field string, values any) []Optioner {
return []Optioner{In(field, values)}
}
func OptEq(field string, value any) []Optioner {
return []Optioner{Eq(field, value)}
}
func OptNe(field string, value any) []Optioner {
return []Optioner{Ne(field, value)}
}
func OptNotIn(field string, values any) []Optioner {
return []Optioner{NotIn(field, values)}
}
func Eq(field string, value any) EqOption {
return EqOption{Query: field, Args: value}
}
func Ne(field string, value any) NeOption {
return NeOption{Query: field, Args: value}
}
func In(field string, value any) InOption {
return InOption{Query: field, Args: value}
}
func NotIn(field string, value any) NotInOption {
return NotInOption{Query: field, Args: value}
}
func Gt(field string, value any) GtOption {
return GtOption{Query: field, Args: value}
}
func Gte(field string, value any) GteOption {
return GteOption{Query: field, Args: value}
}
func Lt(field string, value any) LtOption {
return LtOption{Query: field, Args: value}
}
func Lte(field string, value any) LteOption {
return LteOption{Query: field, Args: value}
}
// Exist 创建一个检查字段是否存在的条件
// field: 字段名
// exists: true表示字段必须存在,false表示字段必须不存在
func Exist(field string, exists bool) ExistOption {
return ExistOption{Query: field, Args: exists}
}
// Like 创建一个模糊匹配条件
// fields: 字段名列表,支持多个字段进行OR查询
// value: 匹配值(应包含适当的通配符,如%或_,由调用者控制)
func Like(fields []string, value string) LikeOption {
return LikeOption{Fields: fields, Arg: value}
}
type MongoOptioner interface {
GenMongoOption(m bson.M)
}
type Optioner interface {
MongoOptioner
MysqlOptioner
}
// GenMongoOption 实现MongoOptioner接口,将EqOption转换为MongoDB的等于查询
func (o EqOption) GenMongoOption(m bson.M) {
m[o.Query] = o.Args
}
// GenMongoOption 实现MongoOptioner接口,将NeOption转换为MongoDB的不等于查询
func (o NeOption) GenMongoOption(m bson.M) {
m[o.Query] = bson.M{"$ne": o.Args}
}
// GenMongoOption 实现MongoOptioner接口,将InOption转换为MongoDB的in查询
func (o InOption) GenMongoOption(m bson.M) {
m[o.Query] = bson.M{"$in": o.Args}
}
// GenMongoOption 实现MongoOptioner接口,将NotInOption转换为MongoDB的nin查询
func (o NotInOption) GenMongoOption(m bson.M) {
m[o.Query] = bson.M{"$nin": o.Args}
}
// GenMongoOption 实现MongoOptioner接口,将GtOption转换为MongoDB的大于查询
func (o GtOption) GenMongoOption(m bson.M) {
m[o.Query] = bson.M{"$gt": o.Args}
}
// GenMongoOption 实现MongoOptioner接口,将GteOption转换为MongoDB的大于等于查询
func (o GteOption) GenMongoOption(m bson.M) {
m[o.Query] = bson.M{"$gte": o.Args}
}
// GenMongoOption 实现MongoOptioner接口,将LtOption转换为MongoDB的小于查询
func (o LtOption) GenMongoOption(m bson.M) {
m[o.Query] = bson.M{"$lt": o.Args}
}
// GenMongoOption 实现MongoOptioner接口,将LteOption转换为MongoDB的小于等于查询
func (o LteOption) GenMongoOption(m bson.M) {
m[o.Query] = bson.M{"$lte": o.Args}
}
// GenMongoOption 实现MongoOptioner接口,将ExistOption转换为MongoDB的$exists查询
func (o ExistOption) GenMongoOption(m bson.M) {
m[o.Query] = bson.M{"$exists": o.Args}
}
// GenMongoOption 实现MongoOptioner接口,将LikeOption转换为MongoDB的正则查询
func (o LikeOption) GenMongoOption(m bson.M) {
if len(o.Fields) == 0 {
return
}
regex := bson.M{"$regex": o.Arg, "$options": "i"}
if len(o.Fields) == 1 {
// Single field: direct regex query
m[o.Fields[0]] = regex
} else {
// Multiple fields: use $or
conditions := []bson.M{}
for _, field := range o.Fields {
conditions = append(conditions, bson.M{
field: regex,
})
}
m["$or"] = conditions
}
}
// bsonRegexEscapeForLike 转义MongoDB正则表达式中的特殊字符并将SQL通配符转换为正则表达式
func bsonRegexEscapeForLike(pattern string) string {
// 先转义正则特殊字符
escaped := pattern
// 转义正则表达式特殊字符
specialChars := []string{`\`, `^`, `$`, `.`, `+`, `*`, `?`, `(`, `)`, `[`, `]`, `{`, `}`, `|`}
for _, char := range specialChars {
escaped = strings.ReplaceAll(escaped, char, `\`+char)
}
// 将SQL LIKE通配符转换为正则表达式(如果有的话)
escaped = strings.ReplaceAll(escaped, `%`, `.*`)
escaped = strings.ReplaceAll(escaped, `_`, `.`)
return escaped
}
// OrOption 表示MongoDB的$or操作符,用于组合多个查询条件(任一条件满足即可)
type OrOption struct {
Options []Optioner
}
// AndOption 表示MongoDB的$and操作符,用于组合多个查询条件(所有条件都必须满足)
type AndOption struct {
Options []Optioner
}
// Or 创建一个OR条件,满足任一条件即可
func Or(options ...Optioner) OrOption {
return OrOption{Options: options}
}
// And 创建一个AND条件,必须满足所有条件
func And(options ...Optioner) AndOption {
return AndOption{Options: options}
}
// GenMongoOption 实现MongoOptioner接口,将OrOption转换为MongoDB的$or查询
func (o OrOption) GenMongoOption(m bson.M) {
var conditions []bson.M
for _, option := range o.Options {
condition := make(bson.M)
option.GenMongoOption(condition)
conditions = append(conditions, condition)
}
m["$or"] = conditions
}
// GenMongoOption 实现MongoOptioner接口,将AndOption转换为MongoDB的$and查询
func (o AndOption) GenMongoOption(m bson.M) {
var conditions []bson.M
for _, option := range o.Options {
condition := make(bson.M)
option.GenMongoOption(condition)
conditions = append(conditions, condition)
}
m["$and"] = conditions
}
func GenMongoOption(option []Optioner) bson.M {
m := make(bson.M)
for _, v := range option {
v.GenMongoOption(m)
}
return m
}
type MysqlOptioner interface {
GenMysqlWhere(db *gorm.DB)
}
// GenMysqlWhere 实现MysqlOptioner接口,将EqOption转换为MySQL的等于查询
func (o EqOption) GenMysqlWhere(db *gorm.DB) {
db.Where(o.Query+" = ?", o.Args)
}
// GenMysqlWhere 实现MysqlOptioner接口,将NeOption转换为MySQL的不等于查询
func (o NeOption) GenMysqlWhere(db *gorm.DB) {
db.Where(o.Query+" <> ?", o.Args)
}
// GenMysqlWhere 实现MysqlOptioner接口,将InOption转换为MySQL的IN查询
func (o InOption) GenMysqlWhere(db *gorm.DB) {
db.Where(o.Query+" IN (?)", o.Args)
}
// GenMysqlWhere 实现MysqlOptioner接口,将NotInOption转换为MySQL的NOT IN查询
func (o NotInOption) GenMysqlWhere(db *gorm.DB) {
db.Where(o.Query+" NOT IN (?)", o.Args)
}
// GenMysqlWhere 实现MysqlOptioner接口,将GtOption转换为MySQL的大于查询
func (o GtOption) GenMysqlWhere(db *gorm.DB) {
db.Where(o.Query+" > ?", o.Args)
}
// GenMysqlWhere 实现MysqlOptioner接口,将GteOption转换为MySQL的大于等于查询
func (o GteOption) GenMysqlWhere(db *gorm.DB) {
db.Where(o.Query+" >= ?", o.Args)
}
// GenMysqlWhere 实现MysqlOptioner接口,将LtOption转换为MySQL的小于查询
func (o LtOption) GenMysqlWhere(db *gorm.DB) {
db.Where(o.Query+" < ?", o.Args)
}
// GenMysqlWhere 实现MysqlOptioner接口,将LteOption转换为MySQL的小于等于查询
func (o LteOption) GenMysqlWhere(db *gorm.DB) {
db.Where(o.Query+" <= ?", o.Args)
}
// GenMysqlWhere 实现MysqlOptioner接口,将ExistOption转换为MySQL的IS NULL/IS NOT NULL查询
func (o ExistOption) GenMysqlWhere(db *gorm.DB) {
if o.Args.(bool) {
db.Where(o.Query + " IS NOT NULL")
} else {
db.Where(o.Query + " IS NULL")
}
}
// GenMysqlWhere 实现MysqlOptioner接口,将LikeOption转换为MySQL的LIKE查询
func (o LikeOption) GenMysqlWhere(db *gorm.DB) {
if len(o.Fields) == 0 {
return
}
if len(o.Fields) == 1 {
db.Where(o.Fields[0]+" LIKE ?", o.Arg)
} else {
// Multiple fields: generate OR condition
query := "("
args := make([]any, len(o.Fields))
for i, field := range o.Fields {
if i > 0 {
query += " OR "
}
query += field + " LIKE ?"
args[i] = o.Arg
}
query += ")"
db.Where(query, args...)
}
}
// GenMysqlWhere 实现MysqlOptioner接口,将OrOption转换为MySQL的OR查询
func (o OrOption) GenMysqlWhere(db *gorm.DB) {
for i, option := range o.Options {
if i == 0 {
option.GenMysqlWhere(db)
} else {
db.Or(func(orDb *gorm.DB) {
option.GenMysqlWhere(orDb)
})
}
}
}
// GenMysqlWhere 实现MysqlOptioner接口,将AndOption转换为MySQL的AND查询
func (o AndOption) GenMysqlWhere(db *gorm.DB) {
for _, option := range o.Options {
option.GenMysqlWhere(db)
}
}
// 添加一个辅助函数,用于生成MySQL查询条件
func GenMysqlWhere(db *gorm.DB, options []Optioner) *gorm.DB {
for _, option := range options {
option.GenMysqlWhere(db)
}
return db
}
type MongoQueryOperation struct {
Collection *mongo.Collection
Context context.Context
filter bson.M
findOptions *options.FindOptions
}
func NewMongoQueryOperation(context context.Context, collection *mongo.Collection, opts []Optioner) *MongoQueryOperation {
return &MongoQueryOperation{
Collection: collection,
Context: context,
findOptions: options.Find(),
filter: GenMongoOption(opts),
}
}
func (op *MongoQueryOperation) Count() (total int64, err error) {
total, err = op.Collection.CountDocuments(op.Context, op.filter)
if err != nil {
return 0, err
}
return total, nil
}
func (op *MongoQueryOperation) Query(result any) (err error) {
cur, err := op.Collection.Find(op.Context, op.filter, op.findOptions)
if err != nil {
return err
}
defer cur.Close(op.Context)
err = cur.All(op.Context, result)
if err != nil {
return err
}
return err
}
func (op *MongoQueryOperation) SetSkip(skip int64) *options.FindOptions {
op.findOptions.SetSkip(skip)
return op.findOptions
}
func (op *MongoQueryOperation) SetLimit(limit int64) *options.FindOptions {
op.findOptions.SetLimit(limit)
return op.findOptions
}
func (op *MongoQueryOperation) SetProjection(cols []string) *options.FindOptions {
projection := bson.M{}
for _, col := range cols {
projection[col] = 1
}
op.findOptions.SetProjection(projection)
return op.findOptions
}