Minor changes
This commit is contained in:
parent
a96c317795
commit
7e3400de02
10
pg/pg.go
10
pg/pg.go
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user