database/sql2go/generate_dao.go

56 lines
1.3 KiB
Go

// Package sql2go...
//
// Description : sql2go...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-17 10:14 上午
package sql2go
import (
"strings"
)
// GenerateDao 根据sql自动生成dao
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:15 上午 2021/11/17
func GenerateDao(sql string, packageName string, withGetTableNameFunc bool) (string, error) {
if len(packageName) == 0 {
packageName = "dao"
}
basic, err := ParseCreateTableSql(sql)
if nil != err {
return "", err
}
replaceTable := map[string]string{
"{PACKAGE}": packageName,
"{DATA_STRUCT_DAO}": basic.ModelStructName + "Dao",
"{DATA_STRUCT_NAME}": basic.ModelStruct,
"{DATA_STRUCT}": basic.ModelStruct,
"{DAO_RECEIVER}": getDaoReceiver(basic.TableName),
"{DB_TABLE_NAME}": basic.TableName,
"{PRIMARY_KEY}": "ID",
"{PRIMARY_KEY_TYPE}": basic.PrimaryFieldType,
}
tpl := DaoTpl
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
}