forked from jaekwon/go-modeldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodeldb.go
379 lines (308 loc) · 9.8 KB
/
modeldb.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package modeldb
import (
"log"
"reflect"
"errors"
"strings"
"database/sql"
)
////// INIT
// A global db instance, for convenience
var _db *sql.DB
// Set one here once per app
func SetDB(db *sql.DB) {
_db = db
}
// To get the bare db instance should you need it
func GetDB() *sql.DB {
return _db
}
////// HELPER INTERFACES
// Common interface between *sql.Row and *sql.Rows
type RowScanner interface {
Scan(dest ...interface{}) error
}
// Common interface between *sql.Tx and *sql.DB
type Queryer interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
}
////// MODELFIELD & MODELINFO
// Represents meta info about the field of a "model"
type ModelField struct {
reflect.StructField
Column string
Null bool
Autoinc bool
}
// Represents meta info about a model
type ModelInfo struct {
Type reflect.Type
TableName string
Fields []*ModelField
FieldsSimple string
FieldsPrefixed string
FieldsInsert string
Placeholders string
}
// Global cache
var allModelInfos = map[string]*ModelInfo{}
func GetModelInfo(i interface{}) *ModelInfo {
t := reflect.TypeOf(i)
return GetModelInfoFromType(t)
}
// Call this once after each struct type declaration
func GetModelInfoFromType(modelType reflect.Type) *ModelInfo {
if modelType.Kind() == reflect.Ptr {
modelType = modelType.Elem()
}
if modelType.Kind() != reflect.Struct {
return nil
}
modelName := modelType.Name()
// Check cache
if allModelInfos[modelName] != nil {
return allModelInfos[modelName]
}
// Construct
m := &ModelInfo{}
allModelInfos[modelName] = m
m.Type = modelType
m.TableName = strings.ToLower(modelName)
// Fields
numFields := m.Type.NumField()
for i:=0; i<numFields; i++ {
field := m.Type.Field(i)
if field.Tag.Get("db") != "" {
column, null, autoinc := parseDBTag(field.Tag.Get("db"))
m.Fields = append(m.Fields, &ModelField{field, column, null, autoinc})
}
}
// Simple & Prefixed
fieldNames := []string{}
fieldInsertNames := []string{}
ph := []string{}
for _, field := range m.Fields {
fieldName, _, _ := parseDBTag(field.Tag.Get("db"))
fieldNames = append(fieldNames, fieldName)
if !field.Autoinc {
fieldInsertNames = append(fieldInsertNames, fieldName)
ph = append(ph, "?")
}
}
m.FieldsSimple = strings.Join(fieldNames, ", ")
m.FieldsPrefixed = m.TableName+"."+strings.Join(fieldNames, ", "+m.TableName+".")
m.FieldsInsert = strings.Join(fieldInsertNames, ", ")
m.Placeholders = strings.Join(ph, ", ")
return m
}
// Helper
func parseDBTag(tag string) (fieldName string, null bool, autoinc bool) {
s := strings.Split(tag, ",")
fieldName = s[0]
for _, ss := range s[1:] {
if ss == "null" { null = true }
if ss == "autoinc" { autoinc = true }
}
return
}
// Expand any model structs in args into its field components, for insertion
func ExpandArgs(args ...interface{}) []interface{} {
a := []interface{}{}
for _, arg := range args {
modelInfo := GetModelInfo(arg)
if modelInfo == nil {
a = append(a, arg)
} else {
a = append(a, modelInfo.FieldValues(arg)...)
}
}
return a
}
// Used by ExpandArgs to split a struct value into field values, for insertion
func (m *ModelInfo) FieldValues(i interface{}) []interface{} {
v := reflect.ValueOf(i)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Type() != m.Type {
log.Panicf("Invalid argument for FieldValues: Type mismatch. Expected %v but got %v",
v.Type(), m.Type)
}
fvs := []interface{}{}
for _, field := range m.Fields {
name := field.Name
fieldValue := v.FieldByName(name)
if field.Autoinc && fieldValue.Interface() == reflect.Zero(field.Type).Interface() {
continue
} else if field.Null && fieldValue.Interface() == reflect.Zero(field.Type).Interface() {
fvs = append(fvs, nil)
} else {
fvs = append(fvs, fieldValue.Interface())
}
}
return fvs
}
// Common between external & tx methods
func _Exec(qt Queryer, query string, args ...interface{}) (sql.Result, error) {
return qt.Exec(query, ExpandArgs(args...)...)
}
func _QueryRow(qt Queryer, query string, args ...interface{}) *ModelRow {
return &ModelRow{qt.QueryRow(query, ExpandArgs(args...)...)}
}
func _Query(qt Queryer, query string, args ...interface{}) (*ModelRows, error) {
rows, err := qt.Query(query, ExpandArgs(args...)...)
if err != nil { return nil, err }
return &ModelRows{rows}, nil
}
func _QueryAll(qt Queryer, proto interface{}, query string, args ...interface{}) (interface{}, error) {
protos := reflect.MakeSlice(reflect.SliceOf(reflect.PtrTo(reflect.TypeOf(proto))), 0, 0)
rows, err := qt.Query(query, ExpandArgs(args...)...)
if err != nil { return nil, err }
for rows.Next() {
protoValueP := reflect.New(reflect.TypeOf(proto))
err := ScanStruct(rows, protoValueP.Interface())
if err != nil { return nil, err }
protos = reflect.Append(protos, protoValueP)
}
return protos.Interface(), nil
}
// Convenience methods
func Exec(query string, args ...interface{}) (sql.Result, error) {
return _Exec(GetDB(), query, args...)
}
func QueryRow(query string, args ...interface{}) *ModelRow {
return _QueryRow(GetDB(), query, args...)
}
func Query(query string, args ...interface{}) (*ModelRows, error) {
return _Query(GetDB(), query, args...)
}
func QueryAll(proto interface{}, query string, args ...interface{}) (interface{}, error) {
return _QueryAll(GetDB(), proto, query, args...)
}
func Begin() (*ModelTx, error) {
tx, err := GetDB().Begin()
if err != nil { return nil, err }
return &ModelTx{tx, false}, nil
}
// NOTE: This only works with https://github.com/jaekwon/mysql/,
// which was modified to start transactions lazily.
func BeginSerializable() (*ModelTx, error) {
tx, err := GetDB().Begin()
if err != nil { return nil, err }
_, err = tx.Exec(`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE`)
if err != nil { return nil, err }
return &ModelTx{tx, false}, nil
}
// Scan row result fields into dest, which can include structs.
func ScanStruct(scanner RowScanner, dest ...interface{}) error {
destValuesP := []interface{}{}
for _, d := range dest {
dValueP := reflect.ValueOf(d)
dValue := dValueP.Elem()
if dValue.Kind() != reflect.Struct {
destValuesP = append(destValuesP, dValueP.Interface())
} else {
m := GetModelInfoFromType(dValue.Type())
for _, field := range m.Fields {
dField := dValue.FieldByName(field.Name)
if field.Null {
switch field.Type.Name() {
case "string":
ns := NullString(dField.Interface().(string))
destValuesP = append(destValuesP, &ns)
default:
panic(errors.New("Dunno how to convert nil to "+field.Type.Name()))
}
} else {
destValuesP = append(destValuesP, dField.Addr().Interface())
}
}
}
}
return scanner.Scan(destValuesP...)
}
// ModelRow
type ModelRow struct {
Row *sql.Row
}
func (s *ModelRow) Scan(dest ...interface{}) error {
return ScanStruct(s.Row, dest...)
}
// ModelRows
type ModelRows struct {
Rows *sql.Rows
}
func (s *ModelRows) Close() error {
return s.Rows.Close()
}
func (s *ModelRows) Columns() ([]string, error) {
return s.Rows.Columns()
}
func (s *ModelRows) Err() error {
return s.Rows.Err()
}
func (s *ModelRows) Next() bool {
return s.Rows.Next()
}
func (s *ModelRows) Scan(dest ...interface{}) error {
return ScanStruct(s.Rows, dest...)
}
// ModelDB
type ModelDB struct {
DB *sql.DB
}
func (sdb *ModelDB) Exec(query string, args ...interface{}) (sql.Result, error) {
return _Exec(sdb.DB, query, args...)
}
func (sdb *ModelDB) Query(query string, args ...interface{}) (*ModelRows, error) {
return _Query(sdb.DB, query, args...)
}
func (sdb *ModelDB) QueryRow(query string, args ...interface{}) *ModelRow {
return _QueryRow(sdb.DB, query, args...)
}
func (sdb *ModelDB) QueryAll(proto interface{}, query string, args ...interface{}) (interface{}, error) {
return _QueryAll(sdb.DB, proto, query, args...)
}
// ModelTx
type ModelTx struct {
Tx *sql.Tx
Finalized bool
}
func (stx *ModelTx) Exec(query string, args ...interface{}) (sql.Result, error) {
return _Exec(stx.Tx, query, args...)
}
func (stx *ModelTx) Query(query string, args ...interface{}) (*ModelRows, error) {
return _Query(stx.Tx, query, args...)
}
func (stx *ModelTx) QueryRow(query string, args ...interface{}) *ModelRow {
return _QueryRow(stx.Tx, query, args...)
}
func (stx *ModelTx) QueryAll(proto interface{}, query string, args ...interface{}) (interface{}, error) {
return _QueryAll(stx.Tx, proto, query, args...)
}
func (stx *ModelTx) Rollback() error {
stx.Finalized = true
return stx.Tx.Rollback()
}
func (stx *ModelTx) Commit() error {
stx.Finalized = true
return stx.Tx.Commit()
}
func (stx *ModelTx) Finalize() {
if !stx.Finalized {
rbErr := stx.Tx.Rollback()
if rbErr != nil { panic(rbErr) }
}
}
// NullString
type NullString string
func (ns *NullString) Scan(value interface{}) error {
if value == nil {
*ns = NullString("")
} else {
*ns = NullString(string(value.([]uint8)))
}
return nil
}