@@ -1,3 +1,13 @@
1+{-|
2+Module: Squeal.PostgreSQL.Expression.Sort
3+Description: Sort expressions
4+Copyright: (c) Eitan Chatav, 2019
5+Maintainer: eitan@morphism.tech
6+Stability: experimental
7+
8+Sort expressions
9+-}
10+
111{-# LANGUAGE
212 DataKinds
313 , GADTs
@@ -19,8 +29,10 @@ import Squeal.PostgreSQL.Schema
1929-- of a `Squeal.PostgreSQL.Query.Query`. `Asc` or `Desc`
2030-- set the sort direction of a `NotNull` result
2131-- column to ascending or descending. Ascending order puts smaller values
22−-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
23−-- descending order is determined with the `.>` operator. `AscNullsFirst`,
32+-- first, where "smaller" is defined in terms of the
33+-- `Squeal.PostgreSQL.Expression.Comparison..<` operator. Similarly,
34+-- descending order is determined with the
35+-- `Squeal.PostgreSQL.Expression.Comparison..>` operator. `AscNullsFirst`,
2436-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
2537-- determine whether nulls appear before or after non-null values in the sort
2638-- ordering of a `Null` result column.
@@ -44,6 +56,16 @@ data SortExpression outer commons grp schemas params from where
4456 :: Expression outer commons grp schemas params from ('Null ty)
4557 -> SortExpression outer commons grp schemas params from
4658deriving instance Show (SortExpression outer commons grp schemas params from)
59+instance RenderSQL (SortExpression outer commons grp schemas params from) where
60+ renderSQL = \case
61+ Asc expression -> renderSQL expression <+> "ASC"
62+ Desc expression -> renderSQL expression <+> "DESC"
63+ AscNullsFirst expression -> renderSQL expression
64+ <+> "ASC NULLS FIRST"
65+ DescNullsFirst expression -> renderSQL expression
66+ <+> "DESC NULLS FIRST"
67+ AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
68+ DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
4769
4870{- |
4971The `orderBy` clause causes the result rows of a `Squeal.PostgreSQL.Query.TableExpression`
@@ -61,15 +83,3 @@ class OrderBy expr where
6183 :: [SortExpression outer commons grp schemas params from]
6284 -> expr outer commons grp schemas params from
6385 -> expr outer commons grp schemas params from
64−
65−-- | Render a `SortExpression`.
66−instance RenderSQL (SortExpression outer commons grp schemas params from) where
67− renderSQL = \case
68− Asc expression -> renderSQL expression <+> "ASC"
69− Desc expression -> renderSQL expression <+> "DESC"
70− AscNullsFirst expression -> renderSQL expression
71− <+> "ASC NULLS FIRST"
72− DescNullsFirst expression -> renderSQL expression
73− <+> "DESC NULLS FIRST"
74− AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
75− DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"