on bf19395 help sign in
rel8/src/Rel8/Query/List.hs
119 lines 4.3 kB history raw
1{-# language FlexibleContexts #-}
2{-# language GADTs #-}
3{-# language NamedFieldPuns #-}
5module Rel8.Query.List
6 ( many, some
7 , manyExpr, someExpr
8 , catListTable, catNonEmptyTable
9 , catList, catNonEmpty
10 )
11where
13-- base
14import Data.Functor.Identity ( runIdentity )
15import Data.List.NonEmpty ( NonEmpty )
16import Prelude
18-- opaleye
19import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
21-- rel8
22import Rel8.Expr ( Col( E, unE ), Expr )
23import Rel8.Expr.Aggregate ( listAggExpr, nonEmptyAggExpr )
24import Rel8.Expr.Opaleye ( mapPrimExpr )
25import Rel8.Query ( Query )
26import Rel8.Query.Aggregate ( aggregate )
27import Rel8.Query.Evaluate ( rebind )
28import Rel8.Query.Maybe ( optional )
29import Rel8.Schema.HTable.Vectorize ( hunvectorize )
30import Rel8.Schema.Null ( Sql, Unnullify )
31import Rel8.Schema.Spec ( SSpec( SSpec, info ) )
32import Rel8.Table ( Table, fromColumns, toColumns )
33import Rel8.Table.Aggregate ( listAgg, nonEmptyAgg )
34import Rel8.Table.List ( ListTable( ListTable ) )
35import Rel8.Table.Maybe ( maybeTable )
36import Rel8.Table.NonEmpty ( NonEmptyTable( NonEmptyTable ) )
37import Rel8.Type ( DBType, typeInformation )
38import Rel8.Type.Array ( extractArrayElement )
39import Rel8.Type.Information ( TypeInformation )
42-- | Aggregate a 'Query' into a 'ListTable'. If the supplied query returns 0
43-- rows, this function will produce a 'Query' that returns one row containing
44-- the empty @ListTable@. If the supplied @Query@ does return rows, @many@ will
45-- return exactly one row, with a @ListTable@ collecting all returned rows.
46--
47-- @many@ is analogous to 'Control.Applicative.many' from
48-- @Control.Applicative@.
49many :: Table Expr a => Query a -> Query (ListTable a)
50many =
51 fmap (maybeTable mempty (\(ListTable a) -> ListTable a)) .
52 optional .
53 aggregate .
54 fmap (listAgg . toColumns)
57-- | Aggregate a 'Query' into a 'NonEmptyTable'. If the supplied query returns
58-- 0 rows, this function will produce a 'Query' that is empty - that is, will
59-- produce zero @NonEmptyTable@s. If the supplied @Query@ does return rows,
60-- @some@ will return exactly one row, with a @NonEmptyTable@ collecting all
61-- returned rows.
62--
63-- @some@ is analogous to 'Control.Applicative.some' from
64-- @Control.Applicative@.
65some :: Table Expr a => Query a -> Query (NonEmptyTable a)
66some =
67 fmap (\(NonEmptyTable a) -> NonEmptyTable a) .
68 aggregate .
69 fmap (nonEmptyAgg . toColumns)
72-- | A version of 'many' specialised to single expressions.
73manyExpr :: Sql DBType a => Query (Expr a) -> Query (Expr [a])
74manyExpr = fmap (maybeTable mempty id) . optional . aggregate . fmap listAggExpr
77-- | A version of 'many' specialised to single expressions.
78someExpr :: Sql DBType a => Query (Expr a) -> Query (Expr (NonEmpty a))
79someExpr = aggregate . fmap nonEmptyAggExpr
82-- | Expand a 'ListTable' into a 'Query', where each row in the query is an
83-- element of the given @ListTable@.
84--
85-- @catListTable@ is an inverse to 'many'.
86catListTable :: Table Expr a => ListTable a -> Query a
87catListTable (ListTable as) = rebind $ fromColumns $ runIdentity $
88 hunvectorize (\SSpec {info} -> pure . E . sunnest info . unE) as
91-- | Expand a 'NonEmptyTable' into a 'Query', where each row in the query is an
92-- element of the given @NonEmptyTable@.
93--
94-- @catNonEmptyTable@ is an inverse to 'some'.
95catNonEmptyTable :: Table Expr a => NonEmptyTable a -> Query a
96catNonEmptyTable (NonEmptyTable as) = rebind $ fromColumns $ runIdentity $
97 hunvectorize (\SSpec {info} -> pure . E . sunnest info . unE) as
100-- | Expand an expression that contains a list into a 'Query', where each row
101-- in the query is an element of the given list.
102--
103-- @catList@ is an inverse to 'manyExpr'.
104catList :: Sql DBType a => Expr [a] -> Query (Expr a)
105catList = rebind . sunnest typeInformation
108-- | Expand an expression that contains a non-empty list into a 'Query', where
109-- each row in the query is an element of the given list.
110--
111-- @catNonEmpty@ is an inverse to 'someExpr'.
112catNonEmpty :: Sql DBType a => Expr (NonEmpty a) -> Query (Expr a)
113catNonEmpty = rebind . sunnest typeInformation
116sunnest :: TypeInformation (Unnullify a) -> Expr (list a) -> Expr a
117sunnest info = mapPrimExpr $
118 extractArrayElement info .
119 Opaleye.UnExpr (Opaleye.UnOpOther "UNNEST")