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,51 +1,46 @@
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)
} else {
keys = append(keys, key.DataKey)
func genDelete(table Table, withCache bool) (string, error) {
keySet := collection.NewSet()
keyVariableSet := collection.NewSet()
for fieldName, key := range table.CacheKey {
keySet.AddStr(key.KeyExpression)
if fieldName != table.PrimaryKey.Name.Source() {
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(camel).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
}