-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add emit_methods_with_db_argument config option (#1279)
Support a new configuration parameter `emit_methods_with_db_argument `that modifies the generated method sets to provide methods like shown in WidgetInserter. If set to true, the generated *Queries omits storing a DBTX as a struct field and requires it be passed in to all method calls. In doing so, it allows callers to easily provide the connection for standalone use or for use as part of a broader transaction and makes it easy for the surrounding code to use a narrowly defined interface.
- Loading branch information
1 parent
2aa00e2
commit 768ccb6
Showing
30 changed files
with
522 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
{{define "interfaceCodePgx"}} | ||
type Querier interface { | ||
{{- $dbtxParam := .EmitMethodsWithDBArgument -}} | ||
{{- range .GoQueries}} | ||
{{- if eq .Cmd ":one"}} | ||
{{- if and (eq .Cmd ":one") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) | ||
{{- else if eq .Cmd ":one" }} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) | ||
{{- end}} | ||
{{- if eq .Cmd ":many"}} | ||
{{- if and (eq .Cmd ":many") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) | ||
{{- else if eq .Cmd ":many" }} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) | ||
{{- end}} | ||
{{- if eq .Cmd ":exec"}} | ||
{{- if and (eq .Cmd ":exec") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) error | ||
{{- else if eq .Cmd ":exec" }} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error | ||
{{- end}} | ||
{{- if eq .Cmd ":execrows"}} | ||
{{- if and (eq .Cmd ":execrows") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error) | ||
{{- else if eq .Cmd ":execrows" }} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error) | ||
{{- end}} | ||
{{- if eq .Cmd ":execresult"}} | ||
{{- if and (eq .Cmd ":execresult") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (pgconn.CommandTag, error) | ||
{{- else if eq .Cmd ":execresult" }} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (pgconn.CommandTag, error) | ||
{{- end}} | ||
{{- end}} | ||
} | ||
|
||
var _ Querier = (*Queries)(nil) | ||
{{end}} | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 17 additions & 6 deletions
23
internal/codegen/golang/templates/stdlib/interfaceCode.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
{{define "interfaceCodeStd"}} | ||
type Querier interface { | ||
{{- $dbtxParam := .EmitMethodsWithDBArgument -}} | ||
{{- range .GoQueries}} | ||
{{- if eq .Cmd ":one"}} | ||
{{- if and (eq .Cmd ":one") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) | ||
{{- else if eq .Cmd ":one"}} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) | ||
{{- end}} | ||
{{- if eq .Cmd ":many"}} | ||
{{- if and (eq .Cmd ":many") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) | ||
{{- else if eq .Cmd ":many"}} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) | ||
{{- end}} | ||
{{- if eq .Cmd ":exec"}} | ||
{{- if and (eq .Cmd ":exec") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) error | ||
{{- else if eq .Cmd ":exec"}} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error | ||
{{- end}} | ||
{{- if eq .Cmd ":execrows"}} | ||
{{- if and (eq .Cmd ":execrows") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error) | ||
{{- else if eq .Cmd ":execrows"}} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error) | ||
{{- end}} | ||
{{- if eq .Cmd ":execresult"}} | ||
{{- if and (eq .Cmd ":execresult") ($dbtxParam) }} | ||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (sql.Result, error) | ||
{{- else if eq .Cmd ":execresult"}} | ||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (sql.Result, error) | ||
{{- end}} | ||
{{- end}} | ||
} | ||
|
||
var _ Querier = (*Queries)(nil) | ||
{{end}} | ||
{{end}} |
Oops, something went wrong.