-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcolumn.go
143 lines (121 loc) · 2.61 KB
/
column.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
package genModels
import (
"fmt"
"strings"
)
type Column struct {
ColumnName string
Type string
Nullable string
TableName string
ColumnComment string
Tag string
MaxLength int64
NumberPrecision int64
ColumnType string
ColumnKey string
Default interface{}
}
func (c Column) GetTag(format Format) string {
propertyString := c.getProperty(format)
if propertyString != "" {
propertyString = strings.TrimRight(propertyString, ";")
}
json := c.Tag
if !format.JsonUseCamel {
json = CaseCamel(json)
}
value := fmt.Sprintf(format.GetTabFormat(), c.Tag, propertyString, json)
if value != "" {
if propertyString == "" {
index := strings.Index(value, ";")
if index > -1 {
value = value[0:index] + value[index+1:]
}
}
}
return value
}
func (c Column) GetGoType() string {
v, ok := TypeMappingMysqlToGo[c.Type]
if ok {
if strings.Index(v, "int") > -1 {
if strings.Index(c.ColumnType, "unsigned") > -1 {
v = "u" + v
}
}
return v
}
return ""
}
func (c Column) GetMysqlType() string {
return c.Type
}
func (c Column) GetGoColumn(prefix string, ucFirst bool) string {
return CamelCase(c.ColumnName, prefix, ucFirst)
}
func (c Column) getProperty(format Format) string {
if &format.PropertyFormat == nil {
return ""
}
pf := format.GetPropertyFormat()
value := ""
var size int64
if c.MaxLength > 0 {
size = c.MaxLength
} else if c.NumberPrecision > 0 {
size = c.NumberPrecision
}
useSize := true
tpFormat := pf.GetTypeFormat()
if tpFormat != "" {
//only support time type
//if strings.Index(strings.ToLower(c.ColumnType), "time") > -1 {
value += fmt.Sprintf(tpFormat, c.ColumnType)
value += ";"
//}
if format.Framework == "gorm" {
useSize = false
}
}
if useSize {
szFormat := pf.GetSizeFormat()
if size > 0 {
if szFormat != "" {
value += fmt.Sprintf(szFormat, size)
value += ";"
}
}
}
defaultF := pf.GetDefaultFormat()
if defaultF != "" {
if c.IsPrimaryKey() {
return value
}
value += formatDefault(c.Default, defaultF, c.IsAllowEmpty())
}
return value
}
func (c Column) IsPrimaryKey() bool {
return c.ColumnKey == "PRI"
}
func (c Column) IsAllowEmpty() bool {
return c.Nullable == "YES"
}
func formatDefault(v interface{}, format string, allowedNull bool) (value string) {
if v != nil {
str := string(v.([]byte))
strLow := strings.ToLower(str)
if strLow != "current_timestamp" {
str = "'" + str + "'"
}
value += fmt.Sprintf(format, str)
value += ";"
} else {
if allowedNull {
value += fmt.Sprintf(format, "null")
value += ";"
}
}
return
}