{-# options_ghc -fno-warn-redundant-constraints #-}
module Rel8.Query.Distinct
( distinct
, distinctOn
, distinctOnBy
)
where
import Prelude
import qualified Opaleye.Distinct as Opaleye
import qualified Opaleye.Internal.Order as Opaleye
import qualified Opaleye.Internal.QueryArr as Opaleye
import Rel8.Order ( Order( Order ) )
import Rel8.Query ( Query )
import Rel8.Query.Opaleye ( mapOpaleye )
import Rel8.Table.Eq ( EqTable )
import Rel8.Table.Opaleye ( distinctspec, unpackspec )
distinct :: EqTable a => Query a -> Query a
distinct :: Query a -> Query a
distinct = (Select a -> Select a) -> Query a -> Query a
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye (Distinctspec a a -> Select a -> Select a
forall fields fields'.
Distinctspec fields fields' -> Select fields -> Select fields'
Opaleye.distinctExplicit Distinctspec a a
forall a. Table Expr a => Distinctspec a a
distinctspec)
distinctOn :: EqTable b => (a -> b) -> Query a -> Query a
distinctOn :: (a -> b) -> Query a -> Query a
distinctOn a -> b
proj =
(Select a -> Select a) -> Query a -> Query a
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye (\Select a
q -> (((), Tag) -> (a, PrimQuery, Tag)) -> Select a
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Opaleye.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)
Opaleye.distinctOn Unpackspec b b
forall a. Table Expr a => Unpackspec a a
unpackspec 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)
Opaleye.runSimpleQueryArr Select a
q))
distinctOnBy :: EqTable b => (a -> b) -> Order a -> Query a -> Query a
distinctOnBy :: (a -> b) -> Order a -> Query a -> Query a
distinctOnBy a -> b
proj (Order Order a
order) =
(Select a -> Select a) -> Query a -> Query a
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye (\Select a
q -> (((), Tag) -> (a, PrimQuery, Tag)) -> Select a
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Opaleye.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)
Opaleye.distinctOnBy Unpackspec b b
forall a. Table Expr a => Unpackspec a a
unpackspec a -> b
proj Order a
order ((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)
Opaleye.runSimpleQueryArr Select a
q))