{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleContexts #-}
module Opaleye.Order (
orderBy
, O.Order
, asc
, desc
, ascNullsFirst
, descNullsLast
, limit
, offset
, distinctOn
, distinctOnBy
, O.exact
, PGOrd
, SqlOrd
) where
import qualified Data.Profunctor.Product.Default as D
import qualified Opaleye.Column as C
import qualified Opaleye.Internal.HaskellDB.PrimQuery as HPQ
import qualified Opaleye.Internal.Order as O
import qualified Opaleye.Internal.QueryArr as Q
import qualified Opaleye.Internal.Unpackspec as U
import qualified Opaleye.Select as S
import qualified Opaleye.SqlTypes as T
orderBy :: O.Order a -> S.Select a -> S.Select a
orderBy :: Order a -> Select a -> Select a
orderBy Order a
os Select a
q =
(((), Tag) -> (a, PrimQuery, Tag)) -> Select a
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Q.productQueryArr (Order a -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
forall a. Order a -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
O.orderByU Order a
os ((a, PrimQuery, Tag) -> (a, PrimQuery, Tag))
-> (((), Tag) -> (a, PrimQuery, Tag))
-> ((), Tag)
-> (a, PrimQuery, Tag)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Select a -> ((), Tag) -> (a, PrimQuery, Tag)
forall a b. QueryArr a b -> (a, Tag) -> (b, PrimQuery, Tag)
Q.runSimpleQueryArr Select a
q)
asc :: SqlOrd b => (a -> C.Column b) -> O.Order a
asc :: (a -> Column b) -> Order a
asc = OrderOp -> (a -> Column b) -> Order a
forall a b. OrderOp -> (a -> Column b) -> Order a
O.order OrderOp :: OrderDirection -> OrderNulls -> OrderOp
HPQ.OrderOp { orderDirection :: OrderDirection
HPQ.orderDirection = OrderDirection
HPQ.OpAsc
, orderNulls :: OrderNulls
HPQ.orderNulls = OrderNulls
HPQ.NullsLast }
desc :: SqlOrd b => (a -> C.Column b) -> O.Order a
desc :: (a -> Column b) -> Order a
desc = OrderOp -> (a -> Column b) -> Order a
forall a b. OrderOp -> (a -> Column b) -> Order a
O.order OrderOp :: OrderDirection -> OrderNulls -> OrderOp
HPQ.OrderOp { orderDirection :: OrderDirection
HPQ.orderDirection = OrderDirection
HPQ.OpDesc
, orderNulls :: OrderNulls
HPQ.orderNulls = OrderNulls
HPQ.NullsFirst }
ascNullsFirst :: SqlOrd b => (a -> C.Column b) -> O.Order a
ascNullsFirst :: (a -> Column b) -> Order a
ascNullsFirst = OrderOp -> (a -> Column b) -> Order a
forall a b. OrderOp -> (a -> Column b) -> Order a
O.order OrderOp :: OrderDirection -> OrderNulls -> OrderOp
HPQ.OrderOp { orderDirection :: OrderDirection
HPQ.orderDirection = OrderDirection
HPQ.OpAsc
, orderNulls :: OrderNulls
HPQ.orderNulls = OrderNulls
HPQ.NullsFirst }
descNullsLast :: SqlOrd b => (a -> C.Column b) -> O.Order a
descNullsLast :: (a -> Column b) -> Order a
descNullsLast = OrderOp -> (a -> Column b) -> Order a
forall a b. OrderOp -> (a -> Column b) -> Order a
O.order OrderOp :: OrderDirection -> OrderNulls -> OrderOp
HPQ.OrderOp { orderDirection :: OrderDirection
HPQ.orderDirection = OrderDirection
HPQ.OpDesc
, orderNulls :: OrderNulls
HPQ.orderNulls = OrderNulls
HPQ.NullsLast }
limit :: Int -> S.Select a -> S.Select a
limit :: Int -> Select a -> Select a
limit Int
n Select a
a = (((), Tag) -> (a, PrimQuery, Tag)) -> Select a
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Q.productQueryArr (Int -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
forall a. Int -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
O.limit' Int
n ((a, PrimQuery, Tag) -> (a, PrimQuery, Tag))
-> (((), Tag) -> (a, PrimQuery, Tag))
-> ((), Tag)
-> (a, PrimQuery, Tag)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Select a -> ((), Tag) -> (a, PrimQuery, Tag)
forall a b. QueryArr a b -> (a, Tag) -> (b, PrimQuery, Tag)
Q.runSimpleQueryArr Select a
a)
offset :: Int -> S.Select a -> S.Select a
offset :: Int -> Select a -> Select a
offset Int
n Select a
a = (((), Tag) -> (a, PrimQuery, Tag)) -> Select a
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Q.productQueryArr (Int -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
forall a. Int -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
O.offset' Int
n ((a, PrimQuery, Tag) -> (a, PrimQuery, Tag))
-> (((), Tag) -> (a, PrimQuery, Tag))
-> ((), Tag)
-> (a, PrimQuery, Tag)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Select a -> ((), Tag) -> (a, PrimQuery, Tag)
forall a b. QueryArr a b -> (a, Tag) -> (b, PrimQuery, Tag)
Q.runSimpleQueryArr Select a
a)
distinctOn :: D.Default U.Unpackspec b b => (a -> b) -> S.Select a -> S.Select a
distinctOn :: (a -> b) -> Select a -> Select a
distinctOn a -> b
proj Select a
q = (((), Tag) -> (a, PrimQuery, Tag)) -> Select a
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Q.productQueryArr (Unpackspec b b
-> (a -> b) -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
forall b a.
Unpackspec b b
-> (a -> b) -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
O.distinctOn Unpackspec b b
forall (p :: * -> * -> *) a b. Default p a b => p a b
D.def a -> b
proj ((a, PrimQuery, Tag) -> (a, PrimQuery, Tag))
-> (((), Tag) -> (a, PrimQuery, Tag))
-> ((), Tag)
-> (a, PrimQuery, Tag)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Select a -> ((), Tag) -> (a, PrimQuery, Tag)
forall a b. QueryArr a b -> (a, Tag) -> (b, PrimQuery, Tag)
Q.runSimpleQueryArr Select a
q)
distinctOnBy :: D.Default U.Unpackspec b b => (a -> b) -> O.Order a
-> S.Select a -> S.Select a
distinctOnBy :: (a -> b) -> Order a -> Select a -> Select a
distinctOnBy a -> b
proj Order a
ord Select a
q = (((), Tag) -> (a, PrimQuery, Tag)) -> Select a
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Q.productQueryArr (Unpackspec b b
-> (a -> b)
-> Order a
-> (a, PrimQuery, Tag)
-> (a, PrimQuery, Tag)
forall b a.
Unpackspec b b
-> (a -> b)
-> Order a
-> (a, PrimQuery, Tag)
-> (a, PrimQuery, Tag)
O.distinctOnBy Unpackspec b b
forall (p :: * -> * -> *) a b. Default p a b => p a b
D.def a -> b
proj Order a
ord ((a, PrimQuery, Tag) -> (a, PrimQuery, Tag))
-> (((), Tag) -> (a, PrimQuery, Tag))
-> ((), Tag)
-> (a, PrimQuery, Tag)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Select a -> ((), Tag) -> (a, PrimQuery, Tag)
forall a b. QueryArr a b -> (a, Tag) -> (b, PrimQuery, Tag)
Q.runSimpleQueryArr Select a
q)
class SqlOrd a where
{-# DEPRECATED PGOrd "Use SqlOrd instead" #-}
type PGOrd = SqlOrd
instance SqlOrd T.SqlBool
instance SqlOrd T.SqlDate
instance SqlOrd T.SqlFloat8
instance SqlOrd T.SqlFloat4
instance SqlOrd T.SqlInt8
instance SqlOrd T.SqlInt4
instance SqlOrd T.SqlInt2
instance SqlOrd T.SqlNumeric
instance SqlOrd T.SqlText
instance SqlOrd T.SqlTime
instance SqlOrd T.SqlTimestamptz
instance SqlOrd T.SqlTimestamp
instance SqlOrd T.SqlCitext
instance SqlOrd T.SqlUuid
instance SqlOrd a => SqlOrd (C.Nullable a)