Skip to content

Commit 2d8892b

Browse files
authored
Adding LastInsertId for Go (#1484)
* adding :execlastinsertid for golang
1 parent bb9b258 commit 2d8892b

File tree

10 files changed

+140
-2
lines changed

10 files changed

+140
-2
lines changed

docs/reference/query-annotations.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ func (q *Queries) DeleteAllAuthors(ctx context.Context) (int64, error) {
5959
}
6060
```
6161

62+
## `:execlastid`
63+
64+
The generated method will return the number generated by the database from the
65+
[result](https://golang.org/pkg/database/sql/#Result) returned by
66+
[ExecContext](https://golang.org/pkg/database/sql/#DB.ExecContext).
67+
68+
```sql
69+
-- name: InsertAuthor :execlastid
70+
INSERT INTO authors (name) VALUES (?);
71+
```
72+
73+
```go
74+
func (q *Queries) InsertAuthor(ctx context.Context, name string) (int64, error) {
75+
_, err := q.db.ExecContext(ctx, insertAuthor, name)
76+
// ...
77+
}
78+
```
79+
6280
## `:many`
6381

6482
The generated method will return a slice of records via

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
{{end -}}
3939
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error)
4040
{{- end}}
41+
{{- if and (eq .Cmd ":execlastid") ($dbtxParam) }}
42+
{{range .Comments}}//{{.}}
43+
{{end -}}
44+
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error)
45+
{{- else if eq .Cmd ":execlastid"}}
46+
{{range .Comments}}//{{.}}
47+
{{end -}}
48+
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error)
49+
{{- end}}
4150
{{- if and (eq .Cmd ":execresult") ($dbtxParam) }}
4251
{{range .Comments}}//{{.}}
4352
{{end -}}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,28 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, er
124124
}
125125
{{end}}
126126

127+
{{if eq .Cmd ":execlastid"}}
128+
{{range .Comments}}//{{.}}
129+
{{end -}}
130+
{{- if $.EmitMethodsWithDBArgument -}}
131+
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error) {
132+
{{- else -}}
133+
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error) {
134+
{{- end -}}
135+
{{- if $.EmitPreparedQueries}}
136+
result, err := q.exec(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
137+
{{- else if $.EmitMethodsWithDBArgument}}
138+
result, err := db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
139+
{{- else}}
140+
result, err := q.db.ExecContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
141+
{{- end}}
142+
if err != nil {
143+
return 0, err
144+
}
145+
return result.LastInsertId()
146+
}
147+
{{end}}
148+
127149
{{if eq .Cmd ":execresult"}}
128150
{{range .Comments}}//{{.}}
129151
{{end -}}

internal/endtoend/testdata/exec_lastid/go_postgresql_stdlib/go/db.go

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

internal/endtoend/testdata/exec_lastid/go_postgresql_stdlib/go/models.go

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

internal/endtoend/testdata/exec_lastid/go_postgresql_stdlib/go/querier.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/exec_lastid/go_postgresql_stdlib/go/query.sql.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE bar (id integer(10) NOT NULL AUTO_INCREMENT PRIMARY KEY);
2+
3+
-- name: InsertBar :execlastid
4+
INSERT INTO bar () VALUES ();
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+
"path": "go",
6+
"name": "querytest",
7+
"engine": "mysql",
8+
"schema": "query.sql",
9+
"queries": "query.sql",
10+
"emit_interface": true
11+
}
12+
]
13+
}

internal/metadata/meta.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
CmdExec = ":exec"
1717
CmdExecResult = ":execresult"
1818
CmdExecRows = ":execrows"
19+
CmdExecLastId = ":execlastid"
1920
CmdMany = ":many"
2021
CmdOne = ":one"
2122
CmdCopyFrom = ":copyfrom"
@@ -83,15 +84,15 @@ func Parse(t string, commentStyle CommentSyntax) (string, string, error) {
8384
part = part[:len(part)-1] // removes the trailing "*/" element
8485
}
8586
if len(part) == 2 {
86-
return "", "", fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
87+
return "", "", fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
8788
}
8889
if len(part) != 4 {
8990
return "", "", fmt.Errorf("invalid query comment: %s", line)
9091
}
9192
queryName := part[2]
9293
queryType := strings.TrimSpace(part[3])
9394
switch queryType {
94-
case CmdOne, CmdMany, CmdExec, CmdExecResult, CmdExecRows, CmdCopyFrom, CmdBatchExec, CmdBatchMany, CmdBatchOne:
95+
case CmdOne, CmdMany, CmdExec, CmdExecResult, CmdExecRows, CmdExecLastId, CmdCopyFrom, CmdBatchExec, CmdBatchMany, CmdBatchOne:
9596
default:
9697
return "", "", fmt.Errorf("invalid query type: %s", queryType)
9798
}

0 commit comments

Comments
 (0)