add @fk to note
添加外键注解。
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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+)+\]`)
|
||||
|
||||
@@ -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 == "" {
|
||||
|
||||
@@ -53,13 +53,44 @@ func GetModel() model.IModel {
|
||||
return &SQLiteModel
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
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 {
|
||||
em.ForeignKeyList = append(em.ForeignKeyList, model.ForeignKey{
|
||||
TableName: tmp[0],
|
||||
ColumnName: tmp[1],
|
||||
})
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -28,4 +28,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+)+\]`)
|
||||
|
||||
@@ -147,7 +147,7 @@ func (m *sqliteModel) getTableElement(orm *gorm.DB, tab string) (el []model.Colu
|
||||
var tmp model.ColumnsInfo
|
||||
tmp.Name = v.Name
|
||||
tmp.Type = v.Type
|
||||
FixElementTag(&tmp, "")
|
||||
FixNotes(&tmp, "")
|
||||
if v.Pk == 1 { // 主键
|
||||
tmp.Index = append(tmp.Index, model.KList{
|
||||
Key: model.ColumnsKeyPrimary,
|
||||
|
||||
Reference in New Issue
Block a user