Skip to content

Commit f534623

Browse files
authored
Merge pull request #975 from go-gorm/feat_table_comment
feat: table comment
2 parents d18f8d3 + 2c2b91f commit f534623

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

internal/generate/export.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func GetQueryStructMeta(db *gorm.DB, conf *model.Config) (*QueryStructMeta, erro
4242
Generated: true,
4343
FileName: fileName,
4444
TableName: tableName,
45+
TableComment: getTableComment(db, tableName),
4546
ModelStructName: structName,
4647
QueryStructName: uncaptialize(structName),
4748
S: strings.ToLower(structName[0:1]),

internal/generate/query.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type QueryStructMeta struct {
3232
QueryStructName string // internal query struct name
3333
ModelStructName string // origin/model struct name
3434
TableName string // table name in db server
35+
TableComment string // table comment in db server
3536
StructInfo parser.Param
3637
Fields []*model.Field
3738
Source model.SourceCode
@@ -151,12 +152,24 @@ func (b *QueryStructMeta) Relations() (result []field.Relation) {
151152

152153
// StructComment struct comment
153154
func (b *QueryStructMeta) StructComment() string {
155+
if b.TableComment != "" {
156+
return b.TableComment
157+
}
154158
if b.TableName != "" {
155159
return fmt.Sprintf(`mapped from table <%s>`, b.TableName)
156160
}
157161
return `mapped from object`
158162
}
159163

164+
// QueryStructComment query struct comment
165+
func (b *QueryStructMeta) QueryStructComment() string {
166+
if b.TableComment != "" {
167+
return fmt.Sprintf(`// %s %s`, b.QueryStructName, b.TableComment)
168+
}
169+
170+
return ``
171+
}
172+
160173
// ReviseDIYMethod check diy method duplication name
161174
func (b *QueryStructMeta) ReviseDIYMethod() error {
162175
var duplicateMethodName []string

internal/generate/table.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ func getTableInfo(db *gorm.DB) ITableInfo {
2020
return &tableInfo{db}
2121
}
2222

23+
func getTableComment(db *gorm.DB, tableName string) string {
24+
table, err := getTableType(db, tableName)
25+
if err != nil || table == nil {
26+
return ""
27+
}
28+
if comment, ok := table.Comment(); ok {
29+
return comment
30+
}
31+
return ""
32+
}
33+
34+
func getTableType(db *gorm.DB, tableName string) (result gorm.TableType, err error) {
35+
if db == nil || db.Migrator() == nil {
36+
return
37+
}
38+
return db.Migrator().TableType(tableName)
39+
}
40+
2341
func getTableColumns(db *gorm.DB, schemaName string, tableName string, indexTag bool) (result []*model.Column, err error) {
2442
if db == nil {
2543
return nil, errors.New("gorm db is nil")

internal/template/struct.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package template
33
const (
44
// TableQueryStruct table query struct
55
TableQueryStruct = createMethod + `
6+
{{.QueryStructComment}}
67
type {{.QueryStructName}} struct {
78
{{.QueryStructName}}Do
89
` + fields + `
@@ -11,6 +12,7 @@ const (
1112

1213
// TableQueryStructWithContext table query struct with context
1314
TableQueryStructWithContext = createMethod + `
15+
{{.QueryStructComment}}
1416
type {{.QueryStructName}} struct {
1517
{{.QueryStructName}}Do {{.QueryStructName}}Do
1618
` + fields + `

0 commit comments

Comments
 (0)