-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
108 lines (97 loc) · 2.84 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gen
import (
"sort"
"strings"
"github.com/zeromicro/go-zero/core/collection"
"github.com/jageros/goctl/model/sql/template"
"github.com/jageros/goctl/util"
"github.com/jageros/goctl/util/pathx"
"github.com/jageros/goctl/util/stringx"
)
func genUpdate(table Table, withCache, postgreSql bool) (
string, string, error,
) {
expressionValues := make([]string, 0)
pkg := "data."
if table.ContainsUniqueCacheKey {
pkg = "newData."
}
for _, field := range table.Fields {
camel := util.SafeString(field.Name.ToCamel())
if table.isIgnoreColumns(field.Name.Source()) {
continue
}
if field.Name.Source() == table.PrimaryKey.Name.Source() {
continue
}
expressionValues = append(expressionValues, pkg+camel)
}
keySet := collection.NewSet()
keyVariableSet := collection.NewSet()
keySet.AddStr(table.PrimaryCacheKey.DataKeyExpression)
keyVariableSet.AddStr(table.PrimaryCacheKey.KeyLeft)
for _, key := range table.UniqueCacheKey {
keySet.AddStr(key.DataKeyExpression)
keyVariableSet.AddStr(key.KeyLeft)
}
keys := keySet.KeysStr()
sort.Strings(keys)
keyVars := keyVariableSet.KeysStr()
sort.Strings(keyVars)
if postgreSql {
expressionValues = append(
[]string{pkg + table.PrimaryKey.Name.ToCamel()},
expressionValues...,
)
} else {
expressionValues = append(
expressionValues, pkg+table.PrimaryKey.Name.ToCamel(),
)
}
camelTableName := table.Name.ToCamel()
text, err := pathx.LoadTemplate(category, updateTemplateFile, template.Update)
if err != nil {
return "", "", err
}
output, err := util.With("update").Parse(text).Execute(
map[string]any{
"withCache": withCache,
"containsIndexCache": table.ContainsUniqueCacheKey,
"upperStartCamelObject": camelTableName,
"keys": strings.Join(keys, "\n"),
"keyValues": strings.Join(keyVars, ", "),
"primaryCacheKey": table.PrimaryCacheKey.DataKeyExpression,
"primaryKeyVariable": table.PrimaryCacheKey.KeyLeft,
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
"upperStartCamelPrimaryKey": util.EscapeGolangKeyword(
stringx.From(table.PrimaryKey.Name.ToCamel()).Title(),
),
"originalPrimaryKey": wrapWithRawString(
table.PrimaryKey.Name.Source(), postgreSql,
),
"expressionValues": strings.Join(
expressionValues, ", ",
),
"postgreSql": postgreSql,
"data": table,
},
)
if err != nil {
return "", "", nil
}
// update interface method
text, err = pathx.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod)
if err != nil {
return "", "", err
}
updateMethodOutput, err := util.With("updateMethod").Parse(text).Execute(
map[string]any{
"upperStartCamelObject": camelTableName,
"data": table,
},
)
if err != nil {
return "", "", nil
}
return output.String(), updateMethodOutput.String(), nil
}