@@ -5,12 +5,53 @@ CREATE TABLE authors (
5
5
id SERIAL PRIMARY KEY ,
6
6
bio text NOT NULL
7
7
);
8
+ ```
8
9
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
+ }
12
48
```
13
49
50
+ ## Multiple parameters
51
+
52
+ If your query has more than one parameter, your Go method will accept a
53
+ ` Params ` struct.
54
+
14
55
``` go
15
56
package db
16
57
@@ -36,8 +77,14 @@ UPDATE authors SET bio = $2
36
77
WHERE id = $1
37
78
`
38
79
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 )
41
87
return err
42
88
}
43
89
```
90
+
0 commit comments