Commit a05d8eeb authored by Adhityaa Chandrasekar's avatar Adhityaa Chandrasekar

commenter_get.go: clean up SQL

parent 024859ff
......@@ -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
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment