diff --git a/internal/dinosql/gen.go b/internal/dinosql/gen.go index 24e1563112..22a7bcfe05 100644 --- a/internal/dinosql/gen.go +++ b/internal/dinosql/gen.go @@ -143,6 +143,7 @@ func (v GoQueryValue) Scan() string { // A struct used to generate methods and fields on the Queries struct type GoQuery struct { Cmd string + Comments []string MethodName string FieldName string ConstantName string @@ -646,6 +647,7 @@ func (r Result) GoQueries() []GoQuery { MethodName: query.Name, SourceName: query.Filename, SQL: code, + Comments: query.Comments, } if len(query.Params) == 1 { @@ -862,7 +864,8 @@ import ( {{range .GoQueries}} {{if eq .SourceName $.SourceName}} -const {{.ConstantName}} = {{$.Q}}{{.SQL}} +const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}} +{{.SQL}} {{$.Q}} {{if .Arg.EmitStruct}} @@ -880,6 +883,7 @@ type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}} {{end}} {{if eq .Cmd ":one"}} +{{range .Comments}}//{{.}}{{end}} func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.Type}}, error) { {{- if $.EmitPreparedQueries}} row := q.queryRow(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}}) @@ -893,6 +897,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.Ty {{end}} {{if eq .Cmd ":many"}} +{{range .Comments}}//{{.}}{{end}} func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.Type}}, error) { {{- if $.EmitPreparedQueries}} rows, err := q.query(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}}) @@ -922,6 +927,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret. {{end}} {{if eq .Cmd ":exec"}} +{{range .Comments}}//{{.}}{{end}} func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error { {{- if $.EmitPreparedQueries}} _, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}}) @@ -933,6 +939,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error { {{end}} {{if eq .Cmd ":execrows"}} +{{range .Comments}}//{{.}}{{end}} func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error) { {{- if $.EmitPreparedQueries}} result, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}}) diff --git a/internal/dinosql/parser.go b/internal/dinosql/parser.go index fbea71775a..b083cbdf68 100644 --- a/internal/dinosql/parser.go +++ b/internal/dinosql/parser.go @@ -1,6 +1,7 @@ package dinosql import ( + "bufio" "errors" "fmt" "io/ioutil" @@ -153,11 +154,12 @@ type Parameter struct { // Name and Cmd may be empty // Maybe I don't need the SQL string if I have the raw Stmt? type Query struct { - SQL string - Columns []core.Column - Params []Parameter - Name string - Cmd string // TODO: Pick a better name. One of: one, many, exec, execrows + SQL string + Columns []core.Column + Params []Parameter + Name string + Cmd string // TODO: Pick a better name. One of: one, many, exec, execrows + Comments []string // XXX: Hack NeedsEdit bool @@ -409,16 +411,38 @@ func parseQuery(c core.Catalog, stmt nodes.Node, source string) (*Query, error) return nil, err } + trimmed, comments, err := stripComments(rawSQL) + if err != nil { + return nil, err + } + return &Query{ Cmd: cmd, + Comments: comments, Name: name, Params: params, Columns: cols, - SQL: rawSQL, + SQL: trimmed, NeedsEdit: needsEdit(stmt), }, nil } +func stripComments(sql string) (string, []string, error) { + s := bufio.NewScanner(strings.NewReader(sql)) + var lines, comments []string + for s.Scan() { + if strings.HasPrefix(s.Text(), "-- name:") { + continue + } + if strings.HasPrefix(s.Text(), "--") { + comments = append(comments, strings.TrimPrefix(s.Text(), "--")) + continue + } + lines = append(lines, s.Text()) + } + return strings.Join(lines, "\n"), comments, s.Err() +} + type QueryCatalog struct { catalog core.Catalog ctes map[string]core.Table diff --git a/internal/dinosql/testdata/ondeck/city.sql.go b/internal/dinosql/testdata/ondeck/city.sql.go index 02fced1897..e8ba44d6e7 100644 --- a/internal/dinosql/testdata/ondeck/city.sql.go +++ b/internal/dinosql/testdata/ondeck/city.sql.go @@ -22,6 +22,7 @@ type CreateCityParams struct { Slug string `json:"slug"` } +// Create a new city. The slug must be unique func (q *Queries) CreateCity(ctx context.Context, arg CreateCityParams) (City, error) { row := q.db.QueryRowContext(ctx, createCity, arg.Name, arg.Slug) var i City diff --git a/internal/dinosql/testdata/ondeck/prepared/city.sql.go b/internal/dinosql/testdata/ondeck/prepared/city.sql.go index d7986b3384..6e73ecef45 100644 --- a/internal/dinosql/testdata/ondeck/prepared/city.sql.go +++ b/internal/dinosql/testdata/ondeck/prepared/city.sql.go @@ -22,6 +22,7 @@ type CreateCityParams struct { Slug string } +// Create a new city. The slug must be unique func (q *Queries) CreateCity(ctx context.Context, arg CreateCityParams) (City, error) { row := q.queryRow(ctx, q.createCityStmt, createCity, arg.Name, arg.Slug) var i City diff --git a/internal/dinosql/testdata/ondeck/query/city.sql b/internal/dinosql/testdata/ondeck/query/city.sql index 58335ed12c..e04ea5cf0f 100644 --- a/internal/dinosql/testdata/ondeck/query/city.sql +++ b/internal/dinosql/testdata/ondeck/query/city.sql @@ -9,6 +9,7 @@ FROM city WHERE slug = $1; -- name: CreateCity :one +-- Create a new city. The slug must be unique INSERT INTO city ( name, slug