on 293706d help sign in
rel8/src/Rel8/Query/Distinct.hs
48 lines 1.7 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@.
28--
29-- >>> select c $ distinct $ values [ lit True, lit True, lit False ]
30-- [False,True]
31distinct :: EqTable a => Query a -> Query a
32distinct = mapOpaleye (Opaleye.distinctExplicit distinctspec)
35-- | Select all distinct rows from a query, where rows are equivalent according
36-- to a projection. If multiple rows have the same projection, it is
37-- unspecified which row will be returned. If this matters, use 'distinctOnBy'.
38distinctOn :: EqTable b => (a -> b) -> Query a -> Query a
39distinctOn proj =
40 mapOpaleye (\q -> Opaleye.productQueryArr (Opaleye.distinctOn unpackspec proj . Opaleye.runSimpleQueryArr q))
43-- | Select all distinct rows from a query, where rows are equivalent according
44-- to a projection. If there are multiple rows with the same projection, the
45-- first row according to the specified 'Order' will be returned.
46distinctOnBy :: EqTable b => (a -> b) -> Order a -> Query a -> Query a
47distinctOnBy proj (Order order) =
48 mapOpaleye (\q -> Opaleye.productQueryArr (Opaleye.distinctOnBy unpackspec proj order . Opaleye.runSimpleQueryArr q))