func support
支持 函数导出。支持外键关联
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/config"
|
||||
"github.com/xxjwxc/gormt/data/view/cnf"
|
||||
"github.com/xxjwxc/gormt/data/view/genfunc"
|
||||
"github.com/xxjwxc/public/mybigcamel"
|
||||
)
|
||||
|
||||
@@ -89,3 +93,94 @@ func getGormModelElement() []EmInfo {
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
func buildFList(list *[]FList, key ColumusKey, keyName, tp, colName string) {
|
||||
for i := 0; i < len(*list); i++ {
|
||||
if (*list)[i].KeyName == keyName {
|
||||
(*list)[i].Kem = append((*list)[i].Kem, FEm{
|
||||
Type: tp,
|
||||
ColName: colName,
|
||||
ColStructName: getCamelName(colName),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
// 没有 添加一个
|
||||
*list = append(*list, FList{
|
||||
Key: key,
|
||||
KeyName: keyName,
|
||||
Kem: []FEm{FEm{
|
||||
Type: tp,
|
||||
ColName: colName,
|
||||
ColStructName: getCamelName(colName),
|
||||
}},
|
||||
})
|
||||
}
|
||||
|
||||
// GenPreloadList 生成list
|
||||
func GenPreloadList(list []PreloadInfo, multi bool) string {
|
||||
if len(list) > 0 {
|
||||
tmpl, err := template.New("gen_preload").Parse(genfunc.GetGenPreloadTemp(multi))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
tmpl.Execute(&buf, list)
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// GenFListIndex 生成list status(1:获取函数名,2:获取参数列表,3:获取sql case,4:值列表)
|
||||
func GenFListIndex(info FList, status int) string {
|
||||
switch status {
|
||||
case 1: // 1:获取函数名
|
||||
{
|
||||
return widthFunctionName(info)
|
||||
}
|
||||
case 2: // 2:获取参数列表
|
||||
{
|
||||
var strs []string
|
||||
for _, v := range info.Kem {
|
||||
strs = append(strs, fmt.Sprintf("%v %v ", v.ColStructName, v.Type))
|
||||
}
|
||||
return strings.Join(strs, ",")
|
||||
}
|
||||
case 3: // 3:获取sql case,
|
||||
{
|
||||
var strs []string
|
||||
for _, v := range info.Kem {
|
||||
strs = append(strs, fmt.Sprintf("%v = ?", v.ColName))
|
||||
}
|
||||
return strings.Join(strs, " AND ")
|
||||
}
|
||||
case 4: // 4:值列表
|
||||
{
|
||||
var strs []string
|
||||
for _, v := range info.Kem {
|
||||
strs = append(strs, v.ColStructName)
|
||||
}
|
||||
return strings.Join(strs, " , ")
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func widthFunctionName(info FList) string {
|
||||
switch info.Key {
|
||||
// case ColumusKeyDefault:
|
||||
case ColumusKeyPrimary: // primary key.主键
|
||||
return "FetchByPrimaryKey"
|
||||
case ColumusKeyUnique: // unique key.唯一索引
|
||||
return "FetchByUnique"
|
||||
case ColumusKeyIndex: // index key.复合索引
|
||||
return "FetchBy" + getCamelName(info.KeyName) + "Index"
|
||||
case ColumusKeyUniqueIndex: // unique index key.唯一复合索引
|
||||
return "FetchBy" + getCamelName(info.KeyName) + "UniqueIndex"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -82,19 +82,37 @@ type PreloadInfo struct {
|
||||
}
|
||||
|
||||
// EmInfo func 表结构定义
|
||||
|
||||
// FEm ...
|
||||
type FEm struct {
|
||||
Type string // 类型
|
||||
ColName string // 列名
|
||||
ColStructName string // 列结构体
|
||||
}
|
||||
|
||||
// FList index of list
|
||||
type FList struct {
|
||||
Key ColumusKey // non_unique of (show keys from [table])
|
||||
KeyName string // key_name of (show keys from [table])
|
||||
Kem []FEm
|
||||
}
|
||||
|
||||
// EmInfo element of func info
|
||||
type EmInfo struct {
|
||||
IsMulti bool
|
||||
Notes string // 注释
|
||||
Type string // 类型
|
||||
ColName string // 表名
|
||||
ColStructName string // 表结构体
|
||||
ColName string // 列名
|
||||
ColStructName string // 列结构体
|
||||
}
|
||||
|
||||
type funDef struct {
|
||||
StructName string
|
||||
TableName string
|
||||
PreloadList []PreloadInfo
|
||||
Em []EmInfo
|
||||
PreloadList []PreloadInfo // 外键列表,(生成关联数据)
|
||||
Em []EmInfo // index 列表
|
||||
Primay []FList // primay unique
|
||||
Index []FList // index
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@@ -33,7 +33,9 @@ func Generate(info DBInfo) (out []GenOutInfo) {
|
||||
// ------end
|
||||
|
||||
// gen function
|
||||
out = append(out, m.generateFunc()...)
|
||||
if config.GetIsOutFunc() {
|
||||
out = append(out, m.generateFunc()...)
|
||||
}
|
||||
// -------------- end
|
||||
return
|
||||
}
|
||||
@@ -213,26 +215,32 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
|
||||
TableName: tab.Name,
|
||||
}
|
||||
|
||||
var primary, unique, uniqueIndex, index []FList
|
||||
for _, el := range tab.Em {
|
||||
if strings.EqualFold(el.Type, "gorm.Model") {
|
||||
data.Em = append(data.Em, getGormModelElement()...)
|
||||
pkg.AddImport(`"time"`)
|
||||
buildFList(&primary, ColumusKeyPrimary, "", "int64", "id")
|
||||
} else {
|
||||
typeName := getTypeName(el.Type)
|
||||
isMulti := true
|
||||
for _, v1 := range el.Index {
|
||||
switch v1.Key {
|
||||
// case ColumusKeyDefault:
|
||||
case ColumusKeyPrimary: // primary key.主键
|
||||
isMulti = false
|
||||
buildFList(&primary, ColumusKeyPrimary, "", typeName, el.Name)
|
||||
case ColumusKeyUnique: // unique key.唯一索引
|
||||
isMulti = false
|
||||
//case ColumusKeyIndex: // index key.复合索引
|
||||
buildFList(&unique, ColumusKeyUnique, "", typeName, el.Name)
|
||||
case ColumusKeyIndex: // index key.复合索引
|
||||
buildFList(&uniqueIndex, ColumusKeyIndex, v1.KeyName, typeName, el.Name)
|
||||
case ColumusKeyUniqueIndex: // unique index key.唯一复合索引
|
||||
isMulti = false
|
||||
buildFList(&index, ColumusKeyUniqueIndex, v1.KeyName, typeName, el.Name)
|
||||
}
|
||||
}
|
||||
|
||||
typeName := getTypeName(el.Type)
|
||||
data.Em = append(data.Em, EmInfo{
|
||||
IsMulti: isMulti,
|
||||
Notes: el.Notes,
|
||||
@@ -263,10 +271,16 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
|
||||
}
|
||||
}
|
||||
// ---------end--
|
||||
|
||||
}
|
||||
|
||||
tmpl, err := template.New("gen_logic").Funcs(template.FuncMap{"GenPreloadList": GenPreloadList}).Parse(genfunc.GetGenLogicTemp())
|
||||
data.Primay = append(data.Primay, primary...)
|
||||
data.Primay = append(data.Primay, unique...)
|
||||
data.Primay = append(data.Primay, uniqueIndex...)
|
||||
data.Index = append(data.Index, index...)
|
||||
|
||||
tmpl, err := template.New("gen_logic").
|
||||
Funcs(template.FuncMap{"GenPreloadList": GenPreloadList, "GenFListIndex": GenFListIndex}).
|
||||
Parse(genfunc.GetGenLogicTemp())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -282,19 +296,3 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GenPreloadList 生成list
|
||||
func GenPreloadList(list []PreloadInfo, multi bool) string {
|
||||
if len(list) > 0 {
|
||||
tmpl, err := template.New("gen_preload").Parse(genfunc.GetGenPreloadTemp(multi))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
tmpl.Execute(&buf, list)
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user