forked from coocood/qbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
338 lines (316 loc) · 7.34 KB
/
model.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
package qbs
import (
"bytes"
"reflect"
"strconv"
"strings"
"time"
)
// Index represents a table index and is returned via the Indexed interface.
type index struct {
name string
columns []string
unique bool
}
// Indexes represents an array of indexes.
type Indexes []*index
type Indexed interface {
Indexes(indexes *Indexes)
}
// Add adds an index
func (ix *Indexes) Add(columns ...string) {
name := strings.Join(columns, "_")
*ix = append(*ix, &index{name: name, columns: columns, unique: false})
}
// AddUnique adds an unique index
func (ix *Indexes) AddUnique(columns ...string) {
name := strings.Join(columns, "_")
*ix = append(*ix, &index{name: name, columns: columns, unique: true})
}
// ModelField represents a schema field of a parsed model.
type modelField struct {
name string // Column name
camelName string
value interface{} // Value
sqlTags map[string]string // The sql struct tags for this field
pk bool
}
// NotNull tests if the field is declared as NOT NULL
func (field *modelField) notNull() bool {
_, ok := field.sqlTags["notnull"]
return ok
}
// Default returns the default value for the field
func (field *modelField) dfault() string {
return field.sqlTags["default"]
}
// Size returns the field size, e.g. for varchars
func (field *modelField) size() int {
v, ok := field.sqlTags["size"]
if ok {
i, _ := strconv.Atoi(v)
return i
}
return 0
}
// Model represents a parsed schema interface{}.
type model struct {
pk *modelField
table string
fields []*modelField
refs map[string]*reference
indexes Indexes
}
type reference struct {
refKey string
model *model
foreignKey bool
}
func (model *model) columnsAndValues(forUpdate bool) ([]string, []interface{}) {
columns := make([]string, 0, len(model.fields))
values := make([]interface{}, 0, len(columns))
for _, column := range model.fields {
var include bool
if forUpdate {
include = column.value != nil && !column.pk
} else {
include = true
if column.value == nil {
include = false
} else if column.pk {
if intValue, ok := column.value.(int64); ok {
include = intValue != 0
} else if strValue, ok := column.value.(string); ok {
include = strValue != ""
}
}
}
if include {
columns = append(columns, column.name)
values = append(values, column.value)
}
}
return columns, values
}
func (model *model) timeFiled(name string) *modelField {
for _, v := range model.fields {
if _, ok := v.value.(time.Time); ok {
if _, ok := v.sqlTags[name]; ok {
return v
}
if v.name == name {
return v
}
}
}
return nil
}
func (model *model) pkZero() bool {
if model.pk == nil {
return true
}
switch model.pk.value.(type) {
case string:
return model.pk.value.(string) == ""
case int64:
return model.pk.value.(int64) == 0
}
return true
}
func structPtrToModel(f interface{}, root bool, omitFields []string) *model {
model := &model{
pk: nil,
table: tableName(f),
fields: []*modelField{},
indexes: Indexes{},
refs: make(map[string]*reference),
}
structType := reflect.TypeOf(f).Elem()
structValue := reflect.ValueOf(f).Elem()
for i := 0; i < structType.NumField(); i++ {
structFiled := structType.Field(i)
omit := false
for _, v := range omitFields {
if v == structFiled.Name {
omit = true
}
}
if omit {
continue
}
sqlTag := structFiled.Tag.Get("qbs")
if sqlTag == "-" {
continue
}
kind := structFiled.Type.Kind()
switch kind {
case reflect.Ptr, reflect.Map:
continue
case reflect.Slice:
elemKind := structFiled.Type.Elem().Kind()
if elemKind != reflect.Uint8 {
continue
}
}
parsedSqlTags := parseTags(sqlTag)
fd := new(modelField)
fd.camelName = structFiled.Name
fd.name = toSnake(structFiled.Name)
fd.sqlTags = parsedSqlTags
fd.value = structValue.FieldByName(structFiled.Name).Interface()
if _, ok := fd.sqlTags["pk"]; ok {
fd.pk = true
}
if _, ok := fd.value.(int64); ok && fd.name == "id" {
fd.pk = true
}
if fd.pk {
model.pk = fd
}
model.fields = append(model.fields, fd)
// fill in references map only in root model.
if root {
var fk, explicitJoin, implicitJoin bool
var refName string
refName, fk = parsedSqlTags["fk"]
if !fk {
refName, explicitJoin = parsedSqlTags["join"]
}
if len(fd.name) > 3 && strings.HasSuffix(fd.name, "_id") {
fdValue := reflect.ValueOf(fd.value)
if fdValue.Kind() == reflect.Int64 {
i := strings.LastIndex(fd.name, "_id")
refName = snakeToUpperCamel(fd.name[:i])
implicitJoin = true
}
}
if fk || explicitJoin || implicitJoin {
omit := false
for _, v := range omitFields {
if v == refName {
omit = true
}
}
if field, ok := structType.FieldByName(refName); ok && !omit {
fieldValue := structValue.FieldByName(refName)
if fieldValue.Kind() == reflect.Ptr {
model.indexes.Add(fd.name)
if fieldValue.IsNil() {
fieldValue.Set(reflect.New(field.Type.Elem()))
}
refModel := structPtrToModel(fieldValue.Interface(), false, nil)
ref := new(reference)
ref.foreignKey = fk
ref.model = refModel
ref.refKey = fd.name
model.refs[refName] = ref
} else if !implicitJoin {
panic("Referenced field is not pointer")
}
} else if !implicitJoin {
panic("Can not find referenced field")
}
}
if _, ok := parsedSqlTags["unique"]; ok {
model.indexes.AddUnique(fd.name)
} else if _, ok := parsedSqlTags["index"]; ok {
model.indexes.Add(fd.name)
}
}
}
if root {
if indexed, ok := f.(Indexed); ok {
indexed.Indexes(&model.indexes)
}
}
return model
}
func tableName(talbe interface{}) string {
if t, ok := talbe.(string); ok {
return t
}
t := reflect.TypeOf(talbe).Elem()
for {
c := false
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
t = t.Elem()
c = true
}
if !c {
break
}
}
return toSnake(t.Name())
}
func parseTags(s string) map[string]string {
if s == "" {
return nil
}
c := strings.Split(s, ",")
m := make(map[string]string)
for _, v := range c {
c2 := strings.Split(v, ":")
if len(c2) == 2 {
m[c2[0]] = c2[1]
} else {
m[v] = ""
}
}
validateTag(m)
return m
}
func toSnake(s string) string {
buf := bytes.NewBufferString("")
for i, v := range s {
if i > 0 && v >= 'A' && v <= 'Z' {
buf.WriteRune('_')
}
buf.WriteRune(v)
}
return strings.ToLower(buf.String())
}
func snakeToUpperCamel(s string) string {
buf := bytes.NewBufferString("")
for _, v := range strings.Split(s, "_") {
if len(v) > 0 {
buf.WriteString(strings.ToUpper(v[:1]))
buf.WriteString(v[1:])
}
}
return buf.String()
}
func validateTag(tagMap map[string]string) {
for k, v := range tagMap {
if _, ok := ValidTags[k]; !ok {
panic("invalid tag:" + k + v)
}
switch k {
case "fk", "join", "default":
if len(v) == 0 {
panic(k + " tag syntax error")
}
case "size":
if _, err := strconv.Atoi(v); err != nil {
panic(k + " tag syntax error")
}
default:
if len(v) != 0 {
panic(k + " tag syntax error")
}
}
}
}
var ValidTags = map[string]bool{
"pk": true, //primary key
"fk": true, //foreign key
"size": true,
"default": true,
"join": true,
"-": true, //ignore
"index": true,
"unique": true,
"notnull": true,
"updated": true,
"created": true,
}