From a212583f41e04b490b8c8194cb98b100759b3373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 17 Nov 2021 12:15:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9F=BA=E4=BA=8Esql?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E5=9F=BA=E7=A1=80dao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/sql2go/dao_tpl.tpl | 59 +++++++++++++++++++++++++++++++++++++ tool/sql2go/generate_dao.go | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 tool/sql2go/dao_tpl.tpl create mode 100644 tool/sql2go/generate_dao.go diff --git a/tool/sql2go/dao_tpl.tpl b/tool/sql2go/dao_tpl.tpl new file mode 100644 index 0000000..be7f268 --- /dev/null +++ b/tool/sql2go/dao_tpl.tpl @@ -0,0 +1,59 @@ +// Package {PACKAGE} ... +// +// Description : {PACKAGE} ... +package {PACKAGE} + +import ( + "gorm.io/gorm" + "errors" +) + +// {DATA_STRUCT_NAME} 数据库表的数据结构 +{DATA_STRUCT} + +// New{DATA_STRUCT_DAO} 获取DAO实例 +func New{DATA_STRUCT_DAO}() *{DATA_STRUCT_DAO} { + return &{DATA_STRUCT_DAO}{ + table : "{DB_TABLE_NAME}", + } +} + +// {DATA_STRUCT_DAO} sql2go tool generate +type {DATA_STRUCT_DAO} struct { + table string +} + +// GetDetailBy{PRIMARY_KEY} 根据主键ID获取详情 +func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetDetailBy{PRIMARY_KEY}(dbInstance *gorm.DB, {PRIMARY_KEY} {PRIMARY_KEY_TYPE}) (*{DATA_STRUCT_NAME}, error) { + var ( + err error + detail {DATA_STRUCT_NAME} + ) + if err = dbInstance.Table({DAO_RECEIVER}.table).Where("{PRIMARY_KEY_FIELD} = ?", {PRIMARY_KEY}).Limit(1).First(&detail).Error; nil != err { + return nil, err + } + return &detail, nil +} + +// GetList 获取数据列表 +func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetList(dbInstance *gorm.DB, condition map[string]interface{}, limit int, offset int) ([]{DATA_STRUCT_NAME}, error) { + if nil == condition { + condition = make(map[string]interface{}) + } + var ( + err error + list []{DATA_STRUCT_NAME} + ) + if err = dbInstance.Table({DAO_RECEIVER}.table).Where(condition).Limit(limit).Offset(offset).Find(&list).Error; nil != err { + return make([]{DATA_STRUCT_NAME}, 0), err + } + return list, nil +} + +// Create 创建数据 +func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) Create(dbInstance *gorm.DB, data *{DATA_STRUCT_NAME}) error { + if nil == data { + return errors.New("data is nil") + } + return dbInstance.Table({DAO_RECEIVER}.table).Create(data).Error +} diff --git a/tool/sql2go/generate_dao.go b/tool/sql2go/generate_dao.go new file mode 100644 index 0000000..806189f --- /dev/null +++ b/tool/sql2go/generate_dao.go @@ -0,0 +1,59 @@ +// Package sql2go... +// +// Description : sql2go... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-11-17 10:14 上午 +package sql2go + +import ( + "strings" + + "git.zhangdeman.cn/zhangdeman/gopkg/util" +) + +// GenerateDao 根据sql自动生成dao +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:15 上午 2021/11/17 +func GenerateDao(sql string, packageName string) (string, error) { + if len(packageName) == 0 { + packageName = "dao" + } + dataStruct, basic, err := ParseCreateTableSql(sql) + if nil != err { + return "", err + } + // 读入模板 + tplByte, _ := util.ReadFileContent("dao_tpl.tpl") + tpl := string(tplByte) + replaceTable := map[string]string{ + "{PACKAGE}": packageName, + "{DATA_STRUCT_DAO}": basic.ModelStruct + "Dao", + "{DATA_STRUCT_NAME}": basic.ModelStruct, + "{DATA_STRUCT}": dataStruct, + "{DAO_RECEIVER}": getDaoReceiver(basic.TableName), + "{DB_TABLE_NAME}": basic.TableName, + "{PRIMARY_KEY}": "ID", + "{PRIMARY_KEY_TYPE}": basic.PrimaryFieldType, + } + + for oldVal, newVal := range replaceTable { + tpl = strings.ReplaceAll(tpl, oldVal, newVal) + } + return tpl, nil +} + +func getDaoReceiver(tableName string) string { + nameArr := strings.Split(tableName, "_") + result := "" + for _, item := range nameArr { + if len(item) > 0 { + result += string([]byte(item)[0]) + } + } + result += "d" + return result +}