Skip to content

Commit 0a71841

Browse files
authored
docs: Add more examples to updating rows (#1499)
1 parent 30253d3 commit 0a71841

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

docs/howto/update.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,53 @@ CREATE TABLE authors (
55
id SERIAL PRIMARY KEY,
66
bio text NOT NULL
77
);
8+
```
89

9-
-- name: UpdateAuthor :exec
10-
UPDATE authors SET bio = $2
11-
WHERE id = $1;
10+
## Single parameter
11+
12+
If your query has a single parameter, your Go method will also have a single
13+
parameter.
14+
15+
```sql
16+
-- name: UpdateAuthorBios :exec
17+
UPDATE authors SET bio = $1;
18+
```
19+
20+
```go
21+
package db
22+
23+
import (
24+
"context"
25+
"database/sql"
26+
)
27+
28+
type DBTX interface {
29+
ExecContext(context.Context, string, ...interface{}) error
30+
}
31+
32+
func New(db DBTX) *Queries {
33+
return &Queries{db: db}
34+
}
35+
36+
type Queries struct {
37+
db DBTX
38+
}
39+
40+
const updateAuthorBios = `-- name: UpdateAuthorBios :exec
41+
UPDATE authors SET bio = $1
42+
`
43+
44+
func (q *Queries) UpdateAuthorBios(ctx context.Context, bio string) error {
45+
_, err := q.db.ExecContext(ctx, updateAuthorBios, bio)
46+
return err
47+
}
1248
```
1349

50+
## Multiple parameters
51+
52+
If your query has more than one parameter, your Go method will accept a
53+
`Params` struct.
54+
1455
```go
1556
package db
1657

@@ -36,8 +77,14 @@ UPDATE authors SET bio = $2
3677
WHERE id = $1
3778
`
3879

39-
func (q *Queries) UpdateAuthor(ctx context.Context, id int, bio string) error {
40-
_, err := q.db.ExecContext(ctx, updateAuthor, id, bio)
80+
type UpdateAuthorParams struct {
81+
ID int32
82+
Bio string
83+
}
84+
85+
func (q *Queries) UpdateAuthor(ctx context.Context, arg UpdateAuthorParams) error {
86+
_, err := q.db.ExecContext(ctx, updateAuthor, arg.ID, arg.Bio)
4187
return err
4288
}
4389
```
90+

0 commit comments

Comments
 (0)