From d85b27cb9cec3d5ebbc8756155994f11fefdd8a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 22 May 2022 23:44:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dsql=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=E6=B2=A1=E6=9C=89COMMENT=E5=AF=BC=E8=87=B4=E7=9A=84panic?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20&&=20=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql2go/parser.go | 8 +++++++- sql2go/parser_test.go | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sql2go/parser.go b/sql2go/parser.go index cb98b54..70791a0 100644 --- a/sql2go/parser.go +++ b/sql2go/parser.go @@ -64,12 +64,18 @@ func ParseCreateTableSql(sql string) (string, *BasicTableInfo, error) { structResult := "type " + basic.ModelStruct + " struct { \n" for _, item := range r.TableSpec.Columns { + comment := "" + if item.Type.Comment == nil { + comment = item.Name.String() + } else { + comment = string(item.Type.Comment.Val) + } data := map[string]string{ "{FIELD}": util.String.SnakeCaseToCamel(item.Name.String()), "{COLUMN}": item.Name.String(), "{JSON_TAG}": item.Name.String(), "{DEFAULT_VALUE}": "", - "{COMMENT}": item.Name.String() + " " + string(item.Type.Comment.Val), + "{COMMENT}": comment, "{TYPE}": sqlTypeMap[item.Type.Type], } if data["{FIELD}"] == "ID" { diff --git a/sql2go/parser_test.go b/sql2go/parser_test.go index 34801bb..0492750 100644 --- a/sql2go/parser_test.go +++ b/sql2go/parser_test.go @@ -16,3 +16,8 @@ func TestParseSql(t *testing.T) { sql := "CREATE TABLE `app` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',\n `code` varchar(128) NOT NULL DEFAULT '' COMMENT '分配的app_code',\n `secret` varchar(64) NOT NULL DEFAULT '' COMMENT '分配的私钥',\n `status` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '当前状态 o - 初始化 1- 使用中 2 - 禁用 3 - 删除',\n `description` varchar(256) NOT NULL DEFAULT '' COMMENT '描述信息',\n `apply_user_name` varchar(64) NOT NULL DEFAULT '' COMMENT '申请人姓名',\n `apply_user_contact` varchar(128) NOT NULL DEFAULT '' COMMENT '申请人联系方式',\n `create_user_id` varchar(128) NOT NULL DEFAULT '' COMMENT '创建人ID',\n `modify_user_id` varchar(128) NOT NULL DEFAULT '' COMMENT '修改人ID',\n PRIMARY KEY (`id`),\n UNIQUE KEY `uniq_code` (`code`)\n) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8mb4 COMMENT='APP信息表'" fmt.Println(ParseCreateTableSql(sql)) } + +func TestParseSqlForNoComment(t *testing.T) { + sql := "CREATE TABLE `app` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n `code` varchar(128) NOT NULL DEFAULT '' COMMENT '分配的app_code',\n `secret` varchar(64) NOT NULL DEFAULT '' COMMENT '分配的私钥',\n `status` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '当前状态 o - 初始化 1- 使用中 2 - 禁用 3 - 删除',\n `description` varchar(256) NOT NULL DEFAULT '' COMMENT '描述信息',\n `apply_user_name` varchar(64) NOT NULL DEFAULT '' COMMENT '申请人姓名',\n `apply_user_contact` varchar(128) NOT NULL DEFAULT '' COMMENT '申请人联系方式',\n `create_user_id` varchar(128) NOT NULL DEFAULT '' COMMENT '创建人ID',\n `modify_user_id` varchar(128) NOT NULL DEFAULT '' COMMENT '修改人ID',\n PRIMARY KEY (`id`),\n UNIQUE KEY `uniq_code` (`code`)\n) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8mb4 COMMENT='APP信息表'" + fmt.Println(ParseCreateTableSql(sql)) +}