2020-07-29 17:11:41 +08:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
2021-07-23 11:45:15 +08:00
|
|
|
"fmt"
|
|
|
|
|
2020-08-19 10:41:19 +08:00
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
2020-10-19 23:13:18 +08:00
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
2022-01-03 21:32:40 +08:00
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
2020-07-29 17:11:41 +08:00
|
|
|
)
|
|
|
|
|
2021-07-23 11:45:15 +08:00
|
|
|
func genNew(table Table, withCache, postgreSql bool) (string, error) {
|
2022-01-03 21:32:40 +08:00
|
|
|
text, err := pathx.LoadTemplate(category, modelNewTemplateFile, template.New)
|
2020-10-21 14:59:35 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:45:15 +08:00
|
|
|
t := fmt.Sprintf(`"%s"`, wrapWithRawString(table.Name.Source(), postgreSql))
|
|
|
|
if postgreSql {
|
|
|
|
t = "`" + fmt.Sprintf(`"%s"."%s"`, table.Db.Source(), table.Name.Source()) + "`"
|
|
|
|
}
|
|
|
|
|
2020-10-19 23:13:18 +08:00
|
|
|
output, err := util.With("new").
|
2020-10-21 14:59:35 +08:00
|
|
|
Parse(text).
|
2020-08-19 10:41:19 +08:00
|
|
|
Execute(map[string]interface{}{
|
2021-07-23 11:45:15 +08:00
|
|
|
"table": t,
|
2020-08-19 10:41:19 +08:00
|
|
|
"withCache": withCache,
|
2020-08-20 10:29:18 +08:00
|
|
|
"upperStartCamelObject": table.Name.ToCamel(),
|
2020-08-19 10:41:19 +08:00
|
|
|
})
|
2020-07-29 17:11:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-10-21 14:59:35 +08:00
|
|
|
|
2020-08-19 10:41:19 +08:00
|
|
|
return output.String(), nil
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|