From c94e5ca41f4f3084a59b5324408b2c0a4ff98657 Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Fri, 27 Dec 2019 17:00:14 -0800 Subject: [PATCH] comment_get.go: clean up SQL --- api/comment_get.go | 54 ++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/api/comment_get.go b/api/comment_get.go index 5c14540..14b33a7 100644 --- a/api/comment_get.go +++ b/api/comment_get.go @@ -2,29 +2,20 @@ package main import () -func commentGetByCommentHex(commentHex string) (comment, error) { - if commentHex == "" { - return comment{}, errorMissingField - } +var commentsRowColumns = ` + comments.commentHex, + comments.commenterHex, + comments.markdown, + comments.html, + comments.parentHex, + comments.score, + comments.state, + comments.deleted, + comments.creationDate +` - statement := ` - SELECT - commentHex, - commenterHex, - markdown, - html, - parentHex, - score, - state, - deleted, - creationDate - FROM comments - WHERE comments.commentHex = $1; - ` - row := db.QueryRow(statement, commentHex) - - c := comment{} - if err := row.Scan( +func commentsRowScan(s sqlScanner, c *comment) error { + return s.Scan( &c.CommentHex, &c.CommenterHex, &c.Markdown, @@ -33,7 +24,24 @@ func commentGetByCommentHex(commentHex string) (comment, error) { &c.Score, &c.State, &c.Deleted, - &c.CreationDate); err != nil { + &c.CreationDate, + ) +} + +func commentGetByCommentHex(commentHex string) (comment, error) { + if commentHex == "" { + return comment{}, errorMissingField + } + + statement := ` + SELECT ` + commentsRowColumns + ` + FROM comments + WHERE comments.commentHex = $1; + ` + row := db.QueryRow(statement, commentHex) + + var c comment + if err := commentsRowScan(row, &c); err != nil { // TODO: is this the only error? return c, errorNoSuchComment }