commenter_get.go: clean up SQL
This commit is contained in:
parent
024859ff45
commit
a05d8eeb07
@ -2,20 +2,42 @@ package main
|
|||||||
|
|
||||||
import ()
|
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) {
|
func commenterGetByHex(commenterHex string) (commenter, error) {
|
||||||
if commenterHex == "" {
|
if commenterHex == "" {
|
||||||
return commenter{}, errorMissingField
|
return commenter{}, errorMissingField
|
||||||
}
|
}
|
||||||
|
|
||||||
statement := `
|
statement := `
|
||||||
SELECT commenterHex, email, name, link, photo, provider, joinDate
|
SELECT ` + commentersRowColumns + `
|
||||||
FROM commenters
|
FROM commenters
|
||||||
WHERE commenterHex = $1;
|
WHERE commenterHex = $1;
|
||||||
`
|
`
|
||||||
row := db.QueryRow(statement, commenterHex)
|
row := db.QueryRow(statement, commenterHex)
|
||||||
|
|
||||||
c := commenter{}
|
var c commenter
|
||||||
if err := row.Scan(&c.CommenterHex, &c.Email, &c.Name, &c.Link, &c.Photo, &c.Provider, &c.JoinDate); err != nil {
|
if err := commentersRowScan(row, &c); err != nil {
|
||||||
// TODO: is this the only error?
|
// TODO: is this the only error?
|
||||||
return commenter{}, errorNoSuchCommenter
|
return commenter{}, errorNoSuchCommenter
|
||||||
}
|
}
|
||||||
@ -29,14 +51,14 @@ func commenterGetByEmail(provider string, email string) (commenter, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
statement := `
|
statement := `
|
||||||
SELECT commenterHex, email, name, link, photo, provider, joinDate
|
SELECT ` + commentersRowColumns + `
|
||||||
FROM commenters
|
FROM commenters
|
||||||
WHERE email = $1 AND provider = $2;
|
WHERE email = $1 AND provider = $2;
|
||||||
`
|
`
|
||||||
row := db.QueryRow(statement, email, provider)
|
row := db.QueryRow(statement, email, provider)
|
||||||
|
|
||||||
c := commenter{}
|
var c commenter
|
||||||
if err := row.Scan(&c.CommenterHex, &c.Email, &c.Name, &c.Link, &c.Photo, &c.Provider, &c.JoinDate); err != nil {
|
if err := commentersRowScan(row, &c); err != nil {
|
||||||
// TODO: is this the only error?
|
// TODO: is this the only error?
|
||||||
return commenter{}, errorNoSuchCommenter
|
return commenter{}, errorNoSuchCommenter
|
||||||
}
|
}
|
||||||
@ -50,15 +72,15 @@ func commenterGetByCommenterToken(commenterToken string) (commenter, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
statement := `
|
statement := `
|
||||||
SELECT commenters.commenterHex, commenters.email, commenters.name, commenters.link, commenters.photo, commenters.provider, commenters.joinDate
|
SELECT ` + commentersRowColumns + `
|
||||||
FROM commenterSessions
|
FROM commenterSessions
|
||||||
JOIN commenters ON commenterSessions.commenterHex = commenters.commenterHex
|
JOIN commenters ON commenterSessions.commenterHex = commenters.commenterHex
|
||||||
WHERE commenterToken = $1;
|
WHERE commenterToken = $1;
|
||||||
`
|
`
|
||||||
row := db.QueryRow(statement, commenterToken)
|
row := db.QueryRow(statement, commenterToken)
|
||||||
|
|
||||||
c := commenter{}
|
var c commenter
|
||||||
if err := row.Scan(&c.CommenterHex, &c.Email, &c.Name, &c.Link, &c.Photo, &c.Provider, &c.JoinDate); err != nil {
|
if err := commentersRowScan(row, &c); err != nil {
|
||||||
// TODO: is this the only error?
|
// TODO: is this the only error?
|
||||||
return commenter{}, errorNoSuchToken
|
return commenter{}, errorNoSuchToken
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user