-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
84 lines (75 loc) · 2.4 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
package db
import (
"fmt"
"github.com/team-ide/go-dialect/dialect"
"github.com/team-ide/go-tool/util"
"strings"
)
type modelGen struct {
modelType string
tables []*dialect.TableModel
content string
}
func (this_ *modelGen) gen() (content string, err error) {
this_.content = ""
for _, table := range this_.tables {
err = this_.append(table)
if err != nil {
return
}
this_.content += "\n"
}
content = this_.content
return
}
func (this_ *modelGen) append(table *dialect.TableModel) (err error) {
switch this_.modelType {
case "table":
err = this_.appendTableModel(table)
break
}
return
}
func (this_ *modelGen) appendTableModel(table *dialect.TableModel) (err error) {
name := strings.ToLower(table.TableName)
name = util.Marshal(name)
this_.content += "" + name + "Model = &dialect.TableModel{" + "\n"
this_.content += "\t" + "TableName: `" + table.TableName + "`," + "\n"
this_.content += "\t" + "TableComment: `" + table.TableComment + "`," + "\n"
this_.content += "\t" + "ColumnList: []*dialect.ColumnModel{" + "\n"
for _, column := range table.ColumnList {
this_.content += "\t\t{"
this_.content += "ColumnName: `" + column.ColumnName + "`, "
this_.content += "ColumnDataType: `" + column.ColumnDataType + "`, "
if column.ColumnLength > 0 {
this_.content += "ColumnLength: " + fmt.Sprintf("%d", column.ColumnLength) + ", "
}
if column.ColumnPrecision > 0 {
this_.content += "ColumnPrecision: " + fmt.Sprintf("%d", column.ColumnPrecision) + ", "
}
if column.ColumnScale > 0 {
this_.content += "ColumnScale: " + fmt.Sprintf("%d", column.ColumnScale) + ", "
}
if column.ColumnNotNull {
this_.content += "ColumnNotNull: true, "
}
if column.PrimaryKey {
this_.content += "PrimaryKey: true, "
}
this_.content += "ColumnComment: `" + column.ColumnComment + "`"
this_.content += "}," + "\n"
}
this_.content += "\t" + "}," + "\n"
this_.content += "\t" + "IndexList: []*dialect.IndexModel{" + "\n"
for _, index := range table.IndexList {
this_.content += "\t\t{"
this_.content += "IndexName: `" + index.IndexName + "`, "
this_.content += "IndexType: `" + index.IndexType + "`, "
this_.content += "ColumnNames: []string{\"" + strings.Join(index.ColumnNames, "\", \"") + "\"}, "
this_.content += "IndexComment: `" + index.IndexComment + "`"
this_.content += "}," + "\n"
}
this_.content += "\t" + "}," + "\n"
this_.content += "}" + "\n"
return
}