2020-07-29 17:11:41 +08:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
2021-07-23 11:45:15 +08:00
|
|
|
"fmt"
|
2022-04-09 00:25:23 +08:00
|
|
|
"sort"
|
2020-07-29 17:11:41 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-01-25 23:15:07 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/collection"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/model/sql/template"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/stringx"
|
2020-07-29 17:11:41 +08:00
|
|
|
)
|
|
|
|
|
2021-07-23 11:45:15 +08:00
|
|
|
func genInsert(table Table, withCache, postgreSql bool) (string, string, error) {
|
2020-10-11 21:55:44 +08:00
|
|
|
keySet := collection.NewSet()
|
|
|
|
keyVariableSet := collection.NewSet()
|
2021-10-29 09:55:41 +08:00
|
|
|
keySet.AddStr(table.PrimaryCacheKey.DataKeyExpression)
|
|
|
|
keyVariableSet.AddStr(table.PrimaryCacheKey.KeyLeft)
|
2021-03-01 17:29:07 +08:00
|
|
|
for _, key := range table.UniqueCacheKey {
|
2020-10-11 21:55:44 +08:00
|
|
|
keySet.AddStr(key.DataKeyExpression)
|
2021-03-01 17:29:07 +08:00
|
|
|
keyVariableSet.AddStr(key.KeyLeft)
|
2020-10-11 21:55:44 +08:00
|
|
|
}
|
2022-04-09 00:25:23 +08:00
|
|
|
keys := keySet.KeysStr()
|
|
|
|
sort.Strings(keys)
|
|
|
|
keyVars := keyVariableSet.KeysStr()
|
|
|
|
sort.Strings(keyVars)
|
2020-10-11 21:55:44 +08:00
|
|
|
|
2020-07-29 17:11:41 +08:00
|
|
|
expressions := make([]string, 0)
|
|
|
|
expressionValues := make([]string, 0)
|
2021-07-23 11:45:15 +08:00
|
|
|
var count int
|
2021-01-13 18:07:31 +08:00
|
|
|
for _, field := range table.Fields {
|
2022-04-11 10:11:40 +08:00
|
|
|
camel := util.SafeString(field.Name.ToCamel())
|
2022-09-19 11:49:39 +08:00
|
|
|
if table.isIgnoreColumns(field.Name.Source()) {
|
2020-08-19 10:41:19 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-03-01 17:29:07 +08:00
|
|
|
|
|
|
|
if field.Name.Source() == table.PrimaryKey.Name.Source() {
|
|
|
|
if table.PrimaryKey.AutoIncrement {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|
2021-03-01 17:29:07 +08:00
|
|
|
|
2021-07-23 11:45:15 +08:00
|
|
|
count += 1
|
|
|
|
if postgreSql {
|
|
|
|
expressions = append(expressions, fmt.Sprintf("$%d", count))
|
|
|
|
} else {
|
|
|
|
expressions = append(expressions, "?")
|
|
|
|
}
|
2020-08-19 10:41:19 +08:00
|
|
|
expressionValues = append(expressionValues, "data."+camel)
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|
2021-03-01 17:29:07 +08:00
|
|
|
|
2020-08-20 10:29:18 +08:00
|
|
|
camel := table.Name.ToCamel()
|
2022-03-01 20:27:59 +08:00
|
|
|
text, err := pathx.LoadTemplate(category, insertTemplateFile, template.Insert)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
2020-10-21 14:59:35 +08:00
|
|
|
}
|
|
|
|
|
2020-10-19 23:13:18 +08:00
|
|
|
output, err := util.With("insert").
|
2020-10-21 14:59:35 +08:00
|
|
|
Parse(text).
|
2023-01-24 17:51:03 +08:00
|
|
|
Execute(map[string]any{
|
2020-08-19 10:41:19 +08:00
|
|
|
"withCache": withCache,
|
|
|
|
"upperStartCamelObject": camel,
|
2020-12-08 23:01:25 +08:00
|
|
|
"lowerStartCamelObject": stringx.From(camel).Untitle(),
|
2020-08-19 10:41:19 +08:00
|
|
|
"expression": strings.Join(expressions, ", "),
|
|
|
|
"expressionValues": strings.Join(expressionValues, ", "),
|
2022-04-09 00:25:23 +08:00
|
|
|
"keys": strings.Join(keys, "\n"),
|
|
|
|
"keyValues": strings.Join(keyVars, ", "),
|
2022-01-18 10:36:38 +08:00
|
|
|
"data": table,
|
2020-08-19 10:41:19 +08:00
|
|
|
})
|
2020-07-29 17:11:41 +08:00
|
|
|
if err != nil {
|
2020-11-24 22:36:23 +08:00
|
|
|
return "", "", err
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|
2020-10-21 14:59:35 +08:00
|
|
|
|
2020-11-24 22:36:23 +08:00
|
|
|
// interface method
|
2022-03-01 20:27:59 +08:00
|
|
|
text, err = pathx.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
2020-11-24 22:36:23 +08:00
|
|
|
}
|
|
|
|
|
2023-01-24 17:51:03 +08:00
|
|
|
insertMethodOutput, err := util.With("insertMethod").Parse(text).Execute(map[string]any{
|
2021-03-01 17:29:07 +08:00
|
|
|
"upperStartCamelObject": camel,
|
2022-01-18 10:36:38 +08:00
|
|
|
"data": table,
|
2021-03-01 17:29:07 +08:00
|
|
|
})
|
2020-11-24 22:36:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.String(), insertMethodOutput.String(), nil
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|