mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 16:30:01 +08:00
reactor sql generation
This commit is contained in:
@@ -13,7 +13,9 @@ 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.KeyExpression)
|
||||
}
|
||||
if fieldName != table.PrimaryKey.Name.Source() {
|
||||
keySet.AddStr(key.DataKeyExpression)
|
||||
}
|
||||
@@ -33,7 +35,7 @@ func genDelete(table Table, withCache bool) (string, error) {
|
||||
"upperStartCamelObject": camel,
|
||||
"withCache": withCache,
|
||||
"containsIndexCache": containsIndexCache,
|
||||
"lowerStartCamelPrimaryKey": stringx.From(camel).LowerStart(),
|
||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.Snake2Camel()).LowerStart(),
|
||||
"dataType": table.PrimaryKey.DataType,
|
||||
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||
|
||||
@@ -21,13 +21,18 @@ func genFields(fields []parser.Field) (string, error) {
|
||||
}
|
||||
|
||||
func genField(field parser.Field) (string, error) {
|
||||
tag, err := genTag(field.Name.Source())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
output, err := templatex.With("types").
|
||||
Parse(template.Field).
|
||||
Execute(map[string]string{
|
||||
"name": field.Name.Snake2Camel(),
|
||||
"type": field.DataType,
|
||||
"tag": field.Name.Source(),
|
||||
"comment": field.Comment,
|
||||
Execute(map[string]interface{}{
|
||||
"name": field.Name.Snake2Camel(),
|
||||
"type": field.DataType,
|
||||
"tag": tag,
|
||||
"hasComment": field.Comment != "",
|
||||
"comment": field.Comment,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -14,7 +14,7 @@ func genFineOneByField(table Table, withCache bool) (string, error) {
|
||||
var list []string
|
||||
camelTableName := table.Name.Snake2Camel()
|
||||
for _, field := range table.Fields {
|
||||
if field.IsPrimaryKey {
|
||||
if field.IsPrimaryKey || !field.IsKey {
|
||||
continue
|
||||
}
|
||||
camelFieldName := field.Name.Snake2Camel()
|
||||
@@ -25,7 +25,7 @@ func genFineOneByField(table Table, withCache bool) (string, error) {
|
||||
"withCache": withCache,
|
||||
"cacheKey": table.CacheKey[field.Name.Source()].KeyExpression,
|
||||
"cacheKeyVariable": table.CacheKey[field.Name.Source()].Variable,
|
||||
"primaryKeyLeft": table.CacheKey[table.Name.Source()].Left,
|
||||
"primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
|
||||
"lowerStartCamelObject": stringx.From(camelTableName).LowerStart(),
|
||||
"lowerStartCamelField": stringx.From(camelFieldName).LowerStart(),
|
||||
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.Snake2Camel(),
|
||||
|
||||
@@ -104,7 +104,10 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
importsCode := genImports()
|
||||
importsCode, err := genImports(withCache)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var table Table
|
||||
table.Table = in
|
||||
table.CacheKey = m
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
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/templatex"
|
||||
)
|
||||
|
||||
func genImports() string {
|
||||
return sqltemplate.Imports
|
||||
func genImports(withCache bool) (string, error) {
|
||||
output, err := templatex.With("import").
|
||||
Parse(template.Imports).
|
||||
Execute(map[string]interface{}{
|
||||
"withCache": withCache,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func genCacheKeys(table parser.Table) (map[string]Key, error) {
|
||||
}
|
||||
camelFieldName := field.Name.Snake2Camel()
|
||||
lowerStartCamelFieldName := stringx.From(camelFieldName).LowerStart()
|
||||
left := fmt.Sprintf("cache%s%sPrefix", camelTableName, camelFieldName)
|
||||
left := fmt.Sprintf("cache%s%sPrefix", lowerStartCamelTableName, camelFieldName)
|
||||
right := fmt.Sprintf("cache#%s#%s#", lowerStartCamelTableName, lowerStartCamelFieldName)
|
||||
variable := fmt.Sprintf("%s%sKey", lowerStartCamelTableName, camelFieldName)
|
||||
m[field.Name.Source()] = Key{
|
||||
@@ -42,9 +42,9 @@ func genCacheKeys(table parser.Table) (map[string]Key, error) {
|
||||
Left: left,
|
||||
Right: right,
|
||||
Variable: variable,
|
||||
KeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("cache#user#id#%s", %s)`, variable, "%s", lowerStartCamelFieldName),
|
||||
DataKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("cache#user#id#%s", data.%s)`, variable, "%s", lowerStartCamelFieldName),
|
||||
RespKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("cache#user#id#%s", resp.%s)`, variable, "%s", lowerStartCamelFieldName),
|
||||
KeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("cache#%s#%s#%s", %s)`, variable, lowerStartCamelTableName, lowerStartCamelFieldName, "%v", lowerStartCamelFieldName),
|
||||
DataKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("cache#%s#%s#%s", data.%s)`, variable, lowerStartCamelTableName, lowerStartCamelFieldName, "%v", camelFieldName),
|
||||
RespKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("cache#%s#%s#%s", resp.%s)`, variable, lowerStartCamelTableName, lowerStartCamelFieldName, "%v", camelFieldName),
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
|
||||
Reference in New Issue
Block a user