-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflect.go
More file actions
181 lines (164 loc) · 4.15 KB
/
reflect.go
File metadata and controls
181 lines (164 loc) · 4.15 KB
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
package qsql
import (
"database/sql/driver"
"fmt"
"reflect"
"strings"
"github.com/gwaylib/errors"
"github.com/jmoiron/sqlx/reflectx"
)
var refxM = reflectx.NewMapperTagFunc("db", func(in string) string {
// for tag name
return in
}, func(in string) string {
// for options
trims := []string{}
options := strings.Split(in, ",")
for _, op := range options {
trims = append(trims, strings.TrimSpace(op))
}
return strings.Join(trims, ",")
})
// return is it a auto_increment field
func _travelStructField(f *reflectx.FieldInfo, v *reflect.Value, drvName *string, fieldIdx *int, selectNames *[]string, stmtParams *[]string, scanVals *[]interface{}) *reflect.Value {
*fieldIdx += 1
switch v.Kind() {
case reflect.Invalid:
// nil value
return nil
case
reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64,
reflect.String:
// continue
break
case reflect.Struct, reflect.Ptr:
if _, ok := v.Interface().(driver.Valuer); ok {
break
}
switch v.Type().String() {
case "time.Time":
break
default:
var autoIncrement *reflect.Value
childrenLen := len(f.Children)
for i := 0; i < childrenLen; i++ {
child := f.Children[i]
if child == nil {
// found ignore tag, do next.
continue
}
fieldVal := reflect.Indirect(*v).Field(i)
autoFiled := _travelStructField(
child, &fieldVal, drvName,
fieldIdx, selectNames, stmtParams, scanVals,
)
if autoFiled != nil {
autoIncrement = autoFiled
}
}
return autoIncrement
}
default:
// unsupport
switch v.Type().String() {
case "[]uint8":
break
default:
return nil
}
}
//
// decode fileds
//
_, ok := f.Options["autoincrement"]
if ok {
// ignore 'autoincrement' for insert data
return v
}
_, ok = f.Options["auto_increment"]
if ok {
// ignore 'auto_increment' for insert data
return v
}
switch *drvName {
case DRV_NAME_ORACLE, _DRV_NAME_OCI8:
*fieldIdx += 1
*selectNames = append(*selectNames, "\""+f.Name+"\"")
*stmtParams = append(*stmtParams, fmt.Sprintf(":%s", f.Name))
case DRV_NAME_POSTGRES:
*selectNames = append(*selectNames, "\""+f.Name+"\"")
*stmtParams = append(*stmtParams, fmt.Sprintf(":%d", *fieldIdx))
*fieldIdx += 1
case DRV_NAME_SQLSERVER, _DRV_NAME_MSSQL:
*selectNames = append(*selectNames, "["+f.Name+"]")
*stmtParams = append(*stmtParams, fmt.Sprintf("@p%d", *fieldIdx))
*fieldIdx += 1
case DRV_NAME_MYSQL:
*fieldIdx += 1
*selectNames = append(*selectNames, "`"+f.Name+"`")
*stmtParams = append(*stmtParams, "?")
default:
*selectNames = append(*selectNames, "\""+f.Name+"\"")
*stmtParams = append(*stmtParams, "?")
}
*scanVals = append(*scanVals, v.Interface())
// recursive end by nil
return nil
}
type reflectInsertField struct {
Names []string
Stmts []string
Values []interface{}
AutoIncrement *reflect.Value
}
func (r *reflectInsertField) SetAutoIncrement(v reflect.Value) {
if r.AutoIncrement == nil {
return
}
r.AutoIncrement.Set(v)
}
func reflectInsertStruct(i interface{}, drvName string) (*reflectInsertField, error) {
v := reflect.ValueOf(i)
k := v.Kind()
switch k {
case reflect.Ptr:
default:
return nil, errors.New("Unsupport reflect type").As(k.String())
}
v = reflect.Indirect(v)
tm := refxM.TypeMap(v.Type())
outputSelectNames := []string{}
outputStmtParams := []string{}
outputFieldVals := []interface{}{}
var autoIncrement *reflect.Value
childrenLen := len(tm.Tree.Children)
fieldIdx := 0
for i := 0; i < childrenLen; i++ {
field := tm.Tree.Children[i]
if field == nil {
// found ignore tag, do next.
continue
}
fieldVal := v.Field(i)
autoField := _travelStructField(
field, &fieldVal, &drvName,
&fieldIdx,
&outputSelectNames, &outputStmtParams, &outputFieldVals,
)
if autoField != nil {
autoIncrement = autoField
}
}
if len(outputSelectNames) == 0 {
panic("No public field in struct")
}
return &reflectInsertField{
Names: outputSelectNames,
Stmts: outputStmtParams,
Values: outputFieldVals,
AutoIncrement: autoIncrement,
}, nil
}