升级生成dao的工具

This commit is contained in:
白茶清欢 2023-02-28 16:18:20 +08:00
parent 0dd215a1b9
commit e2aed27ba7
4 changed files with 27 additions and 7 deletions

View File

@ -16,11 +16,11 @@ import (
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:15 上午 2021/11/17
func GenerateDao(sql string, packageName string) (string, error) {
func GenerateDao(sql string, packageName string, withGetTableNameFunc bool) (string, error) {
if len(packageName) == 0 {
packageName = "dao"
}
dataStruct, basic, err := ParseCreateTableSql(sql)
dataStruct, basic, err := ParseCreateTableSql(sql, withGetTableNameFunc)
if nil != err {
return "", err
}

View File

@ -19,5 +19,5 @@ import (
// Date : 12:05 下午 2021/11/17
func TestGenerateDao(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(GenerateDao(sql, "sql2go"))
fmt.Println(GenerateDao(sql, "sql2go", true))
}

View File

@ -18,7 +18,7 @@ import (
const (
// CreateSQLColumnTPL 每个字段的模版
CreateSQLColumnTPL = "{FIELD} {TYPE} `json:\"{JSON_TAG}\" gorm:\"column:{COLUMN};default:{DEFAULT_VALUE};{NOT_NULL}\"` // {COMMENT}"
CreateSQLColumnTPL = " {FIELD} {TYPE} `json:\"{JSON_TAG}\" gorm:\"column:{COLUMN};default:{DEFAULT_VALUE};{NOT_NULL}\"` // {COMMENT}"
)
// BasicTableInfo ...
@ -38,7 +38,7 @@ type BasicTableInfo struct {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:49 下午 2021/10/25
func ParseCreateTableSql(sql string) (string, *BasicTableInfo, error) {
func ParseCreateTableSql(sql string, withGetTableFunc bool) (string, *BasicTableInfo, error) {
var (
stmt sqlparser.Statement
err error
@ -94,5 +94,25 @@ func ParseCreateTableSql(sql string) (string, *BasicTableInfo, error) {
structResult += val + "\n"
}
structResult = structResult + "}"
if withGetTableFunc {
// 生成表名称获取方法
tableFirst := string([]byte(basic.TableName)[:1])
funcTpl := `
// TableName 获取表名称
func ({{TABLE_FIRST}} {{TABLE_STRUCT_NAME}}) TableName() string {
return "{{TABLE_NAME}}"
}`
replaceTable := map[string]string{
"{{TABLE_FIRST}}": tableFirst,
"{{TABLE_STRUCT_NAME}}": basic.ModelStruct,
"{{TABLE_NAME}}": basic.TableName,
}
for k, v := range replaceTable {
funcTpl = strings.ReplaceAll(funcTpl, k, v)
}
structResult = structResult + funcTpl
}
return structResult, basic, nil
}

View File

@ -14,10 +14,10 @@ import (
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))
fmt.Println(ParseCreateTableSql(sql, true))
}
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))
fmt.Println(ParseCreateTableSql(sql, true))
}