Skip to content

Commit 4bbe1ca

Browse files
authored
Consistent whitespace handling in Go query template (#1301)
* codegen/golang: test prepared query output * codegen/golang: consistent whitespace in query tmpl
1 parent 8402860 commit 4bbe1ca

File tree

11 files changed

+559
-11
lines changed

11 files changed

+559
-11
lines changed

internal/codegen/golang/templates/stdlib/queryCode.tmpl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.De
2929
{{- end -}}
3030
{{- if $.EmitPreparedQueries}}
3131
row := q.queryRow(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
32-
{{- else if $.EmitMethodsWithDBArgument -}}
32+
{{- else if $.EmitMethodsWithDBArgument}}
3333
row := db.QueryRowContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
34-
{{- else -}}
34+
{{- else}}
3535
row := q.db.QueryRowContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
3636
{{- end}}
3737
{{- if ne .Arg.Pair .Ret.Pair }}
@@ -52,9 +52,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.
5252
{{- end -}}
5353
{{- if $.EmitPreparedQueries}}
5454
rows, err := q.query(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
55-
{{- else if $.EmitMethodsWithDBArgument -}}
55+
{{- else if $.EmitMethodsWithDBArgument}}
5656
rows, err := db.QueryContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
57-
{{- else -}}
57+
{{- else}}
5858
rows, err := q.db.QueryContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
5959
{{- end}}
6060
if err != nil {
@@ -93,9 +93,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error {
9393
{{- end -}}
9494
{{- if $.EmitPreparedQueries}}
9595
_, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
96-
{{- else if $.EmitMethodsWithDBArgument -}}
96+
{{- else if $.EmitMethodsWithDBArgument}}
9797
_, err := db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
98-
{{- else -}}
98+
{{- else}}
9999
_, err := q.db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
100100
{{- end}}
101101
return err
@@ -109,12 +109,12 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error {
109109
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error) {
110110
{{- else -}}
111111
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error) {
112-
{{end -}}
112+
{{- end -}}
113113
{{- if $.EmitPreparedQueries}}
114114
result, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
115-
{{- else if $.EmitMethodsWithDBArgument -}}
115+
{{- else if $.EmitMethodsWithDBArgument}}
116116
result, err := db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
117-
{{- else -}}
117+
{{- else}}
118118
result, err := q.db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
119119
{{- end}}
120120
if err != nil {
@@ -134,9 +134,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (sql.Resul
134134
{{- end -}}
135135
{{- if $.EmitPreparedQueries}}
136136
return q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
137-
{{- else if $.EmitMethodsWithDBArgument -}}
137+
{{- else if $.EmitMethodsWithDBArgument}}
138138
return db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
139-
{{- else -}}
139+
{{- else}}
140140
return q.db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
141141
{{- end}}
142142
}

internal/endtoend/testdata/prepared_queries/mysql/go/db.go

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

internal/endtoend/testdata/prepared_queries/mysql/go/models.go

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

internal/endtoend/testdata/prepared_queries/mysql/go/query.sql.go

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CREATE TABLE users (
2+
id SERIAL NOT NULL,
3+
first_name varchar(255) NOT NULL,
4+
last_name varchar(255)
5+
);
6+
7+
/* name: GetUserByID :one */
8+
SELECT first_name, id, last_name FROM users WHERE id = sqlc.arg('target_id');
9+
10+
/* name: ListUsers :many */
11+
SELECT first_name, last_name FROM users;
12+
13+
/* name: InsertNewUser :exec */
14+
INSERT INTO users (first_name, last_name) VALUES (?, ?);
15+
16+
/* name: InsertNewUserWithResult :execresult */
17+
INSERT INTO users (first_name, last_name) VALUES (?, ?);
18+
19+
/* name: DeleteUsersByName :execrows */
20+
DELETE FROM users WHERE first_name = ? AND last_name = ?;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"name": "querytest",
6+
"path": "go",
7+
"schema": "query.sql",
8+
"queries": "query.sql",
9+
"engine": "mysql",
10+
"emit_prepared_queries": true
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)