@@ -1,23 +1,29 @@
1{-# language FlexibleContexts #-}
1{-# language FlexibleContexts #-}
2+{-# language MonoLocalBinds #-}
3module Rel8.Query.List
4module Rel8.Query.List
4 ( many, some
5 ( many, some
11+import Data.List.NonEmpty ( NonEmpty )
9import Prelude
12import Prelude
12import Rel8.Expr ( Expr )
15import Rel8.Expr ( Expr )
16+import Rel8.Expr.Aggregate ( listAggExpr, nonEmptyAggExpr )
13import Rel8.Query ( Query )
17import Rel8.Query ( Query )
14import Rel8.Query.Aggregate ( aggregate )
18import Rel8.Query.Aggregate ( aggregate )
15import Rel8.Query.Maybe ( optional )
19import Rel8.Query.Maybe ( optional )
20+import Rel8.Schema.Null ( Sql )
16import Rel8.Table ( Table )
21import Rel8.Table ( Table )
17import Rel8.Table.Aggregate ( listAgg, nonEmptyAgg )
22import Rel8.Table.Aggregate ( listAgg, nonEmptyAgg )
18import Rel8.Table.List ( ListTable )
23import Rel8.Table.List ( ListTable )
19import Rel8.Table.Maybe ( maybeTable )
24import Rel8.Table.Maybe ( maybeTable )
20import Rel8.Table.NonEmpty ( NonEmptyTable )
25import Rel8.Table.NonEmpty ( NonEmptyTable )
26+import Rel8.Type ( DBType )
23-- | Aggregate a 'Query' into a 'NonEmptyTable'. If the supplied query returns
29-- | Aggregate a 'Query' into a 'NonEmptyTable'. If the supplied query returns
@@ -41,3 +47,13 @@ some = aggregate . fmap nonEmptyAgg
41-- @Control.Applicative@.
47-- @Control.Applicative@.
42many :: Table Expr a => Query a -> Query (ListTable a)
48many :: Table Expr a => Query a -> Query (ListTable a)
43many = fmap (maybeTable mempty id) . optional . aggregate . fmap listAgg
49many = fmap (maybeTable mempty id) . optional . aggregate . fmap listAgg
52+-- | A version of 'many' specialised to single expressions.
53+manyExpr :: Sql DBType a => Query (Expr a) -> Query (Expr [a])
54+manyExpr = fmap (maybeTable mempty id) . optional . aggregate . fmap listAggExpr
57+-- | A version of 'many' specialised to single expressions.
58+someExpr :: Query (Expr a) -> Query (Expr (NonEmpty a))
59+someExpr = aggregate . fmap nonEmptyAggExpr