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