-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
77 lines (65 loc) · 1.59 KB
/
doc.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
/*
Package wrapper is a thin wrapper of *xorm.Session.
It aims at eliminating tedious if statements.
List can be replaced by List2:
type ListReq struct {
CondA []int64
CondB []string
CondC string
CondD bool
CondE int64
CondF *Ranger
Limit int
Offset int
}
type Model struct{}
func List(ctx context.Context, req *ListReq) (int64, []*Model, error) {
var sess *xorm.Session
sess.Context(ctx)
if len(req.CondA) != 0 {
sess.Where("column_a IN ?", req.CondA)
}
if len(req.CondB) != 0 {
sess.Where("column_b IN ?", req.CondB)
}
if condC := strings.TrimSpace(req.CondC); condC != "" {
sess.Where("column_c LIKE ?", "%"+condC+"%")
}
sess.Where("column_d = ?", req.CondD)
if req.CondE != 0 {
sess.Where("column_e = ?", req.CondE)
}
if req.CondF != nil {
sess.Where("column_f BETWEEN ? AND ?",
req.CondF.Start, req.CondF.End)
}
var models []*Model
count, err := sess.
Desc("id").
Limit(req.Limit, req.Offset).
FindAndCount(&models)
if err != nil {
return 0, nil, err
}
return count, models, nil
}
func List2(ctx context.Context, req *ListReq) (int64, []*Model, error) {
sess := NewSession((*xorm.Session)(nil))
var models []*Model
count, err := sess.Context(ctx).
In("column_a", req.CondA).
In("column_b", req.CondB).
Like("column_c", req.CondC).
Where("column_d = ?", req.CondD).
Equal("column_e", req.CondE).
Between("column_f", req.CondF).
Desc("id").
Limit(req.Limit, req.Offset).
FindAndCount(&models)
if err != nil {
return 0, nil, err
}
return count, models, nil
}
*/
package wrapper