Skip to content

Commit 2c11cfc

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 b3c3474 commit 2c11cfc

File tree

13 files changed

+332
-133
lines changed

13 files changed

+332
-133
lines changed

internal/cmd/shim.go

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

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
@@ -159,6 +159,7 @@ type SQLGo struct {
159159
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
160160
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
161161
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
162+
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
162163
}
163164

164165
type SQLJSON struct {

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type v1PackageSettings struct {
5252
StrictOrderBy *bool `json:"strict_order_by" yaml:"strict_order_by"`
5353
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
5454
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
55+
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
5556
Rules []string `json:"rules" yaml:"rules"`
5657
}
5758

@@ -176,6 +177,7 @@ func (c *V1GenerateSettings) Translate() Config {
176177
OutputFilesSuffix: pkg.OutputFilesSuffix,
177178
QueryParameterLimit: pkg.QueryParameterLimit,
178179
OmitUnusedStructs: pkg.OmitUnusedStructs,
180+
EmitSqlAsComment: pkg.EmitSqlAsComment,
179181
},
180182
},
181183
StrictFunctionChecks: pkg.StrictFunctionChecks,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"query_parameter_limit": 1,
4343
"output_batch_file_name": "",
4444
"json_tags_id_uppercase": false,
45-
"omit_unused_structs": false
45+
"omit_unused_structs": false,
46+
"emit_sql_as_comment": false
4647
},
4748
"json": {
4849
"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+
}

0 commit comments

Comments
 (0)