Minor changes

This commit is contained in:
Dmitry Anderson 2024-11-12 09:53:46 +01:00
parent a96c317795
commit 3586f61719

View File

@ -25,14 +25,13 @@ type PgxQuerier interface {
// Usage:
//
// type User struct {
// id int
// name string
// Id int `db:"id"`
// Name string `db:"name"`
// }
//
// db := pgx.Connect(context.Background(), "<url>")
// users, err := pgUtils.Select[User](context.Background(), db, "SELECT * FROM users")
// users, err := pgUtils.Select[User](context.Background(), db, "SELECT id, name FROM users")
func Select[T any](ctx context.Context, db PgxQuerier, query string, args ...any) (out []*T, err error) {
out = []*T{}
rows, err := db.Query(ctx, query, args)
if err != nil {
switch {
@ -47,7 +46,8 @@ func Select[T any](ctx context.Context, db PgxQuerier, query string, args ...any
for i, fd := range rows.FieldDescriptions() {
columns[i] = fd.Name
}
itemFieldPtrs := make([]interface{}, len(columns))
itemFieldPtrs := make([]any, len(columns))
out = []*T{}
defer rows.Close()
for rows.Next() {