diff options
| author | Jonathan Amsterdam <jba@google.com> | 2021-07-21 08:21:39 -0400 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2021-07-21 13:37:24 +0000 |
| commit | 5b7094a7c3ccffe2079c9b4074f155239877929d (patch) | |
| tree | 5dcc66f1653e0dfedab643623edea3d117ebc3e1 /internal/database/database.go | |
| parent | 10cf157e826a13661e24e04d25af91b09729bb5b (diff) | |
| download | go-x-pkgsite-5b7094a7c3ccffe2079c9b4074f155239877929d.tar.xz | |
internal/{database,postgres}: export CollectStrings
Export the CollectStrings function and move to the database package so
it can be used elsewhere.
Change-Id: I1032e4f5042209e42e8f0de9cebfb48653d56f69
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/336209
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'internal/database/database.go')
| -rw-r--r-- | internal/database/database.go | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/internal/database/database.go b/internal/database/database.go index dbbde425..227ff4c0 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -535,6 +535,25 @@ func buildBulkUpdateQuery(table string, columns, types []string) string { ) } +// 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 +} + // emptyStringScanner wraps the functionality of sql.NullString to just write // an empty string if the value is NULL. type emptyStringScanner struct { |
