增加生成dao的模版
This commit is contained in:
parent
f9d322e453
commit
da6e156f11
@ -10,6 +10,8 @@ package mysql
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.zhangdeman.cn/zhangdeman/mysql/sql2go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -42,5 +44,6 @@ func init() {
|
|||||||
// Date : 11:24 2022/5/16
|
// Date : 11:24 2022/5/16
|
||||||
func TestGetCreateTableSQL(t *testing.T) {
|
func TestGetCreateTableSQL(t *testing.T) {
|
||||||
sys := &SystemDao{}
|
sys := &SystemDao{}
|
||||||
fmt.Println(sys.GetCreateTableSQL(testDBClient.GetMaster(nil), "app"))
|
sql, _ := sys.GetCreateTableSQL(testDBClient.GetMaster(nil), "app")
|
||||||
|
fmt.Println(sql2go.GenerateDao(sql, "aaa"))
|
||||||
}
|
}
|
||||||
|
@ -49,3 +49,67 @@ var sqlTypeMap = map[string]string{
|
|||||||
"binary": "string",
|
"binary": "string",
|
||||||
"varbinary": "string",
|
"varbinary": "string",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DaoTpl 模版
|
||||||
|
DaoTpl = `// 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
|
||||||
|
}
|
||||||
|
`
|
||||||
|
)
|
||||||
|
@ -9,8 +9,6 @@ package sql2go
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GenerateDao 根据sql自动生成dao
|
// GenerateDao 根据sql自动生成dao
|
||||||
@ -26,9 +24,6 @@ func GenerateDao(sql string, packageName string) (string, error) {
|
|||||||
if nil != err {
|
if nil != err {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// 读入模板
|
|
||||||
tplByte, _ := util.File.ReadFileContent("dao_tpl.tpl")
|
|
||||||
tpl := string(tplByte)
|
|
||||||
replaceTable := map[string]string{
|
replaceTable := map[string]string{
|
||||||
"{PACKAGE}": packageName,
|
"{PACKAGE}": packageName,
|
||||||
"{DATA_STRUCT_DAO}": basic.ModelStruct + "Dao",
|
"{DATA_STRUCT_DAO}": basic.ModelStruct + "Dao",
|
||||||
@ -40,6 +35,7 @@ func GenerateDao(sql string, packageName string) (string, error) {
|
|||||||
"{PRIMARY_KEY_TYPE}": basic.PrimaryFieldType,
|
"{PRIMARY_KEY_TYPE}": basic.PrimaryFieldType,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tpl := DaoTpl
|
||||||
for oldVal, newVal := range replaceTable {
|
for oldVal, newVal := range replaceTable {
|
||||||
tpl = strings.ReplaceAll(tpl, oldVal, newVal)
|
tpl = strings.ReplaceAll(tpl, oldVal, newVal)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user