Skip to content

Commit a2c8d09

Browse files
authored
gen: Turn SQL comments into Go comments (#136)
Remove comments from SQL and add them as Go comments
1 parent 4bdd9a3 commit a2c8d09

5 files changed

Lines changed: 41 additions & 7 deletions

File tree

internal/dinosql/gen.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func (v GoQueryValue) Scan() string {
143143
// A struct used to generate methods and fields on the Queries struct
144144
type GoQuery struct {
145145
Cmd string
146+
Comments []string
146147
MethodName string
147148
FieldName string
148149
ConstantName string
@@ -646,6 +647,7 @@ func (r Result) GoQueries() []GoQuery {
646647
MethodName: query.Name,
647648
SourceName: query.Filename,
648649
SQL: code,
650+
Comments: query.Comments,
649651
}
650652

651653
if len(query.Params) == 1 {
@@ -862,7 +864,8 @@ import (
862864
863865
{{range .GoQueries}}
864866
{{if eq .SourceName $.SourceName}}
865-
const {{.ConstantName}} = {{$.Q}}{{.SQL}}
867+
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
868+
{{.SQL}}
866869
{{$.Q}}
867870
868871
{{if .Arg.EmitStruct}}
@@ -880,6 +883,7 @@ type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
880883
{{end}}
881884
882885
{{if eq .Cmd ":one"}}
886+
{{range .Comments}}//{{.}}{{end}}
883887
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.Type}}, error) {
884888
{{- if $.EmitPreparedQueries}}
885889
row := q.queryRow(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
@@ -893,6 +897,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.Ty
893897
{{end}}
894898
895899
{{if eq .Cmd ":many"}}
900+
{{range .Comments}}//{{.}}{{end}}
896901
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.Type}}, error) {
897902
{{- if $.EmitPreparedQueries}}
898903
rows, err := q.query(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
@@ -922,6 +927,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.
922927
{{end}}
923928
924929
{{if eq .Cmd ":exec"}}
930+
{{range .Comments}}//{{.}}{{end}}
925931
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error {
926932
{{- if $.EmitPreparedQueries}}
927933
_, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
@@ -933,6 +939,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error {
933939
{{end}}
934940
935941
{{if eq .Cmd ":execrows"}}
942+
{{range .Comments}}//{{.}}{{end}}
936943
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error) {
937944
{{- if $.EmitPreparedQueries}}
938945
result, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})

internal/dinosql/parser.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dinosql
22

33
import (
4+
"bufio"
45
"errors"
56
"fmt"
67
"io/ioutil"
@@ -153,11 +154,12 @@ type Parameter struct {
153154
// Name and Cmd may be empty
154155
// Maybe I don't need the SQL string if I have the raw Stmt?
155156
type Query struct {
156-
SQL string
157-
Columns []core.Column
158-
Params []Parameter
159-
Name string
160-
Cmd string // TODO: Pick a better name. One of: one, many, exec, execrows
157+
SQL string
158+
Columns []core.Column
159+
Params []Parameter
160+
Name string
161+
Cmd string // TODO: Pick a better name. One of: one, many, exec, execrows
162+
Comments []string
161163

162164
// XXX: Hack
163165
NeedsEdit bool
@@ -409,16 +411,38 @@ func parseQuery(c core.Catalog, stmt nodes.Node, source string) (*Query, error)
409411
return nil, err
410412
}
411413

414+
trimmed, comments, err := stripComments(rawSQL)
415+
if err != nil {
416+
return nil, err
417+
}
418+
412419
return &Query{
413420
Cmd: cmd,
421+
Comments: comments,
414422
Name: name,
415423
Params: params,
416424
Columns: cols,
417-
SQL: rawSQL,
425+
SQL: trimmed,
418426
NeedsEdit: needsEdit(stmt),
419427
}, nil
420428
}
421429

430+
func stripComments(sql string) (string, []string, error) {
431+
s := bufio.NewScanner(strings.NewReader(sql))
432+
var lines, comments []string
433+
for s.Scan() {
434+
if strings.HasPrefix(s.Text(), "-- name:") {
435+
continue
436+
}
437+
if strings.HasPrefix(s.Text(), "--") {
438+
comments = append(comments, strings.TrimPrefix(s.Text(), "--"))
439+
continue
440+
}
441+
lines = append(lines, s.Text())
442+
}
443+
return strings.Join(lines, "\n"), comments, s.Err()
444+
}
445+
422446
type QueryCatalog struct {
423447
catalog core.Catalog
424448
ctes map[string]core.Table

internal/dinosql/testdata/ondeck/city.sql.go

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

internal/dinosql/testdata/ondeck/prepared/city.sql.go

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

internal/dinosql/testdata/ondeck/query/city.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ FROM city
99
WHERE slug = $1;
1010

1111
-- name: CreateCity :one
12+
-- Create a new city. The slug must be unique
1213
INSERT INTO city (
1314
name,
1415
slug

0 commit comments

Comments
 (0)