diff --git a/internal/generate/export.go b/internal/generate/export.go index d83fa45b..bcdea543 100644 --- a/internal/generate/export.go +++ b/internal/generate/export.go @@ -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]), diff --git a/internal/generate/query.go b/internal/generate/query.go index 460aa77d..c9e27e10 100644 --- a/internal/generate/query.go +++ b/internal/generate/query.go @@ -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 @@ -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 diff --git a/internal/generate/table.go b/internal/generate/table.go index e9d68e92..e5222c12 100644 --- a/internal/generate/table.go +++ b/internal/generate/table.go @@ -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") diff --git a/internal/template/struct.go b/internal/template/struct.go index 3aa1a9c2..8b83a12e 100644 --- a/internal/template/struct.go +++ b/internal/template/struct.go @@ -3,6 +3,7 @@ package template const ( // TableQueryStruct table query struct TableQueryStruct = createMethod + ` + {{.QueryStructComment}} type {{.QueryStructName}} struct { {{.QueryStructName}}Do ` + fields + ` @@ -11,6 +12,7 @@ const ( // TableQueryStructWithContext table query struct with context TableQueryStructWithContext = createMethod + ` + {{.QueryStructComment}} type {{.QueryStructName}} struct { {{.QueryStructName}}Do {{.QueryStructName}}Do ` + fields + `