-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathroutines_delete.go.tmpl
More file actions
35 lines (28 loc) · 1.35 KB
/
routines_delete.go.tmpl
File metadata and controls
35 lines (28 loc) · 1.35 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
{{ if .PrimaryKey }} {{ $primaryKey := index .UniqIndexes .PrimaryKey }}{{ $autoIncrementField := .AutoIncrementField }}
func (e *{{ .GoName }}) Delete(ctx context.Context) (err error) {
dbExecutor := fromContext(ctx)
sql := `DELETE FROM "{{ .SQLName }}" WHERE {{ range $i, $f := $primaryKey }}{{ if $i }} AND {{ end }}{{ $f.SQLName }} = ${{ add $f.Num 1 }}{{ end }}`
_, err = dbExecutor.Exec(ctx, sql, {{ range $f := $primaryKey }}e.{{ $f.GoName }}, {{ end }});
if err != nil {
err = fmt.Errorf("Delete query '%s' failed: %+v", sql, err)
}
return
}
func (es {{ .GoName }}s) Delete(ctx context.Context) (err error) {
dbExecutor := fromContext(ctx)
sql := `DELETE FROM "{{ .SQLName }}" WHERE `
rowsSql := make([]string, len(es))
var args []any
for i, e := range es {
rowsSql[i] = fmt.Sprintf(`({{ range $i, $f := $primaryKey }}{{ if $i }} AND {{ end }}{{ $f.SQLName }} = $%d{{ end }})`,
{{- range $i, $f := $primaryKey }}{{ if $i }}, {{ end }}i * {{ len $primaryKey }} + {{ add 1 $i }}{{ end }})
args = append(args, {{ range $i, $f := $primaryKey }}{{ if $i }}, {{ end }}e.{{ $f.GoName }}{{ end }})
}
sql = sql + strings.Join(rowsSql, " OR ")
_, err = dbExecutor.Exec(ctx, sql, args...);
if err != nil {
err = fmt.Errorf("Delete query '%s' failed: %+v", sql, err)
}
return
}
{{ end }}