Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
remvn committed Sep 27, 2024
1 parent dc29bb8 commit 78ef461
Showing 1 changed file with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,43 @@ func pgxInsert(db *database.Database, name string, bio pgtype.Text) (Author, err
// use collect helper function instead of scanning rows
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Author])
}

func pgxSelect(db *database.Database, id int) (Author, error) {
// notice that I dont select id
// and use RowToStructByNameLax to allows some of the column missing
query := `SELECT name, bio from author where id = @id`
args := pgx.NamedArgs{
"id": id,
}

rows, err := db.Pool.Query(context.Background(), query, args)
if err != nil {
return Author{}, err
}
defer rows.Close()

return pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[Author])
}

func pgxSelectAllId(db *database.Database) ([]int, error) {
query := `SELECT id from author`

rows, err := db.Pool.Query(context.Background(), query)
if err != nil {
return []int{}, err
}
defer rows.Close()

idArr := []int{}
idArr, err = pgx.AppendRows(idArr, rows, pgx.RowTo[int])
// use this if you dont need appending to slice
// idArr, err := pgx.CollectRows(rows, pgx.RowTo[int])
if err != nil {
return []int{}, err
}

return idArr, nil
}
```

#### Bulk insert with [Postgres's COPY](https://www.postgresql.org/docs/current/sql-copy.html):
Expand All @@ -153,7 +190,7 @@ func pgxCopyInsert(db *database.Database, authors []Author) (int64, error) {
```

More example can be found here: [Github
repo](https://github.com/remvn/go-pgx-sqlc)
repo](https://github.com/remvn/go-pgx-sqlc/blob/main/database/database_test.go)

### Summary

Expand Down

0 comments on commit 78ef461

Please sign in to comment.