Skip to content

Commit db0157b

Browse files
committed
Add emit_sql_as_comment option to Go code plugin
This option adds the raw SQL query as a comment to the generated query function This is useful when working in an IDE that displays comments over functions, you are able to glance at the actual SQL query that will be executed without having to lose context of where you are in the current file you are working on.
1 parent a4fa8b9 commit db0157b

File tree

12 files changed

+146
-3
lines changed

12 files changed

+146
-3
lines changed

docs/reference/config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ The `gen` mapping supports the following keys:
163163
- `emit_all_enum_values`:
164164
- If true, emit a function per enum type
165165
that returns all valid enum values.
166+
- `emit_sql_as_comment`:
167+
- If true, emits the SQL statement as a code-block comment above the generated function, appending to any existing comments. Defaults to `false`.
166168
- `build_tags`:
167169
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
168170
- `json_tags_id_uppercase`:

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+
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
2728
JsonTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
2829
Package string `json:"package" yaml:"package"`
2930
Out string `json:"out" yaml:"out"`

internal/codegen/golang/result.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,25 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
199199
constantName = sdk.LowerTitle(query.Name)
200200
}
201201

202+
comments := query.Comments
203+
if options.EmitSqlAsComment {
204+
if len(comments) == 0 {
205+
comments = append(comments, query.Name)
206+
}
207+
comments = append(comments, " ")
208+
for _, line := range strings.Split(query.Text, "\n") {
209+
comments = append(comments, " "+line)
210+
}
211+
}
212+
202213
gq := Query{
203214
Cmd: query.Cmd,
204215
ConstantName: constantName,
205216
FieldName: sdk.LowerTitle(query.Name) + "Stmt",
206217
MethodName: query.Name,
207218
SourceName: query.Filename,
208219
SQL: query.Text,
209-
Comments: query.Comments,
220+
Comments: comments,
210221
Table: query.InsertIntoTable,
211222
}
212223
sqlpkg := parseDriver(options.SqlPackage)

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type v1PackageSettings struct {
4141
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
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"`
44+
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
4445
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
4546
SQLPackage string `json:"sql_package" yaml:"sql_package"`
4647
SQLDriver string `json:"sql_driver" yaml:"sql_driver"`
@@ -150,6 +151,7 @@ func (c *V1GenerateSettings) Translate() Config {
150151
EmitPointersForNullTypes: pkg.EmitPointersForNullTypes,
151152
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
152153
EmitAllEnumValues: pkg.EmitAllEnumValues,
154+
EmitSqlAsComment: pkg.EmitSqlAsComment,
153155
Package: pkg.Name,
154156
Out: pkg.Path,
155157
SqlPackage: pkg.SQLPackage,

internal/config/v_one.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@
131131
"emit_all_enum_values": {
132132
"type": "boolean"
133133
},
134+
"emit_sql_as_comment": {
135+
"type": "boolean"
136+
},
134137
"build_tags": {
135138
"type": "string"
136139
},
@@ -340,4 +343,4 @@
340343
}
341344
}
342345
}
343-
}
346+
}

internal/config/v_two.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@
140140
"emit_all_enum_values": {
141141
"type": "boolean"
142142
},
143+
"emit_sql_as_comment": {
144+
"type": "boolean"
145+
},
143146
"build_tags": {
144147
"type": "string"
145148
},

internal/endtoend/testdata/emit_sql_as_comment/stdlib/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/emit_sql_as_comment/stdlib/go/models.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/emit_sql_as_comment/stdlib/go/query.sql.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE bar (id serial not null);
2+
3+
-- name: ListBar :many
4+
-- Lists all bars
5+
SELECT id FROM (
6+
SELECT * FROM bar
7+
) bar;
8+
9+
-- name: RemoveBar :exec
10+
DELETE FROM bar WHERE id = $1;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"name": "querytest",
7+
"schema": "query.sql",
8+
"queries": "query.sql",
9+
"emit_sql_as_comment": true
10+
}
11+
]
12+
}

internal/endtoend/testdata/process_plugin_disabled/gen/codegen.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"query_parameter_limit": 1,
4444
"output_batch_file_name": "",
4545
"json_tags_id_uppercase": false,
46-
"omit_unused_structs": false
46+
"omit_unused_structs": false,
47+
"emit_sql_as_comment": false
4748
},
4849
"json": {
4950
"out": "",

0 commit comments

Comments
 (0)