-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_type.go
More file actions
153 lines (139 loc) · 3.86 KB
/
data_type.go
File metadata and controls
153 lines (139 loc) · 3.86 KB
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
package daogext
import "strings"
// isMySQLTypeCompatibleWithGo 判断 MySQL 数据类型是否可映射到指定的 Go 类型
func isMySQLTypeCompatibleWithGo(mysqlType, goTypeName string) bool {
// 统一转为小写便于比较
mysqlType = strings.ToLower(mysqlType)
// 去除无符号等修饰符,只保留基础类型部分(如:int(11) unsigned -> int)
baseMySQLType := extractBaseMySQLType(mysqlType)
// 定义映射关系:Go 类型 -> 兼容的 MySQL 类型列表
compatible := map[string][]string{
"int8": {
"tinyint", "smallint", "mediumint",
},
"uint8": {
"tinyint unsigned", "smallint unsigned", "mediumint unsigned",
},
"int": {
"int", "integer",
},
"uint": {
"tinyint unsigned", "smallint unsigned", "mediumint unsigned", "int unsigned", "integer unsigned",
},
"int32": {
"int", "integer",
},
"uint32": {
"tinyint unsigned", "smallint unsigned", "mediumint unsigned", "int unsigned", "integer unsigned",
},
"int64": {
"bigint",
},
"uint64": {
"bigint unsigned",
},
"float32": {
"float",
},
"float64": {
"double", "decimal", "dec", "numeric",
},
"decimal.Decimal": {
"double", "decimal", "dec", "numeric",
},
"string": {
"char", "varchar", "text", "tinytext", "mediumtext", "longtext",
"enum", "set", "json",
},
"ttypes.NilableString": {
"char", "varchar", "text", "tinytext", "mediumtext", "longtext",
"enum", "set", "json",
},
"bool": {
"tinyint", // 通常用 tinyint(1) 表示布尔
"boolean", "bool",
},
"[]byte": {
"blob", "tinyblob", "mediumblob", "longblob",
"binary", "varbinary", "bit",
},
"time.time": {
"datetime", "timestamp", "date", "time", "year",
},
"ttypes.NilableDate": {
"datetime", "timestamp", "date", "time", "year",
},
"ttypes.NormalDate": {
"datetime", "timestamp", "date", "time", "year",
},
"ttypes.NilableDatetime": {
"datetime", "timestamp", "date", "time", "year",
},
"ttypes.NormalDatetime": {
"datetime", "timestamp", "date", "time", "year",
},
}
// 获取该 Go 类型支持的 MySQL 类型列表
validTypes, exists := compatible[goTypeName]
if !exists {
return false // 不支持的 Go 类型
}
// 检查 baseMySQLType 是否在兼容列表中
for _, t := range validTypes {
if baseMySQLType == t {
return true
}
}
return false
}
// extractBaseMySQLType 提取基础 MySQL 类型(去掉长度、括号、unsigned 等)
// 例如: "int(11) unsigned" -> "int unsigned", "varchar(255)" -> "varchar"
func extractBaseMySQLType(t string) string {
// 去掉括号及内部内容,例如 (11), (10,2)
noParen := removeParentheses(t)
// 分割并重新组合,保留 unsigned 等关键字
parts := strings.Fields(noParen)
if len(parts) == 0 {
return ""
}
var result []string
for _, part := range parts {
trimmed := strings.TrimSpace(part)
if trimmed == "" {
continue
}
// 只保留有意义的词:类型名和 unsigned
if trimmed == "unsigned" || trimmed == "zerofill" || trimmed == "auto_increment" {
continue // 我们单独处理 unsigned,在主类型后添加
}
result = append(result, trimmed)
}
// 重新组合主类型
base := strings.Join(result, " ")
// 特殊处理:如果原字符串包含 unsigned,则附加
hasUnsigned := strings.Contains(strings.ToLower(t), "unsigned")
if hasUnsigned {
if base == "tinyint" || base == "smallint" || base == "mediumint" || base == "int" || base == "integer" || base == "bigint" {
return base + " unsigned"
}
}
return base
}
// removeParentheses 去除字符串中的括号及其内容
func removeParentheses(s string) string {
var result strings.Builder
parenCount := 0
for _, r := range s {
if r == '(' {
parenCount++
} else if r == ')' {
if parenCount > 0 {
parenCount--
}
} else if parenCount == 0 {
result.WriteRune(r)
}
// 在括号内则跳过
}
return result.String()
}