forked from wenj91/gobatis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_res.go
392 lines (327 loc) · 9.49 KB
/
proc_res.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
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
package gobatis
import (
"database/sql"
"errors"
"reflect"
)
type resultTypeProc = func(rows *sql.Rows, res interface{}) error
var resSetProcMap = map[ResultType]resultTypeProc{
resultTypeMap: resMapProc,
resultTypeMaps: resMapsProc,
resultTypeSlice: resSliceProc,
resultTypeArray: resSliceProc,
resultTypeSlices: resSlicesProc,
resultTypeArrays: resSlicesProc,
resultTypeValue: resValueProc,
resultTypeStructs: resStructsProc,
resultTypeStruct: resStructProc,
}
func resStructProc(rows *sql.Rows, res interface{}) error {
resVal := reflect.ValueOf(res)
if resVal.Kind() != reflect.Ptr {
return errors.New("struct query result must be ptr")
}
if resVal.Elem().Kind() != reflect.Ptr ||
!resVal.Elem().IsValid() ||
resVal.Elem().Elem().Kind() != reflect.Invalid {
tips := `
var res *XXX
queryParams := make(map[string]interface{})
queryParams["id"] = id
gb.Select("selectXXXById", queryParams)(&res)
Tips: "(&res)" --> don't forget "&"
`
return errors.New("Struct query result must be a struct ptr, " +
"and params res is the address of ptr, e.g. " + tips)
}
finalVal := reflect.New(resVal.Elem().Type().Elem())
finalStructPtr := finalVal.Interface()
arr, err := rowsToStructs(rows, reflect.TypeOf(finalStructPtr).Elem())
if nil != err {
return err
}
// fixme: 查询结果是返回错误呢, 觉得如果返回错误就会造成错误的困惑,
// 因为这里的错误定义是用于参数以及异常校验,
// 如果用户结果校验, 那么如果用户单单用err来判断是否存在查询对象而忽略了其它一些类似sql语句错误, 传参错误等,
// 还是不处理好呢??? 如果有人看到这里可以提下意见|・ω・`)
if len(arr) > 1 {
//return errors.New("Struct query result more than one row")
LOG.Warn("Struct query result more than one row")
resVal.Elem().Set(reflect.ValueOf(arr[0]))
}
// fixme: 查询结果是返回错误呢, 觉得如果返回错误就会造成错误的困惑,
// 因为这里的错误定义是用于参数校验以及异常,
// 如果用户结果校验, 那么如果用户单单用err来判断是否存在查询对象而忽略了其它一些类似sql语句错误, 传参错误等,
// 还是不处理好呢??? 如果有人看到这里可以提下意见|・ω・`)
if len(arr) == 0 {
//return errors.New("No result")
LOG.Warn("Struct query result is nil")
}
if len(arr) == 1 {
resVal.Elem().Set(reflect.ValueOf(arr[0]))
}
return nil
}
func resStructsProc(rows *sql.Rows, res interface{}) error {
sliceVal := reflect.ValueOf(res)
if sliceVal.Kind() != reflect.Ptr {
return errors.New("structs query result must be ptr")
}
slicePtr := reflect.Indirect(sliceVal)
if slicePtr.Kind() != reflect.Slice && slicePtr.Kind() != reflect.Array {
return errors.New("structs query result must be slice")
}
//get elem type
elem := slicePtr.Type().Elem()
resultType := elem
isPtr := elem.Kind() == reflect.Ptr
if isPtr {
resultType = elem.Elem()
}
if resultType.Kind() != reflect.Struct {
return errors.New("structs query results item must be struct")
}
arr, err := rowsToStructs(rows, resultType)
if nil != err {
return err
}
for i := 0; i < len(arr); i++ {
if isPtr {
slicePtr.Set(reflect.Append(slicePtr, reflect.ValueOf(arr[i])))
} else {
slicePtr.Set(reflect.Append(slicePtr, reflect.Indirect(reflect.ValueOf(arr[i]))))
}
}
return nil
}
func resValueProc(rows *sql.Rows, res interface{}) error {
resPtr := reflect.ValueOf(res)
if resPtr.Kind() != reflect.Ptr {
return errors.New("value query result must be ptr")
}
arr, err := rowsToSlices(rows)
if nil != err {
return err
}
if len(arr) > 1 {
return errors.New("value query result more than one row")
}
tempResSlice := arr[0].([]interface{})
if len(tempResSlice) > 1 {
return errors.New("value query result more than one col")
}
if len(tempResSlice) > 0 {
if nil != tempResSlice[0] {
value := reflect.Indirect(resPtr)
val := dataToFieldVal(tempResSlice[0], value.Type(), "val")
value.Set(reflect.ValueOf(val))
}
}
return nil
}
func resSlicesProc(rows *sql.Rows, res interface{}) error {
resPtr := reflect.ValueOf(res)
if resPtr.Kind() != reflect.Ptr {
return errors.New("slices query result must be ptr")
}
value := reflect.Indirect(resPtr)
if value.Kind() != reflect.Slice {
return errors.New("slices query result must be slice ptr")
}
arr, err := rowsToSlices(rows)
if nil != err {
return err
}
if len(arr) > 0 {
for _, item := range arr {
// get sub arr type
subVal := reflect.Indirect(reflect.New(reflect.TypeOf(res).Elem().Elem()))
for _, val := range item.([]interface{}) {
// set val to sub arr
subVal.Set(reflect.Append(subVal, reflect.ValueOf(val)))
}
// set sub arr to arr
value.Set(reflect.Append(value, subVal))
}
}
return nil
}
func resSliceProc(rows *sql.Rows, res interface{}) error {
resPtr := reflect.ValueOf(res)
if resPtr.Kind() != reflect.Ptr {
return errors.New("slice query result must be ptr")
}
value := reflect.Indirect(resPtr)
if value.Kind() != reflect.Slice {
return errors.New("slice query result must be slice ptr")
}
arr, err := rowsToSlices(rows)
if nil != err {
return err
}
if len(arr) > 1 {
return errors.New("slice query result more than one row")
}
if len(arr) > 0 {
tempResSlice := arr[0].([]interface{})
for _, v := range tempResSlice {
value.Set(reflect.Append(value, reflect.ValueOf(v)))
}
}
return nil
}
func resMapProc(rows *sql.Rows, res interface{}) error {
resBean := reflect.ValueOf(res)
if resBean.Kind() == reflect.Ptr {
return errors.New("map query result can not be ptr")
}
if resBean.Kind() != reflect.Map {
return errors.New("map query result must be map")
}
arr, err := rowsToMaps(rows)
if nil != err {
return err
}
if len(arr) > 1 {
return errors.New("map query result more than one row")
}
if len(arr) > 0 {
resMap := res.(map[string]interface{})
tempResMap := arr[0].(map[string]interface{})
for k, v := range tempResMap {
resMap[k] = v
}
}
return nil
}
func resMapsProc(rows *sql.Rows, res interface{}) error {
resPtr := reflect.ValueOf(res)
if resPtr.Kind() != reflect.Ptr {
return errors.New("maps query result must be ptr")
}
value := reflect.Indirect(resPtr)
if value.Kind() != reflect.Slice {
return errors.New("maps query result must be slice ptr")
}
arr, err := rowsToMaps(rows)
if nil != err {
return err
}
for i := 0; i < len(arr); i++ {
value.Set(reflect.Append(value, reflect.ValueOf(arr[i])))
}
return nil
}
func rowsToMaps(rows *sql.Rows) ([]interface{}, error) {
res := make([]interface{}, 0)
for rows.Next() {
resMap := make(map[string]interface{})
cols, err := rows.Columns()
if nil != err {
LOG.Error("rows to maps err:%v", err)
return res, err
}
vals := make([]interface{}, len(cols))
scanArgs := make([]interface{}, len(cols))
for i := range vals {
scanArgs[i] = &vals[i]
}
rows.Scan(scanArgs...)
for i := 0; i < len(cols); i++ {
val := vals[i]
if nil != val {
v := reflect.ValueOf(val)
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
val = string(val.([]uint8))
}
}
resMap[cols[i]] = val
}
res = append(res, resMap)
}
return res, nil
}
func rowsToSlices(rows *sql.Rows) ([]interface{}, error) {
res := make([]interface{}, 0)
for rows.Next() {
resSlice := make([]interface{}, 0)
cols, err := rows.Columns()
if nil != err {
LOG.Error("rows to slices err:%v", err)
return nil, err
}
vals := make([]interface{}, len(cols))
scanArgs := make([]interface{}, len(cols))
for i := range vals {
scanArgs[i] = &vals[i]
}
rows.Scan(scanArgs...)
for i := 0; i < len(cols); i++ {
val := vals[i]
if nil != val {
v := reflect.ValueOf(val)
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
val = string(val.([]uint8))
}
}
resSlice = append(resSlice, val)
}
res = append(res, resSlice)
}
return res, nil
}
func rowsToStructs(rows *sql.Rows, resultType reflect.Type) ([]interface{}, error) {
fieldsMapper := make(map[string]string)
fields := resultType.NumField()
for i := 0; i < fields; i++ {
field := resultType.Field(i)
fieldsMapper[field.Name] = field.Name
tag := field.Tag.Get("field")
if tag != "" {
fieldsMapper[tag] = field.Name
}
}
res := make([]interface{}, 0)
for rows.Next() {
cols, err := rows.Columns()
if nil != err {
LOG.Error("rows.Columns() err:%v", err)
return nil, err
}
vals := make([]interface{}, len(cols))
scanArgs := make([]interface{}, len(cols))
for i := range vals {
scanArgs[i] = &vals[i]
}
err = rows.Scan(scanArgs...)
if nil != err {
return nil, err
}
obj := reflect.New(resultType).Elem()
objPtr := reflect.Indirect(obj)
for i := 0; i < len(cols); i++ {
colName := cols[i]
fieldName := fieldsMapper[colName]
field := objPtr.FieldByName(fieldName)
// 设置相关字段的值,并判断是否可设值
if field.CanSet() && vals[i] != nil {
//获取字段类型并设值
data := dataToFieldVal(vals[i], field.Type(), fieldName)
// 数据库返回类型与字段类型不符合的情况下通知用户
if reflect.TypeOf(data).Name() != field.Type().Name() {
warnInfo := "[WARN] fieldType != dataType, filedName:" + fieldName +
" fieldType:" + field.Type().Name() +
" dataType:" + reflect.TypeOf(data).Name()
LOG.Warn(warnInfo)
}
if nil != data {
field.Set(reflect.ValueOf(data))
}
}
}
if objPtr.CanInterface() {
res = append(res, objPtr.Addr().Interface())
}
}
return res, nil
}