Skip to content

Commit

Permalink
feat: table comment
Browse files Browse the repository at this point in the history
  • Loading branch information
qqxhb committed Sep 3, 2023
1 parent d18f8d3 commit 2ca11ea
Show file tree
Hide file tree
Showing 4 changed files with 25 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
4 changes: 4 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,6 +152,9 @@ 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)
}
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 + `
// {{.QueryStructName}} {{.StructComment}}
type {{.QueryStructName}} struct {
{{.QueryStructName}}Do
` + fields + `
Expand All @@ -11,6 +12,7 @@ const (

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

0 comments on commit 2ca11ea

Please sign in to comment.