-
Notifications
You must be signed in to change notification settings - Fork 2
/
table.go
176 lines (156 loc) · 4.25 KB
/
table.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
package main
import (
"fmt"
"strconv"
"strings"
)
const (
_tableSQL = "SELECT `table_name` AS `name`, `table_comment` AS `comment` FROM information_schema.tables WHERE table_schema = ?"
_fieldSQL = "SELECT COLUMN_NAME name, COLUMN_KEY col_key, COLUMN_COMMENT comment, DATA_TYPE data_type FROM information_schema.columns WHERE table_schema = ? AND table_name = ? ORDER BY ordinal_position ASC"
)
var (
importMap = map[string]string{
"time.Time": "time",
"soft_delete.DeletedAt": "gorm.io/plugin/soft_delete",
}
)
type Table struct {
Name string
Comment string
MOD int `gorm:"-"`
PriField *Field `gorm:"-"`
IsSplit bool `gorm:"-"`
FirstLetter string `gorm:"-"`
Imports string `gorm:"-"`
PackageName string `gorm:"-"`
BigCamelName string `gorm:"-"`
Fields []*Field `gorm:"-"`
}
type Field struct {
Name string
ColKey string
Comment string
DataType string
BigCamelName string `gorm:"-"`
SmallCamelName string `gorm:"-"`
Tag string `gorm:"-"`
}
func GetTables() []*Table {
connection := connectMySQL()
var tables []*Table
if len(Cfg.Tables) > 0 {
connection.Raw(fmt.Sprintf("%s%s", _tableSQL, " AND table_name IN (?)"), Cfg.DBName, Cfg.Tables).Scan(&tables)
} else {
connection.Raw(_tableSQL, Cfg.DBName).Scan(&tables)
}
for i, table := range tables {
connection.Raw(_fieldSQL, Cfg.DBName, table.Name).Scan(&tables[i].Fields)
}
return initTables(tables)
}
func initTables(tables []*Table) []*Table {
res := make([]*Table, 0)
uni := make(map[string]*Table)
for i := range tables {
tableName, isSplit, splitNum := parseTableName(tables[i].Name)
if table, ok := uni[tableName]; ok {
if !isSplit {
panic(fmt.Sprintf("table name [%s] duplicate", tableName))
}
table.MOD = MaxFunc(table.MOD, splitNum)
continue
}
imports := make(map[string]struct{})
if isSplit {
imports["fmt"] = struct{}{}
}
var (
longestGormTapLen int
priField *Field
)
for _, field := range tables[i].Fields {
tagGORMLen := len(fmt.Sprintf("column:%s", field.Name))
if field.ColKey == "PRI" {
priField = field
tagGORMLen += len(";primaryKey")
}
longestGormTapLen = MaxFunc(longestGormTapLen, tagGORMLen)
}
for _, field := range tables[i].Fields {
field.Comment = strings.ReplaceAll(field.Comment, "\n", " ")
field.BigCamelName = ToBigCamelCase(field.Name)
field.SmallCamelName = ToSmallCamelCase(field.Name)
field.DataType = TransformType(field.DataType, field.Name)
if imp, ok := importMap[field.DataType]; ok {
imports[imp] = struct{}{}
}
gormTag := fmt.Sprintf("column:%s", field.Name)
if field.ColKey == "PRI" {
gormTag += ";primaryKey"
}
tag := fmt.Sprintf(`gorm:"%s"`, gormTag)
for k := 0; k < longestGormTapLen-len(gormTag)+1; k++ {
tag += " "
}
tag += fmt.Sprintf(`json:"%s"`, field.Name)
field.Tag = fmt.Sprintf("`%s`", tag)
}
table := &Table{
Name: tableName,
Comment: commentString(tables[i].Comment),
MOD: splitNum,
PriField: priField,
IsSplit: isSplit,
FirstLetter: tableName[0:1],
Imports: importString(imports),
PackageName: strings.ReplaceAll(strings.ToLower(tableName), "_", ""),
BigCamelName: ToBigCamelCase(tableName),
Fields: tables[i].Fields,
}
uni[tableName] = table
res = append(res, table)
}
return res
}
func parseTableName(name string) (parsedName string, isSplit bool, splitNum int) {
for i := len(name) - 1; i >= 0; i-- {
if name[i] != '_' {
continue
}
// 最后一个 _ 当作分表符
num, err := strconv.Atoi(name[i+1:])
if err != nil {
parsedName = name
isSplit = false
splitNum = 0
return
}
parsedName = name[:i]
isSplit = true
splitNum = num
return
}
return name, false, 0
}
func importString(imports map[string]struct{}) string {
if len(imports) == 0 {
return ""
}
builder := strings.Builder{}
builder.WriteString("import (\n")
for s := range imports {
builder.WriteString(`"`)
builder.WriteString(s)
builder.WriteString(`"`)
builder.WriteString("\n")
}
builder.WriteString(")")
return builder.String()
}
func commentString(comment string) string {
comment = strings.Trim(comment, " ")
if comment == "" {
return "."
}
return comment
}