on 00a36f5 help sign in
rel8/src/Rel8/Query/List.hs
43 lines 1.5 kB history raw
1{-# language FlexibleContexts #-}
3module Rel8.Query.List
4 ( many, some
5 )
6where
8-- base
9import Prelude
11-- rel8
12import Rel8.Expr ( Expr )
13import Rel8.Query ( Query )
14import Rel8.Query.Aggregate ( aggregate )
15import Rel8.Query.Maybe ( optional )
16import Rel8.Table ( Table )
17import Rel8.Table.Aggregate ( listAgg, nonEmptyAgg )
18import Rel8.Table.List ( ListTable )
19import Rel8.Table.Maybe ( maybeTable )
20import Rel8.Table.NonEmpty ( NonEmptyTable )
23-- | Aggregate a 'Query' into a 'NonEmptyTable'. If the supplied query returns
24-- 0 rows, this function will produce a 'Query' that is empty - that is, will
25-- produce zero @NonEmptyTable@s. If the supplied @Query@ does return rows,
26-- @some@ will return exactly one row, with a @NonEmptyTable@ collecting all
27-- returned rows.
28--
29-- @some@ is analogous to 'Control.Applicative.some' from
30-- @Control.Applicative@.
31some :: Table Expr a => Query a -> Query (NonEmptyTable a)
32some = aggregate . fmap nonEmptyAgg
35-- | Aggregate a 'Query' into a 'ListTable'. If the supplied query returns 0
36-- rows, this function will produce a 'Query' that returns one row containing
37-- the empty @ListTable@. If the supplied @Query@ does return rows, @many@ will
38-- return exactly one row, with a @ListTable@ collecting all returned rows.
39--
40-- @many@ is analogous to 'Control.Applicative.many' from
41-- @Control.Applicative@.
42many :: Table Expr a => Query a -> Query (ListTable a)
43many = fmap (maybeTable mempty id) . optional . aggregate . fmap listAgg