Skip to content
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
9 changes: 8 additions & 1 deletion internal/dinosql/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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}}
Expand All @@ -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}})
Expand All @@ -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}})
Expand Down Expand Up @@ -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}})
Expand All @@ -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}})
Expand Down
36 changes: 30 additions & 6 deletions internal/dinosql/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dinosql

import (
"bufio"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/dinosql/testdata/ondeck/city.sql.go

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

1 change: 1 addition & 0 deletions internal/dinosql/testdata/ondeck/prepared/city.sql.go

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

1 change: 1 addition & 0 deletions internal/dinosql/testdata/ondeck/query/city.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down