Skip to content

Commit b2e30cb

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 33398b7 commit b2e30cb

File tree

18 files changed

+346
-137
lines changed

18 files changed

+346
-137
lines changed

docs/reference/config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ The `gen` mapping supports the following keys:
146146
- `emit_all_enum_values`:
147147
- If true, emit a function per enum type
148148
that returns all valid enum values.
149+
- `emit_sql_as_comment`:
150+
- If true, emits the SQL statement as a code-block comment above the generated function, appending to any existing comments. Defaults to `false`.
149151
- `json_tags_id_uppercase`:
150152
- If true, "Id" in json tags will be uppercase. If false, will be camelcase. Defaults to `false`
151153
- `json_tags_case_style`:

internal/cmd/shim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
110110
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
111111
QueryParameterLimit: s.QueryParameterLimit,
112112
OmitUnusedStructs: s.OmitUnusedStructs,
113+
EmitSqlAsComment: s.EmitSqlAsComment,
113114
}
114115
}
115116

internal/codegen/golang/result.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,25 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
198198
constantName = sdk.LowerTitle(query.Name)
199199
}
200200

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

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ type SQLGo struct {
144144
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
145145
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
146146
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
147+
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
147148
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
148149
Package string `json:"package" yaml:"package"`
149150
Out string `json:"out" yaml:"out"`

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type v1PackageSettings struct {
3939
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
4040
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
4141
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
42+
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
4243
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
4344
SQLPackage string `json:"sql_package" yaml:"sql_package"`
4445
SQLDriver string `json:"sql_driver" yaml:"sql_driver"`
@@ -178,6 +179,7 @@ func (c *V1GenerateSettings) Translate() Config {
178179
OutputFilesSuffix: pkg.OutputFilesSuffix,
179180
QueryParameterLimit: pkg.QueryParameterLimit,
180181
OmitUnusedStructs: pkg.OmitUnusedStructs,
182+
EmitSqlAsComment: pkg.EmitSqlAsComment,
181183
},
182184
},
183185
StrictFunctionChecks: pkg.StrictFunctionChecks,

internal/config/v_one.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
"emit_all_enum_values": {
129129
"type": "boolean"
130130
},
131+
"emit_sql_as_comment": {
132+
"type": "boolean"
133+
},
131134
"json_tags_case_style": {
132135
"type": "string"
133136
},
@@ -334,4 +337,4 @@
334337
}
335338
}
336339
}
337-
}
340+
}

internal/config/v_two.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@
134134
"emit_all_enum_values": {
135135
"type": "boolean"
136136
},
137+
"emit_sql_as_comment": {
138+
"type": "boolean"
139+
},
137140
"json_tags_case_style": {
138141
"type": "string"
139142
},
@@ -433,4 +436,4 @@
433436
}
434437
}
435438
}
436-
}
439+
}

internal/endtoend/testdata/codegen_json/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": "gen",

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": "",

internal/endtoend/testdata/process_plugin_sqlc_gen_json/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)