-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathbatchCode.tmpl
More file actions
138 lines (128 loc) · 3.31 KB
/
batchCode.tmpl
File metadata and controls
138 lines (128 loc) · 3.31 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
{{define "batchCodePgx"}}
var (
ErrBatchAlreadyClosed = errors.New("batch already closed")
)
{{range .GoQueries}}
{{if eq (hasPrefix .Cmd ":batch") true }}
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
{{escape .SQL}}
{{$.Q}}
type {{.MethodName}}BatchResults struct {
br pgx.BatchResults
tot int
closed bool
}
{{if .Arg.Struct}}
type {{.Arg.Type}} struct { {{- range .Arg.Struct.Fields}}
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
{{- end}}
}
{{end}}
{{if .Ret.EmitStruct}}
type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
{{- end}}
}
{{end}}
{{range .Comments}}//{{.}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ if $.EmitMethodsWithDBArgument}}db DBTX,{{end}} {{.Arg.SlicePair}}) *{{.MethodName}}BatchResults {
batch := &pgx.Batch{}
for _, a := range {{index .Arg.Name}} {
vals := []interface{}{
{{- if .Arg.Struct }}
{{- range .Arg.Struct.Fields }}
a.{{.Name}},
{{- end }}
{{- else }}
a,
{{- end }}
}
batch.Queue({{.ConstantName}}, vals...)
}
br := {{if not $.EmitMethodsWithDBArgument}}q.{{end}}db.SendBatch(ctx, batch)
return &{{.MethodName}}BatchResults{br,len({{.Arg.Name}}),false}
}
{{if eq .Cmd ":batchexec"}}
func (b *{{.MethodName}}BatchResults) Exec(f func(int, error)) {
defer b.br.Close()
for t := 0; t < b.tot; t++ {
if b.closed {
if f != nil {
f(t, ErrBatchAlreadyClosed)
}
continue
}
_, err := b.br.Exec()
if f != nil {
f(t, err)
}
}
}
{{end}}
{{if eq .Cmd ":batchmany"}}
func (b *{{.MethodName}}BatchResults) Query(f func(int, []{{.Ret.DefineType}}, error)) {
defer b.br.Close()
for t := 0; t < b.tot; t++ {
{{- if $.EmitEmptySlices}}
items := []{{.Ret.DefineType}}{}
{{else}}
var items []{{.Ret.DefineType}}
{{end -}}
if b.closed {
if f != nil {
f(t, items, ErrBatchAlreadyClosed)
}
continue
}
err := func() error {
rows, err := b.br.Query()
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var {{.Ret.Name}} {{.Ret.Type}}
{{- .Ret.NullableEmbedDecls}}
if err := rows.Scan({{.Ret.Scan}}); err != nil {
return err
}
{{- .Ret.NullableEmbedAssigns}}
items = append(items, {{.Ret.ReturnName}})
}
return rows.Err()
}()
if f != nil {
f(t, items, err)
}
}
}
{{end}}
{{if eq .Cmd ":batchone"}}
func (b *{{.MethodName}}BatchResults) QueryRow(f func(int, {{.Ret.DefineType}}, error)) {
defer b.br.Close()
for t := 0; t < b.tot; t++ {
var {{.Ret.Name}} {{.Ret.Type}}
{{- .Ret.NullableEmbedDecls}}
if b.closed {
if f != nil {
f(t, {{if .Ret.IsPointer}}nil{{else}}{{.Ret.Name}}{{end}}, ErrBatchAlreadyClosed)
}
continue
}
row := b.br.QueryRow()
err := row.Scan({{.Ret.Scan}})
{{- .Ret.NullableEmbedAssigns}}
if f != nil {
f(t, {{.Ret.ReturnName}}, err)
}
}
}
{{end}}
func (b *{{.MethodName}}BatchResults) Close() error {
b.closed = true
return b.br.Close()
}
{{end}}
{{end}}
{{end}}