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
}

View File

@@ -31,4 +31,5 @@ type genForeignKey struct {
ReferencedColumnName string `gorm:"column:referenced_column_name"` // Which column of the affected table.该索引受影响的表的哪一列
}
var noteRegex = regexp.MustCompile(`^\[@gormt\s(\S+)+\]`)
var noteRegex = regexp.MustCompile(`^\[@gorm\s(\S+)+\]`)
var foreignKeyRegex = regexp.MustCompile(`^\[@fk\s(\S+)+\]`)

View File

@@ -142,7 +142,7 @@ func (m *mysqlModel) getTableElement(orm *mysqldb.MySqlDB, tab string) (el []mod
var tmp model.ColumnsInfo
tmp.Name = v.Field
tmp.Type = v.Type
FixElementTag(&tmp, v.Desc) // 分析表注释
FixNotes(&tmp, v.Desc) // 分析表注释
if v.Default != nil {
if *v.Default == "" {