diff options
| author | Jonathan Amsterdam <jba@google.com> | 2022-03-04 13:48:07 -0500 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2022-03-07 15:51:22 +0000 |
| commit | 2beb68e7d8664b09d87cabe67943dc13b1131207 (patch) | |
| tree | 048de29f816f1cbd73bac714abbdd2038dc9c49c /internal/database/database.go | |
| parent | 65d33554b34b666d37b22bed7de136b562d5dba8 (diff) | |
| download | go-x-pkgsite-2beb68e7d8664b09d87cabe67943dc13b1131207.tar.xz | |
internal/database: use generics for Collect functions
Reimplement some reflection-based collection functions to
use generics.
In some cases we still need reflection, but at least we
can provide a type-safe wrapper with generics.
Change-Id: Id95949a7a22ee687166ecdfc1191150d79568889
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/389657
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Diffstat (limited to 'internal/database/database.go')
| -rw-r--r-- | internal/database/database.go | 35 |
1 files changed, 8 insertions, 27 deletions
diff --git a/internal/database/database.go b/internal/database/database.go index c6443347..e90bfbe6 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -538,41 +538,22 @@ func buildBulkUpdateQuery(table string, columns, types []string) string { ) } -// CollectInts runs the query, which must select for a single INTEGER or BIGINT -// column, and returns a slice of the resulting ints. -func (db *DB) CollectInts(ctx context.Context, query string, args ...interface{}) (ints []int, err error) { - defer derrors.WrapStack(&err, "DB.CollectInts(%q)", query) +// Collect1 runs the query, which must select for a single column that can be +// scanned into a value of type T, and returns a slice of the resulting values. +func Collect1[T any](ctx context.Context, db *DB, query string, args ...interface{}) (ts []T, err error) { + defer derrors.WrapStack(&err, "Collect1(%q)", query) err = db.RunQuery(ctx, query, func(rows *sql.Rows) error { - var i int - if err := rows.Scan(&i); err != nil { + var t T + if err := rows.Scan(&t); err != nil { return err } - ints = append(ints, i) + ts = append(ts, t) return nil }, args...) if err != nil { return nil, err } - return ints, nil -} - -// CollectStrings runs the query, which must select for a single string column, and returns -// a slice of the resulting strings. -func (db *DB) CollectStrings(ctx context.Context, query string, args ...interface{}) (ss []string, err error) { - defer derrors.WrapStack(&err, "DB.CollectStrings(%q)", query) - - err = db.RunQuery(ctx, query, func(rows *sql.Rows) error { - var s string - if err := rows.Scan(&s); err != nil { - return err - } - ss = append(ss, s) - return nil - }, args...) - if err != nil { - return nil, err - } - return ss, nil + return ts, nil } // emptyStringScanner wraps the functionality of sql.NullString to just write |
