{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleContexts #-}
module Opaleye.Order (
orderBy
, O.Order
, asc
, desc
, ascNullsFirst
, descNullsLast
, limit
, offset
, distinctOn
, distinctOnBy
, O.exact
, SqlOrd
, distinctOnExplicit
, distinctOnByExplicit
, distinctOnCorrect
, distinctOnByCorrect
) where
import qualified Data.Profunctor.Product.Default as D
import qualified Opaleye.Field as F
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 =
State Tag (a, PrimQuery) -> Select a
forall a. State Tag (a, PrimQuery) -> Query a
Q.productQueryArr (State Tag (a, PrimQuery) -> Select a)
-> State Tag (a, PrimQuery) -> Select a
forall a b. (a -> b) -> a -> b
$ do
(a, PrimQuery)
a_pq <- Select a -> State Tag (a, PrimQuery)
forall a. Select a -> State Tag (a, PrimQuery)
Q.runSimpleSelect Select a
q
(a, PrimQuery) -> State Tag (a, PrimQuery)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Order a -> (a, PrimQuery) -> (a, PrimQuery)
forall a. Order a -> (a, PrimQuery) -> (a, PrimQuery)
O.orderByU Order a
os (a, PrimQuery)
a_pq)
asc :: SqlOrd b => (a -> F.Field b) -> O.Order a
asc :: (a -> Field b) -> Order a
asc = OrderOp -> (a -> Field b) -> Order a
forall a (n :: Nullability) b.
OrderOp -> (a -> Field_ n 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 -> F.Field b) -> O.Order a
desc :: (a -> Field b) -> Order a
desc = OrderOp -> (a -> Field b) -> Order a
forall a (n :: Nullability) b.
OrderOp -> (a -> Field_ n 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 -> F.Field b) -> O.Order a
ascNullsFirst :: (a -> Field b) -> Order a
ascNullsFirst = OrderOp -> (a -> Field b) -> Order a
forall a (n :: Nullability) b.
OrderOp -> (a -> Field_ n 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 -> F.Field b) -> O.Order a
descNullsLast :: (a -> Field b) -> Order a
descNullsLast = OrderOp -> (a -> Field b) -> Order a
forall a (n :: Nullability) b.
OrderOp -> (a -> Field_ n 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 = State Tag (a, PrimQuery) -> Select a
forall a. State Tag (a, PrimQuery) -> Query a
Q.productQueryArr (State Tag (a, PrimQuery) -> Select a)
-> State Tag (a, PrimQuery) -> Select a
forall a b. (a -> b) -> a -> b
$ do
(a, PrimQuery)
a_pq <- Select a -> State Tag (a, PrimQuery)
forall a. Select a -> State Tag (a, PrimQuery)
Q.runSimpleSelect Select a
a
(a, PrimQuery) -> State Tag (a, PrimQuery)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> (a, PrimQuery) -> (a, PrimQuery)
forall a. Int -> (a, PrimQuery) -> (a, PrimQuery)
O.limit' Int
n (a, PrimQuery)
a_pq)
offset :: Int -> S.Select a -> S.Select a
offset :: Int -> Select a -> Select a
offset Int
n Select a
a = State Tag (a, PrimQuery) -> Select a
forall a. State Tag (a, PrimQuery) -> Query a
Q.productQueryArr (State Tag (a, PrimQuery) -> Select a)
-> State Tag (a, PrimQuery) -> Select a
forall a b. (a -> b) -> a -> b
$ do
(a, PrimQuery)
a_pq <- Select a -> State Tag (a, PrimQuery)
forall a. Select a -> State Tag (a, PrimQuery)
Q.runSimpleSelect Select a
a
(a, PrimQuery) -> State Tag (a, PrimQuery)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> (a, PrimQuery) -> (a, PrimQuery)
forall a. Int -> (a, PrimQuery) -> (a, PrimQuery)
O.offset' Int
n (a, PrimQuery)
a_pq)
distinctOnCorrect :: D.Default U.Unpackspec b b
=> (a -> b)
-> S.Select a
-> S.Select a
distinctOnCorrect :: (a -> b) -> Select a -> Select a
distinctOnCorrect = Unpackspec b b -> (a -> b) -> Select a -> Select a
forall b a. Unpackspec b b -> (a -> b) -> Select a -> Select a
distinctOnExplicit Unpackspec b b
forall (p :: * -> * -> *) a b. Default p a b => p a b
D.def
distinctOnByCorrect :: D.Default U.Unpackspec b b
=> (a -> b)
-> O.Order a
-> S.Select a
-> S.Select a
distinctOnByCorrect :: (a -> b) -> Order a -> Select a -> Select a
distinctOnByCorrect = Unpackspec b b -> (a -> b) -> Order a -> Select a -> Select a
forall b a.
Unpackspec b b -> (a -> b) -> Order a -> Select a -> Select a
distinctOnByExplicit Unpackspec b b
forall (p :: * -> * -> *) a b. Default p a b => p a b
D.def
class SqlOrd a where
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.SqlVarcharN
instance SqlOrd T.SqlTime
instance SqlOrd T.SqlTimestamptz
instance SqlOrd T.SqlTimestamp
instance SqlOrd T.SqlCitext
instance SqlOrd T.SqlUuid
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) -> Select a -> Select a
forall b a.
Default Unpackspec b b =>
(a -> b) -> Select a -> Select a
distinctOnCorrect
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) -> Order a -> Select a -> Select a
forall b a.
Default Unpackspec b b =>
(a -> b) -> Order a -> Select a -> Select a
distinctOnByCorrect
distinctOnExplicit :: U.Unpackspec b b
-> (a -> b)
-> S.Select a
-> S.Select a
distinctOnExplicit :: Unpackspec b b -> (a -> b) -> Select a -> Select a
distinctOnExplicit Unpackspec b b
unpack a -> b
proj Select a
q = State Tag (a, PrimQuery) -> Select a
forall a. State Tag (a, PrimQuery) -> Query a
Q.productQueryArr (State Tag (a, PrimQuery) -> Select a)
-> State Tag (a, PrimQuery) -> Select a
forall a b. (a -> b) -> a -> b
$ do
(a, PrimQuery)
a_pq <- Select a -> State Tag (a, PrimQuery)
forall a. Select a -> State Tag (a, PrimQuery)
Q.runSimpleSelect Select a
q
(a, PrimQuery) -> State Tag (a, PrimQuery)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Unpackspec b b -> (a -> b) -> (a, PrimQuery) -> (a, PrimQuery)
forall b a.
Unpackspec b b -> (a -> b) -> (a, PrimQuery) -> (a, PrimQuery)
O.distinctOn Unpackspec b b
unpack a -> b
proj (a, PrimQuery)
a_pq)
distinctOnByExplicit :: U.Unpackspec b b
-> (a -> b)
-> O.Order a
-> S.Select a
-> S.Select a
distinctOnByExplicit :: Unpackspec b b -> (a -> b) -> Order a -> Select a -> Select a
distinctOnByExplicit Unpackspec b b
unpack a -> b
proj Order a
ord Select a
q = State Tag (a, PrimQuery) -> Select a
forall a. State Tag (a, PrimQuery) -> Query a
Q.productQueryArr (State Tag (a, PrimQuery) -> Select a)
-> State Tag (a, PrimQuery) -> Select a
forall a b. (a -> b) -> a -> b
$ do
(a, PrimQuery)
a_pq <- Select a -> State Tag (a, PrimQuery)
forall a. Select a -> State Tag (a, PrimQuery)
Q.runSimpleSelect Select a
q
(a, PrimQuery) -> State Tag (a, PrimQuery)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Unpackspec b b
-> (a -> b) -> Order a -> (a, PrimQuery) -> (a, PrimQuery)
forall b a.
Unpackspec b b
-> (a -> b) -> Order a -> (a, PrimQuery) -> (a, PrimQuery)
O.distinctOnBy Unpackspec b b
unpack a -> b
proj Order a
ord (a, PrimQuery)
a_pq)