From 72a3f87c28851e5c6725a3e9c12b5229a2eda88c Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Fri, 27 Dec 2019 17:13:06 -0800 Subject: [PATCH] domain_get.go, domain_list.go: clean up SQL --- api/domain_get.go | 78 +++++++++++++++++++++++++--------------------- api/domain_list.go | 46 ++------------------------- 2 files changed, 46 insertions(+), 78 deletions(-) diff --git a/api/domain_get.go b/api/domain_get.go index 00c2b89..edf715c 100644 --- a/api/domain_get.go +++ b/api/domain_get.go @@ -2,41 +2,31 @@ package main import () -func domainGet(dmn string) (domain, error) { - if dmn == "" { - return domain{}, errorMissingField - } +var domainsRowColumns = ` + domains.domain, + 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 := ` - SELECT - 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( +func domainsRowScan(s sqlScanner, d *domain) error { + return s.Scan( &d.Domain, &d.OwnerHex, &d.Name, @@ -56,7 +46,25 @@ func domainGet(dmn string) (domain, error) { &d.SsoProvider, &d.SsoSecret, &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 } diff --git a/api/domain_list.go b/api/domain_list.go index d09995b..aa50414 100644 --- a/api/domain_list.go +++ b/api/domain_list.go @@ -10,27 +10,7 @@ func domainList(ownerHex string) ([]domain, error) { } statement := ` - SELECT - domain, - ownerHex, - name, - creationDate, - state, - importedComments, - autoSpamFilter, - requireModeration, - requireIdentification, - moderateAllAnonymous, - emailNotificationPolicy, - commentoProvider, - googleProvider, - twitterProvider, - githubProvider, - gitlabProvider, - ssoProvider, - ssoSecret, - ssoUrl, - defaultSortPolicy + SELECT ` + domainsRowColumns + ` FROM domains WHERE ownerHex=$1; ` @@ -43,28 +23,8 @@ func domainList(ownerHex string) ([]domain, error) { domains := []domain{} for rows.Next() { - d := domain{} - if err = rows.Scan( - &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 { + var d domain + if err = domainsRowScan(rows, &d); err != nil { logger.Errorf("cannot Scan domain: %v", err) return nil, errorInternal }