支持基于sql自动生成基础dao
This commit is contained in:
parent
90cb23d8c9
commit
a212583f41
59
tool/sql2go/dao_tpl.tpl
Normal file
59
tool/sql2go/dao_tpl.tpl
Normal file
@ -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
|
||||
}
|
59
tool/sql2go/generate_dao.go
Normal file
59
tool/sql2go/generate_dao.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user