aboutsummaryrefslogtreecommitdiff
path: root/internal/postgres/postgres.go
diff options
context:
space:
mode:
authorRob Findley <rfindley@google.com>2019-09-06 14:24:21 -0400
committerJulie Qiu <julie@golang.org>2020-03-27 16:46:43 -0400
commitc18481aa2e4d16689fe22efa538397b1689d4a7a (patch)
tree8bd18ab94fc439d0c5dce9d1a0194241f8b7534e /internal/postgres/postgres.go
parent3de10572043766bdfd077b6025df51b7bed6a79c (diff)
downloadgo-x-pkgsite-c18481aa2e4d16689fe22efa538397b1689d4a7a.tar.xz
internal/postgres: merge latest and versioned handling
getLatestPackage and getLatestVersionInfo are removed, with their functionality inlined into GetPackage and GetVersionInfo, respectively. Additionally, the following cleanup is applied: + sql values are scanned directly into the returned struct + a new helper type is added to support scanning null values into empty strings Fixes b/140558033 Change-Id: I101a475bb4a4a3d70075396d60473e0df0d6c8b0 Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/544272 Reviewed-by: Julie Qiu <julieqiu@google.com>
Diffstat (limited to 'internal/postgres/postgres.go')
-rw-r--r--internal/postgres/postgres.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/internal/postgres/postgres.go b/internal/postgres/postgres.go
index f50c229c..6027d3df 100644
--- a/internal/postgres/postgres.go
+++ b/internal/postgres/postgres.go
@@ -198,3 +198,24 @@ func (db *DB) runQuery(ctx context.Context, query string, f func(*sql.Rows) erro
}
return rows.Err()
}
+
+// emptyStringScanner wraps the functionality of sql.NullString to just write
+// an empty string if the value is NULL.
+type emptyStringScanner struct {
+ ptr *string
+}
+
+func (e emptyStringScanner) Scan(value interface{}) error {
+ var ns sql.NullString
+ if err := ns.Scan(value); err != nil {
+ return err
+ }
+ *e.ptr = ns.String
+ return nil
+}
+
+// nullIsEmpty returns a sql.Scanner that writes the empty string to s if the
+// sql.Value is NULL.
+func nullIsEmpty(s *string) sql.Scanner {
+ return emptyStringScanner{s}
+}