forked from go-sql-driver/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields_map.go
323 lines (261 loc) · 7.76 KB
/
fields_map.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
package mysql
import (
"context"
"database/sql"
"errors"
"reflect"
)
// Field db field
// describe struct mapping in DB like:
// type DemoRow struct {
// FieldKey string `sql:"field_key"`
// FieldOne string `sql:"field_one"`
// FieldTwo bool `sql:"field_two"`
// FieldThr int64 `sql:"field_thr"`
// FieldFou float64 `sql:"field_fou"`
// }
//
type Field struct {
Name string
Tag string
Type string
IntValue int64
StringValue string
FloatValue float64
BoolValue bool
Addr interface{}
Save []byte // for null string
}
// FieldsMap hold Field
type FieldsMap interface {
// GetFields Fields
GetFields() []Field
// GetFieldNamesInDB get Names in db from Fields
GetFieldNamesInDB() []string
// GetFieldValues get Values in Object(struct)
GetFieldValues() []interface{}
// GetFieldAddrs get Pointers of Values in Object(struct)
GetFieldAddrs() []interface{}
// MappingBackToObject mapping back to the original object
MappingBackToObject() interface{}
////////////////////////////////////////////////////////////////
// generate SQL string
// SQLFieldsStr generate sqlstr in db from Fields
SQLFieldsStr() string
// SQLFieldsStrForSet generate sqlstr in db from Fields for set
SQLFieldsStrForSet() string
////////////////////////////////////////////////////////////////
// generate statement
// PrepareStmt prepare statement
PrepareStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
sqlstr string) (*sql.Stmt, error)
// SQLSelectStmt generate statement for SELECT
SQLSelectStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
extStr string) (*sql.Stmt, error)
// SQLInsertStmt generate statement for INSERT
SQLInsertStmt(ctx context.Context, tx *sql.Tx, db *sql.DB) (*sql.Stmt, error)
// SQLUpdateStmt generate statement for UPDATE
SQLUpdateStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
extStr string) (*sql.Stmt, error)
// SQLDeleteStmt generate statement for DELETE
SQLDeleteStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
extStr string) (*sql.Stmt, error)
}
////////////////////////////////////////////////////////////////
// NewFieldsMap new Fields
func NewFieldsMap(table string, objptr interface{}) (FieldsMap, error) {
elem := reflect.ValueOf(objptr).Elem()
reftype := elem.Type()
var fields []Field
for i, flen := 0, reftype.NumField(); i < flen; i++ {
var field Field
field.Name = reftype.Field(i).Name
field.Tag = reftype.Field(i).Tag.Get("sql")
field.Type = reftype.Field(i).Type.String()
field.Addr = elem.Field(i).Addr().Interface()
switch field.Type {
case "int64":
field.IntValue = elem.Field(i).Int()
break
case "string":
field.StringValue = elem.Field(i).String()
break
case "float64":
field.FloatValue = elem.Field(i).Float()
break
case "bool":
field.BoolValue = elem.Field(i).Bool()
break
default:
return nil, errors.New("unsupported field.Type:" + field.Type)
}
fields = append(fields, field)
}
return &_FieldsMap{
objptr: objptr,
fields: fields,
table: table,
}, nil
}
////////////////////////////////////////////////////////////////
var _ FieldsMap = &_FieldsMap{}
type _FieldsMap struct {
objptr interface{}
fields []Field
table string
}
// GetFields get Fields for an Object(struct)
func (fds *_FieldsMap) GetFields() []Field {
return fds.fields
}
// GetFieldNamesInDB get Names in db from Fields
// example:
// type DemoRow struct {
// FieldKey string `sql:"field_key"`
// FieldOne string `sql:"field_one"`
// FieldTwo bool `sql:"field_two"`
// FieldThr int64 `sql:"field_thr"`
// FieldFou float64 `sql:"field_fou"`
// }
//
// return ["field_key", "field_one", "field_two","field_thr","field_fou"]
//
func (fds *_FieldsMap) GetFieldNamesInDB() []string {
var tags []string
for i, flen := 0, len(fds.fields); i < flen; i++ {
tags = append(tags, fds.fields[i].Tag)
}
return tags
}
// GetFieldValues get Values in Object(struct)
func (fds *_FieldsMap) GetFieldValues() []interface{} {
var values []interface{}
for i, flen := 0, len(fds.fields); i < flen; i++ {
switch fds.fields[i].Type {
case "int64":
values = append(values, fds.fields[i].IntValue)
break
case "string":
values = append(values, fds.fields[i].StringValue)
break
case "float64":
values = append(values, fds.fields[i].FloatValue)
break
case "bool":
values = append(values, fds.fields[i].BoolValue)
break
default:
values = append(values, nil)
break
}
}
return values
}
// GetFieldAddrs get Pointers of Values in Object(struct)
func (fds *_FieldsMap) GetFieldAddrs() []interface{} {
var addrs []interface{}
for i, flen := 0, len(fds.fields); i < flen; i++ {
if fds.fields[i].Type == "string" {
// "string" need bytes => string for empty string
addrs = append(addrs, &fds.fields[i].Save)
} else {
addrs = append(addrs, &fds.fields[i].Addr)
}
}
return addrs
}
// MappingBackToObject mapping back to the original object
func (fds *_FieldsMap) MappingBackToObject() interface{} {
for i, flen := 0, len(fds.fields); i < flen; i++ {
switch fds.fields[i].Type {
case "string":
*fds.fields[i].Addr.(*string) = string(fds.fields[i].Save)
break
}
}
return fds.objptr
}
////////////////////////////////////////////////////////////////
// generate SQL string
// SQLFieldsStr generate sqlstr in db from Fields
// example:" `field0`, `field1`, `field2`, `field3` "
func (fds *_FieldsMap) SQLFieldsStr() string {
var tagsStr string
for i, flen := 0, len(fds.fields); i < flen; i++ {
if len(tagsStr) > 0 {
tagsStr += ", "
}
tagsStr += "`"
tagsStr += fds.fields[i].Tag
tagsStr += "`"
}
if len(tagsStr) > 0 {
tagsStr += " "
tagsStr = " " + tagsStr
}
return tagsStr
}
// SQLFieldsStrForSet generate sqlstr in db from Fields for set
// example:" `field0` = ?, `field1` = ?, `field2` = ?, `field3` = ? "
func (fds *_FieldsMap) SQLFieldsStrForSet() string {
var tagsStr string
for i, flen := 0, len(fds.fields); i < flen; i++ {
if len(tagsStr) > 0 {
tagsStr += ", "
}
tagsStr += "`"
tagsStr += fds.fields[i].Tag
tagsStr += "`"
tagsStr += " = ?"
}
if len(tagsStr) > 0 {
tagsStr += " "
tagsStr = " " + tagsStr
}
return tagsStr
}
////////////////////////////////////////////////////////////////
// generate statement
// PrepareStmt prepare statement
func (fds *_FieldsMap) PrepareStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
sqlstr string) (*sql.Stmt, error) {
if tx != nil {
return tx.PrepareContext(ctx, sqlstr)
}
if db != nil {
return db.PrepareContext(ctx, sqlstr)
}
return nil, errors.New("tx & db both nil")
}
// SQLSelectStmt generate statement for SELECT
func (fds *_FieldsMap) SQLSelectStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
extStr string) (*sql.Stmt, error) {
sqlstr := "SELECT " + fds.SQLFieldsStr() +
" FROM `" + fds.table + "` " + extStr
return fds.PrepareStmt(ctx, tx, db, sqlstr)
}
// SQLInsertStmt generate statement for INSERT
func (fds *_FieldsMap) SQLInsertStmt(ctx context.Context, tx *sql.Tx, db *sql.DB) (*sql.Stmt, error) {
var vs string
for i, flen := 0, len(fds.fields); i < flen; i++ {
if len(vs) > 0 {
vs += ", "
}
vs += "?"
}
sqlstr := "INSERT INTO `" + fds.table + "` (" + fds.SQLFieldsStr() + ") " +
"VALUES (" + vs + ")"
return fds.PrepareStmt(ctx, tx, db, sqlstr)
}
// SQLUpdateStmt generate statement for UPDATE
func (fds *_FieldsMap) SQLUpdateStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
extStr string) (*sql.Stmt, error) {
sqlstr := "UPDATE `" + fds.table + "` SET " + fds.SQLFieldsStrForSet() + extStr
return fds.PrepareStmt(ctx, tx, db, sqlstr)
}
// SQLDeleteStmt generate statement for DELETE
func (fds *_FieldsMap) SQLDeleteStmt(ctx context.Context, tx *sql.Tx, db *sql.DB,
extStr string) (*sql.Stmt, error) {
sqlstr := "DELETE FROM `" + fds.table + "` " + extStr
return fds.PrepareStmt(ctx, tx, db, sqlstr)
}