@@ -24,26 +24,17 @@ var refxM = reflectx.NewMapperTagFunc("db", func(in string) string {
2424})
2525
2626// return is it a auto_increment field
27- func travelStructField (f * reflectx.FieldInfo , v * reflect.Value , order * int , drvName * string , outputNames * []byte , outputInputs * []byte , outputVals * []interface {}) * reflect.Value {
28- * order += 1
27+ func _travelStructField (f * reflectx.FieldInfo , v * reflect.Value , drvName * string , fieldIdx * int , selectNames * []string , stmtParams * []string , scanVals * []interface {}) * reflect.Value {
28+ * fieldIdx += 1
2929 switch v .Kind () {
3030 case reflect .Invalid :
3131 // nil value
3232 return nil
3333 case
3434 reflect .Bool ,
35- reflect .Int ,
36- reflect .Int8 ,
37- reflect .Int16 ,
38- reflect .Int32 ,
39- reflect .Int64 ,
40- reflect .Uint ,
41- reflect .Uint8 ,
42- reflect .Uint16 ,
43- reflect .Uint32 ,
44- reflect .Uint64 ,
45- reflect .Float32 ,
46- reflect .Float64 ,
35+ reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 ,
36+ reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 ,
37+ reflect .Float32 , reflect .Float64 ,
4738 reflect .String :
4839 // continue
4940 break
@@ -64,11 +55,9 @@ func travelStructField(f *reflectx.FieldInfo, v *reflect.Value, order *int, drvN
6455 continue
6556 }
6657 fieldVal := reflect .Indirect (* v ).Field (i )
67- autoFiled := travelStructField (
68- child ,
69- & fieldVal ,
70- order , drvName ,
71- outputNames , outputInputs , outputVals ,
58+ autoFiled := _travelStructField (
59+ child , & fieldVal , drvName ,
60+ fieldIdx , selectNames , stmtParams , scanVals ,
7261 )
7362 if autoFiled != nil {
7463 autoIncrement = autoFiled
@@ -101,35 +90,36 @@ func travelStructField(f *reflectx.FieldInfo, v *reflect.Value, order *int, drvN
10190 return v
10291 }
10392
104- * outputVals = append (* outputVals , v .Interface ())
10593 switch * drvName {
10694 case DRV_NAME_ORACLE , _DRV_NAME_OCI8 :
107- * order += 1
108- * outputNames = append (* outputNames , [] byte ( "\" " + f .Name + "\" ," ) ... )
109- * outputInputs = append (* outputInputs , [] byte ( fmt .Sprintf (":%s, " , f .Name )) ... )
95+ * fieldIdx += 1
96+ * selectNames = append (* selectNames , "\" " + f .Name + "\" " )
97+ * stmtParams = append (* stmtParams , fmt .Sprintf (":%s" , f .Name ))
11098 case DRV_NAME_POSTGRES :
111- * outputNames = append (* outputNames , [] byte ( "\" " + f .Name + "\" ," ) ... )
112- * outputInputs = append (* outputInputs , [] byte ( fmt .Sprintf (":%d, " , * order )) ... )
113- * order += 1
99+ * selectNames = append (* selectNames , "\" " + f .Name + "\" " )
100+ * stmtParams = append (* stmtParams , fmt .Sprintf (":%d" , * fieldIdx ) )
101+ * fieldIdx += 1
114102 case DRV_NAME_SQLSERVER , _DRV_NAME_MSSQL :
115- * outputNames = append (* outputNames , [] byte ( "[" + f .Name + "]," ) ... )
116- * outputInputs = append (* outputInputs , [] byte ( fmt .Sprintf ("@p%d, " , * order )) ... )
117- * order += 1
103+ * selectNames = append (* selectNames , "[" + f .Name + "]" )
104+ * stmtParams = append (* stmtParams , fmt .Sprintf ("@p%d" , * fieldIdx ) )
105+ * fieldIdx += 1
118106 case DRV_NAME_MYSQL :
119- * order += 1
120- * outputNames = append (* outputNames , [] byte ( "`" + f .Name + "`," ) ... )
121- * outputInputs = append (* outputInputs , [] byte ( "?," ) ... )
107+ * fieldIdx += 1
108+ * selectNames = append (* selectNames , "`" + f .Name + "`" )
109+ * stmtParams = append (* stmtParams , "?" )
122110 default :
123- * outputNames = append (* outputNames , [] byte ( "\" " + f .Name + "\" ," ) ... )
124- * outputInputs = append (* outputInputs , [] byte ( "?," ) ... )
111+ * selectNames = append (* selectNames , "\" " + f .Name + "\" " )
112+ * stmtParams = append (* stmtParams , "?" )
125113 }
114+ * scanVals = append (* scanVals , v .Interface ())
126115
116+ // recursive end by nil
127117 return nil
128118}
129119
130120type reflectInsertField struct {
131- Names string
132- Stmts string
121+ Names [] string
122+ Stmts [] string
133123 Values []interface {}
134124
135125 AutoIncrement * reflect.Value
@@ -154,13 +144,13 @@ func reflectInsertStruct(i interface{}, drvName string) (*reflectInsertField, er
154144
155145 tm := refxM .TypeMap (v .Type ())
156146
157- names := []byte {}
158- inputs := []byte {}
159- vals := []interface {}{}
147+ outputSelectNames := []string {}
148+ outputStmtParams := []string {}
149+ outputFieldVals := []interface {}{}
160150 var autoIncrement * reflect.Value
161151
162152 childrenLen := len (tm .Tree .Children )
163- order := 0
153+ fieldIdx := 0
164154 for i := 0 ; i < childrenLen ; i ++ {
165155 field := tm .Tree .Children [i ]
166156 if field == nil {
@@ -169,19 +159,23 @@ func reflectInsertStruct(i interface{}, drvName string) (*reflectInsertField, er
169159 }
170160
171161 fieldVal := v .Field (i )
172- autoField := travelStructField (field , & fieldVal , & order , & drvName , & names , & inputs , & vals )
162+ autoField := _travelStructField (
163+ field , & fieldVal , & drvName ,
164+ & fieldIdx ,
165+ & outputSelectNames , & outputStmtParams , & outputFieldVals ,
166+ )
173167 if autoField != nil {
174168 autoIncrement = autoField
175169 }
176170 }
177171
178- if len (names ) == 0 {
172+ if len (outputSelectNames ) == 0 {
179173 panic ("No public field in struct" )
180174 }
181175 return & reflectInsertField {
182- Names : string ( names [: len ( names ) - 1 ]) ,
183- Stmts : string ( inputs [: len ( inputs ) - 1 ]) ,
184- Values : vals ,
176+ Names : outputSelectNames ,
177+ Stmts : outputStmtParams ,
178+ Values : outputFieldVals ,
185179 AutoIncrement : autoIncrement ,
186180 }, nil
187181}
0 commit comments