@@ -45,24 +45,28 @@ func (q *Queries) CreateAuthor(ctx context.Context, bio string) error {
4545sqlc has full support for the ` RETURNING ` statement.
4646
4747``` sql
48+ -- Example queries for sqlc
4849CREATE TABLE authors (
49- id SERIAL PRIMARY KEY ,
50- bio text NOT NULL
50+ id BIGSERIAL PRIMARY KEY ,
51+ name text NOT NULL ,
52+ bio text
5153);
5254
53- -- name: Delete :exec
54- DELETE FROM authors WHERE id = $1 ;
55-
56- -- name: DeleteAffected :execrows
57- DELETE FROM authors WHERE id = $1 ;
55+ -- name: CreateAuthor :one
56+ INSERT INTO authors (
57+ name, bio
58+ ) VALUES (
59+ $1 , $2
60+ )
61+ RETURNING * ;
5862
59- -- name: DeleteID :one
60- DELETE FROM authors WHERE id = $1
63+ -- name: CreateAuthorAndReturnId :one
64+ INSERT INTO authors (
65+ name, bio
66+ ) VALUES (
67+ $1 , $2
68+ )
6169RETURNING id;
62-
63- -- name: DeleteAuthor :one
64- DELETE FROM authors WHERE id = $1
65- RETURNING * ;
6670```
6771
6872``` go
@@ -73,67 +77,46 @@ import (
7377 " database/sql"
7478)
7579
76- type Author struct {
77- ID int
78- Bio string
79- }
80-
81- type DBTX interface {
82- ExecContext (context.Context , string , ...interface {}) (sql.Result , error )
83- QueryRowContext (context.Context , string , ...interface {}) *sql.Row
84- }
85-
86- func New (db DBTX ) *Queries {
87- return &Queries{db: db}
88- }
89-
90- type Queries struct {
91- db DBTX
92- }
93-
94- const delete = ` -- name: Delete :exec
95- DELETE FROM authors WHERE id = $1
80+ const createAuthor = ` -- name: CreateAuthor :one
81+ INSERT INTO authors (
82+ name, bio
83+ ) VALUES (
84+ $1, $2
85+ )
86+ RETURNING id, name, bio
9687`
9788
98- func ( q * Queries ) Delete ( ctx context . Context , id int ) error {
99- _ , err := q. db . ExecContext (ctx, delete, id)
100- return err
89+ type CreateAuthorParams struct {
90+ Name string
91+ Bio sql. NullString
10192}
10293
103- const deleteAffected = ` -- name: DeleteAffected :execrows
104- DELETE FROM authors WHERE id = $1
105- `
106-
107- func (q *Queries ) DeleteAffected (ctx context .Context , id int ) (int64 , error ) {
108- result , err := q.db .ExecContext (ctx, deleteAffected, id)
109- if err != nil {
110- return 0 , err
111- }
112- return result.RowsAffected ()
94+ func (q *Queries ) CreateAuthor (ctx context .Context , arg CreateAuthorParams ) (Author , error ) {
95+ row := q.db .QueryRowContext (ctx, createAuthor, arg.Name , arg.Bio )
96+ var i Author
97+ err := row.Scan (&i.ID , &i.Name , &i.Bio )
98+ return i, err
11399}
114100
115- const deleteID = ` -- name: DeleteID :one
116- DELETE FROM authors WHERE id = $1
101+ const createAuthorAndReturnId = ` -- name: CreateAuthorAndReturnId :one
102+ INSERT INTO authors (
103+ name, bio
104+ ) VALUES (
105+ $1, $2
106+ )
117107RETURNING id
118108`
119109
120- func (q *Queries ) DeleteID (ctx context .Context , id int ) (int , error ) {
121- row := q.db .QueryRowContext (ctx, deleteID, id)
122- var i int
123- err := row.Scan (&i)
124- return i, err
110+ type CreateAuthorAndReturnIdParams struct {
111+ Name string
112+ Bio sql.NullString
125113}
126114
127- const deleteAuthor = ` -- name: DeleteAuthor :one
128- DELETE FROM authors WHERE id = $1
129- RETURNING id, bio
130- `
131-
132- func (q *Queries ) DeleteAuthor (ctx context .Context , id int ) (Author , error ) {
133- row := q.db .QueryRowContext (ctx, deleteAuthor, id)
134- var i Author
135- err := row.Scan (&i.ID , &i.Bio )
136- return i, err
115+ func (q *Queries ) CreateAuthorAndReturnId (ctx context .Context , arg CreateAuthorAndReturnIdParams ) (int64 , error ) {
116+ row := q.db .QueryRowContext (ctx, createAuthorAndReturnId, arg.Name , arg.Bio )
117+ var id int64
118+ err := row.Scan (&id)
119+ return id, err
137120}
138121```
139122
0 commit comments