-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
360 lines (324 loc) · 9.06 KB
/
view.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
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
package grest
import (
"errors"
"fmt"
"strings"
"github.com/jinzhu/gorm"
)
// APIView Model construct
type APIView struct{}
// toPrimaryQueryParams generate query params based on primary key, multiple primary value are linked with a comma
func (p *APIView) toPrimaryQueryParams(result interface{}, primaryValue string, context *Context) (string, []interface{}) {
if primaryValue != "" {
scope := context.GetDB().NewScope(result)
if primaryField := scope.PrimaryField(); primaryField != nil {
primaryFields := []*gorm.StructField{primaryField.StructField}
if len(primaryFields) > 1 {
if primaryValueStrs := strings.Split(primaryValue, ","); len(primaryValueStrs) == len(primaryFields) {
sqls := make([]string, 0)
primaryValues := make([]interface{}, 0)
for idx, field := range primaryFields {
sqls = append(sqls, fmt.Sprintf("%v.%v = ?", scope.QuotedTableName(), scope.Quote(field.DBName)))
primaryValues = append(primaryValues, primaryValueStrs[idx])
}
return strings.Join(sqls, " AND "), primaryValues
}
}
// fallback to first configured primary field
if len(primaryFields) > 0 {
return fmt.Sprintf("%v.%v = ?", scope.QuotedTableName(), scope.Quote(primaryFields[0].DBName)), []interface{}{primaryValue}
}
}
//multiple primary fields
// if no configured primary fields found
if primaryField := scope.PrimaryField(); primaryField != nil {
return fmt.Sprintf("%v.%v = ?", scope.QuotedTableName(), scope.Quote(primaryField.DBName)), []interface{}{primaryValue}
}
}
return "", []interface{}{}
}
// findCount2 query data count
// NOTE: not use
func (p *APIView) findCount2(result interface{}, filter map[string]interface{}, context *Context) (int, error) {
db := context.GetDB()
if db == nil {
return 0, errors.New("db is nil")
}
db = db.Begin()
db = db.Find(result)
if filter != nil {
if where, ok := filter["where"]; ok {
if whereArr, ok := where.([]interface{}); ok {
if len(whereArr) == 1 {
db = db.Where(whereArr[0])
} else if len(whereArr) > 1 {
db = db.Where(whereArr[0], whereArr[1:]...)
}
} else {
return 0, errors.New("where format is incorrect, non-array")
}
}
}
count := 0
if db.Count(&count).Commit(); db.Error != nil {
return 0, db.Error
}
return count, nil
}
// findCount query data count
func (p *APIView) findCount(result interface{}, where []interface{}, context *Context) (int, error) {
db := context.GetDB()
if db == nil {
return 0, errors.New("db is nil")
}
db = db.Begin()
db = db.Find(result)
if where != nil {
if len(where) == 1 {
db = db.Where(where[0])
} else if len(where) > 1 {
db = db.Where(where[0], where[1:]...)
}
}
count := 0
if db.Error != nil {
return 0, db.Error
}
db = db.Count(&count)
if db == nil {
return 0, errors.New("db is nil")
}
if db.Error != nil {
return 0, db.Error
}
if db.Commit(); db.Error != nil {
return 0, db.Error
}
return count, nil
}
// FindMany2 query data
// NOTE: not use
func (p *APIView) FindMany2(result interface{}, filter map[string]interface{}, context *Context) (int, error) {
db := context.GetDB()
if db == nil {
return 0, errors.New("db is nil")
}
db = db.Begin()
var count = 0
if filter != nil {
// query fields
if fields, ok := filter["fields"]; ok {
if fieldsArr, ok := fields.([]interface{}); ok {
db = db.Select(fieldsArr)
} else {
return 0, errors.New("fields format is incorrect, non-array")
}
}
// query result sorting
if order, ok := filter["order"]; ok {
db = db.Order(order)
}
// query by where condition
if where, ok := filter["where"]; ok {
if whereArr, ok := where.([]interface{}); ok {
if len(whereArr) == 1 {
db = db.Where(whereArr[0])
} else if len(whereArr) > 1 {
db = db.Where(whereArr[0], whereArr[1:]...)
}
} else {
return 0, errors.New("where format is incorrect, non-array")
}
}
// whether the query count
if withCount, ok := filter["withCount"]; ok {
if withCountBool, ok := withCount.(bool); ok {
if withCountBool {
c, err := p.findCount2(result, filter, context)
if err != nil {
return 0, err
}
count = c
}
} else {
return 0, errors.New("withCount format is incorrect, non-bool")
}
}
joins, joinsOk := filter["joins"]
groups, groupOk := filter["groups"]
if joinsOk && groupOk {
joinArr, joinArrOk := joins.([]interface{})
groupArr, groupArrOk := groups.([]interface{})
if joinArrOk && groupArrOk {
for _, join := range joinArr {
if joinStr, ok := join.(string); ok {
db = db.Joins(joinStr)
}
}
for _, group := range groupArr {
if groupStr, ok := group.(string); ok {
db = db.Group(groupStr)
}
}
}
}
// query contains related data
if includes, ok := filter["include"]; ok {
switch includes.(type) {
case string:
db = db.Preload(includes.(string))
case []interface{}:
for _, include := range includes.([]interface{}) {
switch include.(type) {
case string:
db = db.Preload(include.(string))
case []interface{}:
arr := include.([]interface{})
if len(arr) == 1 {
if a, ok := arr[0].(string); ok {
db = db.Preload(a)
} else {
return 0, errors.New("include format is incorrect, the first element of the array is not a string")
}
} else if len(arr) > 1 {
if a, ok := arr[0].(string); ok {
db = db.Preload(a, arr[1:])
} else {
return 0, errors.New("include format is incorrect, the first element of the array is not a string")
}
}
default:
return 0, errors.New("include format is incorrect,non-string or non-array")
}
}
default:
return 0, errors.New("include format is incorrect,non-string or non-array")
}
}
// query offset
offset, offsetOk := filter["offset"]
// query limit
limit, limitOk := filter["limit"]
// exist offset and limit
if offsetOk && limitOk {
db = db.Limit(limit).Offset(offset)
}
}
if db.Find(result).Commit(); db.Error != nil {
return 0, db.Error
}
return count, nil
}
// FindMany query data
func (p *APIView) FindMany(result interface{}, filter *Filter, context *Context) (int, error) {
db := context.GetDB()
if db == nil {
return 0, errors.New("db is nil")
}
db = db.Begin()
var count = 0
if filter != nil {
// query fields
if filter.Fields != nil && len(filter.Fields) > 0 {
db = db.Select(filter.Fields)
}
// query result sorting
if filter.Order != "" {
db = db.Order(filter.Order)
}
// query by where condition
if filter.Where != nil && len(filter.Where) > 0 {
if len(filter.Where) == 1 {
db = db.Where(filter.Where[0])
} else if len(filter.Where) > 1 {
db = db.Where(filter.Where[0], filter.Where[1:]...)
}
}
// whether the query count
c, err := p.findCount(result, filter.Where, context)
if err != nil {
return 0, err
}
count = c
if filter.Joins != nil && len(filter.Joins) > 0 {
for _, join := range filter.Joins {
db = db.Joins(join)
}
}
if filter.Groups != nil && len(filter.Groups) > 0 {
for _, group := range filter.Groups {
db = db.Group(group)
}
}
// query contains related data
if filter.Preloads != nil && len(filter.Groups) > 0 {
if len(filter.Groups) == 1 {
db = db.Preload(filter.Groups[0])
} else if len(filter.Groups) > 1 {
db = db.Preload(filter.Groups[0], filter.Groups[1:])
}
}
// query offset
// query limit
// if filter.Offset != "" && filter.Limit != "" {
// offset, err := strconv.Atoi(filter.Offset)
// if err != nil {
// return 0, fmt.Errorf("offset format is incorrect,%v", err.Error())
// }
// limit, err := strconv.Atoi(filter.Limit)
// if err != nil {
// return 0, fmt.Errorf("limit format is incorrect,%v", err.Error())
// }
// db = db.Limit(limit).Offset(offset)
// }
if filter.Limit != 0 {
db = db.Limit(filter.Limit)
db = db.Offset(filter.Offset)
}
}
db = db.Find(result)
if db == nil {
return 0, errors.New("db is nil")
}
if db.Error != nil {
return 0, db.Error
}
if db.Commit(); db.Error != nil {
return 0, db.Error
}
return count, nil
}
// Save is Model create
func (p *APIView) Save(result interface{}, context *Context) error {
db := context.GetDB()
if db == nil {
return errors.New("db is nil")
}
if db.NewScope(result).PrimaryKeyZero() {
return db.Create(result).Error
}
return db.Save(result).Error
}
// FindOne Model query one data
func (p *APIView) FindOne(result interface{}, context *Context) error {
primaryQuerySQL, primaryParams := p.toPrimaryQueryParams(result, context.ResourceID, context)
db := context.GetDB()
if db == nil {
return errors.New("db is nil")
}
if primaryQuerySQL != "" {
return db.First(result, append([]interface{}{primaryQuerySQL}, primaryParams...)...).Error
}
return errors.New("failed to find")
}
// Delete Model delete one data
func (p *APIView) Delete(result interface{}, context *Context) error {
db := context.GetDB()
if db == nil {
return errors.New("db is nil")
}
if !db.Find(result).RecordNotFound() {
return db.Delete(result).Error
}
return gorm.ErrRecordNotFound
}