reactor sql generation

This commit is contained in:
anqiansong
2020-08-12 09:19:37 +08:00
parent 40895ba8d9
commit f226ffb57c
45 changed files with 892 additions and 1045 deletions

View File

@@ -1,36 +1,32 @@
package gen
import (
"bytes"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genVars(table *InnerTable) (string, error) {
t, err := template.New("vars").Parse(sqltemplate.Vars)
if err != nil {
return "", err
}
varBuffer := new(bytes.Buffer)
m, err := genCacheKeys(table)
if err != nil {
return "", err
}
func genVars(table Table) (string, error) {
keys := make([]string, 0)
for _, v := range m {
keys = append(keys, v.Expression)
for _, v := range table.CacheKey {
keys = append(keys, v.VarExpression)
}
err = t.Execute(varBuffer, map[string]interface{}{
"lowerObject": table.LowerCamelCase,
"upperObject": table.UpperCamelCase,
"createNotFound": table.CreateNotFound,
"keysDefine": strings.Join(keys, "\r\n"),
"snakePrimaryKey": table.PrimaryField.SnakeCase,
})
camel := table.Name.Snake2Camel()
output, err := templatex.With("var").
Parse(template.Vars).
GoFmt(true).
Execute(map[string]interface{}{
"lowerStartCamelObject": stringx.From(camel).LowerStart(),
"upperStartCamelObject": camel,
"cacheKeys": strings.Join(keys, "\r\n"),
"autoIncrement": table.PrimaryKey.AutoIncrement,
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
})
if err != nil {
return "", err
}
return varBuffer.String(), nil
return output.String(), nil
}