Skip to content

Commit de8ae61

Browse files
jasoncabotandrewmbenton
authored andcommitted
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 738d153 commit de8ae61

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
@@ -165,6 +165,8 @@ The `gen` mapping supports the following keys:
165165
that returns all valid enum values.
166166
- `build_tags`:
167167
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
168+
- `emit_sql_as_comment`:
169+
- If true, emits the SQL statement as a code-block comment above the generated function, appending to any existing comments. Defaults to `false`.
168170
- `json_tags_id_uppercase`:
169171
- If true, "Id" in json tags will be uppercase. If false, will be camelcase. Defaults to `false`
170172
- `json_tags_case_style`:

internal/codegen/golang/opts/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Options struct {
4242
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
4343
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
4444
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
45+
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
4546
}
4647

4748
type GlobalOptions struct {

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
@@ -58,6 +58,7 @@ type v1PackageSettings struct {
5858
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
5959
Rules []string `json:"rules" yaml:"rules"`
6060
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
61+
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
6162
}
6263

6364
func v1ParseConfig(rd io.Reader) (Config, error) {
@@ -166,6 +167,7 @@ func (c *V1GenerateSettings) Translate() Config {
166167
OmitSqlcVersion: pkg.OmitSqlcVersion,
167168
OmitUnusedStructs: pkg.OmitUnusedStructs,
168169
BuildTags: pkg.BuildTags,
170+
EmitSqlAsComment: pkg.EmitSqlAsComment,
169171
},
170172
},
171173
StrictFunctionChecks: pkg.StrictFunctionChecks,

internal/config/v_one.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@
134134
"build_tags": {
135135
"type": "string"
136136
},
137+
"emit_sql_as_comment": {
138+
"type": "boolean"
139+
},
137140
"json_tags_case_style": {
138141
"type": "string"
139142
},
@@ -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
@@ -143,6 +143,9 @@
143143
"build_tags": {
144144
"type": "string"
145145
},
146+
"emit_sql_as_comment": {
147+
"type": "boolean"
148+
},
146149
"json_tags_case_style": {
147150
"type": "string"
148151
},

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)