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 78ef461 commit 5ff4849
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/_default/markup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ goldmark:
unsafe: true
highlight:
noClasses: true
style: dracula
tableOfContents:
startLevel: 2
endLevel: 4
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ query.
`pgx` also comes with plenty of useful tools which made it easier to
write raw SQL:

#### Named argument & collect rows:
#### 1. Named argument & collect rows:

* **Named arguments** (@id, @name, @description...) as placeholder instead of
positional placeholder ($1, $2, $3...):
Expand All @@ -106,6 +106,7 @@ write raw SQL:
manually:
[pgx.RowToStructByName](https://pkg.go.dev/github.com/jackc/pgx/v5#RowToStructByName)

*Insert with named argument*
```go
type Author struct {
Id int `db:"id"`
Expand All @@ -129,7 +130,10 @@ 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])
}
```

*Select and collect one row*
```go
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
Expand All @@ -146,7 +150,10 @@ func pgxSelect(db *database.Database, id int) (Author, error) {

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

*Select, collect and append to slice*
```go
func pgxSelectAllId(db *database.Database) ([]int, error) {
query := `SELECT id from author`

Expand All @@ -156,10 +163,10 @@ func pgxSelectAllId(db *database.Database) ([]int, error) {
}
defer rows.Close()

// use this if you dont need appending to slice
// idArr, err := pgx.CollectRows(rows, pgx.RowTo[int])
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
}
Expand All @@ -168,7 +175,7 @@ func pgxSelectAllId(db *database.Database) ([]int, error) {
}
```

#### Bulk insert with [Postgres's COPY](https://www.postgresql.org/docs/current/sql-copy.html):
#### 2. Bulk insert with [Postgres's COPY](https://www.postgresql.org/docs/current/sql-copy.html):

```go
func pgxCopyInsert(db *database.Database, authors []Author) (int64, error) {
Expand All @@ -189,7 +196,7 @@ func pgxCopyInsert(db *database.Database, authors []Author) (int64, error) {
}
```

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

### Summary
Expand Down

0 comments on commit 5ff4849

Please sign in to comment.