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/update.go

39 lines
1.2 KiB
Go

4 years ago
package gen
import (
"bytes"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
4 years ago
)
func genUpdate(table *InnerTable) (string, error) {
t, err := template.New("update").Parse(sqltemplate.Update)
if err != nil {
return "", nil
}
updateBuffer := new(bytes.Buffer)
expressionValues := make([]string, 0)
for _, filed := range table.Fields {
if filed.SnakeCase == "create_time" || filed.SnakeCase == "update_time" || filed.IsPrimaryKey {
continue
}
expressionValues = append(expressionValues, "data."+filed.UpperCamelCase)
}
expressionValues = append(expressionValues, "data."+table.PrimaryField.UpperCamelCase)
err = t.Execute(updateBuffer, map[string]interface{}{
"containsCache": table.ContainsCache,
"upperObject": table.UpperCamelCase,
"primaryCacheKey": table.CacheKey[table.PrimaryField.SnakeCase].DataKey,
"primaryKeyVariable": table.CacheKey[table.PrimaryField.SnakeCase].KeyVariable,
"lowerObject": table.LowerCamelCase,
"primarySnakeCase": table.PrimaryField.SnakeCase,
"expressionValues": strings.Join(expressionValues, ", "),
})
if err != nil {
return "", err
}
return updateBuffer.String(), nil
}