Skip to content

Commit a977099

Browse files
author
alex cai
committed
增加多行记录返回map结构的功能,优化生成的文档注释
1 parent 05d7357 commit a977099

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ go-db-models
2626
- 单行结果查询函数
2727
- 根据某个字段查询单行记录的函数,例如根据id字段进行查询
2828
- 多行结果查询函数
29+
- 返回多行结果时,可以选择生成list(默认)或map格式
2930

3031
`*_tb_gen.go`文件样例如下:
3132

@@ -129,6 +130,9 @@ func AdProjectQuery(db *sql.DB, queryString string) (ad_project []*AdProjectTabl
129130
"fields": ["id", "name", "status", "daily_budget", "start_date", "created_at"],
130131
// [可选]这是可选项,如果定义了该字段,则会自动生成一个类似QueryById的查询函数。
131132
"queryBy": "id",
133+
// [可选]返回多行数据时,默认返回格式是list,但是可以通过指定该字段,来返回map结构,其下标就是对应的字段的值。
134+
// 该字段通常是主键或者唯一索引的字段,否则就可能出现重复值
135+
"mapIndex": "id",
132136
// [可选]结构体增加注释
133137
"msg": "这是注释。。。"
134138
},

models/db.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ type Table struct {
2222
Fields []Field
2323
}
2424

25-
// 字段定义
25+
/*
26+
字段定义
27+
使用MySQL数据库desc 表名时,我们看到Key那一栏,可能会有4种值,即 ' ','PRI','UNI','MUL'。
28+
29+
- 如果Key是空的,那么该列值的可以重复,表示该列没有索引,或者是一个非唯一的复合索引的非前导列;
30+
- 如果Key是PRI,那么该列是主键的组成部分;
31+
- 如果Key是UNI,那么该列是一个唯一值索引的第一列(前导列),并别不能含有空值(NULL);
32+
- 如果Key是MUL,那么该列的值可以重复,该列是一个非唯一索引的前导列(第一列)或者是一个唯一性索引的组成部分但是可以含有空值NULL。
33+
*/
2634
type Field struct {
2735
Name string
2836
Type string

models/json.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ type JsonConf struct {
1212

1313
// 单个数据表的配置
1414
type JsonTableConf struct {
15-
Name string // 表名
16-
Fields []string // 字段名
17-
QueryBy string // QueryBy函数定义
18-
Msg string // 表结构说明
15+
Name string // 表名
16+
Fields []string // 字段名
17+
QueryBy string // QueryBy函数定义
18+
MapIndex string // 返回map结构时的下标字段名
19+
Msg string // 表结构说明
1920
}
2021

2122
// 将json文件decode成结构体

models/parse_table.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ const (
1515
type ParseTable struct {
1616
Name string // 数据表名,如:ad_plan,对应结构体名为:AdPlanTable,对应文件名为:gen_ad_plan.go
1717
PackageName string // 生成的程序的包名
18-
PrimaryType string // 主键的类型,如:uint32, sql.NullString等
18+
PrimaryType string // 主键的类型,如:uint32, string等
1919
Imports []string // 需要import的包
2020
SelectFields string // sql查询中的select fields
2121
Msg string // 表结构的说明
2222
Fields []ParseField
2323
QueryBy QueryBy // QueryBy函数,例如QueryById等
24+
25+
// 如果该字段不为空,则返回多行记录时,是map结构(默认是list结构)
26+
// 关联json中的MapIndex字段
27+
MapIndex MapIndexField
2428
}
2529

2630
// 字段的定义
@@ -29,6 +33,11 @@ type ParseField struct {
2933
Type string // 字段类型,对应golang中的类型,如:uint32, sql.NullString
3034
}
3135

36+
type MapIndexField struct {
37+
Name string // map下标的字段名
38+
Type string // map下标字段的类型
39+
}
40+
3241
// 生成QueryBy函数时需要该结构
3342
type QueryBy struct {
3443
FieldName string // query by函数的参数名
@@ -47,6 +56,7 @@ func ParseTablesStruct(tables []Table, packageName string, modelsConf *JsonConf)
4756
var modelsConfMap = map[string]map[string]bool{} // 第一个下标是表名,第二个下标是字段名
4857
var queryByConf = map[string]string{} // 下标是表名,值是字段名,例如id。
4958
var tableConfMsg = map[string]string{} // 表的注释(在json文件中的)
59+
var mapIndexConf = map[string]string{} // map下标配置
5060
if len(modelsConf.Tables) > 0 {
5161
for _, tb := range modelsConf.Tables {
5262
modelsConfMap[tb.Name] = map[string]bool{}
@@ -58,6 +68,10 @@ func ParseTablesStruct(tables []Table, packageName string, modelsConf *JsonConf)
5868
queryByConf[tb.Name] = tb.QueryBy
5969
}
6070

71+
if tb.MapIndex != "" {
72+
mapIndexConf[tb.Name] = tb.MapIndex
73+
}
74+
6175
tableConfMsg[tb.Name] = tb.Msg
6276
}
6377
}
@@ -98,6 +112,23 @@ func ParseTablesStruct(tables []Table, packageName string, modelsConf *JsonConf)
98112
}
99113
}
100114

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+
101132
// 生成代码文件
102133
err = GenFile(ptable)
103134
if err != nil {

models/template.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package models
33
const (
44
// 生成的数据表的文件的模版: gen_table_*.go
55
codeTemplate = `// DON'T EDIT *** generated by go-db-models *** DON'T EDIT //
6-
// 数据表:{{.Name}}
6+
77
package {{.PackageName}}
88
99
{{range $k, $v := .Imports}}
@@ -65,7 +65,7 @@ func do{{.Name|Format2StructName}}QueryRow(db *sql.DB, queryString string) ({{.N
6565
6666
// 根据where条件查询多行记录
6767
// 如果查询所有记录,只需要where="1"
68-
func {{.Name|Format2StructName}}QueryWhere(db *sql.DB, where string) ({{.Name}} []*{{.Name|Format2StructName}}Table, err error) {
68+
func {{.Name|Format2StructName}}QueryWhere(db *sql.DB, where string) ({{.Name}} {{if .MapIndex.Name}}map[{{.MapIndex.Type}}]*{{.Name|Format2StructName}}Table{{else}}[]*{{.Name|Format2StructName}}Table{{end}}, err error) {
6969
queryString := "SELECT " + {{.Name|Format2Title}}SelectFields + " FROM {{.Name|AddBackquote}} WHERE " + where
7070
return do{{.Name|Format2StructName}}Query(db, queryString)
7171
}
@@ -74,13 +74,13 @@ func {{.Name|Format2StructName}}QueryWhere(db *sql.DB, where string) ({{.Name}}
7474
// 外部使用时,SQL语句应该这样写:
7575
// select {select-fields-temp} from ad_plan where status = 1
7676
// 其中{select-fields-temp}会自动替换为相应的字段名
77-
func {{.Name|Format2StructName}}Query(db *sql.DB, queryString string) ({{.Name}} []*{{.Name|Format2StructName}}Table, err error) {
77+
func {{.Name|Format2StructName}}Query(db *sql.DB, queryString string) ({{.Name}} {{if .MapIndex.Name}}map[{{.MapIndex.Type}}]*{{.Name|Format2StructName}}Table{{else}}[]*{{.Name|Format2StructName}}Table{{end}}, err error) {
7878
queryString = strings.Replace(queryString, SelectFieldsTemp, {{.Name|Format2Title}}SelectFields, 1)
7979
return do{{.Name|Format2StructName}}Query(db, queryString)
8080
}
8181
8282
// 查询多行记录(内部调用)
83-
func do{{.Name|Format2StructName}}Query(db *sql.DB, queryString string) ({{.Name}} []*{{.Name|Format2StructName}}Table, err error) {
83+
func do{{.Name|Format2StructName}}Query(db *sql.DB, queryString string) ({{.Name}} {{if .MapIndex.Name}}map[{{.MapIndex.Type}}]*{{.Name|Format2StructName}}Table{{else}}[]*{{.Name|Format2StructName}}Table{{end}}, err error) {
8484
rows, err := db.Query(queryString)
8585
if err != nil {
8686
return nil, err
@@ -96,14 +96,19 @@ func do{{.Name|Format2StructName}}Query(db *sql.DB, queryString string) ({{.Name
9696
return nil, err
9797
}
9898
99+
{{if .MapIndex.Name}}
100+
{{.Name}}[oneRow.{{.MapIndex.Name|Format2StructName}}] = oneRow
101+
{{else}}
99102
{{.Name}} = append({{.Name}}, oneRow)
103+
{{end}}
100104
}
101105
102106
return {{.Name}}, nil
103107
}
104108
`
105109
// 生成的gen_common.go文件的模版
106110
commonCodeTemplate = `// DON'T EDIT *** generated by go-db-models *** DON'T EDIT //
111+
107112
package {{.PackageName}}
108113
109114
const (

0 commit comments

Comments
 (0)