| Copyright | (c) digitally induced GmbH 2020 |
|---|---|
| Safe Haskell | None |
| Language | GHC2021 |
IHP.QueryBuilder.Union
Description
This module provides functions for combining queries with UNION and OR operations.
Synopsis
- queryUnion :: forall (model :: Symbol). QueryBuilder model -> QueryBuilder model -> QueryBuilder model
- queryUnionList :: forall (table :: Symbol). (Table (GetModelByTableName table), KnownSymbol table, GetTableName (GetModelByTableName table) ~ table) => [QueryBuilder table] -> QueryBuilder table
- queryOr :: forall (model :: Symbol). (QueryBuilder model -> QueryBuilder model) -> (QueryBuilder model -> QueryBuilder model) -> QueryBuilder model -> QueryBuilder model
Documentation
queryUnion :: forall (model :: Symbol). QueryBuilder model -> QueryBuilder model -> QueryBuilder model Source #
Merges the results of two query builders by ORing their WHERE conditions.
Take a look at queryOr as well, as this might be a bit shorter.
Example: Return all pages owned by the user or owned by the users team.
let userPages = query @Page |> filterWhere (#ownerId, currentUserId) let teamPages = query @Page |> filterWhere (#teamId, currentTeamId) pages <- queryUnion userPages teamPages |> fetch -- SELECT * FROM pages WHERE (owner_id = '..') OR (team_id = '..')
queryUnionList :: forall (table :: Symbol). (Table (GetModelByTableName table), KnownSymbol table, GetTableName (GetModelByTableName table) ~ table) => [QueryBuilder table] -> QueryBuilder table Source #
Like queryUnion, but applied on all the elements on the list
action ProjectsAction = do
let values :: [(ProjectType, Int)] = [(ProjectTypeOngoing, 3), (ProjectTypeNotStarted, 2)]
valuePairToCondition :: (ProjectType, Int) -> QueryBuilder "projects"
valuePairToCondition (projectType, participants) =
query @Project
|> filterWhere (#projectType, projectType)
|> filterWhere (#participants, participants)
theQuery = queryUnionList (map valuePairToCondition values)
projects <- fetch theQuery
render IndexView { .. }queryOr :: forall (model :: Symbol). (QueryBuilder model -> QueryBuilder model) -> (QueryBuilder model -> QueryBuilder model) -> QueryBuilder model -> QueryBuilder model Source #
Adds an a OR b condition
Example: Return all pages owned by the user or public.
query @Page
|> queryOr
(filterWhere (#createdBy, currentUserId))
(filterWhere (#public, True))
|> fetch
-- SELECT * FROM pages WHERE created_by = '..' OR public = True