...
|
...
|
@@ -5,6 +5,7 @@ import ( |
|
|
"fmt"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
|
|
|
"reflect"
|
|
|
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
"github.com/go-pg/pg/v10/orm"
|
...
|
...
|
@@ -38,6 +39,9 @@ func init() { |
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
if !constant.DISABLE_SQL_GENERATE_COMMENT {
|
|
|
AddComments(DB, model)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -56,3 +60,26 @@ func (hook SqlGeneratePrintHook) AfterQuery(c context.Context, q *pg.QueryEvent) |
|
|
log.Logger.Debug(string(sqlStr))
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func AddComments(db *pg.DB, model interface{}) {
|
|
|
tableName := db.Model(model).TableModel().Table().SQLName
|
|
|
columnsMap := make(map[string]*orm.Field)
|
|
|
columns := db.Model(model).TableModel().Table().Fields
|
|
|
for _, item := range columns {
|
|
|
columnsMap[item.GoName] = item
|
|
|
}
|
|
|
valueOf := reflect.TypeOf(model)
|
|
|
for i := 0; i < valueOf.Elem().NumField(); i++ {
|
|
|
field := valueOf.Elem().Field(i)
|
|
|
comment := field.Tag.Get("comment")
|
|
|
if comment != "" {
|
|
|
if field.Name == "tableName" {
|
|
|
_, _ = db.Exec(fmt.Sprintf("COMMENT ON TABLE %s IS '%s';", tableName, comment))
|
|
|
} else {
|
|
|
if columnField, ok := columnsMap[field.Name]; ok {
|
|
|
_, _ = db.Exec(fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s';", tableName, columnField.SQLName, comment))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|