-
Notifications
You must be signed in to change notification settings - Fork 4
/
seeddata.go
185 lines (174 loc) · 4.78 KB
/
seeddata.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package sql
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/samber/lo"
"golang.org/x/exp/slices"
"projectforge.dev/projectforge/app/file"
"projectforge.dev/projectforge/app/lib/types"
"projectforge.dev/projectforge/app/project/export/golang"
"projectforge.dev/projectforge/app/project/export/model"
"projectforge.dev/projectforge/app/util"
)
const (
nilStr = "<nil>"
nullStr = "null"
)
func SeedData(m *model.Model, _ *model.Args) (*file.File, error) {
g := golang.NewGoTemplate([]string{"queries", "seeddata"}, fmt.Sprintf("seed_%s.sql", m.Name))
seed, err := sqlSeedData(m)
if err != nil {
return nil, err
}
g.AddBlocks(seed)
return g.Render(false)
}
func sqlSeedData(m *model.Model) (*golang.Block, error) {
ret := golang.NewBlock("SQLCreate", "sql")
ret.W("-- {%% func " + m.Proper() + "SeedData() %%}")
if m.History == model.RevisionType {
err := sqlSeedDataColumns(m, ret, m.Name, m.HistoryColumns(true).Const)
if err != nil {
return nil, err
}
ret.WB()
err = sqlSeedDataColumns(m, ret, m.Name+"_"+m.HistoryColumn().Name, m.HistoryColumns(true).Var)
if err != nil {
return nil, err
}
} else {
err := sqlSeedDataColumns(m, ret, m.Name, m.Columns)
if err != nil {
return nil, err
}
}
ret.W("-- {%% endfunc %%}")
return ret, nil
}
//nolint:gocognit
func sqlSeedDataColumns(m *model.Model, block *golang.Block, tableName string, cols model.Columns) error {
block.W("insert into %q (", tableName)
block.W(" " + strings.Join(cols.NamesQuoted(), ", "))
block.W(") values (")
for idx, row := range m.SeedData {
if len(row) != len(m.Columns) {
return errors.Errorf("seed data row [%d] expected [%d] columns, but only [%d] were provided", idx+1, len(m.Columns), len(row))
}
var vs []string
for _, col := range cols {
colIdx := slices.IndexFunc(m.Columns, func(c *model.Column) bool {
return col.Name == c.Name
})
if colIdx == -1 && strings.HasPrefix(col.Name, m.Name+"_") {
trimmed := strings.TrimPrefix(col.Name, m.Name+"_")
colIdx = slices.IndexFunc(m.Columns, func(c *model.Column) bool {
return trimmed == c.Name
})
}
if colIdx == -1 && strings.HasPrefix(col.Name, "current_") {
trimmed := strings.TrimPrefix(col.Name, "current_")
colIdx = slices.IndexFunc(m.Columns, func(c *model.Column) bool {
return trimmed == c.Name
})
}
if colIdx == -1 {
return errors.Errorf("unable to find column [%s]", col.Name)
}
cell := row[colIdx]
cellStr := fmt.Sprint(cell)
switch col.Type.Key() {
case types.KeyString, types.KeyEnum:
vs = append(vs, processString(cellStr, "''"))
case types.KeyDate, types.KeyTimestamp:
if cellStr == nilStr {
vs = append(vs, nullStr)
} else if _, err := util.TimeFromString(cellStr); err == nil {
vs = append(vs, processString(cellStr, "''"))
} else {
vs = append(vs, cellStr)
}
case types.KeyUUID:
vs = append(vs, processString(cellStr, "'00000000-0000-0000-0000-000000000000'"))
case types.KeyList:
vs = append(vs, processList(cell, cellStr))
case types.KeyInt:
if cellStr == nilStr {
vs = append(vs, "0")
continue
}
vs = append(vs, fmt.Sprintf("%.0f", cell))
case types.KeyFloat:
if cellStr == nilStr {
vs = append(vs, "0")
continue
}
vs = append(vs, fmt.Sprintf("%f", cell))
case types.KeyMap, types.KeyValueMap, types.KeyReference:
if cellStr == nilStr {
vs = append(vs, nullStr)
continue
}
switch cell.(type) {
case string:
vs = append(vs, "'"+cellStr+"'")
default:
vs = append(vs, "'"+util.ToJSONCompact(cell)+"'")
}
default:
if cellStr == nilStr {
vs = append(vs, nullStr)
continue
}
vs = append(vs, cellStr)
}
}
block.W(" " + strings.Join(vs, ", "))
if idx < len(m.SeedData)-1 {
block.W("), (")
}
}
block.W(") on conflict do nothing;")
return nil
}
func processString(cellStr string, dflt string) string {
if cellStr == nilStr {
return dflt
}
return "'" + clean(cellStr) + "'"
}
func processList(cell any, cellStr string) string {
if cellStr == nilStr {
return "'[]'"
}
a, ok := cell.([]any)
if !ok {
s, ok := cell.([]string)
if ok {
a = util.InterfaceArrayFrom(s...)
} else {
str, ok := cell.(string)
if ok {
return "'" + str + "'"
}
return "'[\"error:invalid_type\"]'"
}
}
ret := make([]string, 0, len(a))
lo.ForEach(a, func(x any, _ int) {
switch t := x.(type) {
case string:
ret = append(ret, "\""+t+"\"")
default:
ret = append(ret, fmt.Sprint(t))
}
})
return "'[" + clean(strings.Join(ret, ", ")) + "]'"
}
var cleanRpl *strings.Replacer
func clean(s string) string {
if cleanRpl == nil {
cleanRpl = strings.NewReplacer("'", "''", "\f", "", "\v", "", "\u0000", "", "\u0082", "", "%", "%%", "{{{", "{ {{")
}
return cleanRpl.Replace(s)
}