{-# LANGUAGE
DataKinds
, FlexibleInstances
, FunctionalDependencies
, GADTs
, LambdaCase
, MultiParamTypeClasses
, OverloadedStrings
, StandaloneDeriving
#-}
module Squeal.PostgreSQL.Expression.Sort
(
SortExpression (..)
, OrderBy (..)
) where
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
data SortExpression grp lat with db params from where
Asc
:: Expression grp lat with db params from ('NotNull ty)
-> SortExpression grp lat with db params from
Desc
:: Expression grp lat with db params from ('NotNull ty)
-> SortExpression grp lat with db params from
AscNullsFirst
:: Expression grp lat with db params from ('Null ty)
-> SortExpression grp lat with db params from
AscNullsLast
:: Expression grp lat with db params from ('Null ty)
-> SortExpression grp lat with db params from
DescNullsFirst
:: Expression grp lat with db params from ('Null ty)
-> SortExpression grp lat with db params from
DescNullsLast
:: Expression grp lat with db params from ('Null ty)
-> SortExpression grp lat with db params from
deriving instance Show (SortExpression grp lat with db params from)
instance RenderSQL (SortExpression grp lat with db params from) where
renderSQL :: SortExpression grp lat with db params from -> ByteString
renderSQL = \case
Asc Expression grp lat with db params from ('NotNull ty)
expression -> Expression grp lat with db params from ('NotNull ty) -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('NotNull ty)
expression ByteString -> ByteString -> ByteString
<+> ByteString
"ASC"
Desc Expression grp lat with db params from ('NotNull ty)
expression -> Expression grp lat with db params from ('NotNull ty) -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('NotNull ty)
expression ByteString -> ByteString -> ByteString
<+> ByteString
"DESC"
AscNullsFirst Expression grp lat with db params from ('Null ty)
expression -> Expression grp lat with db params from ('Null ty) -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression
ByteString -> ByteString -> ByteString
<+> ByteString
"ASC NULLS FIRST"
DescNullsFirst Expression grp lat with db params from ('Null ty)
expression -> Expression grp lat with db params from ('Null ty) -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression
ByteString -> ByteString -> ByteString
<+> ByteString
"DESC NULLS FIRST"
AscNullsLast Expression grp lat with db params from ('Null ty)
expression -> Expression grp lat with db params from ('Null ty) -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression ByteString -> ByteString -> ByteString
<+> ByteString
"ASC NULLS LAST"
DescNullsLast Expression grp lat with db params from ('Null ty)
expression -> Expression grp lat with db params from ('Null ty) -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression ByteString -> ByteString -> ByteString
<+> ByteString
"DESC NULLS LAST"
instance RenderSQL [SortExpression grp lat with db params from] where
renderSQL :: [SortExpression grp lat with db params from] -> ByteString
renderSQL = \case
[] -> ByteString
""
[SortExpression grp lat with db params from]
srts -> ByteString
" ORDER BY"
ByteString -> ByteString -> ByteString
<+> [ByteString] -> ByteString
commaSeparated (SortExpression grp lat with db params from -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL (SortExpression grp lat with db params from -> ByteString)
-> [SortExpression grp lat with db params from] -> [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [SortExpression grp lat with db params from]
srts)
class OrderBy expr grp | expr -> grp where
orderBy
:: [SortExpression grp lat with db params from]
-> expr lat with db params from
-> expr lat with db params from