-
Notifications
You must be signed in to change notification settings - Fork 112
/
table2struct.go
373 lines (332 loc) · 9.53 KB
/
table2struct.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
package converter
import (
"database/sql"
"errors"
"fmt"
"log"
"os"
"os/exec"
"strings"
_ "github.com/go-sql-driver/mysql"
)
//map for converting mysql type to golang types
var typeForMysqlToGo = map[string]string{
"int": "int64",
"integer": "int64",
"tinyint": "int64",
"smallint": "int64",
"mediumint": "int64",
"bigint": "int64",
"int unsigned": "int64",
"integer unsigned": "int64",
"tinyint unsigned": "int64",
"smallint unsigned": "int64",
"mediumint unsigned": "int64",
"bigint unsigned": "int64",
"bit": "int64",
"bool": "bool",
"enum": "string",
"set": "string",
"varchar": "string",
"char": "string",
"tinytext": "string",
"mediumtext": "string",
"text": "string",
"longtext": "string",
"blob": "string",
"tinyblob": "string",
"mediumblob": "string",
"longblob": "string",
"date": "time.Time", // time.Time or string
"datetime": "time.Time", // time.Time or string
"timestamp": "time.Time", // time.Time or string
"time": "time.Time", // time.Time or string
"float": "float64",
"double": "float64",
"decimal": "float64",
"binary": "string",
"varbinary": "string",
"json": "json.RawMessage",
}
type Table2Struct struct {
dsn string
savePath string
db *sql.DB
table string
prefix string
config *T2tConfig
err error
realNameMethod string
enableJsonTag bool // 是否添加json的tag, 默认不添加
packageName string // 生成struct的包名(默认为空的话, 则取名为: package model)
tagKey string // tag字段的key值,默认是orm
dateToTime bool // 是否将 date相关字段转换为 time.Time,默认否
}
type T2tConfig struct {
StructNameToHump bool // 结构体名称是否转为驼峰式,默认为false
RmTagIfUcFirsted bool // 如果字段首字母本来就是大写, 就不添加tag, 默认false添加, true不添加
TagToLower bool // tag的字段名字是否转换为小写, 如果本身有大写字母的话, 默认false不转
JsonTagToHump bool // json tag是否转为驼峰,默认为false,不转换
UcFirstOnly bool // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换
SeperatFile bool // 每个struct放入单独的文件,默认false,放入同一个文件
}
func NewTable2Struct() *Table2Struct {
return &Table2Struct{}
}
func (t *Table2Struct) Dsn(d string) *Table2Struct {
t.dsn = d
return t
}
func (t *Table2Struct) TagKey(r string) *Table2Struct {
t.tagKey = r
return t
}
func (t *Table2Struct) PackageName(r string) *Table2Struct {
t.packageName = r
return t
}
func (t *Table2Struct) RealNameMethod(r string) *Table2Struct {
t.realNameMethod = r
return t
}
func (t *Table2Struct) SavePath(p string) *Table2Struct {
t.savePath = p
return t
}
func (t *Table2Struct) DB(d *sql.DB) *Table2Struct {
t.db = d
return t
}
func (t *Table2Struct) Table(tab string) *Table2Struct {
t.table = tab
return t
}
func (t *Table2Struct) Prefix(p string) *Table2Struct {
t.prefix = p
return t
}
func (t *Table2Struct) EnableJsonTag(p bool) *Table2Struct {
t.enableJsonTag = p
return t
}
func (t *Table2Struct) DateToTime(d bool) *Table2Struct {
t.dateToTime = d
return t
}
func (t *Table2Struct) Config(c *T2tConfig) *Table2Struct {
t.config = c
return t
}
func (t *Table2Struct) Run() error {
if t.config == nil {
t.config = new(T2tConfig)
}
// 链接mysql, 获取db对象
t.dialMysql()
if t.err != nil {
return t.err
}
// 获取表和字段的shcema
tableColumns, err := t.getColumns()
if err != nil {
return err
}
// 包名
var packageName string
if t.packageName == "" {
packageName = "package model\n\n"
} else {
packageName = fmt.Sprintf("package %s\n\n", t.packageName)
}
// 组装struct
var structContent string
for tableRealName, item := range tableColumns {
// 去除前缀
if t.prefix != "" {
tableRealName = tableRealName[len(t.prefix):]
}
tableName := tableRealName
structName := tableName
if t.config.StructNameToHump {
structName = t.camelCase(structName)
}
/*
switch len(tableName) {
case 0:
case 1:
tableName = strings.ToUpper(tableName[0:1])
default:
// 字符长度大于1时
tableName = strings.ToUpper(tableName[0:1]) + tableName[1:]
}
*/
tableName = t.camelCase(tableName)
depth := 1
structContent += "type " + structName + " struct {\n"
for _, v := range item {
//structContent += tab(depth) + v.ColumnName + " " + v.Type + " " + v.Json + "\n"
// 字段注释
var clumnComment string
if v.ColumnComment != "" {
clumnComment = fmt.Sprintf(" // %s", v.ColumnComment)
}
structContent += fmt.Sprintf("%s%s %s %s%s\n",
tab(depth), v.ColumnName, v.Type, v.Tag, clumnComment)
}
structContent += tab(depth-1) + "}\n\n"
// 添加 method 获取真实表名
if t.realNameMethod != "" {
structContent += fmt.Sprintf("func (%s) %s() string {\n",
structName, t.realNameMethod)
structContent += fmt.Sprintf("%sreturn \"%s\"\n",
tab(depth), tableRealName)
structContent += "}\n\n"
}
}
// 如果有引入 time.Time, 则需要引入 time 包
var importContent string
if strings.Contains(structContent, "time.Time") {
importContent = "import \"time\"\n\n"
}
// 添加json类型支持
if strings.Contains(structContent, "json.RawMessage") {
importContent += "import \"encoding/json\"\n\n"
}
// 写入文件struct
var savePath = t.savePath
// 是否指定保存路径
if savePath == "" {
savePath = "model.go"
}
filePath := fmt.Sprintf("%s", savePath)
f, err := os.Create(filePath)
if err != nil {
log.Println("Can not write file")
return err
}
defer f.Close()
f.WriteString(packageName + importContent + structContent)
cmd := exec.Command("gofmt", "-w", filePath)
cmd.Run()
log.Println("gen model finish!!!")
return nil
}
func (t *Table2Struct) dialMysql() {
if t.db == nil {
if t.dsn == "" {
t.err = errors.New("dsn数据库配置缺失")
return
}
t.db, t.err = sql.Open("mysql", t.dsn)
}
return
}
type column struct {
ColumnName string
Type string
Nullable string
TableName string
ColumnComment string
Tag string
}
// Function for fetching schema definition of passed table
func (t *Table2Struct) getColumns(table ...string) (tableColumns map[string][]column, err error) {
// 根据设置,判断是否要把 date 相关字段替换为 string
if t.dateToTime == false {
typeForMysqlToGo["date"] = "string"
typeForMysqlToGo["datetime"] = "string"
typeForMysqlToGo["timestamp"] = "string"
typeForMysqlToGo["time"] = "string"
}
tableColumns = make(map[string][]column)
// sql
var sqlStr = `SELECT COLUMN_NAME,DATA_TYPE,IS_NULLABLE,TABLE_NAME,COLUMN_COMMENT
FROM information_schema.COLUMNS
WHERE table_schema = DATABASE()`
// 是否指定了具体的table
if t.table != "" {
sqlStr += fmt.Sprintf(" AND TABLE_NAME = '%s'", t.prefix+t.table)
}
// sql排序
sqlStr += " order by TABLE_NAME asc, ORDINAL_POSITION asc"
rows, err := t.db.Query(sqlStr)
if err != nil {
log.Println("Error reading table information: ", err.Error())
return
}
defer rows.Close()
for rows.Next() {
col := column{}
err = rows.Scan(&col.ColumnName, &col.Type, &col.Nullable, &col.TableName, &col.ColumnComment)
if err != nil {
log.Println(err.Error())
return
}
//col.Json = strings.ToLower(col.ColumnName)
col.Tag = col.ColumnName
col.ColumnComment = col.ColumnComment
col.ColumnName = t.camelCase(col.ColumnName)
col.Type = typeForMysqlToGo[col.Type]
jsonTag := col.Tag
// 字段首字母本身大写, 是否需要删除tag
if t.config.RmTagIfUcFirsted &&
col.ColumnName[0:1] == strings.ToUpper(col.ColumnName[0:1]) {
col.Tag = "-"
} else {
// 是否需要将tag转换成小写
if t.config.TagToLower {
col.Tag = strings.ToLower(col.Tag)
jsonTag = col.Tag
}
if t.config.JsonTagToHump {
jsonTag = t.camelCase(jsonTag)
}
//if col.Nullable == "YES" {
// col.Json = fmt.Sprintf("`json:\"%s,omitempty\"`", col.Json)
//} else {
//}
}
if t.tagKey == "" {
t.tagKey = "orm"
}
if t.enableJsonTag {
//col.Json = fmt.Sprintf("`json:\"%s\" %s:\"%s\"`", col.Json, t.config.TagKey, col.Json)
col.Tag = fmt.Sprintf("`%s:\"%s\" json:\"%s\"`", t.tagKey, col.Tag, jsonTag)
} else {
col.Tag = fmt.Sprintf("`%s:\"%s\"`", t.tagKey, col.Tag)
}
//columns = append(columns, col)
if _, ok := tableColumns[col.TableName]; !ok {
tableColumns[col.TableName] = []column{}
}
tableColumns[col.TableName] = append(tableColumns[col.TableName], col)
}
return
}
func (t *Table2Struct) camelCase(str string) string {
// 是否有表前缀, 设置了就先去除表前缀
if t.prefix != "" {
str = strings.Replace(str, t.prefix, "", 1)
}
var text string
//for _, p := range strings.Split(name, "_") {
for _, p := range strings.Split(str, "_") {
// 字段首字母大写的同时, 是否要把其他字母转换为小写
switch len(p) {
case 0:
case 1:
text += strings.ToUpper(p[0:1])
default:
// 字符长度大于1时
if t.config.UcFirstOnly == true {
text += strings.ToUpper(p[0:1]) + strings.ToLower(p[1:])
} else {
text += strings.ToUpper(p[0:1]) + p[1:]
}
}
}
return text
}
func tab(depth int) string {
return strings.Repeat("\t", depth)
}