Skip to content

Commit d2bf5b9

Browse files
committed
feat(codegen): add emit_query_comments option for golang code generation
Signed-off-by: Nathanael DEMACON <quantumsheep@users.noreply.github.com>
1 parent 3da0b82 commit d2bf5b9

File tree

17 files changed

+140
-0
lines changed

17 files changed

+140
-0
lines changed

bin/sqlc-dev

75.6 MB
Binary file not shown.

docs/reference/config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ The `gen` mapping supports the following keys:
167167
that returns all valid enum values.
168168
- `emit_sql_as_comment`:
169169
- If true, emits the SQL statement as a code-block comment above the generated function, appending to any existing comments. Defaults to `false`.
170+
- `emit_query_comments`:
171+
- If true, emits the SQL query comments inside the generated query constants. Defaults to `false`.
170172
- `build_tags`:
171173
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
172174
- `initialisms`:
@@ -416,6 +418,7 @@ packages:
416418
emit_pointers_for_null_types: false
417419
emit_enum_valid_method: false
418420
emit_all_enum_values: false
421+
emit_query_comments: false
419422
build_tags: "some_tag"
420423
json_tags_case_style: "camel"
421424
omit_unused_structs: false

internal/codegen/golang/gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type tmplCtx struct {
3737
EmitMethodsWithDBArgument bool
3838
EmitEnumValidMethod bool
3939
EmitAllEnumValues bool
40+
EmitQueryComments bool
4041
UsesCopyFrom bool
4142
UsesBatch bool
4243
OmitSqlcVersion bool
@@ -181,6 +182,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
181182
EmitMethodsWithDBArgument: options.EmitMethodsWithDbArgument,
182183
EmitEnumValidMethod: options.EmitEnumValidMethod,
183184
EmitAllEnumValues: options.EmitAllEnumValues,
185+
EmitQueryComments: options.EmitQueryComments,
184186
UsesCopyFrom: usesCopyFrom(queries),
185187
UsesBatch: usesBatch(queries),
186188
SQLDriver: parseDriver(options.SqlPackage),

internal/codegen/golang/opts/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Options struct {
2424
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
2525
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
2626
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
27+
EmitQueryComments bool `json:"emit_query_comments,omitempty" yaml:"emit_query_comments"`
2728
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
2829
JsonTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
2930
Package string `json:"package" yaml:"package"`

internal/codegen/golang/templates/pgx/batchCode.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ var (
77
{{range .GoQueries}}
88
{{if eq (hasPrefix .Cmd ":batch") true }}
99
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
10+
{{if $.EmitQueryComments -}}
11+
{{range .Comments}}--{{.}}
12+
{{end -}}
13+
{{end -}}
1014
{{escape .SQL}}
1115
{{$.Q}}
1216

internal/codegen/golang/templates/pgx/queryCode.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
{{if $.OutputQuery .SourceName}}
44
{{if and (ne .Cmd ":copyfrom") (ne (hasPrefix .Cmd ":batch") true)}}
55
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
6+
{{if $.EmitQueryComments -}}
7+
{{range .Comments}}--{{.}}
8+
{{end -}}
9+
{{end -}}
610
{{escape .SQL}}
711
{{$.Q}}
812
{{end}}

internal/codegen/golang/templates/stdlib/queryCode.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
{{range .GoQueries}}
33
{{if $.OutputQuery .SourceName}}
44
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
5+
{{if $.EmitQueryComments -}}
6+
{{range .Comments}}--{{.}}
7+
{{end -}}
8+
{{end -}}
59
{{escape .SQL}}
610
{{$.Q}}
711

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type v1PackageSettings struct {
4242
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
4343
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
4444
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
45+
EmitQueryComments bool `json:"emit_query_comments,omitempty" yaml:"emit_query_comments"`
4546
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
4647
SQLPackage string `json:"sql_package" yaml:"sql_package"`
4748
SQLDriver string `json:"sql_driver" yaml:"sql_driver"`
@@ -152,6 +153,7 @@ func (c *V1GenerateSettings) Translate() Config {
152153
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
153154
EmitAllEnumValues: pkg.EmitAllEnumValues,
154155
EmitSqlAsComment: pkg.EmitSqlAsComment,
156+
EmitQueryComments: pkg.EmitQueryComments,
155157
Package: pkg.Name,
156158
Out: pkg.Path,
157159
SqlPackage: pkg.SQLPackage,

internal/config/v_one.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@
134134
"emit_sql_as_comment": {
135135
"type": "boolean"
136136
},
137+
"emit_query_comments": {
138+
"type": "boolean"
139+
},
137140
"build_tags": {
138141
"type": "string"
139142
},

internal/config/v_two.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@
143143
"emit_sql_as_comment": {
144144
"type": "boolean"
145145
},
146+
"emit_query_comments": {
147+
"type": "boolean"
148+
},
146149
"build_tags": {
147150
"type": "string"
148151
},

0 commit comments

Comments
 (0)