-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp_query.go
286 lines (254 loc) · 6.07 KB
/
http_query.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
/* 解析http请求的各种条件 */
package ogo
import (
"fmt"
"strconv"
"strings"
"time"
)
const (
_DEF_PAGE = 1 //1-base
_DEF_PER_PAGE = 100
_MAX_PER_PAGE = 1000 //每页最大个数
//time
_DATE_FORM = "2006-01-02"
_DATE_FORM1 = "20060102"
_TIME_FORM = "20060102150405"
)
//时间段
type TimeRange struct {
Start time.Time
End time.Time
}
// 分页信息
type Pagination struct {
Page int
PerPage int
Offset int
}
// 条件信息
/* {{{ func NewPagation(page, perPage string) (p *Pagination)
*/
func NewPagination(page, perPage string) (p *Pagination) {
var pageNum, offset, perNum int
if page == "" {
pageNum = _DEF_PAGE
} else {
pageNum, _ = strconv.Atoi(page)
if pageNum < 1 {
pageNum = _DEF_PAGE
}
}
if perPage == "" {
perNum = _DEF_PER_PAGE
} else {
perNum, _ = strconv.Atoi(perPage)
if perNum > _MAX_PER_PAGE {
perNum = _MAX_PER_PAGE
}
}
offset = (pageNum - 1) * perNum
p = &Pagination{
Page: pageNum,
PerPage: perNum,
Offset: offset,
}
return
}
/* }}} */
/* {{{ func (rc *RESTContext) GetCondition(k string) (con *Condition, err error)
* 设置参数查询条件
*/
func (rc *RESTContext) GetCondition(k string) (con *Condition, err error) {
if cs, ok := rc.Env[ConditionsKey]; !ok {
//没有conditions,自动初始化
rc.SetEnv(ConditionsKey, make([]*Condition, 0))
return nil, fmt.Errorf("Not found conditions! %s", ConditionsKey)
} else {
return GetCondition(cs.([]*Condition), k)
}
return
}
/* }}} */
/* {{{ func (rc *RESTContext) setCondition(con *Condition) (err error) {
return
*
*/
func (rc *RESTContext) setCondition(con *Condition) {
//Debug("[setCondition][key: %s]%v", con.Field, con)
if _, ok := rc.Env[ConditionsKey]; !ok {
//没有conditions,自动初始化
rc.SetEnv(ConditionsKey, make([]*Condition, 0))
}
//rc.Env[ConditionsKey] = append(rc.Env[ConditionsKey].([]*Condition), con)
set := false
for _, ec := range rc.Env[ConditionsKey].([]*Condition) {
if ec.Field == con.Field {
ec.Merge(con)
set = true
}
}
if !set {
rc.Env[ConditionsKey] = append(rc.Env[ConditionsKey].([]*Condition), con)
}
}
/* }}} */
/* {{{ func (rc *RESTContext) setTimeRangeFromDate(p []string) {
* 时间段信息
*/
func (rc *RESTContext) setTimeRangeFromDate(p []string) {
tr := new(TimeRange)
rc.SetEnv(TimeRangeKey, tr)
var s, e, format string
if len(p) > 1 { //有多个,第一个是start, 第二个是end, 其余忽略
s, e = p[0], p[1]
} else if len(p) > 0 { //只有一个, 可通过 "{start},{end}"方式传
pieces := strings.SplitN(p[0], ",", 2)
s = pieces[0]
if len(pieces) > 1 {
e = pieces[1]
}
}
if len(s) == len(_DATE_FORM) {
format = _DATE_FORM
} else if len(s) == len(_DATE_FORM1) {
format = _DATE_FORM1
}
if ts, err := time.ParseInLocation(format, s, Env().Location); err == nil {
tr.Start = ts
dura, _ := time.ParseDuration("86399s") // 一天少一秒
tr.End = ts.Add(dura) //当天的最后一秒
//只有成功获取了start, end才有意义
if t, err := time.ParseInLocation(format, e, Env().Location); err == nil {
te := t.Add(dura)
if te.After(ts) { //必须比开始大
tr.End = te
}
}
}
return
}
/* }}} */
/* {{{ func (rc *RESTContext) setTimeRangeFromStartEnd() {
* 时间段信息
*/
func (rc *RESTContext) setTimeRangeFromStartEnd() {
var sp, ep []string
var ok bool
r := rc.Request
if sp, ok = r.Form[_PARAM_START]; !ok {
//没有传入start,do nothing
return
}
//删除
r.Form.Del(_PARAM_START)
if ep, ok = r.Form[_PARAM_END]; !ok {
//没有传入end,do nothing
return
}
//删除
r.Form.Del(_PARAM_END)
s, e := sp[0], ep[0]
if len(s) != len(e) {
//长度不一致,返回
return
}
var format string
if len(s) == len(_DATE_FORM) {
format = _DATE_FORM
} else if len(s) == len(_DATE_FORM1) {
format = _DATE_FORM1
}
tr := new(TimeRange)
if ts, err := time.ParseInLocation(format, s, Env().Location); err != nil {
return
} else {
tr.Start = ts
}
if te, err := time.ParseInLocation(format, e, Env().Location); err != nil {
return
} else {
dura, _ := time.ParseDuration("86399s") // 一天少一秒
tr.End = te.Add(dura) //当天的最后一秒
}
rc.SetEnv(TimeRangeKey, tr)
return
}
/* }}} */
/* {{{ func (rc *RESTContext) setOrderBy(p string) {
* 时间段信息
*/
func (rc *RESTContext) setOrderBy(p []string) {
ob := new(OrderBy)
rc.SetEnv(OrderByKey, ob)
if len(p) > 0 { //只有一个, 可通过 "{start},{end}"方式传
pieces := strings.SplitN(p[0], ",", 2)
ob.Field = pieces[0]
ob.Sort = "DESC" //默认降序
if len(pieces) > 1 && strings.ToUpper(pieces[1]) == "ASC" {
ob.Sort = "ASC"
}
Debug("[orderby][field: %s][sort: %s]", ob.Field, ob.Sort)
}
return
}
/* }}} */
/* {{{ func (rc *RESTContext) GetQueryParam(key string) (string, int)
*/
func (rc *RESTContext) GetQueryParam(key string) (r string, c int) {
v := rc.Request.Form[key]
c = len(v)
if c == 1 {
return string(v[0]), c
} else {
return string(strings.Join(v, ",")), c
}
}
/* }}} */
/* {{{ func (rc *RESTContext) GetQueryParams(key string) (string, int)
*/
func (rc *RESTContext) GetQueryParams(key string) (rs []string) {
rs = rc.Request.Form[key]
return
}
/* }}} */
/* {{{ func ParseCondition(typ string, con *Condition) *Condition
*
*/
func ParseCondition(typ string, con *Condition) *Condition {
switch typ {
case "*time.Time":
if con.Is != nil {
if cv, ok := con.Is.(string); ok {
if t, err := time.ParseInLocation(_TIME_FORM, cv, Env().Location); err == nil {
con.Is = t
}
}
}
if con.Not != nil {
if cv, ok := con.Not.(string); ok {
if t, err := time.ParseInLocation(_TIME_FORM, cv, Env().Location); err == nil {
con.Not = t
}
}
}
if con.Gt != nil {
if cv, ok := con.Gt.(string); ok {
if t, err := time.ParseInLocation(_TIME_FORM, cv, Env().Location); err == nil {
con.Gt = t
}
}
}
if con.Lt != nil {
if cv, ok := con.Lt.(string); ok {
if t, err := time.ParseInLocation(_TIME_FORM, cv, Env().Location); err == nil {
con.Lt = t
}
}
}
return con
default:
return con
}
}
/* }}} */