on bdf2d87 help sign in
rel8/src/Rel8/Query/List.hs
59 lines 2.1 kB history raw
1{-# language FlexibleContexts #-}
2{-# language MonoLocalBinds #-}
4module Rel8.Query.List
5 ( many, some
6 , manyExpr, someExpr
7 )
8where
10-- base
11import Data.List.NonEmpty ( NonEmpty )
12import Prelude
14-- rel8
15import Rel8.Expr ( Expr )
16import Rel8.Expr.Aggregate ( listAggExpr, nonEmptyAggExpr )
17import Rel8.Query ( Query )
18import Rel8.Query.Aggregate ( aggregate )
19import Rel8.Query.Maybe ( optional )
20import Rel8.Schema.Null ( Sql )
21import Rel8.Table ( Table )
22import Rel8.Table.Aggregate ( listAgg, nonEmptyAgg )
23import Rel8.Table.List ( ListTable )
24import Rel8.Table.Maybe ( maybeTable )
25import Rel8.Table.NonEmpty ( NonEmptyTable )
26import Rel8.Type ( DBType )
29-- | Aggregate a 'Query' into a 'NonEmptyTable'. If the supplied query returns
30-- 0 rows, this function will produce a 'Query' that is empty - that is, will
31-- produce zero @NonEmptyTable@s. If the supplied @Query@ does return rows,
32-- @some@ will return exactly one row, with a @NonEmptyTable@ collecting all
33-- returned rows.
34--
35-- @some@ is analogous to 'Control.Applicative.some' from
36-- @Control.Applicative@.
37some :: Table Expr a => Query a -> Query (NonEmptyTable a)
38some = aggregate . fmap nonEmptyAgg
41-- | Aggregate a 'Query' into a 'ListTable'. If the supplied query returns 0
42-- rows, this function will produce a 'Query' that returns one row containing
43-- the empty @ListTable@. If the supplied @Query@ does return rows, @many@ will
44-- return exactly one row, with a @ListTable@ collecting all returned rows.
45--
46-- @many@ is analogous to 'Control.Applicative.many' from
47-- @Control.Applicative@.
48many :: Table Expr a => Query a -> Query (ListTable a)
49many = fmap (maybeTable mempty id) . optional . aggregate . fmap listAgg
52-- | A version of 'many' specialised to single expressions.
53manyExpr :: Sql DBType a => Query (Expr a) -> Query (Expr [a])
54manyExpr = fmap (maybeTable mempty id) . optional . aggregate . fmap listAggExpr
57-- | A version of 'many' specialised to single expressions.
58someExpr :: Query (Expr a) -> Query (Expr (NonEmpty a))
59someExpr = aggregate . fmap nonEmptyAggExpr