Skip to content

Commit

Permalink
add @fk to note
Browse files Browse the repository at this point in the history
添加外键注解。
  • Loading branch information
xxjwxc committed Feb 2, 2021
1 parent dccbfce commit 689c8a3
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 18 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

![1](/image/gormt/1.png)
Expand Down
5 changes: 3 additions & 2 deletions README_zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. 下一步计划
Expand Down
39 changes: 37 additions & 2 deletions data/view/model/genmysql/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/xxjwxc/gormt/data/config"
"github.com/xxjwxc/public/mylog"

"github.com/xxjwxc/gormt/data/view/model"
)
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion data/view/model/genmysql/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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+)+\]`)
2 changes: 1 addition & 1 deletion data/view/model/genmysql/genmysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
35 changes: 33 additions & 2 deletions data/view/model/gensqlite/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion data/view/model/gensqlite/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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+)+\]`)
2 changes: 1 addition & 1 deletion data/view/model/gensqlite/gensqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions doc/export.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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']用户账号'
```

-------------
Expand Down
12 changes: 9 additions & 3 deletions doc/export_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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']用户账号'
```

-------------
Expand Down Expand Up @@ -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`

0 comments on commit 689c8a3

Please sign in to comment.