Skip to content

Commit

Permalink
feat: add "use_interface_return_types" option
Browse files Browse the repository at this point in the history
Add an option to direct sqlc generation to use the querier interface for the return type of the WithTx method on the Queries struct.

Include WithTx in the Querier interface (when "emit_methods_with_db_argument" is false)
  • Loading branch information
Jesse0Michael committed Jan 11, 2024
1 parent dc311e7 commit 37d14e9
Showing 64 changed files with 466 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
@@ -141,6 +141,8 @@ The `gen` mapping supports the following keys:
- If true, include support for prepared queries. Defaults to `false`.
- `emit_interface`:
- If true, output a `Querier` interface in the generated package. Defaults to `false`.
- `use_interface_return_types`:
- If true, output will use the `Querier` interface as the return type the methods in the generated package. Defaults to `false`.
- `emit_exact_table_names`:
- If true, struct names will mirror table names. Otherwise, sqlc attempts to singularize plural table names. Defaults to `false`.
- `emit_empty_slices`:
2 changes: 2 additions & 0 deletions examples/batch/postgresql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/ondeck/mysql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/ondeck/postgresql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/ondeck/sqlite/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ type tmplCtx struct {
EmitAllEnumValues bool
UsesCopyFrom bool
UsesBatch bool
UseInterfaceReturnTypes bool
OmitSqlcVersion bool
BuildTags string
}
@@ -169,6 +170,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,

tctx := tmplCtx{
EmitInterface: options.EmitInterface,
UseInterfaceReturnTypes: options.UseInterfaceReturnTypes,
EmitJSONTags: options.EmitJsonTags,
JsonTagsIDUppercase: options.JsonTagsIdUppercase,
EmitDBTags: options.EmitDbTags,
10 changes: 10 additions & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
@@ -263,6 +263,16 @@ func (i *importer) interfaceImports() fileImports {

std["context"] = struct{}{}

sqlpkg := parseDriver(i.Options.SqlPackage)
switch sqlpkg {
case SQLDriverPGXV4:
pkg[ImportSpec{Path: "github.com/jackc/pgx/v4"}] = struct{}{}
case SQLDriverPGXV5:
pkg[ImportSpec{Path: "github.com/jackc/pgx/v5"}] = struct{}{}
default:
std["database/sql"] = struct{}{}
}

return sortedImports(std, pkg)
}

1 change: 1 addition & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import (

type Options struct {
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
UseInterfaceReturnTypes bool `json:"use_interface_return_types" yaml:"use_interface_return_types"`
EmitJsonTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
JsonTagsIdUppercase bool `json:"json_tags_id_uppercase" yaml:"json_tags_id_uppercase"`
EmitDbTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
4 changes: 4 additions & 0 deletions internal/codegen/golang/templates/pgx/dbCode.tmpl
Original file line number Diff line number Diff line change
@@ -28,7 +28,11 @@ type Queries struct {
}

{{if not .EmitMethodsWithDBArgument}}
{{- if and .EmitInterface .UseInterfaceReturnTypes}}
func (q *Queries) WithTx(tx pgx.Tx) Querier {
{{- else -}}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
{{- end}}
return &Queries{
db: tx,
}
5 changes: 5 additions & 0 deletions internal/codegen/golang/templates/pgx/interfaceCode.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{{define "interfaceCodePgx"}}
type Querier interface {
{{- if .UseInterfaceReturnTypes}}
WithTx(tx pgx.Tx) Querier
{{- else -}}
WithTx(tx pgx.Tx) *Queries
{{- end}}
{{- $dbtxParam := .EmitMethodsWithDBArgument -}}
{{- range .GoQueries}}
{{- if and (eq .Cmd ":one") ($dbtxParam) }}
4 changes: 4 additions & 0 deletions internal/codegen/golang/templates/stdlib/dbCode.tmpl
Original file line number Diff line number Diff line change
@@ -90,7 +90,11 @@ type Queries struct {
}

{{if not .EmitMethodsWithDBArgument}}
{{- if and .EmitInterface .UseInterfaceReturnTypes}}
func (q *Queries) WithTx(tx *sql.Tx) Querier {
{{- else -}}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
{{- end}}
return &Queries{
db: tx,
{{- if .EmitPreparedQueries}}
7 changes: 7 additions & 0 deletions internal/codegen/golang/templates/stdlib/interfaceCode.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{{define "interfaceCodeStd"}}
type Querier interface {
{{if not .EmitMethodsWithDBArgument}}
{{- if .UseInterfaceReturnTypes}}
WithTx(tx *sql.Tx) Querier
{{- else -}}
WithTx(tx *sql.Tx) *Queries
{{- end}}
{{- end}}
{{- $dbtxParam := .EmitMethodsWithDBArgument -}}
{{- range .GoQueries}}
{{- if and (eq .Cmd ":one") ($dbtxParam) }}
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ type v1PackageSettings struct {
Schema Paths `json:"schema" yaml:"schema"`
Queries Paths `json:"queries" yaml:"queries"`
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
UseInterfaceReturnTypes bool `json:"use_interface_return_types" yaml:"use_interface_return_types"`
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
JsonTagsIDUppercase bool `json:"json_tags_id_uppercase" yaml:"json_tags_id_uppercase"`
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
@@ -138,6 +139,7 @@ func (c *V1GenerateSettings) Translate() Config {
Gen: SQLGen{
Go: &golang.Options{
EmitInterface: pkg.EmitInterface,
UseInterfaceReturnTypes: pkg.UseInterfaceReturnTypes,
EmitJsonTags: pkg.EmitJSONTags,
JsonTagsIdUppercase: pkg.JsonTagsIDUppercase,
EmitDbTags: pkg.EmitDBTags,
3 changes: 3 additions & 0 deletions internal/config/v_one.json
Original file line number Diff line number Diff line change
@@ -92,6 +92,9 @@
"emit_interface": {
"type": "boolean"
},
"use_interface_return_types": {
"type": "boolean"
},
"emit_json_tags": {
"type": "boolean"
},
3 changes: 3 additions & 0 deletions internal/config/v_two.json
Original file line number Diff line number Diff line change
@@ -101,6 +101,9 @@
"emit_interface": {
"type": "boolean"
},
"use_interface_return_types": {
"type": "boolean"
},
"emit_json_tags": {
"type": "boolean"
},

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/exec_imports/pgx/v4/go/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/exec_imports/pgx/v5/go/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/endtoend/testdata/exec_imports/stdlib/go/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 37d14e9

Please # to comment.