This repository was archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.go
More file actions
291 lines (263 loc) · 6.08 KB
/
struct.go
File metadata and controls
291 lines (263 loc) · 6.08 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
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
package database
import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"strings"
"time"
"github.com/gwaylib/errors"
"github.com/jmoiron/sqlx/reflectx"
)
// Bool type
type Bool bool
func (v *Bool) Scan(i interface{}) error {
b := sql.NullBool{}
if err := b.Scan(i); err != nil {
return err
}
*v = Bool(b.Bool)
return nil
}
func (v *Bool) Value() (driver.Value, error) {
return v, nil
}
// Int64 type
type Int64 int64
func (v *Int64) Scan(i interface{}) error {
b := sql.NullInt64{}
if err := b.Scan(i); err != nil {
return err
}
*v = Int64(b.Int64)
return nil
}
func (v Int64) Value() (driver.Value, error) {
return int64(v), nil
}
// Float64 type
type Float64 float64
func (v *Float64) Scan(i interface{}) error {
b := sql.NullFloat64{}
if err := b.Scan(i); err != nil {
return err
}
*v = Float64(b.Float64)
return nil
}
func (v Float64) Value() (driver.Value, error) {
return float64(v), nil
}
// String type
type String string
func (v *String) Scan(i interface{}) error {
b := sql.NullString{}
if err := b.Scan(i); err != nil {
return err
}
*v = String(b.String)
return nil
}
func (v String) Value() (driver.Value, error) {
return string(v), nil
}
func (v *String) String() string {
return string(*v)
}
// 通用的字符串查询
type DBData string
func (d *DBData) Scan(i interface{}) error {
if i == nil {
*d = ""
return nil
}
switch i.(type) {
case int64:
*d = DBData(fmt.Sprintf("%d", i))
case float64:
*d = DBData(fmt.Sprint(i))
case []byte:
*d = DBData(string(i.([]byte)))
case string:
*d = DBData(i.(string))
case bool:
*d = DBData(fmt.Sprintf("%t", i))
case time.Time:
*d = DBData(i.(time.Time).Format(time.RFC3339))
default:
*d = DBData(fmt.Sprint(i))
}
return nil
}
func (d *DBData) String() string {
return string(*d)
}
func makeDBData(l int) []interface{} {
r := make([]interface{}, l)
for i := 0; i < l; i++ {
d := DBData("")
r[i] = &d
}
return r
}
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, order *int, drvName *string, outputNames *[]byte, outputInputs *[]byte, outputVals *[]interface{}) *reflect.Value {
*order += 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,
order, drvName,
outputNames, outputInputs, outputVals,
)
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
}
*outputVals = append(*outputVals, v.Interface())
switch {
case strings.Index(*drvName, "oracle") > -1, strings.Index(*drvName, "oci8") > -1:
*order += 1
*outputNames = append(*outputNames, []byte("\""+f.Name+"\",")...)
*outputInputs = append(*outputInputs, []byte(fmt.Sprintf(":%s,", f.Name))...)
case strings.Index(*drvName, "postgres") > -1:
*outputNames = append(*outputNames, []byte("\""+f.Name+"\",")...)
*outputInputs = append(*outputInputs, []byte(fmt.Sprintf(":%d,", *order))...)
*order += 1
case strings.Index(*drvName, "sqlserver") > -1, strings.Index(*drvName, "mssql") > -1:
*outputNames = append(*outputNames, []byte("["+f.Name+"],")...)
*outputInputs = append(*outputInputs, []byte(fmt.Sprintf("@p%d,", *order))...)
*order += 1
case strings.Index(*drvName, "mysql") > -1:
*order += 1
*outputNames = append(*outputNames, []byte("`"+f.Name+"`,")...)
*outputInputs = append(*outputInputs, []byte("?,")...)
default:
*outputNames = append(*outputNames, []byte("\""+f.Name+"\",")...)
*outputInputs = append(*outputInputs, []byte("?,")...)
}
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())
names := []byte{}
inputs := []byte{}
vals := []interface{}{}
var autoIncrement *reflect.Value
childrenLen := len(tm.Tree.Children)
order := 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, &order, &drvName, &names, &inputs, &vals)
if autoField != nil {
autoIncrement = autoField
}
}
if len(names) == 0 {
panic("No public field in struct")
}
return &reflectInsertField{
Names: string(names[:len(names)-1]),
Stmts: string(inputs[:len(inputs)-1]),
Values: vals,
AutoIncrement: autoIncrement,
}, nil
}