1{-# language FlexibleContexts #-}
3{-# language NamedFieldPuns #-}
8 , catListTable, catNonEmptyTable
14import Data.Functor.Identity ( runIdentity )
15import Data.List.NonEmpty ( NonEmpty )
19import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
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.
47-- @many@ is analogous to 'Control.Applicative.many' from
48-- @Control.Applicative@.
49many :: Table Expr a => Query a -> Query (ListTable a)
51 fmap (maybeTable mempty (\(ListTable a) -> ListTable a)) .
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
63-- @some@ is analogous to 'Control.Applicative.some' from
64-- @Control.Applicative@.
65some :: Table Expr a => Query a -> Query (NonEmptyTable a)
67 fmap (\(NonEmptyTable a) -> NonEmptyTable a) .
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@.
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@.
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.
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.
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")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list