-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
171 lines (152 loc) · 4.01 KB
/
db.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
package beeorm
import (
"errors"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"net/url"
"strings"
)
// 封装一层 getAll 获取列表 ,方便使用
func GetAll(c *beego.Controller, model interface{}, contains interface{}, defaultLimit int64) (err error) {
f, err := BuildFilter(c, defaultLimit)
if err != nil {
return
}
err = GetAllByFilter(model, contains, f)
return
}
func GetAllWithTotal(c *beego.Controller, model interface{}, contains interface{}, defaultLimit int64) (total int64, err error) {
f, err := BuildFilter(c, defaultLimit)
if err != nil {
return
}
total, err = GetAllByFilterWithTotal(model, contains, f)
return
}
type Filter struct {
Fields []string // 字段
Order []string // 排序 : -id id
Where map[string][]interface{} // id:1;user_id:in1,2,3|<=2
Limit int64 //
Offset int64 //
}
// 根据输入生成过滤条件
// query=id:1;type:in1,3,5 & order=-ascId,descType & limit=0 & offset=0
func BuildFilter(c *beego.Controller, defaultLimit int64) (f *Filter, err error) {
var fields []string
var order []string
var query = make(map[string]string)
var limit int64 = defaultLimit
var offset int64
// fields: col1,col2,entity.col3
if v := c.GetString("fields"); v != "" {
fields = strings.Split(v, ",")
}
// limit: 10 (default is 10)
if v, err := c.GetInt64("limit"); err == nil {
limit = v
if limit > 50 {
limit = 50
}
}
// offset: 0 (default is 0)
if v, err := c.GetInt64("offset"); err == nil {
offset = v
}
// order: desc,asc
if v := c.GetString("order"); v != "" {
order = strings.Split(v, ",")
}
// query: k:v;k:v
if v := c.GetString("query"); v != "" {
v, _ = url.QueryUnescape(v)
for _, cond := range strings.Split(v, ";") {
kv := strings.SplitN(cond, ":", 2)
if len(kv) != 2 {
err = errors.New("invalid query key/value pair")
return
}
k, v := kv[0], kv[1]
query[k] = v
}
}
where := map[string][]interface{}{} // condition => args
// condition : inx,x|<x|>x|>=x|<=x|=x|x|!=x|
for f, cond := range query {
// 支持一个字段多个条件
for _, x := range strings.Split(cond, "|") {
isNot := ""
if len(x) > 1 && x[0] == '!' {
isNot = "!"
x = x[1:]
}
if strings.Index(x, "in") == 0 {
args := []interface{}{}
for _, v := range strings.Split(x[2:], ",") {
args = append(args, v)
}
where[isNot+f+"__in"] = args
} else if strings.Index(x, ">=") == 0 {
where[isNot+f+"__gte"] = []interface{}{x[2:]}
} else if strings.Index(x, ">") == 0 {
where[isNot+f+"__gt"] = []interface{}{x[1:]}
} else if strings.Index(x, "<=") == 0 {
where[isNot+f+"__lte"] = []interface{}{x[2:]}
} else if strings.Index(x, "<") == 0 {
where[isNot+f+"__lt"] = []interface{}{x[1:]}
} else if strings.Index(x, "=") == 0 {
where[isNot+f ] = []interface{}{x[1:]}
} else if strings.Index(x, "like") == 0 {
where[isNot+f+"__icontains"] = []interface{}{x[4:]}
} else {
where[isNot+f ] = []interface{}{x}
}
}
}
f = &Filter{
Fields: fields,
Limit: limit,
Offset: offset,
Order: order,
Where: where,
}
return
}
func GetAllByFilter(model interface{}, contains interface{}, f *Filter) (err error) {
o := orm.NewOrm()
qs := o.QueryTable(model)
// query k=v
for k, v := range f.Where {
if len(k) > 1 && k[0] == '!' {
k = k[1:]
qs = qs.Exclude(k, v...)
} else {
qs = qs.Filter(k, v...)
}
}
// order by:
qs = qs.OrderBy(f.Order...)
_, err = qs.Limit(f.Limit, f.Offset).All(contains, f.Fields...)
return
}
func GetAllByFilterWithTotal(model interface{}, contains interface{}, f *Filter) (total int64, err error) {
o := orm.NewOrm()
qs := o.QueryTable(model)
// query k=v
for k, v := range f.Where {
if len(k) > 1 && k[0] == '!' {
k = k[1:]
qs = qs.Exclude(k, v...)
} else {
qs = qs.Filter(k, v...)
}
}
total, err = qs.Count()
if err != nil {
return
}
// order by:
qs = qs.OrderBy(f.Order...)
_, err = qs.Limit(f.Limit, f.Offset).All(contains, f.Fields...)
return
}