add @fk to note
添加外键注解。
This commit is contained in:
@@ -197,9 +197,10 @@ CHCP 65001
|
||||
|
||||
### table notes default
|
||||
|
||||
- Add a comment to the column starting with `[@gormt default:'test']`
|
||||
- example `[@gormt default:'test';->;<-:create]this is my notes` Indicates that the default value is 'test',can read/creat/write
|
||||
|
||||
- Add a comment to the column starting with `[@gorm default:'test']`
|
||||
- example `[@gorm default:'test';->;<-:create]this is my notes` Indicates that the default value is 'test',can read/creat/write
|
||||
- Use of foreign key notes`[@fk tableName.columnName]this is my notes` Represents the 'columnName' column associated with the 'tableName'
|
||||
|
||||
## 9. one windows gui tools
|
||||
|
||||

|
||||
|
||||
@@ -207,8 +207,9 @@ CHCP 65001
|
||||
|
||||
### 表注释 tag
|
||||
|
||||
- 给列添加注释以`[@gormt default:'test']`开头即可
|
||||
- 比如`[@gormt default:'test';->;<-:create]这是注释内容` 表示默认值为'test',允许读,更新创建
|
||||
- 给列添加注释以`[@gorm default:'test']`开头即可
|
||||
- 比如`[@gorm default:'test';->;<-:create]这是注释内容` 表示默认值为'test',允许读,更新创建
|
||||
- 外键注释使用`[@fk tableName.columnName]这是注释内容` 表示关联到`tableName`的`columnName`列
|
||||
|
||||
|
||||
## 8. 下一步计划
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -17,7 +17,7 @@ CREATE TABLE `user_account_tbl` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`account` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`password` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`account_type` int(11) NOT NULL DEFAULT '0' COMMENT '[@gormt default:'123456']帐号类型:0手机号,1邮件',
|
||||
`account_type` int(11) NOT NULL DEFAULT '0' COMMENT '[@gorm default:'123456']帐号类型:0手机号,1邮件',
|
||||
`app_key` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT 'authbucket_oauth2_client表的id',
|
||||
`user_info_tbl_id` int(11) NOT NULL,
|
||||
`reg_time` datetime DEFAULT NULL,
|
||||
@@ -29,7 +29,7 @@ CREATE TABLE `user_account_tbl` (
|
||||
UNIQUE KEY `UNIQ_5696AD037D3656A4` (`app_key`,`user_info_tbl_id`) USING BTREE,
|
||||
KEY `user_info_id` (`user_info_tbl_id`) USING BTREE,
|
||||
CONSTRAINT `user_account_tbl_ibfk_1` FOREIGN KEY (`user_info_tbl_id`) REFERENCES `user_info_tbl` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='[@gormt default:'admin']用户账号'
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='[@gorm default:'admin']用户账号'
|
||||
```
|
||||
|
||||
-------------
|
||||
|
||||
@@ -17,7 +17,7 @@ CREATE TABLE `user_account_tbl` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`account` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`password` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
|
||||
`account_type` int(11) NOT NULL DEFAULT '0' COMMENT '[@gormt default:'123456']帐号类型:0手机号,1邮件',
|
||||
`account_type` int(11) NOT NULL DEFAULT '0' COMMENT '[@gorm default:'123456']帐号类型:0手机号,1邮件',
|
||||
`app_key` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT 'authbucket_oauth2_client表的id',
|
||||
`user_info_tbl_id` int(11) NOT NULL,
|
||||
`reg_time` datetime DEFAULT NULL,
|
||||
@@ -29,7 +29,7 @@ CREATE TABLE `user_account_tbl` (
|
||||
UNIQUE KEY `UNIQ_5696AD037D3656A4` (`app_key`,`user_info_tbl_id`) USING BTREE,
|
||||
KEY `user_info_id` (`user_info_tbl_id`) USING BTREE,
|
||||
CONSTRAINT `user_account_tbl_ibfk_1` FOREIGN KEY (`user_info_tbl_id`) REFERENCES `user_info_tbl` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='[@gormt default:'admin']用户账号'
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='[@gorm default:'admin']用户账号'
|
||||
```
|
||||
|
||||
-------------
|
||||
@@ -159,4 +159,10 @@ type UserInfoTbl struct {
|
||||
Nickname string
|
||||
Headurl string
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
### 表注释 tag
|
||||
|
||||
- 给列添加注释以`[@gorm default:'test']`开头即可
|
||||
- 比如`[@gorm default:'test';->;<-:create]这是注释内容` 表示默认值为'test',允许读,更新创建
|
||||
- 外键注释使用`[@fk tableName.columnName]这是注释内容` 表示关联到`tableName`的`columnName`列
|
||||
|
||||
Reference in New Issue
Block a user