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,
statement := ` domains.state,
SELECT domains.importedComments,
domain, domains.autoSpamFilter,
ownerHex, domains.requireModeration,
name, domains.requireIdentification,
creationDate, domains.moderateAllAnonymous,
state, domains.emailNotificationPolicy,
importedComments, domains.commentoProvider,
autoSpamFilter, domains.googleProvider,
requireModeration, domains.twitterProvider,
requireIdentification, domains.githubProvider,
moderateAllAnonymous, domains.gitlabProvider,
emailNotificationPolicy, domains.ssoProvider,
commentoProvider, domains.ssoSecret,
googleProvider, domains.ssoUrl,
twitterProvider, domains.defaultSortPolicy
githubProvider,
gitlabProvider,
ssoProvider,
ssoSecret,
ssoUrl,
defaultSortPolicy
FROM domains
WHERE domain = $1;
` `
row := db.QueryRow(statement, dmn)
var err error func domainsRowScan(s sqlScanner, d *domain) error {
d := domain{} return s.Scan(
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
} }