From aa434356e022587c3f04cf1b1a7929eca1eb2382 Mon Sep 17 00:00:00 2001 From: xxjwxc Date: Fri, 11 Sep 2020 23:51:49 +0800 Subject: [PATCH] add default set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加默认值 --- README.md | 12 +++++++++--- README_zh_cn.md | 5 +++++ data/view/model/def.go | 3 ++- data/view/model/genmysql/common.go | 20 ++++++++++++++++++++ data/view/model/genmysql/def.go | 4 ++++ data/view/model/genmysql/genmysql.go | 2 +- data/view/model/model.go | 5 +++++ doc/export.md | 4 ++-- doc/export_cn.md | 4 ++-- 9 files changed, 50 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 04d8fa2..513b13b 100644 --- a/README.md +++ b/README.md @@ -186,9 +186,15 @@ or go generate ``` -## 8. Next step -- update,delete support -- revew +### 8 note : in windows not support utf-8 style . ASCALL model +- Switch encoding mode +``` +CHCP 65001 +``` + +### table notes default + +- Add a comment to the column starting with `[@gormt default:'test']` ## 9. one windows gui tools diff --git a/README_zh_cn.md b/README_zh_cn.md index 9cb1985..8283ddb 100644 --- a/README_zh_cn.md +++ b/README_zh_cn.md @@ -108,6 +108,7 @@ Flags: - UNIQUE_INDEX 和 INDEX 类似,只不过创建的是唯一索引 - 支持外键相关属性 [简单带外键模式导出>>>](https://github.com/xxjwxc/gormt/tree/master/doc/export_cn.md) - 支持函数导出(包括:外键,关联体,索引关...)[简单函数导出示例>>>](https://github.com/xxjwxc/gormt/blob/master/data/view/genfunc/genfunc_test.go) +- 支持默认值default ### 您可以在这里丰富数据映射类型 [def](data/view/cnf/def.go) 。 @@ -198,6 +199,10 @@ go generate CHCP 65001 ``` +### 表注释 default + +- 给列添加注释以`[@gormt default:'test']`开头即可 + ## 8. 下一步计划 diff --git a/data/view/model/def.go b/data/view/model/def.go index 8812874..27ac6ca 100644 --- a/data/view/model/def.go +++ b/data/view/model/def.go @@ -38,9 +38,10 @@ type TabInfo struct { // ColumnsInfo Columns list .表列信息 type ColumnsInfo struct { BaseInfo + IsNull bool // null if db is set null Type string // Type.类型标记 + Default string // 默认值 Index []KList // index list.index列表 - IsNull bool // null if db is set null ForeignKeyList []ForeignKey // Foreign key list . 表的外键信息 } diff --git a/data/view/model/genmysql/common.go b/data/view/model/genmysql/common.go index 965c9af..b8e8215 100644 --- a/data/view/model/genmysql/common.go +++ b/data/view/model/genmysql/common.go @@ -52,3 +52,23 @@ func GetMysqlModel() model.IModel { //now just support mysql return &MySQLModel } + +// FixElementNote 分析元素表注释 +func FixElementNote(em *model.ColumnsInfo, note string) { + matches := noteRegex.FindStringSubmatch(note) + if len(matches) < 2 { + em.Notes = note + return + } + em.Notes = note[len(matches[0]):] + + list := strings.Split(matches[1], ";") + for _, v := range list { + tmp := strings.Split(v, ":") + if len(tmp) == 2 { + if strings.EqualFold(tmp[0], "default") { // 默认值 + em.Default = tmp[1] + } + } + } +} diff --git a/data/view/model/genmysql/def.go b/data/view/model/genmysql/def.go index 0819156..a5e5c47 100644 --- a/data/view/model/genmysql/def.go +++ b/data/view/model/genmysql/def.go @@ -1,5 +1,7 @@ package genmysql +import "regexp" + type keys struct { NonUnique int `gorm:"column:Non_unique"` KeyName string `gorm:"column:Key_name"` @@ -26,3 +28,5 @@ type genForeignKey struct { ReferencedTableName string `gorm:"column:referenced_table_name"` // Affected tables . 该索引受影响的表 ReferencedColumnName string `gorm:"column:referenced_column_name"` // Which column of the affected table.该索引受影响的表的哪一列 } + +var noteRegex = regexp.MustCompile(`^\[@gormt\s(\S+)+\]`) diff --git a/data/view/model/genmysql/genmysql.go b/data/view/model/genmysql/genmysql.go index 05f2a5f..bb4ff03 100644 --- a/data/view/model/genmysql/genmysql.go +++ b/data/view/model/genmysql/genmysql.go @@ -141,8 +141,8 @@ func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumnsInfo) for _, v := range list { var tmp model.ColumnsInfo tmp.Name = v.Field - tmp.Notes = v.Desc tmp.Type = v.Type + FixElementNote(&tmp, v.Desc) // keys if keylist, ok := KeyColumnMp[v.Field]; ok { // maybe have index or key diff --git a/data/view/model/model.go b/data/view/model/model.go index f716b4e..eed99ec 100644 --- a/data/view/model/model.go +++ b/data/view/model/model.go @@ -107,6 +107,10 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement) tmp.AddTag(_tagGorm, "not null") } } + // default tag + if len(v.Default) > 0 { + tmp.AddTag(_tagGorm, "default:"+v.Default) + } // json tag if config.GetIsWEBTag() { @@ -116,6 +120,7 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement) tmp.AddTag(_tagJSON, mybigcamel.UnMarshal(v.Name)) } } + } el = append(el, tmp) diff --git a/doc/export.md b/doc/export.md index 789550c..6d87276 100644 --- a/doc/export.md +++ b/doc/export.md @@ -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 '帐号类型:0手机号,1邮件', + `account_type` int(11) NOT NULL DEFAULT '0' COMMENT '[@gormt 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='用户账号' +) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='[@gormt default:'admin']用户账号' ``` ------------- diff --git a/doc/export_cn.md b/doc/export_cn.md index 8521058..037086d 100644 --- a/doc/export_cn.md +++ b/doc/export_cn.md @@ -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 '帐号类型:0手机号,1邮件', + `account_type` int(11) NOT NULL DEFAULT '0' COMMENT '[@gormt 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='用户账号' +) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='[@gormt default:'admin']用户账号' ``` -------------