on 4caf38d help sign in
rel8/src/Rel8/Query/Distinct.hs
45 lines 1.6 kB history raw
1{-# options_ghc -fno-warn-redundant-constraints #-}
3module Rel8.Query.Distinct
4 ( distinct
5 , distinctOn
6 , distinctOnBy
7 )
8where
10-- base
11import Prelude
13-- opaleye
14import qualified Opaleye.Distinct as Opaleye
15import qualified Opaleye.Internal.Order as Opaleye
16import qualified Opaleye.Internal.QueryArr as Opaleye
18-- rel8
19import Rel8.Order ( Order( Order ) )
20import Rel8.Query ( Query )
21import Rel8.Query.Opaleye ( mapOpaleye )
22import Rel8.Table.Eq ( EqTable )
23import Rel8.Table.Opaleye ( distinctspec, unpackspec )
26-- | Select all distinct rows from a query, removing duplicates. @distinct q@
27-- is equivalent to the SQL statement @SELECT DISTINCT q@.
28distinct :: EqTable a => Query a -> Query a
29distinct = mapOpaleye (Opaleye.distinctExplicit distinctspec)
32-- | Select all distinct rows from a query, where rows are equivalent according
33-- to a projection. If multiple rows have the same projection, it is
34-- unspecified which row will be returned. If this matters, use 'distinctOnBy'.
35distinctOn :: EqTable b => (a -> b) -> Query a -> Query a
36distinctOn proj =
37 mapOpaleye (\q -> Opaleye.productQueryArr (Opaleye.distinctOn unpackspec proj . Opaleye.runSimpleQueryArr q))
40-- | Select all distinct rows from a query, where rows are equivalent according
41-- to a projection. If there are multiple rows with the same projection, the
42-- first row according to the specified 'Order' will be returned.
43distinctOnBy :: EqTable b => (a -> b) -> Order a -> Query a -> Query a
44distinctOnBy proj (Order order) =
45 mapOpaleye (\q -> Opaleye.productQueryArr (Opaleye.distinctOnBy unpackspec proj order . Opaleye.runSimpleQueryArr q))