-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
332 lines (306 loc) · 8.01 KB
/
parse.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
package dao
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
)
//根据传入的struct ,自动生成insert sql
//struct 中的field 中的tag 包含db 时,其值会作为插入数据
func generateInsertSql(table string, format FiledNameFormat, v interface{}) string {
val := reflect.ValueOf(v)
args, names := parseInsertTag(val, format)
return fmt.Sprintf("insert into %s (%s) values (%s);", table, strings.Join(names, ","), strings.Join(args, ","))
}
func getTag(f reflect.StructField,tags ...string)string{
for _, tag := range tags {
t :=f.Tag.Get(tag)
if t != ""{
return t
}
}
return ""
}
func parseInsertTag(val reflect.Value, format FiledNameFormat) (args []string, names []string) {
typ := val.Type()
switch typ.Kind() {
case reflect.Ptr:
typ = typ.Elem()
val = val.Elem()
case reflect.Struct:
default:
panic("unsupported type while generate sql:" + typ.Name())
}
args = make([]string, 0, 5)
names = make([]string, 0, 5)
for i := 0; i < typ.NumField(); i++ {
fv := val.Field(i)
ft := typ.Field(i)
if ft.Anonymous {
as, ns := parseInsertTag(fv, format)
args = append(args, as...)
names = append(names, ns...)
continue
}
sqlTag := getTag(ft,"db","json")
if sqlTag == "" {
continue
}
insert := ft.Tag.Get("insert")
if insert == "0" {
continue
}
sqlTag = format(sqlTag)
names = append(names, sqlTag)
switch fv.Kind() {
case reflect.String:
args = append(args, fmt.Sprintf("'%s'", fv.String()))
case reflect.Int:
args = append(args, fmt.Sprintf("%d", fv.Int()))
case reflect.Bool, reflect.Int32, reflect.Int64,reflect.Float64,reflect.Float32:
args = append(args, fmt.Sprintf("%v", fv.Interface()))
default:
panic("unsupported field type while generate insert sql:" + fv.String())
}
}
return args, names
}
var NoElemError = errors.New("no elem found")
func getSqlQueryTagsByInterface(i interface{}, fmter FiledNameFormat) []string {
t := reflect.TypeOf(i)
switch t.Kind() {
case reflect.Struct:
case reflect.Ptr:
t = t.Elem()
default:
panic("invalid type:" + t.String())
}
return getSqlQueryTags(t, fmter)
}
// 解析要查询的sql
func getSqlQueryTags(vtype reflect.Type, fmter FiledNameFormat) []string {
switch vtype.Kind() {
case reflect.Ptr:
vtype = vtype.Elem()
fallthrough
case reflect.Struct:
tags := make([]string, 0, vtype.NumField())
for i := 0; i < vtype.NumField(); i++ {
field := vtype.Field(i)
if field.Anonymous {
tags = append(tags, getSqlQueryTags(field.Type, fmter)...)
continue
}
tag := getTag(field,"db","json")
if tag == "" {
continue
}
tags = append(tags,fmter(tag))
}
return tags
default:
panic("unknow type when parse tags:"+vtype.String())
}
}
func getStructFieldAddrByInterface(value interface{}) []interface{} {
vv := reflect.ValueOf(value)
switch vv.Kind() {
case reflect.Ptr:
return getStructFieldAddr(vv.Elem())
default:
panic("type must be ptr:" + vv.Type().String())
}
}
func getStructFieldAddr(value reflect.Value) []interface{} {
if value.Kind() != reflect.Struct {
panic("parse rows value must be struct")
}
vtype := value.Type()
sqlVals := make([]interface{}, 0, vtype.NumField())
for i := 0; i < vtype.NumField(); i++ {
field := vtype.Field(i)
if field.Anonymous {
sqlVals = append(sqlVals, getStructFieldAddr(value.Field(i))...)
continue
}
tag := getTag(field,"db","json")
if tag == "" {
continue
}
sqlVals = append(sqlVals, value.Field(i).Addr().Interface())
}
return sqlVals
}
func unmarshalFromRows(rows *sql.Rows, value interface{}) error {
defer rows.Close()
vval := reflect.ValueOf(value)
if vval.Kind() != reflect.Ptr {
panic("unmarshal row value must be ptr")
}
vval = vval.Elem()
switch vval.Kind() {
case reflect.Struct:
valueAddrs := getStructFieldAddr(vval)
hasElem := false
for rows.Next() {
if err := rows.Scan(valueAddrs...); err != nil {
return err
}
hasElem = true
break
}
if !hasElem {
return NoElemError
}
case reflect.Slice:
sliceElemType := vval.Type().Elem()
slice := reflect.MakeSlice(vval.Type(), 0, 1)
for rows.Next() {
var elemValue reflect.Value
if sliceElemType.Kind() == reflect.Ptr {
elemValue = reflect.New(sliceElemType.Elem())
} else {
elemValue = reflect.New(sliceElemType)
}
addrs := getStructFieldAddr(elemValue.Elem())
if err := rows.Scan(addrs...); err != nil {
return err
}
if sliceElemType.Kind() == reflect.Ptr {
slice = reflect.Append(slice, elemValue)
} else {
slice = reflect.Append(slice, elemValue.Elem())
}
}
vval.Set(slice)
default:
panic("unsupport type:" + vval.Kind().String())
}
return nil
}
//根据传入的struct ,自动生成update sql
//字段包含db 的tag 时,会作为查询条件
//以id 为更新条件
func generateUpdateSql(format FiledNameFormat, table string, cond string, v interface{}) string {
val := reflect.ValueOf(v)
attrs := parseUpdateTag(val, format)
return fmt.Sprintf("update %s set %s where %s;", table, strings.Join(attrs, ","), cond)
}
func parseUpdateTag(val reflect.Value, format FiledNameFormat) (attrs []string) {
typ := val.Type()
switch typ.Kind() {
case reflect.Ptr:
typ = typ.Elem()
val = val.Elem()
case reflect.Struct:
case reflect.Map:
it := val.MapRange()
attrs = make([]string, 0, len(val.MapKeys()))
for it.Next(){
key := it.Key()
val := it.Value()
vs := ""
switch val.Kind(){
case reflect.Interface:
vs = formatValueStr(val.Elem())
default:
vs = formatValueStr(val)
}
attr := strings.Builder{}
attr.WriteString(format(key.String()))
attr.WriteString("=")
attr.WriteString(vs)
attrs = append(attrs, attr.String())
}
return attrs
default:
panic("unsupported type while generate sql:" + typ.Name())
}
attrs = make([]string, 0, typ.NumField())
for i := 0; i < typ.NumField(); i++ {
fv := val.Field(i)
ft := typ.Field(i)
if ft.Anonymous {
ats := parseUpdateTag(fv, format)
attrs = append(attrs, ats...)
continue
}
sqlTag := getTag(ft,"db","json")
if sqlTag == "" {
continue
}
updateTag:=ft.Tag.Get("update")
if updateTag == "0"{
continue
}
valStr := formatValueStr(fv)
options := ft.Tag.Get("opt")
if options == "omitempty" && (valStr == "" || valStr == "''") {
continue
}
attr := strings.Builder{}
attr.WriteString(format(sqlTag))
attr.WriteString("=")
attr.WriteString(valStr)
attrs = append(attrs, attr.String())
}
return attrs
}
func formatValueStr(fv reflect.Value)string{
valStr := ""
switch fv.Kind() {
case reflect.String:
valStr = fmt.Sprintf("'%s'", fv.String())
case reflect.Bool, reflect.Int, reflect.Int32, reflect.Int64,reflect.Float64,reflect.Float32:
valStr = fmt.Sprintf("%v", fv.Interface())
case reflect.Struct:
bs,_:=json.Marshal(fv.Interface())
return fmt.Sprintf("'%s'",string(bs))
case reflect.Map:
if fv.IsNil(){
return "''"
}
bs,_:=json.Marshal(fv.Interface())
return fmt.Sprintf("'%s'",string(bs))
case reflect.Invalid:
return "''"
default:
panic("unsupported field type while generate insert sql:" + fv.String())
}
return valStr
}
type FiledNameFormat func(s string) string
func generateCreateTableSql(name string, v interface{}, format FiledNameFormat) string {
vv := reflect.TypeOf(v)
switch vv.Kind() {
case reflect.Ptr:
vv = vv.Elem()
case reflect.Struct:
default:
panic("invalid type:" + vv.String())
}
tags := generatorTableSqlTag(vv, format)
return fmt.Sprintf("CREATE TABLE %s (%s);", format(name), strings.Join(tags, ","))
}
func generatorTableSqlTag(t reflect.Type, format FiledNameFormat) []string {
if t.Kind() != reflect.Struct {
panic("invalid kind:" + t.String())
}
tags := make([]string, 0, t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Anonymous {
tags = append(tags, generatorTableSqlTag(field.Type, format)...)
continue
}
tag := getTag(field,"db","json")// "idd,primary key"
if tag == "" {
continue
}
opt := field.Tag.Get("tb")
tags = append(tags, fmt.Sprintf("%s %s", format(tag), opt))
}
return tags
}