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 authored and kyleconroy committed Nov 25, 2024
1 parent 0b952b4 commit 1cb85e4
Show file tree
Hide file tree
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
Expand Up @@ -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`:
Expand Down
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
Expand Up @@ -39,6 +39,7 @@ type tmplCtx struct {
EmitAllEnumValues bool
UsesCopyFrom bool
UsesBatch bool
UseInterfaceReturnTypes bool
OmitSqlcVersion bool
BuildTags string
}
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ func (i *importer) interfaceImports() fileImports {

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

sqlpkg := parseDriver(i.Options.SqlPackage)
switch sqlpkg {
case SQLDriverPGXV4:

Check failure on line 273 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test

undefined: SQLDriverPGXV4

Check failure on line 273 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test

undefined: SQLDriverPGXV4

Check failure on line 273 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test ubuntu-latest cgo=1

undefined: SQLDriverPGXV4

Check failure on line 273 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test

undefined: SQLDriverPGXV4

Check failure on line 273 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / vuln_check

undefined: SQLDriverPGXV4

Check failure on line 273 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test ubuntu-latest cgo=0

undefined: SQLDriverPGXV4
pkg[ImportSpec{Path: "github.com/jackc/pgx/v4"}] = struct{}{}
case SQLDriverPGXV5:

Check failure on line 275 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test

undefined: SQLDriverPGXV5

Check failure on line 275 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test

undefined: SQLDriverPGXV5

Check failure on line 275 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test ubuntu-latest cgo=1

undefined: SQLDriverPGXV5

Check failure on line 275 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test

undefined: SQLDriverPGXV5

Check failure on line 275 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / vuln_check

undefined: SQLDriverPGXV5

Check failure on line 275 in internal/codegen/golang/imports.go

View workflow job for this annotation

GitHub Actions / test ubuntu-latest cgo=0

undefined: SQLDriverPGXV5
pkg[ImportSpec{Path: "github.com/jackc/pgx/v5"}] = struct{}{}
default:
std["database/sql"] = struct{}{}
}

return sortedImports(std, pkg)
}

Expand Down
1 change: 1 addition & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
4 changes: 4 additions & 0 deletions internal/codegen/golang/templates/pgx/dbCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
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) }}
Expand Down
4 changes: 4 additions & 0 deletions internal/codegen/golang/templates/stdlib/dbCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
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) }}
Expand Down
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions internal/config/v_one.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
"emit_interface": {
"type": "boolean"
},
"use_interface_return_types": {
"type": "boolean"
},
"emit_json_tags": {
"type": "boolean"
},
Expand Down
3 changes: 3 additions & 0 deletions internal/config/v_two.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
"emit_interface": {
"type": "boolean"
},
"use_interface_return_types": {
"type": "boolean"
},
"emit_json_tags": {
"type": "boolean"
},
Expand Down

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 1cb85e4

Please sign in to comment.