-
Notifications
You must be signed in to change notification settings - Fork 40
/
codegen.go
401 lines (363 loc) · 10.2 KB
/
codegen.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"bufio"
"fmt"
"log"
"os"
"path"
"strings"
"text/template"
"unicode"
"github.com/mijia/modelq/drivers"
)
type CodeResult struct {
name string
err error
}
type CodeConfig struct {
packageName string
touchTimestamp bool
template string
}
func (cc CodeConfig) MustCompileTemplate() *template.Template {
if cc.template == "" {
return nil
}
funcMap := template.FuncMap{
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower,
"FirstLower": func(s string) string {
b := []rune(s)
b[0] = unicode.ToLower(b[0])
return string(b)
},
"First": func(s string) string {
if "" == s {
return ""
}
return s[:1]
},
"SubStr": func(from int, to int, s string) string {
l := len(s)
if from > l {
return ""
}
if to > l {
return s[from:]
}
if 0 == to {
return s[from:]
}
return s[from:to]
},
}
tmpl := template.New("tmp").Funcs(funcMap)
tmpl, err := tmpl.ParseFiles(cc.template)
return template.Must(tmpl, err)
}
func generateModels(dbName string, dbSchema drivers.DbSchema, config CodeConfig) {
customTmpl := config.MustCompileTemplate()
if fs, err := os.Stat(config.packageName); err != nil || !fs.IsDir() {
os.Mkdir(config.packageName, os.ModeDir|os.ModePerm)
}
jobs := make(chan CodeResult)
for tbl, cols := range dbSchema {
go func(tableName string, schema drivers.TableSchema) {
err := generateModel(dbName, tableName, schema, config, customTmpl)
jobs <- CodeResult{tableName, err}
}(tbl, cols)
}
for i := 0; i < len(dbSchema); i++ {
result := <-jobs
if result.err != nil {
log.Printf("Error when generating code for %s, %s", result.name, result.err)
} else {
log.Printf("Code generated for table %s, into package %s/%s.go", result.name, config.packageName, result.name)
}
}
close(jobs)
}
func generateModel(dbName, tName string, schema drivers.TableSchema, config CodeConfig, tmpl *template.Template) error {
file, err := os.Create(path.Join(config.packageName, tName+".go"))
if err != nil {
return err
}
w := bufio.NewWriter(file)
defer func() {
w.Flush()
file.Close()
}()
model := ModelMeta{
Name: toCapitalCase(tName),
DbName: dbName,
TableName: tName,
Fields: make([]ModelField, len(schema)),
Uniques: make([]ModelField, 0, len(schema)),
Indexed: make([]ModelField, 0, len(schema)),
config: config,
}
needTime := false
needFmt := false
for i, col := range schema {
field := ModelField{
Name: toCapitalCase(col.ColumnName),
ColumnName: col.ColumnName,
Type: col.DataType,
IsNullable: strings.ToUpper(col.IsNullable) == "YES",
JsonMeta: fmt.Sprintf("`json:\"%s\"`", col.ColumnName),
IsPrimaryKey: strings.ToUpper(col.ColumnKey) == "PRI",
IsUniqueKey: strings.ToUpper(col.ColumnKey) == "UNI",
IsIndexed: strings.ToUpper(col.ColumnKey) == "MUL",
IsAutoIncrement: strings.ToUpper(col.Extra) == "AUTO_INCREMENT",
DefaultValue: col.DefaultValue,
Extra: col.Extra,
Comment: col.Comment,
}
if field.Type == "time.Time" {
needTime = true
}
if field.IsPrimaryKey {
model.PrimaryFields = append(model.PrimaryFields, &field)
needFmt = true
}
if field.IsUniqueKey {
model.Uniques = append(model.Uniques, field)
}
if field.IsIndexed {
model.Indexed = append(model.Indexed, field)
}
model.Fields[i] = field
}
if err := model.GenHeader(w, tmpl, needTime, needFmt); err != nil {
return fmt.Errorf("[%s] Fail to gen model header, %s", tName, err)
}
if err := model.GenStruct(w, tmpl); err != nil {
return fmt.Errorf("[%s] Fail to gen model struct, %s", tName, err)
}
if err := model.GenObjectApi(w, tmpl); err != nil {
return fmt.Errorf("[%s] Fail to gen model object api, %s", tName, err)
}
if err := model.GenQueryApi(w, tmpl); err != nil {
return fmt.Errorf("[%s] Fail to gen model query api, %s", tName, err)
}
if err := model.GenManagedObjApi(w, tmpl); err != nil {
return fmt.Errorf("[%s] Fail to gen model managed objects api, %s", tName, err)
}
return nil
}
type ModelField struct {
Name string
ColumnName string
Type string
JsonMeta string
IsNullable bool
IsPrimaryKey bool
IsUniqueKey bool
IsIndexed bool
IsAutoIncrement bool
DefaultValue string
Extra string
Comment string
}
func (f ModelField) ConverterFuncName() string {
convertors := map[string]string{
"int64": "AsInt64",
"int": "AsInt",
"string": "AsString",
"time.Time": "AsTime",
"float64": "AsFloat64",
"bool": "AsBool",
"[]byte": "AsByteArray",
}
if c, ok := convertors[f.Type]; ok {
return c
}
return "AsString"
}
type PrimaryFields []*ModelField
func (pf PrimaryFields) FormatObject() func(string) string {
return func(name string) string {
// "<Article ArticleId=%v UserId=%v>", obj.ArticleId, obj.UserId
formats := make([]string, len(pf))
for i, field := range pf {
formats[i] = fmt.Sprintf("%s=%%v", field.Name)
}
outputs := make([]string, 1+len(pf))
outputs[0] = fmt.Sprintf("\"<%s %s>\"", name, strings.Join(formats, " "))
for i, field := range pf {
outputs[i+1] = fmt.Sprintf("obj.%s", field.Name)
}
return strings.Join(outputs, ", ")
}
}
func (pf PrimaryFields) FormatIncrementId() func() string {
// obj.Id = {{if eq .PrimaryField.Type "int64"}}id{{else}}{{.PrimaryField.Type}}(id){{end}}
return func() string {
for _, field := range pf {
if field.IsAutoIncrement {
if field.Type == "int64" {
return fmt.Sprintf("obj.%s = id", field.Name)
} else {
return fmt.Sprintf("obj.%s = %s(id)", field.Name, field.Type)
}
}
}
return ""
}
}
func (pf PrimaryFields) FormatFilters() func(string) string {
// filter := {{.Name}}Objs.Filter{{.PrimaryField.Name}}("=", obj.{{.PrimaryField.Name}})
return func(name string) string {
filters := make([]string, len(pf))
for i, field := range pf {
if i == 0 {
filters[i] = fmt.Sprintf("filter := %sObjs.Filter%s(\"=\", obj.%s)", name, field.Name, field.Name)
} else {
filters[i] = fmt.Sprintf("filter = filter.And(%sObjs.Filter%s(\"=\", obj.%s))", name, field.Name, field.Name)
}
}
return strings.Join(filters, "\n")
}
}
type ModelMeta struct {
Name string
DbName string
TableName string
PrimaryFields PrimaryFields
Fields []ModelField
Uniques []ModelField
Indexed []ModelField
config CodeConfig
}
func (m ModelMeta) HasAutoIncrementPrimaryKey() bool {
for _, pField := range m.PrimaryFields {
if pField.IsAutoIncrement {
return true
}
}
return false
}
func (m ModelMeta) AllFields() string {
fields := make([]string, len(m.Fields))
for i, f := range m.Fields {
fields[i] = fmt.Sprintf("\"%s\"", f.Name)
}
return strings.Join(fields, ", ")
}
func (m ModelMeta) InsertableFields() string {
fields := make([]string, 0, len(m.Fields))
for _, f := range m.Fields {
if f.IsPrimaryKey && f.IsAutoIncrement {
continue
}
autoTimestamp := strings.ToUpper(f.DefaultValue) == "CURRENT_TIMESTAMP" ||
strings.ToUpper(f.DefaultValue) == "NOW()"
if f.Type == "time.Time" && autoTimestamp && !m.config.touchTimestamp {
continue
}
fields = append(fields, fmt.Sprintf("\"%s\"", f.Name))
}
return strings.Join(fields, ", ")
}
func (m ModelMeta) GetInsertableFields() []ModelField {
fields := make([]ModelField, 0, len(m.Fields))
for _, f := range m.Fields {
if f.IsPrimaryKey && f.IsAutoIncrement {
continue
}
autoTimestamp := strings.ToUpper(f.DefaultValue) == "CURRENT_TIMESTAMP" ||
strings.ToUpper(f.DefaultValue) == "NOW()"
if f.Type == "time.Time" && autoTimestamp && !m.config.touchTimestamp {
continue
}
fields = append(fields, f)
}
return fields
}
func (m ModelMeta) UpdatableFields() string {
fields := make([]string, 0, len(m.Fields))
for _, f := range m.Fields {
if f.IsPrimaryKey {
continue
}
autoUpdateTime := strings.ToUpper(f.Extra) == "ON UPDATE CURRENT_TIMESTAMP"
if autoUpdateTime && !m.config.touchTimestamp {
continue
}
fields = append(fields, fmt.Sprintf("\"%s\"", f.Name))
}
return strings.Join(fields, ", ")
}
func (m ModelMeta) GetUpdatableFields() []ModelField {
fields := make([]ModelField, 0, len(m.Fields))
for _, f := range m.Fields {
if f.IsPrimaryKey {
continue
}
autoUpdateTime := strings.ToUpper(f.Extra) == "ON UPDATE CURRENT_TIMESTAMP"
if autoUpdateTime && !m.config.touchTimestamp {
continue
}
fields = append(fields, f)
}
return fields
}
func (m ModelMeta) getTemplate(tmpl *template.Template, name string, defaultTmpl *template.Template) *template.Template {
if tmpl != nil {
if definedTmpl := tmpl.Lookup(name); definedTmpl != nil {
return definedTmpl
}
}
return defaultTmpl
}
func (m ModelMeta) GenHeader(w *bufio.Writer, tmpl *template.Template, importTime, importFmt bool) error {
return m.getTemplate(tmpl, "header", tmHeader).Execute(w, map[string]interface{}{
"DbName": m.DbName,
"TableName": m.TableName,
"PkgName": m.config.packageName,
"ImportTime": importTime,
"ImportFmt": importFmt,
})
}
func (m ModelMeta) GenStruct(w *bufio.Writer, tmpl *template.Template) error {
return m.getTemplate(tmpl, "struct", tmStruct).Execute(w, m)
}
func (m ModelMeta) GenObjectApi(w *bufio.Writer, tmpl *template.Template) error {
return m.getTemplate(tmpl, "obj_api", tmObjApi).Execute(w, m)
}
func (m ModelMeta) GenQueryApi(w *bufio.Writer, tmpl *template.Template) error {
return m.getTemplate(tmpl, "query_api", tmQueryApi).Execute(w, m)
}
func (m ModelMeta) GenManagedObjApi(w *bufio.Writer, tmpl *template.Template) error {
return m.getTemplate(tmpl, "managed_api", tmManagedObjApi).Execute(w, m)
}
func toCapitalCase(name string) string {
// cp___hello_12jiu -> CpHello12Jiu
data := []byte(name)
segStart := true
endPos := 0
for i := 0; i < len(data); i++ {
ch := data[i]
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
if segStart {
if ch >= 'a' && ch <= 'z' {
ch = ch - 'a' + 'A'
}
segStart = false
} else {
if ch >= 'A' && ch <= 'Z' {
ch = ch - 'A' + 'a'
}
}
data[endPos] = ch
endPos++
} else if ch >= '0' && ch <= '9' {
data[endPos] = ch
endPos++
segStart = true
} else {
segStart = true
}
}
return string(data[:endPos])
}