Skip to content

Commit 79ec914

Browse files
authored
internal/dinosql: Emit Querier interface (#240)
* internal/dinosql: Emit Querier interface
1 parent 75305f3 commit 79ec914

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

examples/ondeck/db.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/sqlc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"queries": "ondeck/query",
1414
"engine": "postgresql",
1515
"emit_json_tags": true,
16-
"emit_prepared_queries": true
16+
"emit_prepared_queries": true,
17+
"emit_interface": true
1718
},
1819
{
1920
"path": "jets",

internal/dinosql/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ type PackageSettings struct {
4747
Path string `json:"path"`
4848
Schema string `json:"schema"`
4949
Queries string `json:"queries"`
50-
EmitPreparedQueries bool `json:"emit_prepared_queries"`
50+
EmitInterface bool `json:"emit_interface"`
5151
EmitJSONTags bool `json:"emit_json_tags"`
52+
EmitPreparedQueries bool `json:"emit_prepared_queries"`
5253
Overrides []Override `json:"overrides"`
5354
}
5455

internal/dinosql/gen.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,27 @@ func (q *Queries) WithTx(tx *sql.Tx) *Queries {
920920
{{- end}}
921921
}
922922
}
923+
924+
{{if .EmitInterface }}
925+
type Querier interface {
926+
{{- range .GoQueries}}
927+
{{- if eq .Cmd ":one"}}
928+
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.Type}}, error)
929+
{{- end}}
930+
{{- if eq .Cmd ":many"}}
931+
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.Type}}, error)
932+
{{- end}}
933+
{{- if eq .Cmd ":exec"}}
934+
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error
935+
{{- end}}
936+
{{- if eq .Cmd ":execrows"}}
937+
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error)
938+
{{- end}}
939+
{{- end}}
940+
}
941+
942+
var _ Querier = (*Queries)(nil)
943+
{{end}}
923944
`
924945

925946
var modelsTmpl = `// Code generated by sqlc. DO NOT EDIT.
@@ -1084,6 +1105,7 @@ type tmplCtx struct {
10841105

10851106
EmitJSONTags bool
10861107
EmitPreparedQueries bool
1108+
EmitInterface bool
10871109
}
10881110

10891111
func LowerTitle(s string) string {
@@ -1110,8 +1132,9 @@ func Generate(r Generateable, settings GenerateSettings) (map[string]string, err
11101132

11111133
tctx := tmplCtx{
11121134
Settings: settings,
1113-
EmitPreparedQueries: pkgConfig.EmitPreparedQueries,
1135+
EmitInterface: pkgConfig.EmitInterface,
11141136
EmitJSONTags: pkgConfig.EmitJSONTags,
1137+
EmitPreparedQueries: pkgConfig.EmitPreparedQueries,
11151138
Q: "`",
11161139
Package: pkgName,
11171140
GoQueries: r.GoQueries(settings),

0 commit comments

Comments
 (0)