forked from opendoor/pggen
/
gen_interface.go
170 lines (148 loc) · 4.98 KB
/
gen_interface.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
package gen
import (
"fmt"
"io"
"text/template"
"github.com/ferumlabs/pggen/gen/internal/config"
"github.com/ferumlabs/pggen/gen/internal/meta"
)
// genInterfaces emits the DBQueries interface shared between the generated PGClient
// and the generated TxPGClient. This allows user code to be written in such a way to
func (g *Generator) genInterfaces(into io.Writer, conf *config.DbConfig) error {
g.log.Infof(" generating DBQueries interface\n")
var genCtx ifaceGenCtx
// populate tables
genCtx.Tables = make([]tableIfaceGenCtx, 0, len(conf.Tables))
for _, tc := range conf.Tables {
tableInfo, ok := g.metaResolver.TableMeta(tc.Name)
if !ok {
return fmt.Errorf("could get schema info about table '%s'", tc.Name)
}
genCtx.Tables = append(genCtx.Tables, tableIfaceGenCtx{
GoName: tableInfo.Info.GoName,
PkeyType: tableInfo.Info.PkeyCol.TypeInfo.Name,
BoxResults: tableInfo.Config.BoxResults,
})
}
// poplulate queries
genCtx.Queries = make([]meta.QueryMeta, 0, len(conf.Queries))
for i := range conf.Queries {
meta, err := g.metaResolver.QueryMeta(&conf.Queries[i], true /* inferArgTypes */)
if err != nil {
return err
}
genCtx.Queries = append(genCtx.Queries, meta)
}
// populate the statement gen ctx
genCtx.Stmts = make([]meta.StmtMeta, 0, len(conf.Stmts))
for i := range conf.Stmts {
meta, err := g.metaResolver.StmtMeta(&conf.Stmts[i])
if err != nil {
return err
}
genCtx.Stmts = append(genCtx.Stmts, meta)
}
return dbQueriesTmpl.Execute(into, genCtx)
}
type tableIfaceGenCtx struct {
GoName string
PkeyType string
BoxResults bool
}
type ifaceGenCtx struct {
Tables []tableIfaceGenCtx
Queries []meta.QueryMeta
StoredFuncs []meta.QueryMeta
Stmts []meta.StmtMeta
}
var dbQueriesTmpl *template.Template = template.Must(template.New("db-queries-tmpl").Parse(`
type DBQueries interface {
//
// automatic CRUD methods
//
{{ range .Tables }}
// {{ .GoName }} methods
Get{{ .GoName }}(ctx context.Context, id {{ .PkeyType }}, opts ...pggen.GetOpt) (*{{ .GoName }}, error)
List{{ .GoName }}(ctx context.Context, ids []{{ .PkeyType }}, opts ...pggen.ListOpt) ([]{{- if .BoxResults }}*{{- end }}{{ .GoName }}, error)
Insert{{ .GoName }}(ctx context.Context, value *{{ .GoName }}, opts ...pggen.InsertOpt) ({{ .PkeyType }}, error)
BulkInsert{{ .GoName }}(ctx context.Context, values []{{ .GoName }}, opts ...pggen.InsertOpt) ([]{{ .PkeyType }}, error)
Update{{ .GoName }}(ctx context.Context, value *{{ .GoName }}, fieldMask pggen.FieldSet, opts ...pggen.UpdateOpt) (ret {{ .PkeyType }}, err error)
Upsert{{ .GoName }}(ctx context.Context, value *{{ .GoName }}, constraintNames []string, fieldMask pggen.FieldSet, opts ...pggen.UpsertOpt) ({{ .PkeyType }}, error)
BulkUpsert{{ .GoName }}(ctx context.Context, values []{{ .GoName }}, constraintNames []string, fieldMask pggen.FieldSet, opts ...pggen.UpsertOpt) ([]{{ .PkeyType }}, error)
Delete{{ .GoName }}(ctx context.Context, id {{ .PkeyType }}, opts ...pggen.DeleteOpt) error
BulkDelete{{ .GoName }}(ctx context.Context, ids []{{ .PkeyType }}, opts ...pggen.DeleteOpt) error
{{ .GoName }}FillIncludes(ctx context.Context, rec *{{ .GoName }}, includes *include.Spec, opts ...pggen.IncludeOpt) error
{{ .GoName }}BulkFillIncludes(ctx context.Context, recs []*{{ .GoName }}, includes *include.Spec, opts ...pggen.IncludeOpt) error
{{ end }}
//
// query methods
//
{{ range $i, $query := .Queries }}
{{ if .ConfigData.SingleResult }}
// {{ .ConfigData.Name }} query
{{ .ConfigData.Name }}(
ctx context.Context,
{{- range .Args }}
{{- if $query.ConfigData.NullableArguments }}
{{ .GoName }} {{ .TypeInfo.NullName }},
{{- else }}
{{ .GoName }} {{ .TypeInfo.Name }},
{{- end }}
{{- end }}
{{- if (not .MultiReturn) }}
) ({{ .ReturnTypeName }}, error)
{{- else }}
) (*{{ .ReturnTypeName }}, error)
{{- end }}
{{ else }}
// {{ .ConfigData.Name }} query
{{ .ConfigData.Name }}(
ctx context.Context,
{{- range .Args }}
{{- if $query.ConfigData.NullableArguments }}
{{ .GoName }} {{ .TypeInfo.NullName }},
{{- else }}
{{ .GoName }} {{ .TypeInfo.Name }},
{{- end }}
{{- end }}
) ([]{{- if .ConfigData.BoxResults }}*{{- end }}{{ .ReturnTypeName }}, error)
{{ .ConfigData.Name }}Query(
ctx context.Context,
{{- range .Args }}
{{ .GoName }} {{ .TypeInfo.Name }},
{{- end }}
) (*sql.Rows, error)
{{ end }}
{{ end }}
//
// stored function methods
//
{{ range .StoredFuncs }}
// {{ .ConfigData.Name }} stored function
{{ .ConfigData.Name }}(
ctx context.Context,
{{- range .Args }}
{{ .GoName }} {{ .TypeInfo.Name }},
{{- end }}
) ([]{{ .ReturnTypeName }}, error)
{{ .ConfigData.Name }}Query(
ctx context.Context,
{{- range .Args }}
{{ .GoName }} {{ .TypeInfo.Name }},
{{- end }}
) (*sql.Rows, error)
{{ end }}
//
// stmt methods
//
{{ range .Stmts }}
// {{ .ConfigData.Name }} stmt
{{ .ConfigData.Name }}(
ctx context.Context,
{{- range .Args}}
{{ .GoName }} {{ .TypeInfo.Name }},
{{- end}}
) (sql.Result, error)
{{ end }}
}
`))