diff options
Diffstat (limited to '')
-rw-r--r-- | database.go | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/database.go b/database.go index 14e19c0..0459fb7 100644 --- a/database.go +++ b/database.go @@ -5,27 +5,29 @@ package main import ( "context" + + "github.com/jackc/pgx/v5" ) // query_list is a helper function that executes a query and returns a list of // results. -func query_list[T any](ctx context.Context, query string, args ...any) ([]T, error) { - rows, err := database.Query(ctx, query, args...) - if err != nil { +func query_list[T any](ctx context.Context, query string, args ...any) (result []T, err error) { + var rows pgx.Rows + + if rows, err = database.Query(ctx, query, args...); err != nil { return nil, err } defer rows.Close() - var result []T for rows.Next() { var item T - if err := rows.Scan(&item); err != nil { + if err = rows.Scan(&item); err != nil { return nil, err } result = append(result, item) } - if err := rows.Err(); err != nil { + if err = rows.Err(); err != nil { return nil, err } @@ -34,17 +36,17 @@ func query_list[T any](ctx context.Context, query string, args ...any) ([]T, err // query_name_desc_list is a helper function that executes a query and returns a // list of name_desc_t results. -func query_name_desc_list(ctx context.Context, query string, args ...any) ([]name_desc_t, error) { - rows, err := database.Query(ctx, query, args...) - if err != nil { +func query_name_desc_list(ctx context.Context, query string, args ...any) (result []name_desc_t, err error) { + var rows pgx.Rows + + if rows, err = database.Query(ctx, query, args...); err != nil { return nil, err } defer rows.Close() - result := []name_desc_t{} for rows.Next() { var name, description string - if err := rows.Scan(&name, &description); err != nil { + if err = rows.Scan(&name, &description); err != nil { return nil, err } result = append(result, name_desc_t{name, description}) |