insert.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package gen
  2. import (
  3. "strings"
  4. "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
  5. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  6. "github.com/tal-tech/go-zero/tools/goctl/util/templatex"
  7. )
  8. func genInsert(table Table, withCache bool) (string, error) {
  9. expressions := make([]string, 0)
  10. expressionValues := make([]string, 0)
  11. for _, filed := range table.Fields {
  12. camel := filed.Name.Snake2Camel()
  13. if camel == "CreateTime" || camel == "UpdateTime" {
  14. continue
  15. }
  16. if filed.IsPrimaryKey && table.PrimaryKey.AutoIncrement {
  17. continue
  18. }
  19. expressions = append(expressions, "?")
  20. expressionValues = append(expressionValues, "data."+camel)
  21. }
  22. camel := table.Name.Snake2Camel()
  23. output, err := templatex.With("insert").
  24. Parse(template.Insert).
  25. Execute(map[string]interface{}{
  26. "withCache": withCache,
  27. "upperStartCamelObject": camel,
  28. "lowerStartCamelObject": stringx.From(camel).LowerStart(),
  29. "expression": strings.Join(expressions, ", "),
  30. "expressionValues": strings.Join(expressionValues, ", "),
  31. })
  32. if err != nil {
  33. return "", err
  34. }
  35. return output.String(), nil
  36. }