Skip to content

Commit

Permalink
Merge pull request go-gorm#975 from go-gorm/feat_table_comment
Browse files Browse the repository at this point in the history
feat: table comment
  • Loading branch information
qqxhb authored Sep 3, 2023
2 parents d18f8d3 + 2c2b91f commit f534623
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/generate/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func GetQueryStructMeta(db *gorm.DB, conf *model.Config) (*QueryStructMeta, erro
Generated: true,
FileName: fileName,
TableName: tableName,
TableComment: getTableComment(db, tableName),
ModelStructName: structName,
QueryStructName: uncaptialize(structName),
S: strings.ToLower(structName[0:1]),
Expand Down
13 changes: 13 additions & 0 deletions internal/generate/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type QueryStructMeta struct {
QueryStructName string // internal query struct name
ModelStructName string // origin/model struct name
TableName string // table name in db server
TableComment string // table comment in db server
StructInfo parser.Param
Fields []*model.Field
Source model.SourceCode
Expand Down Expand Up @@ -151,12 +152,24 @@ func (b *QueryStructMeta) Relations() (result []field.Relation) {

// StructComment struct comment
func (b *QueryStructMeta) StructComment() string {
if b.TableComment != "" {
return b.TableComment
}
if b.TableName != "" {
return fmt.Sprintf(`mapped from table <%s>`, b.TableName)
}
return `mapped from object`
}

// QueryStructComment query struct comment
func (b *QueryStructMeta) QueryStructComment() string {
if b.TableComment != "" {
return fmt.Sprintf(`// %s %s`, b.QueryStructName, b.TableComment)
}

return ``
}

// ReviseDIYMethod check diy method duplication name
func (b *QueryStructMeta) ReviseDIYMethod() error {
var duplicateMethodName []string
Expand Down
18 changes: 18 additions & 0 deletions internal/generate/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ func getTableInfo(db *gorm.DB) ITableInfo {
return &tableInfo{db}
}

func getTableComment(db *gorm.DB, tableName string) string {
table, err := getTableType(db, tableName)
if err != nil || table == nil {
return ""
}
if comment, ok := table.Comment(); ok {
return comment
}
return ""
}

func getTableType(db *gorm.DB, tableName string) (result gorm.TableType, err error) {
if db == nil || db.Migrator() == nil {
return
}
return db.Migrator().TableType(tableName)
}

func getTableColumns(db *gorm.DB, schemaName string, tableName string, indexTag bool) (result []*model.Column, err error) {
if db == nil {
return nil, errors.New("gorm db is nil")
Expand Down
2 changes: 2 additions & 0 deletions internal/template/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package template
const (
// TableQueryStruct table query struct
TableQueryStruct = createMethod + `
{{.QueryStructComment}}
type {{.QueryStructName}} struct {
{{.QueryStructName}}Do
` + fields + `
Expand All @@ -11,6 +12,7 @@ const (

// TableQueryStructWithContext table query struct with context
TableQueryStructWithContext = createMethod + `
{{.QueryStructComment}}
type {{.QueryStructName}} struct {
{{.QueryStructName}}Do {{.QueryStructName}}Do
` + fields + `
Expand Down

0 comments on commit f534623

Please sign in to comment.