Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions docs/reference/query-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ func (q *Queries) DeleteAllAuthors(ctx context.Context) (int64, error) {
}
```

## `:execlastinsertid`

The generated method will return the number generated by the database from the
[result](https://golang.org/pkg/database/sql/#Result) returned by
[ExecContext](https://golang.org/pkg/database/sql/#DB.ExecContext).

```sql
-- name: InsertAuthor :execlastinsertid
INSERT INTO authors (name) VALUES (?);
```

```go
func (q *Queries) InsertAuthor(ctx context.Context, name string) (int64, error) {
_, err := q.db.ExecContext(ctx, insertAuthor, name)
// ...
}
```

## `:many`

The generated method will return a slice of records via
Expand Down
9 changes: 9 additions & 0 deletions internal/codegen/golang/templates/pgx/interfaceCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
{{end -}}
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error)
{{- end}}
{{- if and (eq .Cmd ":execlastinsertid") ($dbtxParam) }}
{{range .Comments}}//{{.}}
{{end -}}
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error)
{{- else if eq .Cmd ":execlastinsertid" }}
{{range .Comments}}//{{.}}
{{end -}}
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error)
{{- end}}
{{- if and (eq .Cmd ":execresult") ($dbtxParam) }}
{{range .Comments}}//{{.}}
{{end -}}
Expand Down
17 changes: 17 additions & 0 deletions internal/codegen/golang/templates/pgx/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, er
}
{{end}}

{{if eq .Cmd ":execlastinsertid"}}
{{range .Comments}}//{{.}}
{{end -}}
{{if $.EmitMethodsWithDBArgument -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error) {
result, err := db.Exec(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- else -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error) {
result, err := q.db.Exec(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end}}
if err != nil {
return 0, err
}
return result.LastInsertId(), nil
}
{{end}}

{{if eq .Cmd ":execresult"}}
{{range .Comments}}//{{.}}
{{end -}}
Expand Down
9 changes: 9 additions & 0 deletions internal/codegen/golang/templates/stdlib/interfaceCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
{{end -}}
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error)
{{- end}}
{{- if and (eq .Cmd ":execlastinsertid") ($dbtxParam) }}
{{range .Comments}}//{{.}}
{{end -}}
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error)
{{- else if eq .Cmd ":execlastinsertid"}}
{{range .Comments}}//{{.}}
{{end -}}
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error)
{{- end}}
{{- if and (eq .Cmd ":execresult") ($dbtxParam) }}
{{range .Comments}}//{{.}}
{{end -}}
Expand Down
22 changes: 22 additions & 0 deletions internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, er
}
{{end}}

{{if eq .Cmd ":execlastinsertid"}}
{{range .Comments}}//{{.}}
{{end -}}
{{- if $.EmitMethodsWithDBArgument -}}
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 -}}
{{- if $.EmitPreparedQueries}}
result, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
{{- else if $.EmitMethodsWithDBArgument}}
result, err := db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- else}}
result, err := q.db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end}}
if err != nil {
return 0, err
}
return result.LastInsertId()
}
{{end}}

{{if eq .Cmd ":execresult"}}
{{range .Comments}}//{{.}}
{{end -}}
Expand Down
23 changes: 12 additions & 11 deletions internal/metadata/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ type CommentSyntax struct {
}

const (
CmdExec = ":exec"
CmdExecResult = ":execresult"
CmdExecRows = ":execrows"
CmdMany = ":many"
CmdOne = ":one"
CmdCopyFrom = ":copyfrom"
CmdBatchExec = ":batchexec"
CmdBatchMany = ":batchmany"
CmdBatchOne = ":batchone"
CmdExec = ":exec"
CmdExecResult = ":execresult"
CmdExecRows = ":execrows"
CmdExecLastInsertId = ":execlastinsertid"
CmdMany = ":many"
CmdOne = ":one"
CmdCopyFrom = ":copyfrom"
CmdBatchExec = ":batchexec"
CmdBatchMany = ":batchmany"
CmdBatchOne = ":batchone"
)

// A query name must be a valid Go identifier
Expand Down Expand Up @@ -83,15 +84,15 @@ func Parse(t string, commentStyle CommentSyntax) (string, string, error) {
part = part[:len(part)-1] // removes the trailing "*/" element
}
if len(part) == 2 {
return "", "", fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
return "", "", fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execlastinsertid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
}
if len(part) != 4 {
return "", "", fmt.Errorf("invalid query comment: %s", line)
}
queryName := part[2]
queryType := strings.TrimSpace(part[3])
switch queryType {
case CmdOne, CmdMany, CmdExec, CmdExecResult, CmdExecRows, CmdCopyFrom, CmdBatchExec, CmdBatchMany, CmdBatchOne:
case CmdOne, CmdMany, CmdExec, CmdExecResult, CmdExecRows, CmdExecLastInsertId, CmdCopyFrom, CmdBatchExec, CmdBatchMany, CmdBatchOne:
default:
return "", "", fmt.Errorf("invalid query type: %s", queryType)
}
Expand Down