2
0

Replace QueryFunc with ForEachScannedRow

This commit is contained in:
Jack Christensen
2022-07-07 20:29:04 -05:00
parent a86f4f3db9
commit 76946fb5a3
16 changed files with 301 additions and 469 deletions
+12 -17
View File
@@ -63,6 +63,18 @@ pgx implements Query and Scan in the familiar database/sql style.
// No errors found - do something with sum
ForEachScannedRow can be used to execute a callback function for every row. This is often easier than iterating over rows directly.
var sum, n int32
rows, _ := conn.Query(context.Background(), "select generate_series(1,$1)", 10)
_, err := pgx.ForEachScannedRow(rows, []any{&n}, func(pgx.QueryFuncRow) error {
sum += n
return nil
})
if err != nil {
return err
}
pgx also implements QueryRow in the same style as database/sql.
var name string
@@ -82,23 +94,6 @@ Use Exec to execute a query that does not return a result set.
return errors.New("No row found to delete")
}
QueryFunc can be used to execute a callback function for every row. This is often easier to use than Query.
var sum, n int32
_, err = conn.QueryFunc(
context.Background(),
"select generate_series(1,$1)",
[]any{10},
[]any{&n},
func(pgx.QueryFuncRow) error {
sum += n
return nil
},
)
if err != nil {
return err
}
Base Type Mapping
pgx maps between all common base types directly between Go and PostgreSQL. In particular: