Skip to content

Consistent whitespace handling in Go query template #1301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.De
{{- end -}}
{{- if $.EmitPreparedQueries}}
row := q.queryRow(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
{{- else if $.EmitMethodsWithDBArgument -}}
{{- else if $.EmitMethodsWithDBArgument}}
row := db.QueryRowContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- else -}}
{{- else}}
row := q.db.QueryRowContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end}}
{{- if ne .Arg.Pair .Ret.Pair }}
Expand All @@ -52,9 +52,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.
{{- end -}}
{{- if $.EmitPreparedQueries}}
rows, err := q.query(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
{{- else if $.EmitMethodsWithDBArgument -}}
{{- else if $.EmitMethodsWithDBArgument}}
rows, err := db.QueryContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- else -}}
{{- else}}
rows, err := q.db.QueryContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end}}
if err != nil {
Expand Down Expand Up @@ -93,9 +93,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error {
{{- end -}}
{{- if $.EmitPreparedQueries}}
_, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
{{- else if $.EmitMethodsWithDBArgument -}}
{{- else if $.EmitMethodsWithDBArgument}}
_, err := db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- else -}}
{{- else}}
_, err := q.db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end}}
return err
Expand All @@ -109,12 +109,12 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error {
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error) {
{{- else -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error) {
{{end -}}
{{- end -}}
{{- if $.EmitPreparedQueries}}
result, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
{{- else if $.EmitMethodsWithDBArgument -}}
{{- else if $.EmitMethodsWithDBArgument}}
result, err := db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- else -}}
{{- else}}
result, err := q.db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end}}
if err != nil {
Expand All @@ -134,9 +134,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (sql.Resul
{{- end -}}
{{- if $.EmitPreparedQueries}}
return q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
{{- else if $.EmitMethodsWithDBArgument -}}
{{- else if $.EmitMethodsWithDBArgument}}
return db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- else -}}
{{- else}}
return q.db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end}}
}
Expand Down
126 changes: 126 additions & 0 deletions internal/endtoend/testdata/prepared_queries/mysql/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/prepared_queries/mysql/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions internal/endtoend/testdata/prepared_queries/mysql/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions internal/endtoend/testdata/prepared_queries/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE TABLE users (
id SERIAL NOT NULL,
first_name varchar(255) NOT NULL,
last_name varchar(255)
);

/* name: GetUserByID :one */
SELECT first_name, id, last_name FROM users WHERE id = sqlc.arg('target_id');

/* name: ListUsers :many */
SELECT first_name, last_name FROM users;

/* name: InsertNewUser :exec */
INSERT INTO users (first_name, last_name) VALUES (?, ?);

/* name: InsertNewUserWithResult :execresult */
INSERT INTO users (first_name, last_name) VALUES (?, ?);

/* name: DeleteUsersByName :execrows */
DELETE FROM users WHERE first_name = ? AND last_name = ?;
13 changes: 13 additions & 0 deletions internal/endtoend/testdata/prepared_queries/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"name": "querytest",
"path": "go",
"schema": "query.sql",
"queries": "query.sql",
"engine": "mysql",
"emit_prepared_queries": true
}
]
}
Loading