Skip to content

Commit

Permalink
Support for nested ORDER BYs
Browse files Browse the repository at this point in the history
  • Loading branch information
fegu committed Sep 11, 2020
1 parent 17598f2 commit 90fc11b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions IHP/QueryBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ data OrderByDirection = Asc | Desc deriving (Eq, Show)
data SQLQuery = SQLQuery {
selectFrom :: !Text,
whereCondition :: !(Maybe Condition),
orderByClause :: !(Maybe (Text, OrderByDirection)),
orderByClause :: !([(Text, OrderByDirection)]),
limitClause :: !(Maybe Text)
}

Expand All @@ -129,7 +129,7 @@ buildQuery !queryBuilder =
case queryBuilder of
NewQueryBuilder ->
let tableName = symbolVal @(GetTableName model) Proxy
in SQLQuery { selectFrom = cs tableName, whereCondition = Nothing, orderByClause = Nothing, limitClause = Nothing }
in SQLQuery { selectFrom = cs tableName, whereCondition = Nothing, orderByClause = [], limitClause = Nothing }
FilterByQueryBuilder (fieldProxy, operator, value) queryBuilder ->
let
query = buildQuery queryBuilder
Expand All @@ -138,13 +138,13 @@ buildQuery !queryBuilder =
query { whereCondition = Just $ case whereCondition query of Just c -> AndCondition c condition; Nothing -> condition }
OrderByQueryBuilder (fieldProxy, orderByDirection) queryBuilder ->
let query = buildQuery queryBuilder
in query { orderByClause = Just (fieldNameToColumnName . cs $ symbolVal fieldProxy, orderByDirection) }
in query { orderByClause = (orderByClause query) ++ [(fieldNameToColumnName . cs $ symbolVal fieldProxy, orderByDirection)] } -- although adding to the end of a list is bad form, these lists are very short
IncludeQueryBuilder include queryBuilder -> buildQuery queryBuilder
UnionQueryBuilder firstQueryBuilder secondQueryBuilder ->
let
firstQuery = buildQuery firstQueryBuilder
secondQuery = buildQuery secondQueryBuilder
isSimpleQuery query = isNothing (orderByClause query) && isNothing (limitClause query)
isSimpleQuery query = null (orderByClause query) && isNothing (limitClause query)
isSimpleUnion = isSimpleQuery firstQuery && isSimpleQuery secondQuery
unionWhere =
case (whereCondition firstQuery, whereCondition secondQuery) of
Expand Down Expand Up @@ -277,8 +277,8 @@ toSQL' sqlQuery@SQLQuery { selectFrom, orderByClause, limitClause } =
Nothing -> mempty
orderByClause' =
case orderByClause of
Just (column, direction) -> " ORDER BY " <> column <> (if direction == Desc then " DESC" else mempty)
Nothing -> mempty
[] -> mempty
xs -> " ORDER BY " <> intercalate "," ((map (\(column,direction) -> column <> (if direction == Desc then " DESC" else mempty)) xs))
limitClause' = fromMaybe "" limitClause

{-# INLINE compileConditionQuery #-}
Expand Down

0 comments on commit 90fc11b

Please sign in to comment.