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 +}