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:
Keson
2020-08-19 10:41:19 +08:00
committed by GitHub
parent 1252bd9cde
commit d21d770b5b
64 changed files with 1505 additions and 2306 deletions

View File

@@ -1,51 +1,47 @@
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/core/collection"
"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 genDelete(table *InnerTable) (string, error) {
t, err := template.New("delete").Parse(sqltemplate.Delete)
if err != nil {
return "", nil
}
deleteBuffer := new(bytes.Buffer)
keys := make([]string, 0)
keyValues := make([]string, 0)
for snake, key := range table.CacheKey {
if snake == table.PrimaryField.SnakeCase {
keys = append(keys, key.Key)
func genDelete(table Table, withCache bool) (string, error) {
keySet := collection.NewSet()
keyVariableSet := collection.NewSet()
for fieldName, key := range table.CacheKey {
if fieldName == table.PrimaryKey.Name.Source() {
keySet.AddStr(key.KeyExpression)
} else {
keys = append(keys, key.DataKey)
keySet.AddStr(key.DataKeyExpression)
}
keyValues = append(keyValues, key.KeyVariable)
keyVariableSet.AddStr(key.Variable)
}
var isOnlyPrimaryKeyCache = true
var containsIndexCache = false
for _, item := range table.Fields {
if item.IsPrimaryKey {
continue
}
if item.Cache {
isOnlyPrimaryKeyCache = false
if item.IsKey {
containsIndexCache = true
break
}
}
err = t.Execute(deleteBuffer, map[string]interface{}{
"upperObject": table.UpperCamelCase,
"containsCache": table.ContainsCache,
"isNotPrimaryKey": !isOnlyPrimaryKeyCache,
"lowerPrimaryKey": table.PrimaryField.LowerCamelCase,
"dataType": table.PrimaryField.DataType,
"keys": strings.Join(keys, "\r\n"),
"snakePrimaryKey": table.PrimaryField.SnakeCase,
"keyValues": strings.Join(keyValues, ", "),
})
camel := table.Name.Snake2Camel()
output, err := templatex.With("delete").
Parse(template.Delete).
Execute(map[string]interface{}{
"upperStartCamelObject": camel,
"withCache": withCache,
"containsIndexCache": containsIndexCache,
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.Snake2Camel()).LowerStart(),
"dataType": table.PrimaryKey.DataType,
"keys": strings.Join(keySet.KeysStr(), "\n"),
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
})
if err != nil {
return "", err
}
return deleteBuffer.String(), nil
return output.String(), nil
}