@@ -15,12 +15,16 @@ const (
15
15
type ParseTable struct {
16
16
Name string // 数据表名,如:ad_plan,对应结构体名为:AdPlanTable,对应文件名为:gen_ad_plan.go
17
17
PackageName string // 生成的程序的包名
18
- PrimaryType string // 主键的类型,如:uint32, sql.NullString等
18
+ PrimaryType string // 主键的类型,如:uint32, string等
19
19
Imports []string // 需要import的包
20
20
SelectFields string // sql查询中的select fields
21
21
Msg string // 表结构的说明
22
22
Fields []ParseField
23
23
QueryBy QueryBy // QueryBy函数,例如QueryById等
24
+
25
+ // 如果该字段不为空,则返回多行记录时,是map结构(默认是list结构)
26
+ // 关联json中的MapIndex字段
27
+ MapIndex MapIndexField
24
28
}
25
29
26
30
// 字段的定义
@@ -29,6 +33,11 @@ type ParseField struct {
29
33
Type string // 字段类型,对应golang中的类型,如:uint32, sql.NullString
30
34
}
31
35
36
+ type MapIndexField struct {
37
+ Name string // map下标的字段名
38
+ Type string // map下标字段的类型
39
+ }
40
+
32
41
// 生成QueryBy函数时需要该结构
33
42
type QueryBy struct {
34
43
FieldName string // query by函数的参数名
@@ -47,6 +56,7 @@ func ParseTablesStruct(tables []Table, packageName string, modelsConf *JsonConf)
47
56
var modelsConfMap = map [string ]map [string ]bool {} // 第一个下标是表名,第二个下标是字段名
48
57
var queryByConf = map [string ]string {} // 下标是表名,值是字段名,例如id。
49
58
var tableConfMsg = map [string ]string {} // 表的注释(在json文件中的)
59
+ var mapIndexConf = map [string ]string {} // map下标配置
50
60
if len (modelsConf .Tables ) > 0 {
51
61
for _ , tb := range modelsConf .Tables {
52
62
modelsConfMap [tb .Name ] = map [string ]bool {}
@@ -58,6 +68,10 @@ func ParseTablesStruct(tables []Table, packageName string, modelsConf *JsonConf)
58
68
queryByConf [tb .Name ] = tb .QueryBy
59
69
}
60
70
71
+ if tb .MapIndex != "" {
72
+ mapIndexConf [tb .Name ] = tb .MapIndex
73
+ }
74
+
61
75
tableConfMsg [tb .Name ] = tb .Msg
62
76
}
63
77
}
@@ -98,6 +112,23 @@ func ParseTablesStruct(tables []Table, packageName string, modelsConf *JsonConf)
98
112
}
99
113
}
100
114
115
+ // 处理map index
116
+ if mapIndexConf [table .Name ] != "" {
117
+ isMatch := false
118
+ for _ , f := range ptable .Fields {
119
+ if f .Name == mapIndexConf [table .Name ] {
120
+ isMatch = true
121
+ ptable .MapIndex .Name = f .Name
122
+ ptable .MapIndex .Type = f .Type
123
+ }
124
+ }
125
+
126
+ if isMatch == false {
127
+ // query by的字段不在字段列表里
128
+ return nil , errors .New ("ERROR field name: " + mapIndexConf [table .Name ] + " of MapIndex for table name: " + table .Name )
129
+ }
130
+ }
131
+
101
132
// 生成代码文件
102
133
err = GenFile (ptable )
103
134
if err != nil {
0 commit comments