-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathroutines_create.go.tmpl
More file actions
182 lines (159 loc) · 7.7 KB
/
routines_create.go.tmpl
File metadata and controls
182 lines (159 loc) · 7.7 KB
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
func (e *{{ .GoName }}) Insert(ctx context.Context, insertOptions ...InsertOption) (err error) {
dbExecutor := fromContext(ctx)
var sql, returning string
args := make([]any, 0, {{ len .Fields }})
{{ if .JsonFields }}
var {{ range $i, $f := .JsonFields }}{{ if $i }}, {{ end }}{{ $f.GoName }}Buf{{ end }} []byte
{{ range $i, $f := .JsonFields -}}
if {{ $f.GoName }}Buf, err = json.Marshal(e.{{ $f.GoName }}); err != nil {
return fmt.Errorf("failed to marshal {{ $f.GoName }} field: %w", err)
}
{{- end -}}
{{ end }}
{{ if .AutoIncrementField }}if e.{{ .AutoIncrementField.GoName }} == 0 {
sql = `INSERT INTO "{{ .SQLName }}" ({{ range $i, $f := .FieldsExcludeAutoIncrement }}{{ $f.SQLName }}, {{ end }}{{ .AutoIncrementField.SQLName }})
VALUES ({{ range $i, $f := .FieldsExcludeAutoIncrement }}${{ add $i 1 }}, {{ end }}DEFAULT)`
returning = ` RETURNING {{ .AutoIncrementField.SQLName }}`
args = []any{ {{ range $i, $f := .FieldsExcludeAutoIncrement -}}
{{- if $i }}, {{ end -}}
{{- if $f.IsJson -}}
string({{ $f.GoName }}Buf)
{{- else -}}
e.{{ $f.GoName }}
{{- end -}}
{{- end -}} }
} else {
sql = `INSERT INTO "{{ .SQLName }}" ({{ range $i, $f := .FieldsExcludeAutoIncrement }}{{ $f.SQLName }}, {{ end }}{{ .AutoIncrementField.SQLName }})
VALUES ({{ range $i, $f := .FieldsExcludeAutoIncrement }}${{ add $i 1 }}, {{ end }}${{ add 1 (len .FieldsExcludeAutoIncrement) }})`
args = []any{ {{ range $i, $f := .FieldsExcludeAutoIncrement }}{{ if $f.IsJson }}string({{ $f.GoName }}Buf){{ else }}e.{{ $f.GoName }}{{ end }}, {{ end }}e.{{ .AutoIncrementField.GoName }} }
}{{ else }}
sql = `INSERT INTO "{{ .SQLName }}" ({{ range $i, $f := .Fields }}{{ if $i }}, {{ end }}{{ $f.SQLName }}{{ end }})
VALUES ({{ range $i, $f := .Fields }}{{ if $i }}, {{ end }}${{ add $i 1 }}{{ end }})`
args = []any{ {{ range $i, $f := .Fields }}{{ if $i }}, {{ end }}{{ if $f.IsJson }}string({{ $f.GoName }}Buf){{ else }}e.{{ $f.GoName }}{{ end }}{{ end }} }
{{ end }}
var returnAndUpdateVals bool
for _, opt := range insertOptions {
if opt.ReturnAndUpdateVals {
returnAndUpdateVals = true
}
if opt.OnConflictStatement != "" {
sql += " ON CONFLICT "+ opt.OnConflictStatement
}
}
if returnAndUpdateVals {
returning = ` RETURNING {{ range $i, $f := .Fields }}{{ if $i }}, {{ end }}{{ $f.SQLName }}{{ end }}`
}
if returning != "" {
sql += returning
}
var rows pgx.Rows
rows, err = dbExecutor.Query(ctx, sql, args...)
defer func(){
rows.Close()
if err == nil {
err = rows.Err()
}
if err != nil {
err = fmt.Errorf("Insert query '%s' failed: %+v", sql, err)
}
}()
if err != nil {
return
}
if returnAndUpdateVals {
if ! rows.Next() {
// TODO: on conflict do nothing case
if err = rows.Err(); err != nil {
return
}
return fmt.Errorf("Insert-query doesn't return anything, but has returning clause")
}
if err = rows.Scan(
{{- range $i, $f := .Fields }}
{{- if $f.IsJson }}&{{ $f.GoName }}Buf{{ else }}&e.{{ $f.GoName }}{{ end }},
{{- end }}
); err != nil {
return
}
{{ if len .JsonFields }}
{{ range $i, $f := .JsonFields }}
if err = json.Unmarshal({{ $f.GoName }}Buf, &e.{{ $f.GoName }}); err != nil {
return fmt.Errorf("failed to unmarshal {{ $f.GoName }} field: %w", err)
}
{{ end }}
{{ end }}
}{{ if .AutoIncrementField }} else if e.{{ .AutoIncrementField.GoName }} == 0 {
if ! rows.Next() {
// TODO: on conflict do nothing case
if err = rows.Err(); err != nil {
return
}
return fmt.Errorf("Insert-query doesn't return anything, but has returning clause")
}
if err = rows.Scan(&e.{{ .AutoIncrementField.GoName }}); err != nil {
return
}
}{{ end }}
return nil
}
func (es {{ .GoName }}s) Insert(ctx context.Context, insertOptions ...InsertOption) (err error) {
if len(es) == 0 {
return nil
}
dbExecutor := fromContext(ctx)
var sql string
sqlRows := make([]string, 0, len(es))
args := make([]any, 0, {{ len .Fields }} * len(es))
{{ if .JsonFields }}
var {{ range $i, $f := .JsonFields }}{{ if $i }}, {{ end }}{{ $f.GoName }}Buf{{ end }} []byte
{{ end }}
{{ if .AutoIncrementField }}if es[0].{{ .AutoIncrementField.GoName }} == 0 {
sql = `INSERT INTO "{{ .SQLName }}" ({{ range $i, $f := .FieldsExcludeAutoIncrement }}{{ $f.SQLName }}, {{ end }}{{ .AutoIncrementField.SQLName }}) VALUES `
for i, e := range es {
sqlRows = append(sqlRows, fmt.Sprintf(`({{ range $i, $f := .FieldsExcludeAutoIncrement }}$%d, {{ end }}DEFAULT)`{{ range $i, $f := .FieldsExcludeAutoIncrement }}, i * {{ len $.FieldsExcludeAutoIncrement }} + {{ add $i 1 }}{{ end }}))
{{ range $i, $f := .JsonFields -}}
if {{ $f.GoName }}Buf, err = json.Marshal(e.{{ $f.GoName }}); err != nil {
return fmt.Errorf("failed to marshal {{ $f.GoName }} field of row #%d: %w", i, err)
}
{{- end }}
args = append(args, {{ range $i, $f := .FieldsExcludeAutoIncrement }}{{ if $i }}, {{ end }}{{ if $f.IsJson }}string({{ $f.GoName }}Buf){{ else }}e.{{ $f.GoName }}{{ end }}{{ end }})
}
} else {
sql = `INSERT INTO "{{ .SQLName }}" ({{ range $i, $f := .FieldsExcludeAutoIncrement }}{{ $f.SQLName }}, {{ end }}{{ .AutoIncrementField.SQLName }}) VALUES `
for i, e := range es {
sqlRows = append(sqlRows, fmt.Sprintf(`({{ range $i, $f := .Fields }}{{ if $i }}, {{ end }}$%d{{ end }})`{{ range $i, $f := .Fields }}, i * {{ len $.Fields }} + {{ add $i 1 }}{{ end }}))
{{ range $i, $f := .JsonFields -}}
if {{ $f.GoName }}Buf, err = json.Marshal(e.{{ $f.GoName }}); err != nil {
return fmt.Errorf("failed to marshal {{ $f.GoName }} field of row #%d: %w", i, err)
}
{{- end }}
args = append(args, {{ range $i, $f := .FieldsExcludeAutoIncrement }}{{ if $f.IsJson }}string({{ $f.GoName }}Buf){{ else }}e.{{ $f.GoName }}{{ end }}, {{ end }}e.{{ .AutoIncrementField.GoName }})
}
}{{ else }}
sql = `INSERT INTO "{{ .SQLName }}" ({{ range $i, $f := .Fields }}{{ if $i }}, {{ end }}{{ $f.SQLName }}{{ end }}) VALUES `
for i, e := range es {
sqlRows = append(sqlRows, fmt.Sprintf(`({{ range $i, $f := .Fields }}{{ if $i }}, {{ end }}$%d{{ end }})`{{ range $i, $f := .Fields }}, i * {{ len $.Fields }} + {{ add $i 1 }}{{ end }}))
{{ range $i, $f := .JsonFields -}}
if {{ $f.GoName }}Buf, err = json.Marshal(e.{{ $f.GoName }}); err != nil {
return fmt.Errorf("failed to marshal {{ $f.GoName }} field of row #%d: %w", i, err)
}
{{- end}}
args = append(args, {{ range $i, $f := .Fields }}{{ if $i }}, {{ end }}{{ if $f.IsJson }}string({{ $f.GoName }}Buf){{ else }}e.{{ $f.GoName }}{{ end }}{{ end }})
}
{{ end }}
sql += strings.Join(sqlRows, ", ")
for _, opt := range insertOptions {
if opt.ReturnAndUpdateVals {
err = fmt.Errorf("ReturnAndUpdateVals option is not supported for multi-insert now")
return
}
if opt.OnConflictStatement != "" {
sql += " ON CONFLICT "+ opt.OnConflictStatement
}
}
_, err = dbExecutor.Exec(ctx, sql, args...)
if err != nil {
err = fmt.Errorf("Insert query '%s' failed: %+v", sql, err)
}
return
}