on a9b44f5 help sign in
rel8/src/Rel8/Query/List.hs
35 lines 1.1 kB history raw
1{-# language FlexibleContexts #-}
3module Rel8.Query.List
4 ( many, some
5 )
6where
8-- base
9import Prelude
11-- rel8
12import Rel8.Query ( Query )
13import Rel8.Query.Aggregate ( aggregate )
14import Rel8.Query.Maybe ( optional )
15import Rel8.Schema.Context ( DB )
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 )
23some :: Table DB a => Query a -> Query (NonEmptyTable a)
24some = aggregate . fmap nonEmptyAgg
27-- | Aggregate a 'Query' into a 'ListTable'. If the supplied query returns 0
28-- rows, this function will produce a 'Query' that returns one row containing
29-- the empty @ListTable@. If the supplied @Query@ does return rows, @many@ will
30-- return exactly one row, with a @ListTable@ collecting all returned rows.
31--
32-- @many@ is analogous to 'Control.Applicative.many' from
33-- @Control.Applicative@.
34many :: Table DB a => Query a -> Query (ListTable a)
35many = fmap (maybeTable mempty id) . optional . aggregate . fmap listAgg