domain_get.go, domain_list.go: clean up SQL

This commit is contained in:
Adhityaa Chandrasekar 2019-12-27 17:13:06 -08:00
parent c94e5ca41f
commit 72a3f87c28
2 changed files with 46 additions and 78 deletions

View File

@ -2,41 +2,31 @@ package main
import () import ()
func domainGet(dmn string) (domain, error) { var domainsRowColumns = `
if dmn == "" { domains.domain,
return domain{}, errorMissingField domains.ownerHex,
} domains.name,
domains.creationDate,
domains.state,
domains.importedComments,
domains.autoSpamFilter,
domains.requireModeration,
domains.requireIdentification,
domains.moderateAllAnonymous,
domains.emailNotificationPolicy,
domains.commentoProvider,
domains.googleProvider,
domains.twitterProvider,
domains.githubProvider,
domains.gitlabProvider,
domains.ssoProvider,
domains.ssoSecret,
domains.ssoUrl,
domains.defaultSortPolicy
`
statement := ` func domainsRowScan(s sqlScanner, d *domain) error {
SELECT return s.Scan(
domain,
ownerHex,
name,
creationDate,
state,
importedComments,
autoSpamFilter,
requireModeration,
requireIdentification,
moderateAllAnonymous,
emailNotificationPolicy,
commentoProvider,
googleProvider,
twitterProvider,
githubProvider,
gitlabProvider,
ssoProvider,
ssoSecret,
ssoUrl,
defaultSortPolicy
FROM domains
WHERE domain = $1;
`
row := db.QueryRow(statement, dmn)
var err error
d := domain{}
if err = row.Scan(
&d.Domain, &d.Domain,
&d.OwnerHex, &d.OwnerHex,
&d.Name, &d.Name,
@ -56,7 +46,25 @@ func domainGet(dmn string) (domain, error) {
&d.SsoProvider, &d.SsoProvider,
&d.SsoSecret, &d.SsoSecret,
&d.SsoUrl, &d.SsoUrl,
&d.DefaultSortPolicy); err != nil { &d.DefaultSortPolicy,
)
}
func domainGet(dmn string) (domain, error) {
if dmn == "" {
return domain{}, errorMissingField
}
statement := `
SELECT ` + domainsRowColumns + `
FROM domains
WHERE domain = $1;
`
row := db.QueryRow(statement, dmn)
var err error
d := domain{}
if err = domainsRowScan(row, &d); err != nil {
return d, errorNoSuchDomain return d, errorNoSuchDomain
} }

View File

@ -10,27 +10,7 @@ func domainList(ownerHex string) ([]domain, error) {
} }
statement := ` statement := `
SELECT SELECT ` + domainsRowColumns + `
domain,
ownerHex,
name,
creationDate,
state,
importedComments,
autoSpamFilter,
requireModeration,
requireIdentification,
moderateAllAnonymous,
emailNotificationPolicy,
commentoProvider,
googleProvider,
twitterProvider,
githubProvider,
gitlabProvider,
ssoProvider,
ssoSecret,
ssoUrl,
defaultSortPolicy
FROM domains FROM domains
WHERE ownerHex=$1; WHERE ownerHex=$1;
` `
@ -43,28 +23,8 @@ func domainList(ownerHex string) ([]domain, error) {
domains := []domain{} domains := []domain{}
for rows.Next() { for rows.Next() {
d := domain{} var d domain
if err = rows.Scan( if err = domainsRowScan(rows, &d); err != nil {
&d.Domain,
&d.OwnerHex,
&d.Name,
&d.CreationDate,
&d.State,
&d.ImportedComments,
&d.AutoSpamFilter,
&d.RequireModeration,
&d.RequireIdentification,
&d.ModerateAllAnonymous,
&d.EmailNotificationPolicy,
&d.CommentoProvider,
&d.GoogleProvider,
&d.TwitterProvider,
&d.GithubProvider,
&d.GitlabProvider,
&d.SsoProvider,
&d.SsoSecret,
&d.SsoUrl,
&d.DefaultSortPolicy); err != nil {
logger.Errorf("cannot Scan domain: %v", err) logger.Errorf("cannot Scan domain: %v", err)
return nil, errorInternal return nil, errorInternal
} }