forked from zhuxiujia/GoMybatis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoMybatisSqlResultDecoder.go
201 lines (190 loc) · 5.04 KB
/
GoMybatisSqlResultDecoder.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
package GoMybatis
import (
"encoding/json"
"github.com/zhuxiujia/GoMybatis/utils"
"reflect"
"strings"
)
type GoMybatisSqlResultDecoder struct {
SqlResultDecoder
}
func (it GoMybatisSqlResultDecoder) Decode(resultMap map[string]*ResultProperty, sqlResult []map[string][]byte, result interface{}) error {
if sqlResult == nil || result == nil {
return nil
}
var resultV = reflect.ValueOf(result)
if resultV.Kind() == reflect.Ptr {
resultV = resultV.Elem()
} else {
panic("[GoMybatis] SqlResultDecoder only support ptr type,make sure use '*Your Type'!")
}
var value = []byte{}
var sqlResultLen = len(sqlResult)
if sqlResultLen == 0 {
return nil
}
if !isArray(resultV.Kind()) {
//single basic type
if sqlResultLen > 1 {
return utils.NewError("SqlResultDecoder", " Decode one result,but find database result size find > 1 !")
}
// base type convert
if isBasicType(resultV.Type()) {
for _, s := range sqlResult[0] {
var b = strings.Builder{}
if resultV.Kind() == reflect.String || (resultV.Kind() == reflect.Struct) {
b.WriteString("\"")
b.Write(s)
b.WriteString("\"")
} else {
b.Write(s)
}
value = []byte(b.String())
break
}
} else {
var structMap, e = makeStructMap(resultV.Type())
if e != nil {
return e
}
value = makeJsonObjBytes(resultMap, sqlResult[0], structMap)
}
} else {
if resultV.Type().Kind() != reflect.Array && resultV.Type().Kind() != reflect.Slice {
return utils.NewError("SqlResultDecoder", " decode type not an struct array or slice!")
}
var resultVItemType = resultV.Type().Elem()
var structMap, e = makeStructMap(resultVItemType)
if e != nil {
return e
}
var done = len(sqlResult) - 1
var index = 0
var jsonData = strings.Builder{}
jsonData.WriteString("[")
for _, v := range sqlResult {
jsonData.Write(makeJsonObjBytes(resultMap, v, structMap))
//write ','
if index < done {
jsonData.WriteString(",")
}
index += 1
}
jsonData.WriteString("]")
value = []byte(jsonData.String())
}
e := json.Unmarshal(value, result)
return e
}
func makeStructMap(itemType reflect.Type) (map[string]*reflect.Type, error) {
if itemType.Kind() != reflect.Struct {
return nil, nil
}
var structMap = map[string]*reflect.Type{}
for i := 0; i < itemType.NumField(); i++ {
var item = itemType.Field(i)
structMap[strings.ToLower(item.Tag.Get("json"))] = &item.Type
}
return structMap, nil
}
//make an json value
func makeJsonObjBytes(resultMap map[string]*ResultProperty, sqlData map[string][]byte, structMap map[string]*reflect.Type) []byte {
var jsonData = strings.Builder{}
jsonData.WriteString("{")
var done = len(sqlData) - 1
var index = 0
for k, sqlV := range sqlData {
jsonData.WriteString("\"")
jsonData.WriteString(k)
jsonData.WriteString("\":")
var isStringType = false
var fetched = true
if resultMap != nil {
var resultMapItem = resultMap[k]
if resultMapItem != nil && (resultMapItem.LangType == "string" || resultMapItem.LangType == "time.Time") {
isStringType = true
}
if resultMapItem == nil {
fetched = false
}
} else if structMap != nil {
var v = structMap[strings.ToLower(k)]
if v != nil {
if (*v).Kind() == reflect.String || (*v).String() == "time.Time" {
isStringType = true
}
}
if v == nil {
fetched = false
}
} else {
isStringType = true
}
if fetched {
if isStringType {
jsonData.WriteString("\"")
jsonData.WriteString(encodeStringValue(sqlV))
jsonData.WriteString("\"")
} else {
if sqlV == nil || len(sqlV) == 0 {
sqlV = []byte("null")
}
jsonData.Write(sqlV)
}
} else {
sqlV = []byte("null")
jsonData.Write(sqlV)
}
//write ','
if index < done {
jsonData.WriteString(",")
}
index += 1
}
jsonData.WriteString("}")
return []byte(jsonData.String())
}
func encodeStringValue(v []byte) string {
if v == nil {
return "null"
}
if len(v) == 0 {
return ""
}
var s = string(v)
var b, e = json.Marshal(s)
if e != nil || len(b) == 0 {
return "null"
}
s = string(b[1 : len(b)-1])
return s
}
// is an array or slice
func isArray(kind reflect.Kind) bool {
if kind == reflect.Slice || kind == reflect.Array {
return true
}
return false
}
func isBasicType(tItemTypeFieldType reflect.Type) bool {
if tItemTypeFieldType.Kind() == reflect.Bool ||
tItemTypeFieldType.Kind() == reflect.Int ||
tItemTypeFieldType.Kind() == reflect.Int8 ||
tItemTypeFieldType.Kind() == reflect.Int16 ||
tItemTypeFieldType.Kind() == reflect.Int32 ||
tItemTypeFieldType.Kind() == reflect.Int64 ||
tItemTypeFieldType.Kind() == reflect.Uint ||
tItemTypeFieldType.Kind() == reflect.Uint8 ||
tItemTypeFieldType.Kind() == reflect.Uint16 ||
tItemTypeFieldType.Kind() == reflect.Uint32 ||
tItemTypeFieldType.Kind() == reflect.Uint64 ||
tItemTypeFieldType.Kind() == reflect.Float32 ||
tItemTypeFieldType.Kind() == reflect.Float64 ||
tItemTypeFieldType.Kind() == reflect.String {
return true
}
if tItemTypeFieldType.Kind() == reflect.Struct && tItemTypeFieldType.String() == "time.Time" {
return true
}
return false
}