Skip to content

docs: Update samples for HOW-TO GUIDES #1953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion docs/howto/delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

func New(db DBTX) *Queries {
Expand Down
6 changes: 3 additions & 3 deletions docs/howto/insert.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

func New(db DBTX) *Queries {
Expand Down Expand Up @@ -79,8 +79,8 @@ type Author struct {
}

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
QueryRowContext(context.Context, string, ...interface{}) error
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

func New(db DBTX) *Queries {
Expand Down
12 changes: 6 additions & 6 deletions docs/howto/prepared_query.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package db
import (
"context"
"database/sql"
"fmt"
)

type Record struct {
ID int
ID int32
}

type DBTX interface {
Expand All @@ -38,7 +39,7 @@ func Prepare(ctx context.Context, db DBTX) (*Queries, error) {
q := Queries{db: db}
var err error
if q.getRecordStmt, err = db.PrepareContext(ctx, getRecord); err != nil {
return nil, err
return nil, fmt.Errorf("error preparing query GetRecord: %w", err)
}
return &q, nil
}
Expand Down Expand Up @@ -73,11 +74,10 @@ SELECT id FROM records
WHERE id = $1
`

func (q *Queries) GetRecord(ctx context.Context, id int) (Record, error) {
func (q *Queries) GetRecord(ctx context.Context, id int32) (int32, error) {
row := q.queryRow(ctx, q.getRecordStmt, getRecord, id)
var i Record
err := row.Scan(&i.ID)
return i, err
err := row.Scan(&id)
return id, err
}
```

10 changes: 8 additions & 2 deletions docs/howto/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

func New(db DBTX) *Queries {
Expand All @@ -52,6 +52,12 @@ func (q *Queries) UpdateAuthorBios(ctx context.Context, bio string) error {
If your query has more than one parameter, your Go method will accept a
`Params` struct.

```sql
-- name: UpdateAuthor :exec
UPDATE authors SET bio = $2
WHERE id = $1;
```
Comment on lines +55 to +59
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although somewhat obvious, it would be helpful to have SQL examples for Multiple parameters as well as Single parameters.


```go
package db

Expand All @@ -61,7 +67,7 @@ import (
)

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) error
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

func New(db DBTX) *Queries {
Expand Down