Gozero sqlgen patch (#119)
* merge upstream * optimize insert logic * reactor functions
This commit is contained in:
@@ -20,20 +20,14 @@ func genDelete(table Table, withCache bool) (string, error) {
|
||||
}
|
||||
keyVariableSet.AddStr(key.Variable)
|
||||
}
|
||||
var containsIndexCache = false
|
||||
for _, item := range table.Fields {
|
||||
if item.IsUniqueKey {
|
||||
containsIndexCache = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
camel := table.Name.ToCamel()
|
||||
output, err := util.With("delete").
|
||||
Parse(template.Delete).
|
||||
Execute(map[string]interface{}{
|
||||
"upperStartCamelObject": camel,
|
||||
"withCache": withCache,
|
||||
"containsIndexCache": containsIndexCache,
|
||||
"containsIndexCache": table.ContainsUniqueKey,
|
||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
|
||||
"dataType": table.PrimaryKey.DataType,
|
||||
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||
)
|
||||
|
||||
func genFindOneByField(table Table, withCache bool) (string, error) {
|
||||
func genFindOneByField(table Table, withCache bool) (string, string, error) {
|
||||
t := util.With("findOneByField").Parse(template.FindOneByField)
|
||||
var list []string
|
||||
camelTableName := table.Name.ToCamel()
|
||||
@@ -30,12 +30,23 @@ func genFindOneByField(table Table, withCache bool) (string, error) {
|
||||
"lowerStartCamelField": stringx.From(camelFieldName).UnTitle(),
|
||||
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
|
||||
"originalField": field.Name.Source(),
|
||||
"originalPrimaryField": table.PrimaryKey.Name.Source(),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
list = append(list, output.String())
|
||||
}
|
||||
return strings.Join(list, "\n"), nil
|
||||
if withCache {
|
||||
out, err := util.With("findOneByFieldExtraMethod").Parse(template.FindOneByFieldExtraMethod).Execute(map[string]interface{}{
|
||||
"upperStartCamelObject": camelTableName,
|
||||
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
|
||||
"originalPrimaryField": table.PrimaryKey.Name.Source(),
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return strings.Join(list, "\n"), out.String(), nil
|
||||
}
|
||||
return strings.Join(list, "\n"), "", nil
|
||||
|
||||
}
|
||||
|
||||
@@ -113,7 +113,8 @@ func (g *defaultGenerator) genFromDDL(withCache bool) (map[string]string, error)
|
||||
type (
|
||||
Table struct {
|
||||
parser.Table
|
||||
CacheKey map[string]Key
|
||||
CacheKey map[string]Key
|
||||
ContainsUniqueKey bool
|
||||
}
|
||||
)
|
||||
|
||||
@@ -135,6 +136,14 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
var table Table
|
||||
table.Table = in
|
||||
table.CacheKey = m
|
||||
var containsUniqueCache = false
|
||||
for _, item := range table.Fields {
|
||||
if item.IsUniqueKey {
|
||||
containsUniqueCache = true
|
||||
break
|
||||
}
|
||||
}
|
||||
table.ContainsUniqueKey = containsUniqueCache
|
||||
|
||||
varsCode, err := genVars(table, withCache)
|
||||
if err != nil {
|
||||
@@ -162,7 +171,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
return "", err
|
||||
}
|
||||
|
||||
findOneByFieldCode, err := genFindOneByField(table, withCache)
|
||||
findOneByFieldCode, extraMethod, err := genFindOneByField(table, withCache)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -179,14 +188,15 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
}
|
||||
|
||||
output, err := t.Execute(map[string]interface{}{
|
||||
"imports": importsCode,
|
||||
"vars": varsCode,
|
||||
"types": typesCode,
|
||||
"new": newCode,
|
||||
"insert": insertCode,
|
||||
"find": strings.Join(findCode, "\n"),
|
||||
"update": updateCode,
|
||||
"delete": deleteCode,
|
||||
"imports": importsCode,
|
||||
"vars": varsCode,
|
||||
"types": typesCode,
|
||||
"new": newCode,
|
||||
"insert": insertCode,
|
||||
"find": strings.Join(findCode, "\n"),
|
||||
"update": updateCode,
|
||||
"delete": deleteCode,
|
||||
"extraMethod": extraMethod,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -3,12 +3,23 @@ package gen
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||
)
|
||||
|
||||
func genInsert(table Table, withCache bool) (string, error) {
|
||||
keySet := collection.NewSet()
|
||||
keyVariableSet := collection.NewSet()
|
||||
for fieldName, key := range table.CacheKey {
|
||||
if fieldName == table.PrimaryKey.Name.Source() {
|
||||
continue
|
||||
}
|
||||
keySet.AddStr(key.DataKeyExpression)
|
||||
keyVariableSet.AddStr(key.Variable)
|
||||
}
|
||||
|
||||
expressions := make([]string, 0)
|
||||
expressionValues := make([]string, 0)
|
||||
for _, filed := range table.Fields {
|
||||
@@ -27,10 +38,13 @@ func genInsert(table Table, withCache bool) (string, error) {
|
||||
Parse(template.Insert).
|
||||
Execute(map[string]interface{}{
|
||||
"withCache": withCache,
|
||||
"containsIndexCache": table.ContainsUniqueKey,
|
||||
"upperStartCamelObject": camel,
|
||||
"lowerStartCamelObject": stringx.From(camel).UnTitle(),
|
||||
"expression": strings.Join(expressions, ", "),
|
||||
"expressionValues": strings.Join(expressionValues, ", "),
|
||||
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
||||
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
Reference in New Issue
Block a user