From ea01cc78f036848c50d71cb86d60a056cb05d870 Mon Sep 17 00:00:00 2001 From: anqiansong Date: Wed, 12 May 2021 12:28:23 +0800 Subject: [PATCH] Optimize model nl (#686) --- tools/goctl/model/sql/example/sql/user.sql | 31 ++++++++++--------- .../model/sql/model/informationschemamodel.go | 2 ++ tools/goctl/model/sql/parser/parser.go | 3 +- tools/goctl/model/sql/parser/parser_test.go | 12 ++++++- tools/goctl/model/sql/util/newline.go | 9 ++++++ 5 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 tools/goctl/model/sql/util/newline.go diff --git a/tools/goctl/model/sql/example/sql/user.sql b/tools/goctl/model/sql/example/sql/user.sql index 4a0cb2d0..29fd6601 100644 --- a/tools/goctl/model/sql/example/sql/user.sql +++ b/tools/goctl/model/sql/example/sql/user.sql @@ -1,19 +1,20 @@ -- 用户表 -- -CREATE TABLE `user` ( - `id` bigint(10) NOT NULL AUTO_INCREMENT, - `user` varchar(50) NOT NULL DEFAULT '' COMMENT '用户', - `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称', - `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码', - `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号', - `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开', - `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称', - `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, - `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - PRIMARY KEY (`id`), - UNIQUE KEY `name_index` (`name`), - UNIQUE KEY `name_index2` (`name`), - UNIQUE KEY `user_index` (`user`), - UNIQUE KEY `mobile_index` (`mobile`) +CREATE TABLE `user` +( + `id` bigint(10) NOT NULL AUTO_INCREMENT, + `user` varchar(50) NOT NULL DEFAULT '' COMMENT '用户', + `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户\t名称', + `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户\n密码', + `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号', + `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公\r开', + `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称', + `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `name_index` (`name`), + UNIQUE KEY `name_index2` (`name`), + UNIQUE KEY `user_index` (`user`), + UNIQUE KEY `mobile_index` (`mobile`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE `student` ( diff --git a/tools/goctl/model/sql/model/informationschemamodel.go b/tools/goctl/model/sql/model/informationschemamodel.go index 8f1134e8..6a8d95b2 100644 --- a/tools/goctl/model/sql/model/informationschemamodel.go +++ b/tools/goctl/model/sql/model/informationschemamodel.go @@ -5,6 +5,7 @@ import ( "sort" "github.com/tal-tech/go-zero/core/stores/sqlx" + "github.com/tal-tech/go-zero/tools/goctl/model/sql/util" ) const indexPri = "PRIMARY" @@ -151,6 +152,7 @@ func (c *ColumnData) Convert() (*Table, error) { m := make(map[string][]*Column) for _, each := range c.Columns { + each.Comment = util.TrimNewLine(each.Comment) if each.Index != nil { m[each.Index.IndexName] = append(m[each.Index.IndexName], each) } diff --git a/tools/goctl/model/sql/parser/parser.go b/tools/goctl/model/sql/parser/parser.go index 3c8823d2..09ed3f80 100644 --- a/tools/goctl/model/sql/parser/parser.go +++ b/tools/goctl/model/sql/parser/parser.go @@ -8,6 +8,7 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/model/sql/converter" "github.com/tal-tech/go-zero/tools/goctl/model/sql/model" + "github.com/tal-tech/go-zero/tools/goctl/model/sql/util" "github.com/tal-tech/go-zero/tools/goctl/util/console" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" "github.com/xwb1989/sqlparser" @@ -185,7 +186,7 @@ func convertColumns(columns []*sqlparser.ColumnDefinition, primaryColumn string) field.Name = stringx.From(column.Name.String()) field.DataBaseType = column.Type.Type field.DataType = dataType - field.Comment = comment + field.Comment = util.TrimNewLine(comment) if field.Name.Source() == primaryColumn { primaryKey = Primary{ diff --git a/tools/goctl/model/sql/parser/parser_test.go b/tools/goctl/model/sql/parser/parser_test.go index de2faf22..d13da8fb 100644 --- a/tools/goctl/model/sql/parser/parser_test.go +++ b/tools/goctl/model/sql/parser/parser_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/tools/goctl/model/sql/model" + "github.com/tal-tech/go-zero/tools/goctl/model/sql/util" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -20,7 +21,7 @@ func TestParseSelect(t *testing.T) { } func TestParseCreateTable(t *testing.T) { - table, err := Parse("CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL,\n `class` bigint NOT NULL,\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;") + table, err := Parse("CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL comment '手\\t机 号',\n `class` bigint NOT NULL comment '班级',\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL comment '姓\n 名',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP comment '创建\\r时间',\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;") assert.Nil(t, err) assert.Equal(t, "test_user", table.Name.Source()) assert.Equal(t, "id", table.PrimaryKey.Name.Source()) @@ -82,6 +83,15 @@ func TestParseCreateTable(t *testing.T) { }, }) }()) + assert.True(t, func() bool { + for _, e := range table.Fields { + if e.Comment != util.TrimNewLine(e.Comment) { + return false + } + } + + return true + }()) } func TestConvertColumn(t *testing.T) { diff --git a/tools/goctl/model/sql/util/newline.go b/tools/goctl/model/sql/util/newline.go new file mode 100644 index 00000000..b373b991 --- /dev/null +++ b/tools/goctl/model/sql/util/newline.go @@ -0,0 +1,9 @@ +package util + +import "strings" + +func TrimNewLine(s string) string { + s = strings.ReplaceAll(s, "\r", "") + s = strings.ReplaceAll(s, "\n", "") + return s +}