You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/model/sql/gen/fineonebyfield.go

42 lines
1.5 KiB
Go

4 years ago
package gen
import (
"fmt"
4 years ago
"strings"
"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"
4 years ago
)
func genFineOneByField(table Table, withCache bool) (string, error) {
t := templatex.With("findOneByField").Parse(template.FindOneByField)
var list []string
camelTableName := table.Name.ToCamel()
4 years ago
for _, field := range table.Fields {
if field.IsPrimaryKey || !field.IsKey {
4 years ago
continue
}
camelFieldName := field.Name.ToCamel()
output, err := t.Execute(map[string]interface{}{
"upperStartCamelObject": camelTableName,
"upperField": camelFieldName,
"in": fmt.Sprintf("%s %s", stringx.From(camelFieldName).UnTitle(), field.DataType),
"withCache": withCache,
"cacheKey": table.CacheKey[field.Name.Source()].KeyExpression,
"cacheKeyVariable": table.CacheKey[field.Name.Source()].Variable,
"primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
"lowerStartCamelField": stringx.From(camelFieldName).UnTitle(),
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
"originalField": field.Name.Source(),
"originalPrimaryField": table.PrimaryKey.Name.Source(),
4 years ago
})
if err != nil {
return "", err
}
list = append(list, output.String())
4 years ago
}
return strings.Join(list, "\n"), nil
4 years ago
}