Skip to content

feat(codegen): add emit_query_comments option for golang code generation #3993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bin/sqlc-dev
Binary file not shown.
3 changes: 3 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ The `gen` mapping supports the following keys:
that returns all valid enum values.
- `emit_sql_as_comment`:
- If true, emits the SQL statement as a code-block comment above the generated function, appending to any existing comments. Defaults to `false`.
- `emit_query_comments`:
- If true, emits the SQL query comments inside the generated query constants. Defaults to `false`.
- `build_tags`:
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
- `initialisms`:
Expand Down Expand Up @@ -416,6 +418,7 @@ packages:
emit_pointers_for_null_types: false
emit_enum_valid_method: false
emit_all_enum_values: false
emit_query_comments: false
build_tags: "some_tag"
json_tags_case_style: "camel"
omit_unused_structs: false
Expand Down
2 changes: 2 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type tmplCtx struct {
EmitMethodsWithDBArgument bool
EmitEnumValidMethod bool
EmitAllEnumValues bool
EmitQueryComments bool
UsesCopyFrom bool
UsesBatch bool
OmitSqlcVersion bool
Expand Down Expand Up @@ -181,6 +182,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
EmitMethodsWithDBArgument: options.EmitMethodsWithDbArgument,
EmitEnumValidMethod: options.EmitEnumValidMethod,
EmitAllEnumValues: options.EmitAllEnumValues,
EmitQueryComments: options.EmitQueryComments,
UsesCopyFrom: usesCopyFrom(queries),
UsesBatch: usesBatch(queries),
SQLDriver: parseDriver(options.SqlPackage),
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 @@ -24,6 +24,7 @@ type Options struct {
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
EmitQueryComments bool `json:"emit_query_comments,omitempty" yaml:"emit_query_comments"`
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
JsonTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Package string `json:"package" yaml:"package"`
Expand Down
4 changes: 4 additions & 0 deletions internal/codegen/golang/templates/pgx/batchCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var (
{{range .GoQueries}}
{{if eq (hasPrefix .Cmd ":batch") true }}
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
{{if $.EmitQueryComments -}}
{{range .Comments}}--{{.}}
{{end -}}
{{end -}}
{{escape .SQL}}
{{$.Q}}

Expand Down
4 changes: 4 additions & 0 deletions internal/codegen/golang/templates/pgx/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
{{if $.OutputQuery .SourceName}}
{{if and (ne .Cmd ":copyfrom") (ne (hasPrefix .Cmd ":batch") true)}}
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
{{if $.EmitQueryComments -}}
{{range .Comments}}--{{.}}
{{end -}}
{{end -}}
{{escape .SQL}}
{{$.Q}}
{{end}}
Expand Down
4 changes: 4 additions & 0 deletions internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{{range .GoQueries}}
{{if $.OutputQuery .SourceName}}
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
{{if $.EmitQueryComments -}}
{{range .Comments}}--{{.}}
{{end -}}
{{end -}}
{{escape .SQL}}
{{$.Q}}

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 @@ -42,6 +42,7 @@ type v1PackageSettings struct {
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
EmitQueryComments bool `json:"emit_query_comments,omitempty" yaml:"emit_query_comments"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
SQLPackage string `json:"sql_package" yaml:"sql_package"`
SQLDriver string `json:"sql_driver" yaml:"sql_driver"`
Expand Down Expand Up @@ -152,6 +153,7 @@ func (c *V1GenerateSettings) Translate() Config {
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
EmitAllEnumValues: pkg.EmitAllEnumValues,
EmitSqlAsComment: pkg.EmitSqlAsComment,
EmitQueryComments: pkg.EmitQueryComments,
Package: pkg.Name,
Out: pkg.Path,
SqlPackage: pkg.SQLPackage,
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 @@ -134,6 +134,9 @@
"emit_sql_as_comment": {
"type": "boolean"
},
"emit_query_comments": {
"type": "boolean"
},
"build_tags": {
"type": "string"
},
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 @@ -143,6 +143,9 @@
"emit_sql_as_comment": {
"type": "boolean"
},
"emit_query_comments": {
"type": "boolean"
},
"build_tags": {
"type": "string"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"os": ["darwin", "linux"]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/emit_query_comments/stdlib/go/db.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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- name: ListBar :many
-- Lists all bars
SELECT id FROM (
SELECT * FROM bar
) bar;

-- name: RemoveBar :exec
DELETE FROM bar WHERE id = $1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE bar (id serial not null);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/emit_query_comments/stdlib/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql",
"emit_query_comments": true
}
]
}
Loading