add @fk to note

添加外键注解。
This commit is contained in:
xxj
2021-02-02 15:16:10 +08:00
parent dccbfce68a
commit 689c8a30d9
10 changed files with 94 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import (
"strings"
"github.com/xxjwxc/gormt/data/config"
"github.com/xxjwxc/public/mylog"
"github.com/xxjwxc/gormt/data/view/model"
)
@@ -53,13 +54,47 @@ func GetModel() model.IModel {
return &MySQLModel
}
// FixNotes 分析元素表注释
func FixNotes(em *model.ColumnsInfo, note string) {
b0 := FixElementTag(em, note) // gorm
b1 := FixForeignKeyTag(em, em.Notes) // 外键
if !b0 && b1 { // 补偿
FixElementTag(em, em.Notes) // gorm
}
}
// FixElementTag 分析元素表注释
func FixElementTag(em *model.ColumnsInfo, note string) {
func FixElementTag(em *model.ColumnsInfo, note string) bool {
matches := noteRegex.FindStringSubmatch(note)
if len(matches) < 2 {
em.Notes = note
return
return false
}
mylog.Infof("get one gorm tag:(%v) ==> (%v)", em.BaseInfo.Name, matches[1])
em.Notes = note[len(matches[0]):]
em.Gormt = matches[1]
return true
}
// FixForeignKeyTag 分析元素表注释(外键)
func FixForeignKeyTag(em *model.ColumnsInfo, note string) bool {
matches := foreignKeyRegex.FindStringSubmatch(note) // foreign key 外键
if len(matches) < 2 {
em.Notes = note
return false
}
em.Notes = note[len(matches[0]):]
// foreign key 外键
tmp := strings.Split(matches[1], ".")
if len(tmp) > 0 {
mylog.Infof("get one foreign key:(%v) ==> (%v)", em.BaseInfo.Name, matches[1])
em.ForeignKeyList = append(em.ForeignKeyList, model.ForeignKey{
TableName: tmp[0],
ColumnName: tmp[1],
})
}
return true
}