-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
370 lines (327 loc) · 9.6 KB
/
mysql.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
package main
import (
"fmt"
"github.com/xxiiaass/iutils"
"io/ioutil"
"path"
"regexp"
"strings"
)
// todo
/**
build.go 路径
**/
func (mt *MysqlTask) parseFunction() []Func {
str := mt.BuildFileStr
reg := regexp.MustCompile("func \\(build \\*Build\\)\\s+([A-Z]\\S+)\\((.+)?\\) \\*Build \\{")
x := reg.FindAllStringSubmatch(string(str), -1)
funcs := make([]Func, 0)
for _, s := range x {
if iutils.IsExists(s[1], mt.ignoreMethod) {
// 手动忽略的方法
continue
}
if s[2] == "" {
funcs = append(funcs, Func{Name: s[1], Proxy: s[1], Argus: "", ToBuild: ""})
} else {
argus := strings.Split(s[2], ",")
toBuilds := make([]string, len(argus))
for j, a := range argus {
arrs := strings.Split(strings.Trim(a, " "), " ")
if arrs[1][0:3] == "..." {
toBuilds[j] = arrs[0] + "..."
} else {
toBuilds[j] = arrs[0]
}
}
funcs = append(funcs, Func{Name: s[1], Proxy: s[1], Argus: s[2], ToBuild: strings.Join(toBuilds, ",")})
}
}
return funcs
}
func (mt *MysqlTask) parseField(fileTxt string) []DefineField {
// \s+ 用于去除任何空白字符,其后的 \w+? 用于获取字段名,如果存在使用 // 单行注释字段该字段将会被略过
reg := regexp.MustCompile("@\\s+(\\w+?)\\s+(\\S+?)\\s+`gorm:\"(\\S+?)\"[^`]+?json:\"(\\S+?)\"`.*?@")
x := reg.FindAllStringSubmatch(fileTxt, -1)
names := make([]DefineField, 0)
for _, field := range x {
// field[0] Raw Str
// field[1] Struct Field Name
// field[2] Struct Field Type
// field[3] gorm tag
// field[4] json tag
// 获取 column name
if len(field) > 3 {
// 存在 column
if pos := strings.Index(field[3], "column:"); pos >= 0 {
begin := pos + len("column:")
// 区分 gorm:"column:theater_id;PRIMARY_KEY"
if sp := strings.Index(field[3], ";"); sp >= 0 && sp > begin {
field[3] = field[3][begin:sp]
} else {
field[3] = field[3][begin:]
}
} else {
continue
}
} else {
continue
}
isNum := iutils.IsExists(field[2], []string{"int64", "int", "float64", "float32"})
names = append(names, DefineField{StructKey: field[1], Key: field[3], Type: field[2], Number: isNum})
}
return names
}
func NewMysqlTask(driverName, targetPath, PackageName, buildfilestr string) *MysqlTask {
return &MysqlTask{
Task: Task{
FromDirPath: targetPath,
BuildFileStr: buildfilestr,
PackageName: PackageName,
WriteDirPath: targetPath,
DriverName: driverName,
ignoreMethod: []string{"clone", "Clone", "TableName"},
ModelFilterFunc: defaultFilter,
},
}
}
type MysqlTask struct {
Task
}
func (mt *MysqlTask) Run() {
funcs := mt.parseFunction()
debug(fmt.Sprintf("解析驱动文件 parse funcs : %d", len(funcs)))
if mt.IsPrivate {
// 如果是渲染私有的方式,替换方法名
for i, item := range funcs {
if strings.Contains(strings.ToLower(item.Name), "where") {
funcs[i].Name = MethodNameToPrivate(item.Name)
continue
}
}
}
allTypes, fileds := mt.getAllTypeName()
debug(fmt.Sprintf("解析模型文件 getAllTypeName, allTypes:%d, fileds:%d", len(allTypes), len(fileds)))
workTypes := make([]string, 0)
workFileds := make([][]DefineField, 0)
fileNums := 0
for i, _ := range allTypes {
workTypes = append(workTypes, allTypes[i])
workFileds = append(workFileds, fileds[i])
if (i > 0 && i%5 == 0) || i == len(allTypes)-1 {
fileNums++
query := mt.topLine() +
"import \"reflect\"\n" +
"import \"github.com/xxiiaass/iutils\"\n" +
"import \"github.com/xxiiaass/" + mt.DriverName + "\"\n"
for i, tname := range workTypes {
query += mt.renderQuery(funcs, tname, workFileds[i])
}
collect := mt.topLine() +
`
/*
此文件为自动生成,所有修改都不会生效
*/
`
for i, tname := range workTypes {
collect += mt.renderCollect(funcs, tname, workFileds[i])
}
Write(query, path.Join(mt.WriteDirPath, fmt.Sprintf("lib_auto_generate_query_%d.go", fileNums)))
Write(collect, path.Join(mt.WriteDirPath, fmt.Sprintf("lib_auto_generate_collect_%d.go", fileNums)))
workTypes = make([]string, 0)
workFileds = make([][]DefineField, 0)
}
}
// 最后写入一个空文件,解决 n个分卷, 切换到 n-1个分卷时,第n分卷的内容重复的问题
ioutil.WriteFile(path.Join(mt.WriteDirPath, fmt.Sprintf("lib_auto_generate_collect_%d.go", fileNums+1)), []byte(mt.topLine()), 0644)
ioutil.WriteFile(path.Join(mt.WriteDirPath, fmt.Sprintf("lib_auto_generate_query_%d.go", fileNums+1)), []byte(mt.topLine()), 0644)
}
func (mt *MysqlTask) renderQuery(funcs []Func, typeName string, filed []DefineField) string {
nums := make([]DefineField, 0)
for _, i := range filed {
if i.Number {
nums = append(nums, i)
}
}
t := Render{funcs, typeName, typeName + "Query", mt.DriverName, Fields{
All: filed,
Pluck: filed,
PluckUni: filed,
Map: filed,
Number: nums,
}}
code := t.Render(mt.ExecTemplate())
if mt.IsPrivate {
code = ToPrivate(code)
}
code += t.Render(mt.ProxyTemplate())
if mt.IsPrivate {
code += t.Render(mt.PrivateBuildQueryTemplate())
} else {
code += t.Render(mt.PublicBuildQueryTemplate())
}
return code
}
func (mt *MysqlTask) renderCollect(funcs []Func, typeName string, filed []DefineField) string {
t := Render{funcs, typeName, typeName + "Query", mt.DriverName, Fields{
All: filed,
Pluck: filed,
PluckUni: filed,
Map: filed,
}}
return t.Render(collectTemplate())
}
func (mt *MysqlTask) getAllTypeName() ([]string, [][]DefineField) {
files, _ := ioutil.ReadDir(mt.FromDirPath)
debug(fmt.Sprintf("从%s读取模型, 共%d个", mt.FromDirPath, len(files)))
tables := make([]string, 0)
fileds := make([][]DefineField, 0)
for _, f := range files {
if f.IsDir() {
continue
}
if !mt.ModelFilterFunc(f.Name()) {
debug(fmt.Sprintf("%s因名字没命中筛选规则,跳过", f.Name()))
continue
}
str, err := ioutil.ReadFile(path.Join(mt.FromDirPath, f.Name()))
if err != nil {
panic(err)
}
name := f.Name()[:len(f.Name())-3]
debug(mt.DriverName)
if !regexp.MustCompile("\\n\\s+" + mt.DriverName + ".Query\\s*\\n").MatchString(string(str)) {
// 校验 xsorm.Query
debug(fmt.Sprintf("%s因没有驱动申明,跳过", f.Name()))
continue
}
if !regexp.MustCompile(".*type " + name + "Query struct").MatchString(string(str)) {
// 校验 type AccountQuery struct {
debug(fmt.Sprintf("%s因驱动申明不合规,跳过", f.Name()))
continue
}
reg := regexp.MustCompile("\\n")
modelStr := reg.ReplaceAllString(string(str), "@@")
// } 后增加判断换行以防止误判 interface{} 类型为 struct 结尾
targetStructStr := regexp.MustCompile("type " + name + " struct {.+?}@@").FindStringSubmatch(modelStr)
if len(targetStructStr) == 0 {
debug(fmt.Sprintf("%s因结构定义不合规,跳过", f.Name()))
continue
}
filed := mt.parseField(targetStructStr[0])
fileds = append(fileds, filed)
tables = append(tables, name)
}
return tables, fileds
}
// 执行后获得结果的方法模板
func (mt *MysqlTask) ExecTemplate() string {
return `
func New{{.QueryName}}() *{{.QueryName}} {
s := {{.QueryName}}{}
s.SetBuild({{.Driver}}.NewBuild(&s))
i, ok := reflect.ValueOf(&s).Interface().(BeforeHook)
if ok {
i.Before()
}
return &s
}
{{ $name := .TypeName }}
{{ $queryName := .QueryName }}
type _{{$queryName}}ColsStruct struct{
{{range .Fields.All}}{{.StructKey}} string
{{end}}
}
func Get{{$queryName}}Cols() *_{{$queryName}}ColsStruct {
return &_{{$queryName}}ColsStruct{
{{range .Fields.All}}{{.StructKey}} : "{{.Key}}",
{{end}}
}
}
func (m *{{.QueryName}}) First() *{{.TypeName}} {
s := make([]{{.TypeName}}, 0)
i, ok := reflect.ValueOf(m).Interface().(MustHook)
if ok {
i.Must()
}
m.GetBuild().ModelType(&s).Limit(1).Get()
if len(s) > 0{
return &s[0]
}
return &{{.TypeName}}{}
}
func (m *{{.QueryName}}) GetOne() *{{.TypeName}} {
s := make([]{{.TypeName}}, 0)
i, ok := reflect.ValueOf(m).Interface().(MustHook)
if ok {
i.Must()
}
m.GetBuild().ModelType(&s).Limit(1).Get()
if len(s) > 0{
return &s[0]
}
return nil
}
func (m *{{.QueryName}}) Get() {{.TypeName}}Collect {
i, ok := reflect.ValueOf(m).Interface().(MustHook)
if ok {
i.Must()
}
s := make([]{{.TypeName}}, 0)
m.GetBuild().ModelType(&s).Get()
return s
}
func (m *{{.QueryName}}) Clone() *{{.QueryName}} {
nm := New{{.QueryName}}()
nm.SetBuild(m.GetBuild().Clone())
return nm
}
func (m *{{.QueryName}}) Count() int64 {
i, ok := reflect.ValueOf(m).Interface().(MustHook)
if ok {
i.Must()
}
s := {{.TypeName}}{}
return m.GetBuild().ModelType(&s).Count()
}
func (m *{{.QueryName}}) Sum(col string) float64 {
s := {{.TypeName}}{}
return m.GetBuild().ModelType(&s).Sum(col)
}
func (m *{{.QueryName}}) Max(col string) float64 {
s := {{.TypeName}}{}
return m.GetBuild().ModelType(&s).Max(col)
}
func (m *{{.QueryName}}) DoneOperate() int64 {
s := {{.TypeName}}{}
return m.GetBuild().ModelType(&s).DoneOperate()
}
func (m *{{.QueryName}}) Update(h iutils.H) int64 {
s := {{.TypeName}}{}
return m.GetBuild().ModelType(&s).Update(h)
}
func (m *{{.QueryName}}) Delete() int64 {
s := {{.TypeName}}{}
return m.GetBuild().ModelType(&s).Delete()
}
func (m *{{.QueryName}}) Save(x *{{.TypeName}}) {
m.GetBuild().ModelType(x).Save()
}
func (m *{{.QueryName}}) Error() error {
return m.GetBuild().ModelType(m).Error()
}
//支持分表
func (m *{{.QueryName}}) Insert(argu interface{}) {
s := {{.TypeName}}{}
m.GetBuild().ModelType(&s).Insert(argu)
}
func (m *{{.QueryName}}) Increment(column string, amount int) int64 {
i, ok := reflect.ValueOf(m).Interface().(MustHook)
if ok {
i.Must()
}
s := {{.TypeName}}{}
return m.GetBuild().ModelType(&s).Increment(column, amount)
}
`
}