Skip to content

Commit

Permalink
[#118] Refactor database functions to use queryNamed and executeNamed
Browse files Browse the repository at this point in the history
Resolves #118
  • Loading branch information
rashadg1030 committed Jul 21, 2019
1 parent 93b83f1 commit f6280d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
13 changes: 7 additions & 6 deletions src/IW/Db/Issue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@ module IW.Db.Issue
, upsertIssues
) where

import IW.App (WithError)
import IW.Core.Issue (Issue (..), Label (..))
import IW.Core.SqlArray (SqlArray (..))
import IW.Core.WithId (WithId)
import IW.Db.Functions (WithDb, executeMany, query, queryRaw)
import IW.Db.Functions (WithDb, executeMany, queryNamed, queryRaw)


-- | Returns all issues in the database.
getIssues :: (WithDb env m) => m [WithId Issue]
getIssues :: WithDb env m => m [WithId Issue]
getIssues = queryRaw [sql|
SELECT id, repo_owner, repo_name, number, title, body, labels
FROM issues
LIMIT 100
|]

-- | Returns all issues with at least one label in the given list.
getIssuesByLabels :: (WithDb env m) => [Label] -> m [WithId Issue]
getIssuesByLabels = query [sql|
getIssuesByLabels :: (WithDb env m, WithError m) => [Label] -> m [WithId Issue]
getIssuesByLabels labels = queryNamed [sql|
SELECT id, repo_owner, repo_name, number, title, body, labels
FROM issues
WHERE ? && labels
WHERE labels && ?labels
LIMIT 100
|] . Only . SqlArray
|] [ "labels" =? SqlArray labels ]

-- | Insert a list of issues into the database, but update on conflict.
upsertIssues :: (WithDb env m) => [Issue] -> m ()
Expand Down
28 changes: 17 additions & 11 deletions src/IW/Db/Repo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ module IW.Db.Repo
, updateRepoCategories
) where

import IW.App (WithError)
import IW.Core.Repo (Repo, RepoOwner, RepoName, Category)
import IW.Core.SqlArray (SqlArray (..))
import IW.Core.WithId (WithId)
import IW.Db.Functions (WithDb, executeMany, execute, query, queryRaw)
import IW.Db.Functions (WithDb, executeMany, executeNamed, queryNamed, queryRaw)


-- | Returns all repos in the database.
Expand All @@ -24,13 +25,13 @@ getRepos = queryRaw [sql|
|]

-- | Returns all repos with at least one category in the given list.
getReposByCategories :: WithDb env m => [Category] -> m [WithId Repo]
getReposByCategories = query [sql|
getReposByCategories :: (WithDb env m, WithError m) => [Category] -> m [WithId Repo]
getReposByCategories categories = queryNamed [sql|
SELECT id, owner, name, descr, categories
FROM repos
WHERE ? && categories
WHERE categories && ?categories
LIMIT 100
|] . Only . SqlArray
|] [ "categories" =? SqlArray categories ]

-- | Insert a list of repos into the database, but update on conflict.
upsertRepos :: WithDb env m => [Repo] -> m ()
Expand All @@ -47,14 +48,19 @@ upsertRepos = executeMany [sql|

-- | Update a repo's categories field.
updateRepoCategories
:: WithDb env m
:: ( WithDb env m
, WithError m
)
=> RepoOwner
-> RepoName
-> [Category]
-> m ()
updateRepoCategories repoOwner repoName categories = execute [sql|
updateRepoCategories repoOwner repoName categories = void $ executeNamed [sql|
UPDATE repos
SET categories = ?
WHERE owner = ?
AND name = ?
|] (SqlArray categories, repoOwner, repoName)
SET categories = ?categories
WHERE owner = ?owner
AND name = ?name
|] [ "categories" =? SqlArray categories
, "owner" =? repoOwner
, "name" =? repoName
]
2 changes: 2 additions & 0 deletions src/IW/Server/Issue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module IW.Server.Issue
, issuesHandler
) where

import IW.App (WithError)
import IW.Core.Issue (Issue (..), Label (..))
import IW.Core.WithId (WithId (..))
import IW.Db (WithDb, getIssuesByLabels)
Expand All @@ -29,6 +30,7 @@ issueServer = IssueSite

issuesHandler
:: ( WithDb env m
, WithError m
)
=> [Label]
-> m [WithId Issue]
Expand Down
1 change: 1 addition & 0 deletions src/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Database.PostgreSQL.Simple.SqlQQ as Sql (sql)
import Database.PostgreSQL.Simple.ToField as Sql (ToField (toField))
import Database.PostgreSQL.Simple.ToRow as Sql (ToRow (toRow))
import Database.PostgreSQL.Simple.Types as Sql (Only (..))
import PgNamed as Sql ((=?))

import Servant.API as Web ((:>), Capture, Get, Header, Header', JSON, NoContent (NoContent), Post,
QueryFlag, QueryParam, QueryParam', ReqBody)
Expand Down

0 comments on commit f6280d5

Please sign in to comment.