From a05d8eeb076666a95a4f6bed9ee48aa0983e8694 Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Fri, 27 Dec 2019 16:20:05 -0800 Subject: [PATCH] commenter_get.go: clean up SQL --- api/commenter_get.go | 58 ++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/api/commenter_get.go b/api/commenter_get.go index de9bfd9..12df926 100644 --- a/api/commenter_get.go +++ b/api/commenter_get.go @@ -2,20 +2,42 @@ package main import () +var commentersRowColumns string = ` + commenters.commenterHex, + commenters.email, + commenters.name, + commenters.link, + commenters.photo, + commenters.provider, + commenters.joinDate +` + +func commentersRowScan(s sqlScanner, c *commenter) error { + return s.Scan( + &c.CommenterHex, + &c.Email, + &c.Name, + &c.Link, + &c.Photo, + &c.Provider, + &c.JoinDate, + ) +} + func commenterGetByHex(commenterHex string) (commenter, error) { if commenterHex == "" { return commenter{}, errorMissingField } statement := ` - SELECT commenterHex, email, name, link, photo, provider, joinDate - FROM commenters - WHERE commenterHex = $1; - ` + SELECT ` + commentersRowColumns + ` + FROM commenters + WHERE commenterHex = $1; + ` row := db.QueryRow(statement, commenterHex) - c := commenter{} - if err := row.Scan(&c.CommenterHex, &c.Email, &c.Name, &c.Link, &c.Photo, &c.Provider, &c.JoinDate); err != nil { + var c commenter + if err := commentersRowScan(row, &c); err != nil { // TODO: is this the only error? return commenter{}, errorNoSuchCommenter } @@ -29,14 +51,14 @@ func commenterGetByEmail(provider string, email string) (commenter, error) { } statement := ` - SELECT commenterHex, email, name, link, photo, provider, joinDate - FROM commenters - WHERE email = $1 AND provider = $2; - ` + SELECT ` + commentersRowColumns + ` + FROM commenters + WHERE email = $1 AND provider = $2; + ` row := db.QueryRow(statement, email, provider) - c := commenter{} - if err := row.Scan(&c.CommenterHex, &c.Email, &c.Name, &c.Link, &c.Photo, &c.Provider, &c.JoinDate); err != nil { + var c commenter + if err := commentersRowScan(row, &c); err != nil { // TODO: is this the only error? return commenter{}, errorNoSuchCommenter } @@ -50,15 +72,15 @@ func commenterGetByCommenterToken(commenterToken string) (commenter, error) { } statement := ` - SELECT commenters.commenterHex, commenters.email, commenters.name, commenters.link, commenters.photo, commenters.provider, commenters.joinDate - FROM commenterSessions - JOIN commenters ON commenterSessions.commenterHex = commenters.commenterHex - WHERE commenterToken = $1; + SELECT ` + commentersRowColumns + ` + FROM commenterSessions + JOIN commenters ON commenterSessions.commenterHex = commenters.commenterHex + WHERE commenterToken = $1; ` row := db.QueryRow(statement, commenterToken) - c := commenter{} - if err := row.Scan(&c.CommenterHex, &c.Email, &c.Name, &c.Link, &c.Photo, &c.Provider, &c.JoinDate); err != nil { + var c commenter + if err := commentersRowScan(row, &c); err != nil { // TODO: is this the only error? return commenter{}, errorNoSuchToken }