goctl model reactor (#15)
* reactor sql generation * reactor sql generation * add console & example * optimize unit test & add document * modify default config * remove test file * Revert "remove test file" This reverts commit 81041f9e * fix stringx.go & optimize example * remove unused code
This commit is contained in:
@@ -1,38 +1,40 @@
|
||||
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 genUpdate(table *InnerTable) (string, error) {
|
||||
t, err := template.New("update").Parse(sqltemplate.Update)
|
||||
func genUpdate(table Table, withCache bool) (string, error) {
|
||||
expressionValues := make([]string, 0)
|
||||
for _, filed := range table.Fields {
|
||||
camel := filed.Name.Snake2Camel()
|
||||
if camel == "CreateTime" || camel == "UpdateTime" {
|
||||
continue
|
||||
}
|
||||
if filed.IsPrimaryKey {
|
||||
continue
|
||||
}
|
||||
expressionValues = append(expressionValues, "data."+camel)
|
||||
}
|
||||
expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.Snake2Camel())
|
||||
camelTableName := table.Name.Snake2Camel()
|
||||
output, err := templatex.With("update").
|
||||
Parse(template.Update).
|
||||
Execute(map[string]interface{}{
|
||||
"withCache": withCache,
|
||||
"upperStartCamelObject": camelTableName,
|
||||
"primaryCacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].DataKeyExpression,
|
||||
"primaryKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
|
||||
"lowerStartCamelObject": stringx.From(camelTableName).LowerStart(),
|
||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||
"expressionValues": strings.Join(expressionValues, ", "),
|
||||
})
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
updateBuffer := new(bytes.Buffer)
|
||||
expressionValues := make([]string, 0)
|
||||
for _, filed := range table.Fields {
|
||||
if filed.SnakeCase == "create_time" || filed.SnakeCase == "update_time" || filed.IsPrimaryKey {
|
||||
continue
|
||||
}
|
||||
expressionValues = append(expressionValues, "data."+filed.UpperCamelCase)
|
||||
}
|
||||
expressionValues = append(expressionValues, "data."+table.PrimaryField.UpperCamelCase)
|
||||
err = t.Execute(updateBuffer, map[string]interface{}{
|
||||
"containsCache": table.ContainsCache,
|
||||
"upperObject": table.UpperCamelCase,
|
||||
"primaryCacheKey": table.CacheKey[table.PrimaryField.SnakeCase].DataKey,
|
||||
"primaryKeyVariable": table.CacheKey[table.PrimaryField.SnakeCase].KeyVariable,
|
||||
"lowerObject": table.LowerCamelCase,
|
||||
"primarySnakeCase": table.PrimaryField.SnakeCase,
|
||||
"expressionValues": strings.Join(expressionValues, ", "),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return updateBuffer.String(), nil
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user